#include <DHT.h>
#include <LiquidCrystal_I2C.h>
#define DHTPIN 3 //Pin ขาสำหรับ เสียบกับ Nodemcu
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27, 16, 2); //Module IIC/I2C Interface บางรุ่นอาจจะใช้ 0x3f
const int buttonPin = 4;
void setup() {
Serial.begin(9600);
Serial.println("DHTxx test!");
dht.begin();
lcd.init(); // Initialize the LCD
lcd.backlight();
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
delay(2000);
float h = dht.readHumidity();
float t = dht.readTemperature();
float f = dht.readTemperature(true);
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
float hif = dht.computeHeatIndex(f, h);
float hic = dht.computeHeatIndex(t, h, false);
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" *C ");
Serial.print(f);
Serial.print(" *F\t");
Serial.print("Heat index: ");
Serial.print(hic);
Serial.print(" *C ");
Serial.print(hif);
Serial.println(" *F");
lcd.home();
lcd.print("Temp (C) : ");
lcd.print(t);
lcd.setCursor(0, 1);
lcd.print("Humidity : ");
lcd.print(h);
lcd.print("%");
waitForButtonPress();
lcd.setCursor(0, 0);
lcd.print("Temp (F) : ");
lcd.print(f);
lcd.setCursor(0, 1);
lcd.print("Humidity : ");
lcd.print(h);
lcd.print("%");
}
void waitForButtonPress() {
while (digitalRead(buttonPin) == HIGH) {
// Do nothing here until a button is pressed
}
delay(200); // Debouncing delay
}