0% found this document useful (0 votes)
13 views64 pages

IoT PPT07 Demo1 Smart Home ESP8266

The document outlines a project on smart home monitoring using ESP8266 and a web server, detailing the physical setup, router configuration, and cloud server setup with ThingSpeak. It includes instructions for configuring the Arduino IDE, programming the ESP8266 devices, and connecting various sensors for data collection. The project aims to create a local network with multiple ESP8266 devices acting as clients to monitor environmental parameters and upload data to the cloud for analysis.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views64 pages

IoT PPT07 Demo1 Smart Home ESP8266

The document outlines a project on smart home monitoring using ESP8266 and a web server, detailing the physical setup, router configuration, and cloud server setup with ThingSpeak. It includes instructions for configuring the Arduino IDE, programming the ESP8266 devices, and connecting various sensors for data collection. The project aims to create a local network with multiple ESP8266 devices acting as clients to monitor environmental parameters and upload data to the cloud for analysis.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 64

CS578: Internet of Things

Smart Home Monitoring


Using
ESP8266 and Webserver

Dr. Manas Khatua


Assistant Professor, Dept. of CSE, IIT Guwahati
E-mail: [email protected] , URL: http://manaskhatua.github.io/

“Try not to become a man of success. Rather become a man of value.” – Albert Einstein
System Diagram

14-08-2024 Dr. Manas Khatua 2


Physical Setup
ThingSpeak cloud server
accessing from a
Laptop/PC/Smartphone

14-08-2024 Dr. Manas Khatua 3


Router Configuration
To Connect with IITG Internet

14-08-2024 Dr. Manas Khatua 4


Router Configuration
 This is TP-Link WiFi Router
 ESP8266 (local server) will connect to this WiFi AP
 Sensor data will be uploaded to ThingSpeak server
through this WiFi AP.

 Login TP-Link WiFi using given IP (192.168.0.1) and password written on its label.
 Do the following:
 Go to Quick Setup and click on Next.
 Choose Operation Mode as Wireless Router and click on Next.
 Select WAN Connection Type as Static IP and click on Next.
 Set the Static IP, Subnet Mask, Default Gateway, Primary DNS Server, Secondary
DNS Server and click on Next.
 Select the radio bands (2.4 GHz and/or 5 GHz) and click on Next.
 Setup the Wireless radio bands selected above and click on Next.
 Confirm the setup by clicking on Save. The router reboots and reconnects.

14-08-2024 Dr. Manas Khatua 5


Cont…

14-08-2024 Dr. Manas Khatua 6


Cont…

14-08-2024 Dr. Manas Khatua 7


Cont…

14-08-2024 Dr. Manas Khatua 8


Cont…

14-08-2024 Dr. Manas Khatua 9


Cont…

14-08-2024 Dr. Manas Khatua 10


Cont…

14-08-2024 Dr. Manas Khatua 11


Cont…

14-08-2024 Dr. Manas Khatua 12


Cont…

********

14-08-2024 Dr. Manas Khatua 13


Cont…

14-08-2024 Dr. Manas Khatua 14


Connecting with Internet

• You should be able to access Internet in your Mobile/Laptop using TP-Link WiFi AP

14-08-2024 Dr. Manas Khatua 15


Cloud Server Configuration
to Access Web Service

14-08-2024 Dr. Manas Khatua 16


Configure to use Cloud Server

• We use ThingSpeak server http://www.thingspeak.com

• First create an user account


• Then create a channel on the ThingSpeak to upload the data

14-08-2024 Dr. Manas Khatua 17


Cont…

14-08-2024 Dr. Manas Khatua 18


Cont…

14-08-2024 Dr. Manas Khatua 19


Cont…

14-08-2024 Dr. Manas Khatua 20


Create Channel Display

• Select Private View of the


created channel.

• Click Add Widgets

• Select the Numeric Display


widget, and then set the
display options.

14-08-2024 Dr. Manas Khatua 21


API Key and Channel ID

• To send data to ThingSpeak, we need unique write API key and Channel ID, which
will be used later in code to upload the data to ThingSpeak website

• Click on “API Keys” button to get your unique “Write API Key”
• “Channel ID” is also given on the top
14-08-2024 Dr. Manas Khatua 22
IoT Network Configuration

14-08-2024 Dr. Manas Khatua 23


IoT Network Configuration
• There are total five ESP8266
– one is acting as server,
– other four as clients in local network.

• ESP1- ESP8266 acting as local server


• ESP2- ESP8266 with Light sensor
• ESP3- ESP8266 with Pulse sensor
• ESP4- ESP8266 with vibration sensor
• ESP5- ESP8266 with temperature & humidity sensor

• Note: Unique ID for each ESP will be needed in programming

14-08-2024 Dr. Manas Khatua 24


Sensor Configuration

ESP8266 with LDR Sensor


• Connect VCC pin of LDR sensor with 3V3 pin of ESP2
• Connect GND pin of LDR sensor with GND pin of ESP2
• Connect DATA OUT pin of LDR sensor with A0 pin of ESP2.

ESP8266 with Pulse Sensor


• Connect VCC pin of pulse sensor with 3V3 pin of ESP3
• Connect GND pin of pulse sensor with GND pin of ESP3
• Connect SIGNAL pin of pulse sensor with A0 pin of ESP3

14-08-2024 Dr. Manas Khatua 25


Cont…

ESP8266 with Vibration Sensor


• Connect VCC pin of vibration sensor with VIN pin of ESP4
• Connect GND pin of vibration sensor with GND pin of ESP4
• Connect DATA OUT pin of vibration sensor with A0 pin of ESP4

ESP8266 with Temperature & Humidity Sensor (DHT11)


• Connect VCC pin of DHT11 with VIN pin of ESP5
• Connect DATA OUT pin of DHT11 with D3 pin of ESP5
• Connect GND pin of DHT11 with GND pin of ESP5

14-08-2024 Dr. Manas Khatua 26


Arduino
Tool Configuration

14-08-2024 Dr. Manas Khatua 27


Configure Arduino IDE
• Download and Install Arduino IDE https://www.arduino.cc/en/Main/Software

• When the Arduino IDE first opens, this is what you should see:

14-08-2024 Dr. Manas Khatua 28


Install ESP8266 Board in IDE
• Go to File --> Preferences
• Enter the below URL into Additional Board Manager URLs field and press the “OK” button
http://arduino.esp8266.com/stable/package_esp8266com_index.json OR
https://github.com/esp8266/Arduino/releases/download/2.3.0/package_esp8266com_index.json

14-08-2024 Dr. Manas Khatua 29


Cont…
• Go to Tools > Board > Board Manager

• Scroll down, select the ESP8266 board menu and install “esp8266 by ESP8266
Community”

14-08-2024 Dr. Manas Khatua 30


Cont…

• Select the appropriate board


• Go to Tools >Board > NodeMCU 1.0 (ESP-12E Module)
• Finally, re-open the Arduino IDE

14-08-2024 Dr. Manas Khatua 31


Install Sensor Libraries
• In this demo, we use DHT11 sensor for which we will be using DHT.h
header file in the code. So, this header file should be installed.

• Install Using the Library Manager


– click to Sketch menu then Include Library > Manage Libraries
– Search for “DHT” on the Search box and install the DHT library from Adafruit.

14-08-2024 Dr. Manas Khatua 32


Cont…
• After installing the DHT library from Adafruit, install “Adafruit Unified Sensor” libraries.

• There exist other methods for installing libraries


– Importing a .zip Library
• Sketch --> Include Library --> Add .Zip Library

– Manual Installation of Library


• Download the library as .Zip --> extract it
• Place the files in File --> Preferences --> Sketchbook location
• Restart Arduino IDE

14-08-2024 Dr. Manas Khatua 33


MCU Programming

