#include <DHTesp.h>
#include <WiFi.h>
#include <ThingSpeak.h>
DHTesp pritheesh;
TempAndHumidity data;
WiFiClient client;
const long myChannelNumber = 2624963; // Replace with your channel number
const char *myWriteAPIKey = "0Z7QCX5KWN0PMXJJ"; // Replace with your write API key
const char ssid[] = "Wokwi-GUEST"; // Replace with your Wi-Fi SSID
const char pass[] = ""; // Replace with your Wi-Fi password
void setup() {
Serial.begin(115200);
pritheesh.setup(27, DHTesp::DHT22); // Set up the DHT sensor on GPIO 27
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass);
Serial.print("Connecting to ");
Serial.print(ssid);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected.");
ThingSpeak.begin(client); // Initialize ThingSpeak
}
void loop() {
data = pritheesh.getTempAndHumidity();
int humi = int(data.humidity);
int temp = int(data.temperature);
// Set the fields and update ThingSpeak
ThingSpeak.setField(1, temp);
ThingSpeak.setField(2, humi);
int statusCode = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if (statusCode == 200) {
Serial.println("Update successful.");
} else {
Serial.print("Error code: ");
Serial.println(statusCode);
}
Serial.print("Temperature: ");
Serial.println(temp);
Serial.print("Humidity: ");
Serial.println(humi);
delay(20000); // Wait 20 seconds before the next update
}