#include <WiFi.h>
#include <HTTPClient.h>
#include <DHT.h>
const char* token = "Sx44LltaR3MJWgRungG199ljtnpjRUyM4c5Wt4ySU3C";
DHT dht(14, DHT22);
String  temp="";


void setup() {
  Serial.begin(115200);
  //WIFI Setup
  WiFi.begin("Wokwi-GUEST", "", 6);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to WiFi");

}

void loop() {
  if (WiFi.status() == WL_CONNECTED) {
    Serial.println("LINE Sending");
    temp = dht.readTemperature();
    //send Line
    sendLineNotify(temp); 
  }
   
}
//LINE FUNCTION
void sendLineNotify(String message) {
  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");
  String encodedMessage = "message=" + urlEncode(message);
  int httpCode = http.POST(encodedMessage);
  if (httpCode > 0) {
    String response = http.getString();
  } else {
    Serial.println("....");
  }
  http.end();
}
//LINE FUNCTION
String urlEncode(String value) {
  String encodedValue = "";
  char c;
  for (size_t i = 0; i < value.length(); i++) {
    c = value.charAt(i);
    if (isAlphaNumeric(c)) {
      encodedValue += c;
    } else {
      encodedValue += String('%');
      encodedValue += String(c, HEX);
    }
  }
  return encodedValue;
}