#include <WiFi.h>
#include <HTTPClient.h>
#include <DHT.h>
// Replace with your network credentials
const char* ssid = "Raji";
const char* password ="samraji16";
// Replace with your ThingSpeak channel details
const char* server = "api.thingspeak.com";
const String apiKey = "2D9FOLL4G7M6P0IG";
const int fieldId = 1;
// Replace with your sensor details
#define DHTPIN 15 // Pin connected to the DHT sensor
#define DHTTYPE DHT11 // DHT sensor type (DHT11 or DHT22)
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
// Initialize DHT sensor
dht.begin();
}
void loop() {
// Wait a few seconds between measurements
delay(2000);
// Read temperature from sensor
float temperature = dht.readTemperature();
if (isnan(temperature)) {
Serial.println("Failed to read temperature from DHT sensor!");
return;
}
// Create the URL for ThingSpeak update
String url = "http://" + String(server) + "/update?api_key=" + apiKey + "&field" + String(fieldId) + "=" + String(temperature);
// Send HTTP GET request to ThingSpeak
HTTPClient http;
http.begin(url);
int httpCode = http.GET();
if (httpCode > 0) {
Serial.printf("HTTP response code: %d\n", httpCode);
if (httpCode == HTTP_CODE_OK) {
String payload = http.getString();
Serial.println("Response: " + payload);
}
} else {
Serial.println("Failed to send HTTP request");
}
http.end();
}