// Display Setup
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4); // I2C address 0x27, 20 columns and 4 rows
// WiFi Setup
#include <WiFi.h>
// NL NTP Server Setup
#define NTP_SERVER "nl.pool.ntp.org"
#define UTC_OFFSET 0
#define UTC_OFFSET_DST 0
// DHT22
#include "DHT.h"
#define DHTPIN 12
#define DHTTYPE DHT22 // DHT 22
DHT dht(DHTPIN, DHTTYPE);
void spinner() {
static int8_t counter = 0;
const char* glyphs = "\xa1\xa5\xdb";
lcd.setCursor(15, 1);
lcd.print(glyphs[counter % 3]);
counter++;
}
void printLocalTime() {
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
lcd.setCursor(0, 1);
lcd.print("Connection Err");
return;
}
lcd.setCursor(15, 0);
lcd.print(&timeinfo, "%H:%M");
}
void setup() {
Serial.begin(115200);
dht.begin();
lcd.init(); // initialize the lcd
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Connecting to WiFi");
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(250);
spinner();
}
Serial.println("\nWiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Updating time...");
delay(3250);
lcd.clear();
lcd.setCursor(14, 3);
lcd.print("Online");
configTime(UTC_OFFSET, UTC_OFFSET_DST, NTP_SERVER);
}
void loop() {
printLocalTime();
dhtloop();
delay(250);
}
void dhtloop() {
delay(2000);
float h = dht.readHumidity();
float t = dht.readTemperature();
float f = dht.readTemperature(true);
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
float hif = dht.computeHeatIndex(f, h);
float hic = dht.computeHeatIndex(t, h, false);
lcd.setCursor(0, 0);
lcd.print(F("LVI: "));
lcd.setCursor(5, 0);
lcd.print(h);
lcd.setCursor(0, 1);
lcd.print(F("TEI: "));
lcd.setCursor(5, 1);
lcd.print(t);
Serial.print(F("°C "));
Serial.print(f);
Serial.print(F("°F Heat index: "));
Serial.print(hic);
Serial.print(F("°C "));
Serial.print(hif);
Serial.println(F("°F"));
}