#include <WiFi.h>
#include <HTTPClient.h>
#include <DHT.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
//const char* token = "EwWf5eYvserSAengH7lgoc5znXtQ0UuzjrUzf8BPYWR";
const char* token = "91OZMD90qG6Y3irl4qRUOn3sEcTYq8Ru4I8GjQihZt3";
#define DHTPIN 5 // Pin connected to DHT22
#define DHTTYPE DHT22 // DHT sensor type
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
dht.begin();
WiFi.begin("Wokwi-GUEST", "");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.printf("\nWiFi connected\nIP : ");
Serial.println(WiFi.localIP());
Serial.println("Connected to WiFi");
Serial.println(" READY ");
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin("https://notify-api.line.me/api/notify");
http.addHeader("Authorization", "Bearer " + String(token));
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
} else {
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
delay(9000);
if (temperature < 26) {
http.POST("message=อุณหภูมิ Normal "+String(temperature)+"°C🌡");
Serial.println("Humidity: "+String(humidity)+" %\t" "Temperature: "+String(temperature)+" °C");
delay(6000000);
} else {
http.POST("message=อุณหภูมิ High "+String(temperature)+"°C🌡");
Serial.println("High temperature message sent");
Serial.println("==================================");
delay(100000);
}
if (humidity < 40) {
http.POST("message=Low humidity "+String(humidity)+" % 🌫");
Serial.println("Low humidity message sent");
Serial.println("==================================");
delay(1000000);
}
}
int httpCode = http.POST("READY"); // Use an empty POST request to send only headers
if (httpCode > 0) {
String response = http.getString();
//Serial.print("HTTP Response code: ");
//Serial.println(httpCode);
//Serial.print("Server response: ");
//Serial.println(response);
} else {
Serial.println(".....");
}
http.end();
delay(9000); // Send a message and read sensor data every 9 seconds
}
}