14-08-2024 Dr. Manas Khatua 34


ESP8266 with Local Server
For ESP1, write the following code in the Arduino IDE and save as Local_Server_ESP1.ino
Install ThingSpeak.h library. Change the red colored text in code according to your setup.

#include <ESP8266WiFi.h> //Including ESP8266 library


#include<ESP8266WebServer.h> //Including ESP8266WebServer library for web server
#include<ThingSpeak.h> //Including ThingSpeak library

IPAddress IP(172,16,117,192); //Static IP address of local server


IPAddress gateway(172,16,112,1); //Gateway of the network
IPAddress mask(255, 255, 248, 0); //Subnet mask of the network
WiFiClient client;
WiFiServer server(80);

unsigned long myChannelNumber = 2244718; //Replace with channelID of ThingSpeak channel ID


const char * myWriteAPIKey = "T4N14GFNKOPDWIWL"; //Replace WriteAPIKey of channel

const char* softAPssid = "ESP1_Server"; //SSID of the hotspot of ESP8266 acting as local server
const char* password = "12345678"; //Password of the hotspot of ESP8266 acting as local server

const char* wifissid = “TP-Link_A522"; //Replace with SSID of WIFI router providing internet access
const char* pass = "12345678"; //Password of WIFI router providing internet access

14-08-2024 Dr. Manas Khatua 35


Cont…
void setup() {
WiFi.mode(WIFI_AP_STA); //station mode and access point mode both at the same time
Serial.begin(9600); //Serial communication at baud rate of 9600 for debugging purpose
delay(100);
Serial.println(WiFi.getMode());
Serial.print("Configuring SoftAP....");
Serial.println(WiFi.softAPConfig(IP, gateway, mask)? "Ready" : "Failed");
delay(10);
Serial.println("Setting SoftAP...");
Serial.println(WiFi.softAP(softAPssid, password));
delay(10); • Two functions exist in the
Serial.println(WiFi.softAPIP());
delay(500); programme: setup () and loop ()
WiFi.begin(wifissid, pass); • setup(): This function runs once
while(WiFi.status()!=WL_CONNECTED) { when ESP first boots
Serial.print("."); • loop(): This function reads the LDR
delay(500);
} sensor value and connects to local
Serial.print("Connected to Wifi with ssid "); server then send sends data to
Serial.println(wifissid); local server
Serial.print("WiFi IP address: ");
Serial.println(WiFi.localIP()); // WIFI router IP address
ThingSpeak.begin(client);
server.begin(); //Start local server
}

14-08-2024 Dr. Manas Khatua 36


Cont…
void loop() {
Serial.printf("Stations connected = %d\n", WiFi.softAPgetStationNum());
WiFiClient client = server.available(); //Waiting for the incoming data if client is ready to send
if (!client) {return;}
String select_fun = client.readStringUntil('\r'); //Reads the ESP8266 ID (of clients)

if(select_fun==“5") { //If ESP5 sends the data


String temp = client.readStringUntil('\r'); //Reads the temperature value
String Humidity = client.readStringUntil('\r'); //Reads the humidity value
//Upload the temp value to ThingSpeak server as first field of channel

ThingSpeak.writeField(myChannelNumber, 1, temp, myWriteAPIKey);


delay(15000); //Wait for 15 sec after one entry
//Upload the humidity value to ThingSpeak server as second field of channel

ThingSpeak.writeField(myChannelNumber, 2, Humidity, myWriteAPIKey);


Serial.print("Temperature: ");
Serial.print(temp);
Serial.print(" degree celsius, Humidity: ");
Serial.print(Humidity);
Serial.print("%. ");
Serial.println("Sent to ThingSpeak Server...");
}

14-08-2024 Dr. Manas Khatua 37


Cont…
if(select_fun=="2") { //If ESP2 sends the data
String LDRval = client.readStringUntil('\r'); //Reads light sensor value
//Upload the light sensor value to ThingSpeak server as third field of channel
ThingSpeak.writeField(myChannelNumber, 3, LDRval, myWriteAPIKey);
Serial.print("LDR sensor data value: ");
Serial.println(LDRval);
Serial.println("Sent to ThingSpeak Server...");
}
if(select_fun=="3") { //If ESP3 sends the data
String pulseRate = client.readStringUntil('\r'); //Reads pulse rate
//Upload the pulse rate to ThingSpeak server as fourth field of channel
ThingSpeak.writeField(myChannelNumber, 4, pulseRate, myWriteAPIKey);
Serial.print("Pulse rate: ");
Serial.print(pulseRate);
Serial.println(" BPM. Sent to ThingSpeak Server..");
}
if(select_fun=="4"){ //If ESP4 sends the data
String Vibval = client.readStringUntil('\r'); //Reads vibration sensor data
//Upload the vibration sensor data value to ThingSpeak server as fifth field of channel
ThingSpeak.writeField(myChannelNumber, 5, Vibval, myWriteAPIKey);
Serial.print("Vibration Sensor data: ");
Serial.print(Vibval);
Serial.println(" Sent to ThingSpeak server..");
}
delay(15000); //waits for 15 secs after each transmission
}

