#include <LiquidCrystal_I2C.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <DHT.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#define DHTPIN 4
#define DHTTYPE DHT22
LiquidCrystal_I2C lcd(0x27, 16, 2);
DHT dht(DHTPIN, DHTTYPE);
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* lineToken = "ij3BTrbSEN804X54cvkb4h2FdneatPFXkENlWBrXlRp";
unsigned long previousMillis = 0;
const long interval = 300000;
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org", 7 * 3600, 60000);
void setup() {
lcd.init();
lcd.backlight();
Serial.begin(115200);
Serial.println("Hello, Sornnaret009");
dht.begin();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
timeClient.begin();
lcd.setCursor(0, 0);
lcd.print("Hello, Sornnaret009");
delay(5000);
lcd.clear();
}
void loop() {
unsigned long currentMillis = millis();
unsigned long elapsedMillis = currentMillis - previousMillis;
timeClient.update();
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
String formattedTime = timeClient.getFormattedTime();
lcd.setCursor(0, 0);
lcd.print(formattedTime);
lcd.setCursor(0, 1);
lcd.print("T: ");
lcd.print(temperature);
lcd.print("C H: ");
lcd.print(humidity);
lcd.print("%");
if (elapsedMillis >= interval) {
previousMillis = currentMillis;
sendLineNotification("Temperature: " + String(temperature));
lcd.setCursor(15, 0);
lcd.write(byte(1));
} else {
if (elapsedMillis % interval < 1000) {
lcd.setCursor(15, 0);
lcd.print(" ");
}
}
delay(1000);
}
void sendLineNotification(String message) {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin("https://notify-api.line.me/api/notify");
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
http.addHeader("Authorization", "Bearer " + String(lineToken));
if (message.length() == 0) {
message = "Test message from ESP32";
}
String payload = "message=" + message;
Serial.print("Payload: ");
Serial.println(payload);
int httpResponseCode = http.POST(payload);
if (httpResponseCode > 0) {
String response = http.getString();
Serial.println("Response: " + response);
} else {
Serial.print("Error on sending POST: ");
Serial.println(httpResponseCode);
Serial.println(http.errorToString(httpResponseCode).c_str());
}
http.end();
} else {
Serial.println("WiFi Disconnected");
}
}