#include <WiFi.h>
#include<ThingSpeak.h>
#include <HTTPClient.h>
#include <DHT.h>
DHT dht(26, DHT22);
WiFiClient client;
// Set up WiFi credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// Set up ThingSpeak API key and channel ID
String apiKey = "VA71KCRVP7TQ1OHN";
String channelId = "2109205";
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(26, INPUT); // Setting D34 pin as input pin
dht.begin();
delay(2000);
ThingSpeak.begin(client);
WiFi.begin(ssid,password);
}
void loop() {
// put your main code here, to run repeatedly:
float temp = dht.readTemperature();
Serial.print(temp);
Serial.print(",");
delay(500);
while (WiFi.status() != WL_CONNECTED) {
sendData(temp);
}
}
void sendData(float temp) {
// Create an HTTPClient object
HTTPClient http;
// Create the URL with the data to send
String url = "http://api.thingspeak.com/update?";
url += "api_key=" + apiKey;
url += "&field1=" + String(temp);
Serial.println(url);
// Send the HTTP request
http.begin(url);
int httpResponseCode = http.GET();
// Check the response code
if (httpResponseCode > 0) {
Serial.println("Data sent to ThingSpeak successfully");
} else {
Serial.println("Error sending data to ThingSpeak");
}
// Close the connection
http.end();
}