#include <ESP32Servo.h>
#include <WiFi.h>
#include "ThingSpeak.h"
unsigned long myChannelNumber = 2327587;
const char * myWriteAPIKey = "ZL2U9OPIZOYYMBA8";
const char * myReadAPIKey = "VG7VR9017ZPN10FA";
const int servoPin = 18;
unsigned long lastTime=30000;
int pos = 0;
float T=0, H=0;
Servo servo;
WiFiClient client;
void setup() {
Serial.begin(115200);
servo.attach(servoPin);
ThingSpeak.begin(client); // Initialize ThingSpeak
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(250);
Serial.print(".");
}
Serial.println("");
Serial.println("готово!");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
if ((millis() - lastTime) > 30000){
T=ThingSpeak.readFloatField(myChannelNumber, 1, myReadAPIKey);
H=ThingSpeak.readFloatField(myChannelNumber, 2, myReadAPIKey);
Serial.println("Temp: " + String(T, 2) + "°C");
Serial.println("Humidity: " + String(H, 1) + "%");
if (T<=14){
if (H>60){
pos = map(H, 60, 100, 0, 15);
}
else {
pos=0;
}
}
if (T>14&&T<=18){
if (H>=40&&H<=60){
pos = map(H, 40, 60, 0, 45);
}
if (H>60){
pos = map(H, 60, 100, 15, 90);
}
else {
pos = 0;
}
}
if (T>18){
if (H>=0&&H<=40) {
pos = map(H, 0, 40, 0, 10);
}
if (H>=40&&H<=60){
pos = map(H, 40, 60, 45, 90);
}
if (H>60){
pos = map(H, 60, 100, 90, 180);
}
}
Serial.print("<: ");
Serial.println(pos);
servo.write(pos);
lastTime=millis();
}
delay(1000);
}