14-08-2024 Dr. Manas Khatua 38


ESP8266 with LDR Sensor
For ESP2, write the following code in the Arduino IDE and save as LDR_client.ino

#include<ESP8266WiFi.h> // Including ESP8266 library

char ssid[]="ESP1_Server"; //Network ssid of hotspot of local server


char pass[]="12345678"; // Password of hotspot of local server
int val;
int LDRpin = A0; //LDR Pin Connected to A0 pin
IPAddress server(172,16,117,192); // IP address of local server
WiFiClient client;

• Change the IP address of Local Server (i.e. ESP1)


• Change the SSID and Password of WiFi AP hosted in Local Server

• Two functions exist in the programme: setup () and loop ()


• setup(): This function runs once when ESP first boots
• loop(): This function reads the LDR sensor value and connects to local server
then send sends data to local server

14-08-2024 Dr. Manas Khatua 39


Cont…
void setup()
{
Serial.begin(9600); // Serial communication at baud rate of 9600 for debugging purpose
delay(10);
WiFi.mode(WIFI_STA); // ESP8266 in station mode
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, pass);
Serial.println();
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(500);
}
Serial.println();
Serial.println("WiFi connected");
Serial.print("LocalIP:"); Serial.println(WiFi.localIP());
Serial.println("MAC:" + WiFi.macAddress());
Serial.print("Gateway:"); Serial.println(WiFi.gatewayIP());
Serial.print("AP MAC:"); Serial.println(WiFi.BSSIDstr()); // MAC address of access point
}

14-08-2024 Dr. Manas Khatua 40


Cont…
void loop()
{
val = analogRead(LDRpin); // Reads the light sensor value
if(client.connect(server,80)) // Connect to local server
{
client.print("2\r"); // before sending data first send ESP8266 ID as 2
Serial.print("LDR sensor value: ");
Serial.println(val);
String LDRval = String(val);
LDRval += "\r"; // Add end delimiter
client.print(LDRval); // Send to local server
Serial.println("Sent to Local Server..");
delay(15000);
}
client.stop();
}

14-08-2024 Dr. Manas Khatua 41


ESP8266 with Pulse Sensor
#define pulsePin A0 // Pulse sensor input pin A0
#include<ESP8266WiFi.h> // Including ESP8266 library

char ssid[] = "ESP1_Server"; // Replace with SSID of hotspot of local server


