#include <WiFi.h>
#include <HTTPClient.h>
#include <DHT.h>
#define DHTTYPE DHT22
const int DHTPIN = 4;
DHT dht(DHTPIN, DHTTYPE);
const int Led = 8;
const int maxTemp = 80; // nhiet do cao nhat
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* serverName = "http://567.335.276.106:1880/update-sensor";
unsigned long lastTime = 0;
unsigned long timerDelay = 5000;
void setup() {
pinMode(Led,OUTPUT);
Serial.begin(115200);
dht.begin();
//wifi
WiFi.begin(ssid, password);
Serial.println("Connecting");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
Serial.println("Timer 5s");
}
void loop() {
//Send an HTTP POST request every 10 minutes
if ((millis() - lastTime) > timerDelay) {
float tC = dht.readTemperature();
Serial.print(tC);
if(tC >= maxTemp) {
digitalWrite(Led,HIGH);
} else{
digitalWrite(Led,LOW);
}
// kiểm tra xem nhiệt độ có quá không, nếu quá 80 độ => gửi request
if(WiFi.status()== WL_CONNECTED && tC >= maxTemp){
WiFiClient client;
HTTPClient http;
http.begin(client, serverName);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
String httpRequestData = "api_key=1234&sensor=DS18B20&alarm=1";
int httpResponseCode = http.POST(httpRequestData);
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
http.end();
}
lastTime = millis();
}
}