#include <WiFi.h>
#include <HTTPClient.h>
#include <DHT.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* token = "NNDrcNL5cvpJgOKQcJ2yTDzJD4u5344g5fqQHNfHF6k";
#define DHTPIN 2 // 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", "",6);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
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");
if (temperature > 40) {
http.POST("message=High temperature 🌡");
Serial.println("High temperature message sent");
}
if (humidity < 40) {
http.POST("message=Low humidity 🌫");
Serial.println("Low humidity message sent");
Serial.println("==================================");
}
}
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(1000); // Send a message and read sensor data every 5 seconds
}
}