#include <WiFi.h>
#include <ThingSpeak.h>
#include "DHTesp.h"
#define SECRET_SSID "Wokwi-GUEST"
#define SECRET_PASS ""
#define SECRET_CH_ID 2272266
#define SECRET_WRITE_APIKEY "2M1WSUSWETLWV29P" // replace XYZ with your channel write API Key
#define DHT22_PIN 2
DHTesp DHT;
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;
float Hum;
float Temp;
void setup()
{
Serial.begin(115200); //Initialize serial
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo native USB port only
}
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client); // Initialize ThingSpeak
}
void loop()
{
TempAndHumidity data = DHT.getTempAndHumidity();
Hum = data.humidity;
Temp= data.temperature;
// Connect or reconnect to WiFi
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); // Connect to WPA/WPA2 network. Change this line if using open or WEP network
Serial.print(".");
delay(5000);
}
Serial.println("\nConnected.");
}
//Writting to Thinkspeak
ThingSpeak.writeField(myChannelNumber, 1, Temp, myWriteAPIKey);
ThingSpeak.writeField(myChannelNumber, 2, Hum, myWriteAPIKey);
}