#include <DHTesp.h>;
#include <WiFi.h>
#include <ThingSpeak.h>
//wifi setup line
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
WiFiClient client;
unsigned long myChannelNumber = 2253935;
const char * myWriteAPIKey = "WL4X5SBXVM8MYH2G";
int statusCode;
DHTesp dht;
TempAndHumidity data;
void setup() {
Serial.begin(115200);
//
//wifi mode setup
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client);
dht.setup(12,DHTesp::DHT22);
}
void loop() {
//wifi connection setup
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.");
}
// put your main code here, to run repeatedly:
data=dht.getTempAndHumidity();
Serial.println("humidity: " + String(data.humidity));
Serial.println("Temp: " + String(data.temperature));
delay(10); // this speeds up the simulation
ThingSpeak.setField(1, data.humidity);
ThingSpeak.setField(2, data.temperature);
statusCode = ThingSpeak.writeFields(myChannelNumber,myWriteAPIKey);
if(statusCode == 200) { //successful writing code
Serial.println("Channel update successful.");
}
else {
Serial.println("Problem Writing data. HTTP error code :" +
String(statusCode));
}
delay(15000);
}