char pass[] = "12345678"; // Replace with password of hotspot of local server
IPAddress server(172,16,117,192); // IP address of local server
WiFiClient client; For ESP3, write the
following code in the
int rate[10]; // array to hold last ten IBI value Arduino IDE and save as
unsigned long sampleCounter = 0; // used to determine pulse timing
unsigned long lastBeatTime = 0; // used to find IBI Pulse_client.ino
unsigned long lastTime = 0, N;
int BPM = 0; // int that holds raw analog in 0. updated every 2mS
int IBI = 0; // int that holds time interval between beats! Must be seeded!
int P = 512; // used to find peak in pulse wave, seeded
int T = 512; // used to find trough in pulse wave, seeded
int thresh = 512; // used to find instant moment of heart beat, seeded
int amp = 100; // used to hold amplitude of pulse waveform, seeded
int Signal; // holds incoming raw data
boolean Pulse = false; // "True" when heartbeat is detected. "False" when not a "live beat".
boolean firstBeat = true; // used to seed rate array so we startup with reasonable BPM
boolean secondBeat = true; // used to seed rate array so we startup with reasonable BPM
boolean QS = false; // Becomes true when ESP8266 finds a beat

14-08-2024 Dr. Manas Khatua 42


Cont…
void setup()
{
Serial.begin(9600); // Serial communication at baud rate of 9600 for debugging purpose
delay(10);
WiFi.mode(WIFI_STA); // ESP8266 in station mode
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, pass);
Serial.println();
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(500);
}
Serial.println();
Serial.println("WiFi connected");
Serial.print("LocalIP:"); Serial.println(WiFi.localIP());
Serial.println("MAC:" + WiFi.macAddress());
Serial.print("Gateway:"); Serial.println(WiFi.gatewayIP());
Serial.print("AP MAC:"); Serial.println(WiFi.BSSIDstr()); // MAC address of access point
}

14-08-2024 Dr. Manas Khatua 43


Cont…
void loop(){
if (QS == true){ //if ESP8266 finds a beat
if (client.connect(server, 80)){ // Connect to local server
client.print("3\r"); // before sending data first send ESP8266 ID as 3
String pulseRate = String(BPM); // Convert into string
pulseRate +="\r"; // Add "r" as end delimiter
Serial.print("Pulse rate: ");
Serial.print(BPM);
Serial.println(" BPM.");
client.print(pulseRate); // send data to local server
Serial.println("Sent to local server..");
}
QS = false;
client.stop();
delay(15000);
}
else if(millis() >= (lastTime + 2)) {
readPulse();
lastTime = millis();
}
}

14-08-2024 Dr. Manas Khatua 44


Cont…
void readPulse() {
Signal = analogRead(pulsePin); //Read pulse sensor value
sampleCounter += 2; // Keeps track of the time in mS
int N = sampleCounter - lastBeatTime; // Monitor the time since the last beat to avoid noise
detectSetHighLow(); // find the peak and trough of the pulse wave
// Now it's time to look for the heart beat
// signal surges up in value every time there is a pulse
if(N > 250){ // avoid high frequency noise
if((Signal > thresh) && (Pulse == false) && (N > (IBI/5)*3))
pulseDetected();
}
if (Signal < thresh && Pulse == true) {
Pulse = false;
amp = P - T;
thresh = amp / 2 + T; void detectSetHighLow() {
P = thresh; if(Signal < thresh && N > (IBI/5)* 3)
T = thresh; // avoid dichrotic noise by waiting 3/5 of last IBI
} {
if (N > 2500) { if (Signal < T) { // T is the trough
thresh = 512; T = Signal; // Keep track of lowest point in pulse wave
P = 512; }
T = 512; }
lastBeatTime = sampleCounter; if (Signal > thresh && Signal > P) // thresh condition helps avoid noise
firstBeat = true; {
secondBeat = true; P = Signal; // P is the peak
} } // Keep track of highest point in pulse wave
} }

14-08-2024 Dr. Manas Khatua 45


