#define BLYNK_TEMPLATE_ID "TMPL628P7FCFn"
#define BLYNK_TEMPLATE_NAME "esp32 multisensor"
#define BLYNK_AUTH_TOKEN "CJhUHjyTtmuPV_2QExD-TNyFN8Gwnj1j"
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <HTTPClient.h>
#include <DHT.h>
#include <BlynkSimpleEsp32.h>
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
char blynkAuth[] = "CJhUHjyTtmuPV_2QExD-TNyFN8Gwnj1j";
const char* lineToken = "cfp0uNoRffA7IkFABKljTju7hzRTiFDrVMUkiUQqLTW";
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");
Blynk.begin(blynkAuth, ssid, pass);
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() {
Blynk.run();
if (WiFi.status() == WL_CONNECTED) {
int pirValue = digitalRead(pirPin);
if (pirValue == HIGH) {
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(1000);
// Read temperature from DHT sensor
float temperature = dht.readTemperature();
if (!isnan(temperature)) {
// Update Blynk temperature widget
Blynk.virtualWrite(V2, temperature);
// Send LINE Notify message with temperature information
String tempMessage = "อุณหภูมิ: " + String(temperature, 1) + " °C";
sendLineNotify(tempMessage.c_str());
// Send LINE Notify message for new letter
sendLineNotify("จดหมายใหม่");
} else {
Serial.println("Failed to read temperature from DHT sensor");
}
}
}
}
void sendLineNotify(const char* message) {
HTTPClient http;
String url = "https://notify-api.line.me/api/notify";
http.begin(url);
http.addHeader("Authorization", "Bearer " + String(lineToken));
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(response);
} else {
Serial.println("Failed to send LINE Notify message");
}
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;
}