Name: Kamble Tejas Filip
Roll No: 31
Class:TE-IT
Assignment-06
Title:Design and implement parameter monitoring IoT system keeping records on Cloud
such as 'environment humidity and temperature monitoring'.
***********************************************************************************
**********************************
#include <DHT.h> // including the library of DHT11 temperature and humidity sensor
#include <ESP8266WiFi.h>
#define DHTTYPE DHT11 // DHT 11
#define DHTPIN 0
const char* ssid = "realme c2"; // Wifi name
const char* password = "12341234"; //Wifi password
const char* host = "api.thingspeak.com";
const char* writeAPIKey = "ED6CD4XNA0XTCYS2"; //Unique APIkey
DHT dht(DHTPIN, DHTTYPE,15);
void setup()
{
//Initalizing the Sensor
Serial.begin(115200); // serial monitor baud rate (the speed of sending data to
node mcu)
dht.begin();
delay(1000);
Serial.println("Connecting to ....");
Serial.print(ssid);
WiFi.begin(ssid, password); // search for wifi and show connection status
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("Wifi Connected !!!");
}
void loop()
{
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
if(isnan(humidity) || isnan(temperature)) // Check if values are valid
{return;}
// make TCP connections
WiFiClient client;
const int httpPort =80;
if (!client.connect(host, httpPort)) // connect to server with port no 80
{return;}
//Reformatting data to post the string data
String url= "/update?key=";
url += writeAPIKey;
url += "&field1=";
url += String(humidity);
url += "&field2=";
url += String(temperature);
url+= "\r\n";
//Request to the server (for posting data to the server)
client.print(String("GET ") + url + "HTTP/1.1\r\n" + "Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
//For printing the values in Serial Monitor
Serial.print("Humidity");
Serial.print(humidity);
Serial.print("\n");
Serial.print("Temperature");
Serial.print(temperature);
Serial.print("Send to Thingspeak \n");
client.stop(); // stop 15 sec before sending next data packet
Serial.print("Wait for 15sec to update data pack");
delay(1000);
}