Lec 10
Lec 10
Using
ESP8266 and Webserver
“Try not to become a man of success. Rather become a man of value.” – Albert Einstein
System Diagram
30-08-2023 2
Physical Setup
ThingSpeak cloud server
accessing from a
Laptop/PC/Smartphone
30-08-2023 3
Router Configuration
To Connect with IITG Internet
30-08-2023 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.
30-08-2023 5
Cont…
30-08-2023 6
Cont…
30-08-2023 7
Cont…
30-08-2023 8
Cont…
30-08-2023 9
Cont…
30-08-2023 10
Cont…
30-08-2023 11
Cont…
30-08-2023 12
Cont…
********
30-08-2023 13
Cont…
30-08-2023 14
Connecting with Internet
• You should be able to access Internet in your Mobile/Laptop using TP-Link WiFi AP
30-08-2023 15
Cloud Server Configuration
to Access Web Service
30-08-2023 16
Configure to use Cloud Server
30-08-2023 17
Cont…
30-08-2023 18
Cont…
30-08-2023 19
Cont…
30-08-2023 20
Create Channel Display
30-08-2023 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
30-08-2023 22
IoT Network Configuration
30-08-2023 23
IoT Network Configuration
• There are total five ESP8266
– one is acting as server,
– other four as clients in local network.
30-08-2023 24
Sensor Configuration
30-08-2023 25
Cont…
30-08-2023 26
Arduino
Tool Configuration
30-08-2023 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:
30-08-2023 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
30-08-2023 29
Cont…
• Go to Tools > Board > Board Manager
• Scroll down, select the ESP8266 board menu and install “esp8266 by ESP8266
Community”
30-08-2023 30
Cont…
30-08-2023 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.
30-08-2023 32
Cont…
• After installing the DHT library from Adafruit, install “Adafruit Unified Sensor” libraries.
30-08-2023 33
MCU Programming
30-08-2023 34
ESP8266 with Local Server
For ESP5, 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.
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
30-08-2023 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
}
30-08-2023 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)
30-08-2023 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
}
30-08-2023 38
ESP8266 with LDR Sensor
For ESP2, write the following code in the Arduino IDE and save as LDR_client.ino
30-08-2023 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
}
30-08-2023 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();
}
30-08-2023 41
ESP8266 with Pulse Sensor
#define pulsePin A0 // Pulse sensor input pin A0
#include<ESP8266WiFi.h> // Including ESP8266 library
30-08-2023 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
}
30-08-2023 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();
}
}
30-08-2023 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
} }
30-08-2023 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
30-08-2023 46
ESP8266 with Vibration Sensor
For ESP4, write the following code in the Arduino IDE and save as Vibration_client.ino
30-08-2023 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
}
30-08-2023 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();
}
}
30-08-2023 49
ESP8266 with DHT11 Sensor
For ESP5, write the following code in the Arduino IDE and save as Temp_Humidity_Client.ino
IPAddress server(172,16,117,192); // Static IP address of local server. Replace whatever you want.
WiFiClient client;
• Install the DHT11 library and Adafruit Unified Sensor library for DHT11 sensor
30-08-2023 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
}
30-08-2023 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();
}
30-08-2023 52
Code Compilation and Upload
30-08-2023 53
Code Compilation
Compilation
successful
message in
bottom left
corner.
30-08-2023 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.
30-08-2023 55
Observe Outputs
30-08-2023 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
30-08-2023 57
Cont…
Serial Monitor
of ESP2
Serial Monitor
of ESP3
30-08-2023 58
Cont…
Serial Monitor
of ESP4
Serial Monitor
of ESP5
30-08-2023 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’).
30-08-2023 60
Cont…
• click on ‘Private View’ to see the uploaded data
30-08-2023 61
Cont…
• Temperature and Humidity
30-08-2023 62
Cont…
Light Sensor Pulse Sensor Vibration Sensor
30-08-2023 63
30-08-2023 64