#include <dht.h>
#include <TinyWireM.h>
#include <TinyWireM.h>
#include <Tiny4kOLED.h>
#define DHT22_PIN PB1
#define ledBLU_PIN PB3
#define ledGREEN_PIN PB4
#define ledRED_PIN PB5
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(DHT22_PIN, INPUT);
pinMode(ledBLU_PIN, OUTPUT);
pinMode(ledGREEN_PIN, OUTPUT);
pinMode(ledRED_PIN, OUTPUT);
oled.begin(128, 64, sizeof(tiny4koled_init_128x64br), tiny4koled_init_128x64br);
//oled.begin(128, 32, sizeof(tiny4koled_init_128x32br), tiny4koled_init_128x32br);
// Two fonts are supplied with this library, FONT8X16 and FONT6X8
oled.setFont(FONT8X16);
// To clear all the memory
oled.clear();
oled.on();
splash();
delay(3000);
oled.clear();
}
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(15, 0);
oled.print("Temp: ");
// Print to display
oled.print(temperature, 1);
oled.print(" °C ");
// Update humidity
float humidity = getHumidity();
// Set cursor
oled.setCursor(15, 2);
oled.print("Humi: ");
// Print to display
oled.print(humidity, 1);
oled.print(" % ");
oled.bitmap(2, 1, 7, 4, img_thermometer);
//oled.bitmap(120, 5, 125, 8, img_thermometer);
// Accensione Led
if (temperature < 17) {
digitalWrite (ledBLU_PIN, HIGH);
digitalWrite (ledGREEN_PIN, LOW);
digitalWrite (ledRED_PIN, LOW);
} else if (temperature > 22) {
digitalWrite (ledBLU_PIN, LOW);
digitalWrite (ledGREEN_PIN, LOW);
digitalWrite (ledRED_PIN, HIGH);
} else {
digitalWrite (ledBLU_PIN, LOW);
digitalWrite (ledGREEN_PIN, HIGH);
digitalWrite (ledRED_PIN, LOW);
}
}
}
void splash (void) {
oled.setCursor(30, 0);
oled.print("TempHum");
oled.setCursor(0, 2);
oled.print("by herma v1.00");
}
Loading
ssd1306
ssd1306