Sunday, 6 July 2025

IOT LAB EXP3 PROGRAM


 #include <ESP8266WiFi.h>

 #include <ThingSpeak.h>
 #include <DHT.h>
 #define DHTPIN 4
 #define DHTTYPE DHT11
 const char* ssid = "PLACEMENTDRIVE";
 // Replace with your Wi-Fi SSID
 const char* password = "specplacements"; // Replace with your Wi-Fi password
 unsigned long ch_id = 2995467; // Replace with your ThingSpeak Channel ID
 const char* apiKey = "EW2AWHPQ8QW31G1V"; // Replace with your ThingSpeak API Key
 DHT dht(DHTPIN, DHTTYPE);
 WiFiClient client;

 
 void setup() {
 Serial.begin(9600);
 dht.begin();
 WiFi.begin(ssid, password);
 while (WiFi.status() != WL_CONNECTED) {
 delay(500);
 Serial.print(".");
 }
 Serial.println("WiFi connected");
 ThingSpeak.begin(client);
 }
 void loop() {
 
 float h = dht.readHumidity();
 float t = dht.readTemperature();
 if (isnan(h) || isnan(t)) {
 Serial.println("Failed to read from DHT sensor!");
 return;
 }
 int httpCode = ThingSpeak.writeField(ch_id, 1, "Chandu:"+String(h)+","+String(t), apiKey);
 
 if(httpCode == 200){
 Serial.println("Channel write successful.");
 }else{
 Serial.println("Problem writing to channel. HTTP error code " + String(httpCode) );
 }
 delay(20000); // Wait 20 seconds before next reading
 }

2 comments: