#include <dht.h>
#include <TinyWireM.h>
#include <Tiny4kOLED.h>
#define DHT21_PIN PB1
const unsigned char img_thermometer[] PROGMEM = {
0x00, 0xfe, 0x03, 0xfe, 0x50,
0x00, 0xff, 0x00, 0xff, 0x55,
0x60, 0x9f, 0x80, 0x9f, 0x65,
};
dht DHT;
float getTemperature() {
return DHT.temperature;
}
float getHumidity() {
return DHT.humidity;
}
void setup() {
pinMode(DHT21_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(FONT8X16);
// To clear all the memory
oled.clear();
oled.on();
delay(3000);
}
void loop() {
static long startTime = 0;
long currentTime;
DHT.read22(DHT21_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(15, 4);
oled.print("Temp: ");
// Print to display
oled.print(temperature, 1);
oled.print(" °C ");
// Update humidity
float humidity = getHumidity();
// Set cursor
oled.setCursor(15, 6);
oled.print("Humi: ");
// Print to display
oled.print(humidity, 1);
oled.print(" % ");
oled.bitmap(2, 5, 7, 8, img_thermometer);
}
}