#include <dht.h>
#include <TinyWireM.h> // thư viện I2C ATtiny
#include <Tiny4kOLED.h> // Đây là thư viện dành cho ATTiny85 để sử dụng màn hình OLED 128x64 pixel, hỗ trợ SSD1306, trên I 2 C
#define DHT22_PIN PB1
dht DHT;
void splash() {
oled.clear();
oled.setCursor(15, 1);
oled.print(F("ATtiny85+SSD1306"));
oled.setCursor(35, 7);
oled.print(F("wokwi.com"));
}
float getTemperature() {
return DHT.temperature;
}
float getHumidity() {
return DHT.humidity;
}
void setup() {
pinMode(DHT22_PIN, INPUT);
oled.begin(128, 64, sizeof(tiny4koled_init_128x64br), tiny4koled_init_128x64br);
// Two fonts are supplied with this library, FONT8X16 and FONT6X8
oled.setFont(FONT6X8);
// To clear all the memory
oled.clear();
oled.on();
splash();
delay(3000);
}
void loop() {
static long startTime = 0;
long currentTime;
DHT.read22(DHT22_PIN);
// Get current time
currentTime = millis();
// Checks 1 second passed
if ((currentTime - startTime) > 1000) {
startTime = currentTime;
// Update temperature
float temperature = getTemperature();
// Set cursor
oled.setCursor(57, 4);
// Print to display
oled.print(temperature, 1);
oled.print("C ");
// Update humidity
float humidity = getHumidity();
// Set cursor
oled.setCursor(57, 5);
// Print to display
oled.print(humidity, 1);
oled.print("% ");
}
}