Cont…
void pulseDetected()
{
Pulse = true; // set the pulse flag when there is a pulse
IBI = sampleCounter - lastBeatTime; // time between beats in mS
lastBeatTime = sampleCounter; //keep track of time for next pulse
if (firstBeat) // if it's the first time beat is found BPM = 60000 / runningTotal;
{ // how many beats can fit into a minute? that's BPM!
firstBeat = false; //clear firstBeat flag QS = true;
return; if (client.connect(server, 80)) //Connects to local server
} {
if (secondBeat) // if this is second beat client.print("3\r");
{ //before sending the data sends ESP8266 ID as 3
secondBeat = false; // clear secondBeat flag String pulseRate = String(BPM);
for (int i = 0; i <= 9; i++) // Converting integer data into string
{ pulseRate +="\r";
rate[i] = IBI; // Add end Delimiter "r" in the data
} Serial.print("Pulse rate: ");
} Serial.print(BPM);
word runningTotal = 0; // clear the runningTotal variable Serial.println(" BPM.");
for (int i = 0; i <= 8; i++) //Shift data in the rate array client.print(pulseRate); //sends data to locals server
{ Serial.println("Sent to local server..");
rate[i] = rate[i + 1]; // and drop the oldest IBI value }
runningTotal += rate[i]; // add up the 9 oldest IBI value client.stop();
} delay(15000);
rate[9] = IBI; // add the latest IBI to the rate array // Wait for 15 seconds after each transmission
runningTotal += rate[9]; //add the latest IBI to runningTotal }
runningTotal /= 10; // average the last 10 IBI values

14-08-2024 Dr. Manas Khatua 46


ESP8266 with Vibration Sensor
For ESP4, write the following code in the Arduino IDE and save as Vibration_client.ino

#include <ESP8266WiFi.h> // Including ESP8266 library


#define vib A0 // sensor input from A0 pin of ESP8266

char ssid[] = "ESP1_Server"; //Replace with SSID of hotspot of local server


char pass[] = "12345678"; // Replace with password of hotspot of local server

IPAddress server(172,16,117,192); // IP address of local server


WiFiClient client;

• Change the IP address of Local Server (i.e. ESP1)


• Change the SSID and Password of WiFi AP hosted in Local Server

14-08-2024 Dr. Manas Khatua 47


Cont…
void setup(){
Serial.begin(9600); // Serial communication at baud rate of 9600 for debugging purpose
delay(10);
pinMode(vib, INPUT); // Input of vibration sensor
WiFi.mode(WIFI_STA); // ESP8266 as station mode
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, pass);
Serial.println();
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println();
Serial.println("WiFi connected");
Serial.print("LocalIP:"); Serial.println(WiFi.localIP()); // IP address of local server
Serial.println("MAC:" + WiFi.macAddress());
Serial.print("Gateway:"); Serial.println(WiFi.gatewayIP());
Serial.print("AP MAC:"); Serial.println(WiFi.BSSIDstr()); // MAC address of access point
}

14-08-2024 Dr. Manas Khatua 48


Cont…
void loop(){
int val = analogRead(vib); // Reads the sensor value
if(client.connect(server,80)) //connects to local server
{
client.print("4\r"); // Before sending the data sends ESP8266 ID as 4
Serial.print("Vibration sensor value: ");
Serial.println(val);
String data = String(val); // Converting integer data into string type
data += "\r"; // Add end delimiter "r" in the data
client.print(data); // sends sensor data to local server
Serial.println("Sent to Local server..!! ");
delay(15000); // After each transmission wait for 15 seconds
client.stop();
}
}

14-08-2024 Dr. Manas Khatua 49


ESP8266 with DHT11 Sensor
For ESP5, write the following code in the Arduino IDE and save as Temp_Humidity_Client.ino

#include<DHT.h> //Including temperature and Humidity sensor library


#include<ESP8266WiFi.h> //Including ESP8266 library
#define DHTPIN 0 // D3 pin of ESP8266

char ssid[] = "ESP1_Server"; //Replace with ssid of hotspot of local server


char pass[] = "12345678"; // Replace with password of hotspot of local server

IPAddress server(172,16,117,192); // Static IP address of local server. Replace whatever you want.
WiFiClient client;

DHT dht(DHTPIN, DHT11); // Data of DHT11 sensor in D3 pin of ESP8266

• Change the IP address of Local Server (i.e. ESP1)


• Change the SSID and Password of WiFi AP hosted in Local Server

• Install the DHT11 library and Adafruit Unified Sensor library for DHT11 sensor
14-08-2024 Dr. Manas Khatua 50
Cont…
void setup() {
Serial.begin(9600); //serial communication at baud rate of 9600 for debugging purpose
delay(10);
dht.begin(); // start Temperature and Humidity sensor
WiFi.mode(WIFI_STA); // ESP8266 mode as station mode
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, pass);
Serial.println();
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println();
Serial.println("WiFi connected");
Serial.print("LocalIP:"); Serial.println(WiFi.localIP());
Serial.println("MAC:" + WiFi.macAddress());
Serial.print("Gateway:"); Serial.println(WiFi.gatewayIP());
Serial.print("AP MAC:"); Serial.println(WiFi.BSSIDstr()); // MAC address of access point
}

