#include <ESP32Servo.h>
#include <WiFi.h>
#include <ThingSpeak.h>
Servo servo;
WiFiClient client;
unsigned long WeatherChanel = 2347108;
const char *writeAPIKey = "G2G42YKUGVEZQYYD";
const char *readAPIKey = "7QHNX97IUPW2PU7J";
const int servoPin = 18;
unsigned long lastTime = 30000;
int pos = 0;
float T = 0, H = 0;
void setup() {
Serial.begin(115200);
servo.attach(servoPin);
ThingSpeak.begin(client);
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(250);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
if ((millis() - lastTime) > 30000) {
T = ThingSpeak.readFloatField(WeatherChanel, 1, readAPIKey);
H = ThingSpeak.readFloatField(WeatherChanel, 2, readAPIKey);
Serial.print("Temp: " + String(T, 2) + "C ");
Serial.print("Humidity: " + String(H, 1) + "% ");
if (T <= 14) {
if (H >= 0 && H <= 40) {
pos = 0;
} else if (H > 40 && H <= 60) {
pos = map(H, 40, 60, 0, 45);
} else if (H > 60 && H <= 100) {
pos = map(H, 60, 100, 45, 90);
}
} else if (T > 14 && T <= 18) {
if (H >= 0 && H <= 40) {
pos = 0;
} else if (H > 40 && H <= 60) {
pos = map(H, 40, 60, 0, 45);
} else if (H > 60 && H <= 100) {
pos = map(H, 60, 100, 45, 90);
}
} else {
if (H > 60 && H <= 100) {
pos = map(H, 60, 100, 25, 120);
} else if (H > 40 && H <= 60) {
pos = map(H, 40, 60, 10, 65);
} else if (H >= 0 && H <= 40) {
pos = map(H, 0, 40, 0, 25);
}
}
Serial.print("<: ");
Serial.println(pos);
servo.write(pos);
lastTime = millis();
}
}