#include<WiFi.h>
#include <ThingSpeak.h>
#include <DHTesp.h>
DHTesp dhtSensor;
TempAndHumidity data;
const int DHT_PIN=13;
char ssid[]="Wokwi-GUEST";
char pass[]="";
WiFiClient client;
unsigned long mychannelnumber= 2254650;
const char * myWriteAPIKey="JJ3LPOHHF3D0BQW8";
int statusCode;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client);
dhtSensor.setup(DHT_PIN,DHTesp::DHT22);
pinMode(27, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
data=dhtSensor.getTempAndHumidity();
if(WiFi.status()!=WL_CONNECTED)
{
Serial.print("Attempting to connect");
while(WiFi.status()!=WL_CONNECTED)
{
WiFi.begin(ssid,pass);
Serial.print(".");
delay(5000);
}
Serial.println("\nConnected,");
}
Serial.println("Humi :" + String(data.humidity));
Serial.println("Temp :" + String(data.temperature));
Serial.println("--------------------");
if(data.temperature>40)
{
digitalWrite(27, HIGH);
}
else{
digitalWrite(27, LOW);
}
ThingSpeak.setField(1,data.temperature);
ThingSpeak.setField(2,data.humidity);
statusCode=ThingSpeak.writeFields(mychannelnumber,myWriteAPIKey);
if(statusCode==200)
{
Serial.println("Channel update successful");
}
else{
Serial.println("problem writing data.HTTP error code:"+ String(statusCode));
}
delay(10); // this speeds up the simulation
}