14-08-2024 Dr. Manas Khatua 51


Cont…
void loop() {
float h = dht.readHumidity(); // Read Humidity value from sensor
float t = dht.readTemperature(); // Read temp value from sensor
if(isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor"); // Error message
return;
}
if(client.connect(server,80)) // Connect to local server
{
client.print(“5\r"); // before sending the data first send ESP8266 ID as 5
String temp = String(t);
temp += "\r"; // Add “r” as end delimiter
client.print(temp); // send temperature to local server
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" degree celsius, Humidity: ");
Serial.print(h);
Serial.print("%. ");
String humidity = String(h);
humidity += "\r"; // Add “r” in data as end delimiter
client.print(humidity); // send to Local server
Serial.println("Sent to local server ");
delay(15000); // delay of 15sec after each transmission
}
client.stop();
}

14-08-2024 Dr. Manas Khatua 52


Code Compilation and Upload

14-08-2024 Dr. Manas Khatua 53


Code Compilation

Compilation
successful
message in
bottom left
corner.

14-08-2024 Dr. Manas Khatua 54


Code Uploading
• Plug in the ESP8266 boards one by one to PC/Laptop via USB cable
• Go to Tool menu, select Board “NodeMCU 1.0 (ESP-12E Module)” and Port “COM3”.
• Open the corresponding code and do uploading code in Node MCU.

Note: If COM port is not


detected automatically then
it is needed to install.
Download port drivers from
the given link and then
install and then restart the
system:
https://www.silabs.com/pro
ducts/development-
tools/software/usb-to-uart-
bridge-vcp-drivers

14-08-2024 Dr. Manas Khatua 55


Observe Outputs

14-08-2024 Dr. Manas Khatua 56


Open Serial Monitor
• First select the port (go to Tools > Port: ) to which the board is connected
then click the icon of Serial Monitor on the top right side of the Arduino IDE

Serial Monitor
of Local Server

14-08-2024 Dr. Manas Khatua 57


Cont…

Serial Monitor
of ESP2

Serial Monitor
of ESP3

14-08-2024 Dr. Manas Khatua 58


Cont…

Serial Monitor
of ESP4

Serial Monitor
of ESP5

14-08-2024 Dr. Manas Khatua 59


Results & Graphs in Web
• Open the ThingSpeak page and click on Channels > My channels
• Now select the channel that is created for this experiment (In this case ‘Monitoring
Four Sensors in Star Topology’).

14-08-2024 Dr. Manas Khatua 60


Cont…
• click on ‘Private View’ to see the uploaded data

14-08-2024 Dr. Manas Khatua 61


Cont…
• Temperature and Humidity

14-08-2024 Dr. Manas Khatua 62


Cont…
Light Sensor Pulse Sensor Vibration Sensor

14-08-2024 Dr. Manas Khatua 63


14-08-2024 Dr. Manas Khatua 64

You might also like