#include <DHTesp.h>
#include <WiFi.h>
#include <ThingSpeak.h>
#define SECRET_SSID "Wokwi-GUEST"
#define SECRET_PASS ""
#define SECRET_CH_ID 1948282
#define SECRET_WRITE_APIKEY "I8SPYYO2LPQS2IYB"
char ssid[] = SECRET_SSID; // your network SSID(name)
char pass[] = SECRET_PASS; // your network password
int keyIndex = 0; // your network key index number (needed only for WEP)
WiFiClient client;
unsigned long myChannelNumber = SECRET_CH_ID;
const char * mywriteAPIkey = SECRET_WRITE_APIKEY;
const int DHT_PIN = 15;
DHTesp dhtSensor;
void setup() // put your setup code here, to run once:
{
Serial.begin(115200);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
while (!Serial){
;// Wait for serial port to connect.Needed for Leonardo native USB port only
}
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client);
}
void loop()
{
if (WiFi.status() != WL_CONNECTED)
{
Serial.print("Attempting to connect to SSID: ");
Serial.println(SECRET_SSID);
while(WiFi.status() !=WL_CONNECTED)
{
WiFi.begin(ssid, pass);
Serial.print(".");
delay(5000);
}
Serial.println("\nConnected.");
}
TempAndHumidity data = dhtSensor.getTempAndHumidity();
Serial.println("Temp: " + String(data.temperature, 2) + "°C");
Serial.println("Humidity: " + String(data.humidity, 1) + "%");
Serial.println("---");
ThingSpeak.setField(1, data.temperature);
ThingSpeak.setField(2, data.humidity);
int x = ThingSpeak.writeFields(myChannelNumber, mywriteAPIkey);
if(x == 200)
{
Serial.println("Channel update successfull.");
}
else
{
Serial.println("Problem updating channel. HTTP error code" + String(x));
}
delay(15000);
}