#include <DHT.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <TaskScheduler.h>
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(2, DHTTYPE); // ประกาศ DHT22 ตัวที่ 1 พร้อมตำแหน่งขา
DHT dht2(15, DHTTYPE); // ประกาศ DHT22 ตัวที่ 2 พร้อมตำแหน่งขา
const char *ssid = "Wokwi-GUEST";
const char *password = "";
const char *lineNotifyToken = "hF4Adk4DfBezAiV2LJNO8yNhLkRovn2pUBofH9qsURM"; // แทนที่ด้วย Token จริง
const char *lineNotifyToken2 = "zAEqcvjkEPyitisdLpS9lScDikRLjaveITXGmgao0ag"; // แทนที่ด้วย Token จริง
const char *thingspeakApiKey = "UF433JJBANTMFGWD"; // แทนที่ด้วย API Key จริง
const char *thingspeakApiEndpoint = "http://api.thingspeak.com/update";
Scheduler runner;
float temperature, humidity; // ประกาศตัวแปร global สำหรับ DHT22 ตัวที่ 1
float temperature2, humidity2; // ประกาศตัวแปร global สำหรับ DHT22 ตัวที่ 2
float temperature3, humidity3; // ประกาศตัวแปร global สำหรับ DHT22 ตัวที่ 3
float temperature4, humidity4; // ประกาศตัวแปร global สำหรับ DHT22 ตัวที่ 4
void readSensorsAndNotify();
void readSensorsAndNotify1();
void sendLineNotify(String message);
void sendToThingspeak();
Task taskReadSensorsAndNotify(120000, TASK_FOREVER, &readSensorsAndNotify);
Task taskReadSensorsAndNotify1(60000, TASK_FOREVER, &readSensorsAndNotify1);
Task taskSendToThingspeak(60000, TASK_FOREVER, &sendToThingspeak);
void setup() {
Serial.begin(115200);
dht.begin();
dht2.begin();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
// กำหนดค่าเริ่มต้น
temperature3 = dht.readTemperature();
humidity3 = dht.readHumidity();
temperature4 = dht2.readTemperature();
humidity4 = dht2.readHumidity();
runner.init();
runner.addTask(taskReadSensorsAndNotify);
runner.addTask(taskReadSensorsAndNotify1);
runner.addTask(taskSendToThingspeak);
taskReadSensorsAndNotify.enable();
taskReadSensorsAndNotify1.enable();
taskSendToThingspeak.enable();
}
void loop() {
runner.execute();
}
void readSensorsAndNotify() {
temperature = dht.readTemperature();
humidity = dht.readHumidity();
temperature2 = dht2.readTemperature();
humidity2 = dht2.readHumidity();
Serial.print("อุณหภูมิ 1: ");
Serial.print(temperature, 1);
Serial.print(" °C, ความชื้น 1: ");
Serial.print(humidity, 1);
Serial.println(" %");
Serial.print("อุณหภูมิ 2: ");
Serial.print(temperature2, 1);
Serial.print(" °C, ความชื้น 2: ");
Serial.print(humidity2, 1);
Serial.println(" %");
sendLineNotify("อุณหภูมิ : " + String(temperature, 1) + " °C, ความชื้น : " + String(humidity, 1) + "");
sendLineNotify2("อุณหภูมิ : " + String(temperature2, 1) + " °C, ความชื้น : " + String(humidity2, 1) + "");
}
void readSensorsAndNotify1() {
temperature3 = dht.readTemperature();
humidity3 = dht.readHumidity();
temperature4 = dht2.readTemperature();
humidity4 = dht2.readHumidity();
Serial.print("อุณหภูมิ 3: ");
Serial.print(temperature3, 1);
Serial.print(" °C, ความชื้น 3: ");
Serial.print(humidity3, 1);
Serial.println(" %");
Serial.print("อุณหภูมิ 4: ");
Serial.print(temperature4, 1);
Serial.print(" °C, ความชื้น 4: ");
Serial.print(humidity4, 1);
Serial.println(" %");
}
void sendToThingspeak() {
HTTPClient http;
String url = String(thingspeakApiEndpoint) + "?api_key=" + String(thingspeakApiKey) +
"&field1=" + String(temperature3) +
"&field2=" + String(humidity3) +
"&field3=" + String(temperature4) +
"&field4=" + String(humidity4);
http.begin(url);
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
Serial.print("HTTP Response code (Thingspeak): ");
Serial.println(httpResponseCode);
} else {
Serial.print("Error on sending GET request (Thingspeak). Response code: ");
Serial.println(httpResponseCode);
Serial.print("Error detail: ");
Serial.println(http.errorToString(httpResponseCode).c_str());
}
http.end();
}
void sendLineNotify(String message) {
HTTPClient http;
String url = "https://notify-api.line.me/api/notify";
http.begin(url);
http.addHeader("Authorization", "Bearer " + String(lineNotifyToken));
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
String payload = "message=" + message;
int httpResponseCode = http.POST(payload);
if (httpResponseCode > 0) {
Serial.print("HTTP Response code (DHT22-1): ");
Serial.println(httpResponseCode);
} else {
Serial.print("Error on sending POST request (DHT22-1). Response code: ");
Serial.println(httpResponseCode);
Serial.print("Error detail: ");
Serial.println(http.errorToString(httpResponseCode).c_str());
}
http.end();
}
void sendLineNotify2(String message) {
HTTPClient http;
String url = "https://notify-api.line.me/api/notify";
http.begin(url);
http.addHeader("Authorization", "Bearer " + String(lineNotifyToken2));
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
String payload = "message=" + message;
int httpResponseCode = http.POST(payload);
if (httpResponseCode > 0) {
Serial.print("HTTP Response code (DHT22-2): ");
Serial.println(httpResponseCode);
} else {
Serial.print("Error on sending POST request (DHT22-2). Response code: ");
Serial.println(httpResponseCode);
Serial.print("Error detail: ");
Serial.println(http.errorToString(httpResponseCode).c_str());
}
http.end();
}