// channel id : 2342585
//channel api key : 4ONYR207PC74LRTJ
#include <WiFi.h>
#include "DHTesp.h"
#include "ThingSpeak.h"
#include <ESP32Servo.h>
const int DHT_PIN = 15;
//const int LED_PIN = 13;
const char* WIFI_NAME = "Wokwi-GUEST";
const char* WIFI_PASSWORD = "";
const int myChannelNumber =2346004 ;
const char* myApiKey = "4ONYR207PC74LRTJ";
const char* server = "api.thingspeak.com";
DHTesp dhtSensor;
WiFiClient client;
Servo servo;
void setup() {
Serial.begin(115200);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
// pinMode(27, OUTPUT); Servo thì không cần
servo.attach(27, 500, 2400); // Servo cần cái này
WiFi.begin(WIFI_NAME, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED){
delay(1000);
Serial.println("Wifi not connected");
}
Serial.println("Wifi connected !");
Serial.println("Local IP: " + String(WiFi.localIP()));
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client);
}
// int pos = 0;
void loop() {
TempAndHumidity data = dhtSensor.getTempAndHumidity();
ThingSpeak.setField(1,data.temperature);
ThingSpeak.setField(2,data.humidity);
int x = ThingSpeak.writeFields(myChannelNumber,myApiKey);
digitalWrite(12, HIGH);
while (data.temperature > 45 || data.temperature < 12 || data.humidity > 70 || data.humidity < 40){
digitalWrite(13, HIGH);
delay(1000); // Delay 1s
digitalWrite(13, LOW);
delay(1000); // Delay 1s
digitalWrite(13, HIGH);
delay(1000); // Delay 1s
digitalWrite(13, LOW);
delay(1000); // Delay 1s
digitalWrite(13, HIGH);
delay(1000); // Delay 1s
digitalWrite(13, LOW);
delay(1000); // Delay 1s
data = dhtSensor.getTempAndHumidity(); // Cập nhật dữ liệu của điều kiện
x = ThingSpeak.writeFields(myChannelNumber,myApiKey);
ThingSpeak.setField(1,data.temperature);
ThingSpeak.setField(2,data.humidity);
Serial.println("Temp: " + String(data.temperature, 2) + "°C");
Serial.println("Humidity: " + String(data.humidity, 1) + "%");
if(x == 200){
Serial.println("Data pushed successfull");
}else{
Serial.println("Push error" + String(x));
}
Serial.println("---");
}
Serial.println("Temp: " + String(data.temperature, 2) + "°C");
Serial.println("Humidity: " + String(data.humidity, 1) + "%");
if(x == 200){
Serial.println("Data pushed successfull");
}else{
Serial.println("Push error" + String(x));
}
Serial.println("---");
delay(10000);
}