//แจ้งเตือน DHT22 https://drive.google.com/file/d/1gEPCUOQkRUsB4IQErQfmko0XjZ3QgogE/view
//แจ้งเตือนกล่องจดหมาย https://drive.google.com/file/d/1KpVGBsARgmKzzl4IzKU7CWn0g8Zb4J-p/view



#include <WiFi.h>
#include <HTTPClient.h>
#include <DHT.h>

const char* ssid = "Wokwi-GUEST"; //ใส่ชื่อ wifi
const char* password = "";        //ใส่ Password
const char* token = "____"; //ใส่Token Line


#define DHTPIN 18      // Pin connected to DHT22
#define DHTTYPE DHT22 // DHT sensor type

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(115200);
  dht.begin();
  //WiFi.begin(ssid, password);
  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();

//คำสั่งการทำงานของ DHT22
    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");

//ถ้าอุณหภูมิมากกว่า 40
      if (temperature > 40) {
        http.POST("message=High temperature 🌡"); //อุณหภูมิสูง 🌡
        Serial.println("High temperature message sent");  //ส่งข้อความอุณหภูมิสูงแล้ว
      }

//ถ้าความชื้นมากกว่า 40
      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(3000); // Send a message and read sensor data every 5 seconds
  }
}
/*DHT WITH ESP32 Wokwi.txt
กำลังแสดง DHT WITH ESP32 Wokwi.txt*/
$abcdeabcde151015202530fghijfghij