#define BLYNK_TEMPLATE_ID "TMPL6uc0KWDlR"
#define BLYNK_TEMPLATE_NAME "CAMP"
#define BLYNK_AUTH_TOKEN "wdo5sZ2H37hZ8KjSBsN4AiLsEHYJCn80"
#define BLYNK_PRINT Serial /
#include <WiFi.h>
#include <HTTPClient.h>
#include <DHT.h>
#include <BlynkSimpleEsp32.h>
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
const char* token = "cfp0uNoRffA7IkFABKljTju7hzRTiFDrVMUkiUQqLTW"; //Line token form https://notify-bot.line.me/th/
int pirPin = 18;
int ledPin = 22;
#define DHTPIN 4 // Pin connected to DHT22
#define DHTTYPE DHT22 // DHT sensor type
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
pinMode(pirPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
delay(4000);
dht.begin();
delay(2000);
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 temperature = dht.readTemperature(); // Gets the values of the temperature
float humidity = dht.readHumidity(); // Gets the values of the humidity
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);
String humid = String(humidity);
http.POST("message=Humidity : "+humid+" %");
String tempMessage = String(temperature);
http.POST("message=Temperature : "+tempMessage+" °C");
if (temperature > 35) {
http.POST("message=แจ้งเตือนอุณหภูมิสูง🌡");
Serial.println("High temperature message sent");
}
if (humidity > 50) {
http.POST("message=แจ้งเตือนความชื้นสูง🌫");
Serial.println("High 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(500);
if (WiFi.status() == WL_CONNECTED) {// Send a message and read sensor data every 15 Minute
int pirValue = digitalRead(pirPin);
if (pirValue == HIGH)
digitalWrite(ledPin, HIGH); // เปิด LED
delay(1000);
digitalWrite(ledPin, LOW); // ปิด LED
delay(1000);
sendLineNotify("มีจดหมายใหม่"); // ส่งข้อความไปยัง LINE Notify
}
}
}
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();
// Serial.println(httpCode);
// Serial.println(response);
} else {
Serial.println("....");
}
http.end();
}
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;
}