You are here
Home > IoT Experiments >

Experiment-7: To demonstrate web server and client communication using nodeMCU ESP8266 12-E as a Soft AP

AIM : To demonstrate nodeMCU ESP8266 as Soft AP web server

Objective:

At the end of this experiment students will be able to:

  1. Understand mode of connection of ESP8266
  2. Understand the use of Soft AP
  • Create web server and access it on client (Mobile) via the URL
  1. Control LED using mobile phone

Requirement:

  1. Breadboard, connecting jumper wires
  2. NodeMCU ESP8266 12-E
  3. Two 5mm blue Light Emitting Diodes (LEDs)
  4. Two 220 Ohm Resistor

Theory:

You all know that a web server hosts files, images, text, videos, blog posts, or other relevant data of a specific website i.e., Wikipedia, Google, Yahoo, etc. Any clients can connect to such a web server anytime and fetch relevant information. Communication between a client and a web server takes place using HTTP Request or HTTP Response. To get any data or file from a web server, a client sends an HTTP request to the web server. In response, a web server sends the relevant data or file via HTTP Response. In most of the cases, the web browser (Chrome, Edge, Safari, Opera) acts as the HTTP client

Mode of Operation of ESP8266

nodeMCU ESP8266 can operate in the following modes:

  1. Station (STA) Only
  2. Soft Access Point (Soft AP)
  3. Both the Soft Access Point and Station (Soft AP +STA)

Figure on the right side shows two ESP8266, one at the centre acts as a Soft AP and the other at the top right corner as a station. There are other stations as well i.e mobile phone, laptop and a PC.

Configuring ESP8266 Web Server in AP Mode

In Access Point (AP) mode, ESP8266 will advertise its WiFi network and you can access the web server hosted on ESP8266 by connecting to the advertised network. However, you will still need to provide the IP assigned to ESP8266 to access the web server.

Interfacing LEDs with ESP 8266

Figure shows a ESP8266, a breadboard and the resistors and LED connected by jumper wires. LED1 is connected to pin D1 and LED2 connected to the pin D2 of the ESP8266. Ground from the ESP8266 is connected on rail lines where cathode of the LED through resistors is connected thus completing the circuit.

ESP8266 AP Web Server Code

The following line of code will configure ESP8266 in SoftAP mode and will act as a web server for all the connecting devices. It will then turn 02 LEDs On/Off as per input from the connected station devices.

#include <ESP8266WiFi.h>

#include <ESP8266WebServer.h>

/*Specifying the SSID and Password of the AP*/

const char* ap_ssid = “ESP8266”; //Access Point SSID

const char* ap_password= “embedded-robotics”; //Access Point Password

uint8_t max_connections=8;//Maximum Connection Limit for AP

int current_stations=0, new_stations=0;

//Specifying the Webserver instance to connect with HTTP Port: 80

ESP8266WebServer server(80);

//Specifying the Pins connected from LED1 to LED4

uint8_t led1_pin=D0;

uint8_t led2_pin=D1;

//Specifying the boolean variables indicating the status of LED1 to LED4

bool led1_status=false, led2_status=false;

void setup() {

Serial.begin(115200);              //Start the serial communication channel

Serial.println();

//Output mode for the LED Pins

pinMode(led1_pin,OUTPUT);

pinMode(led2_pin,OUTPUT);

//Setting the AP Mode with SSID, Password, and Max Connection Limit

if(WiFi.softAP(ap_ssid,ap_password,1,false,max_connections)==true)

{

Serial.print(“Access Point is Created with SSID: “);

Serial.println(ap_ssid);

Serial.print(“Max Connections Allowed: “);

Serial.println(max_connections);

Serial.print(“Access Point IP: “);

Serial.println(WiFi.softAPIP());

}

else   {

Serial.println(“Unable to Create Access Point”);

}

//One of the following function will get executed upon corresponding GET request from the client

server.on(“/”,handle_OnConnect);

server.on(“/led1on”,handle_led1on);

server.on(“/led1off”,handle_led1off);

server.on(“/led2on”,handle_led2on);

server.on(“/led2off”,handle_led2off);

//Starting the Server

server.begin();

Serial.println(“HTTP Server Started”);

}

void loop() {

//Assign the server to handle the clients

server.handleClient();

//following lines of code continuously check how many stations are connected to //Soft AP and notify whenever a new station is connected or disconnected

new_stations=WiFi.softAPgetStationNum();

 

if(current_stations<new_stations)                            //Device is Connected

{

current_stations=new_stations;

Serial.print(“New Device Connected to SoftAP… Total Connections: “);

Serial.println(current_stations);

}

if(current_stations>new_stations)                //Device is Disconnected

{

current_stations=new_stations;

Serial.print(“Device disconnected from SoftAP… Total Connections: “);

Serial.println(current_stations);

}

//Turn the LEDs ON/OFF as per their status set by the connected client

//LED1

if(led1_status==false)

{

digitalWrite(led1_pin,LOW);

}

else

{

digitalWrite(led1_pin,HIGH);

}

 

//LED2

if(led2_status==false)  {

digitalWrite(led2_pin,LOW);

}  else

{

digitalWrite(led2_pin,HIGH);

}

}

void handle_OnConnect() {

Serial.println(“Client Connected”);

server.send(200, “text/html”, HTML());

}

//code to send update information of LED status on client page

void handle_led1on()

{

Serial.println(“LED1 ON”);

led1_status=true;

server.send(200, “text/html”, HTML());

}

void handle_led1off()

{

Serial.println(“LED1 OFF”);

led1_status=false;

server.send(200, “text/html”, HTML());

}

void handle_led2on()

{

Serial.println(“LED2 ON”);

led2_status=true;

server.send(200, “text/html”, HTML());

}

 

void handle_led2off()

{

Serial.println(“LED2 OFF”);

led2_status=false;

server.send(200, “text/html”, HTML());

}

void handle_NotFound()

{

server.send(404, “text/plain”, “Not found”);

}

//html to display LED button page on client system

String HTML() {

String msg=”<!DOCTYPE html> <html>\n”;

msg+=”<head><meta name=\”viewport\” content=\”width=device-width, initial-scale=1.0, user-scalable=no\”>\n”;

msg+=”<title>LED Control</title>\n”;

msg+=”<style>html{font-family:Helvetica; display:inline-block; margin:0px auto; text-align:center;}\n”;

msg+=”body{margin-top: 50px;} h1{color: #444444; margin: 50px auto 30px;} h3{color:#444444; margin-bottom: 50px;}\n”;

msg+=”.button{display:block; width:80px; background-color:#f48100; border:none; color:white; padding: 13px 30px; text-decoration:none; font-size:25px; margin: 0px auto 35px; cursor:pointer; border-radius:4px;}\n”;

msg+=”.button-on{background-color:#f48100;}\n”;

msg+=”.button-on:active{background-color:#f48100;}\n”;

msg+=”.button-off{background-color:#26282d;}\n”;

msg+=”.button-off:active{background-color:#26282d;}\n”;

msg+=”</style>\n”;

msg+=”</head>\n”;

msg+=”<body>\n”;

msg+=”<h1>ESP8266 Web Server</h1>\n”;

msg+=”<h3>Using Access Point (AP) Mode</h3>\n”;

if(led1_status==false)  {

msg+=”<p>LED1 Status: OFF</p><a class=\”button button-on\” href=\”/led1on\”>ON</a>\n”;

}

else

{

msg+=”<p>LED1 Status: ON</p><a class=\”button button-off\”             href=\”/led1off\”>OFF</a>\n”;

}

if(led2_status==false)

{

msg+=”<p>LED2 Status: OFF</p><a class=\”button button-on\” href=\”/led2on\”>ON</a>\n”;

}

else

{

msg+=”<p>LED2 Status: ON</p><a class=\”button button-off\” href=\”/led2off\”>OFF</a>\n”;

}

msg+=”</body>\n”;

msg+=”</html>\n”;

return msg;

}

Output:

The client mobile display the control page when he types the IP address (192.168.4.1) of web server in the browser. As seen in figure 02 buttons for 02 LEDs are displayed and any or both  pressed the LED connected to ESP will ON or OFF.

 

Summary:

In this experiment we have studied the basic modes of operation of ESP8266 and the libraries and functions required for Wi-Fi commination. We also learnt how to program ESP8266 and interact with sensors and actuators.

Viva Questions:

  • Briefly explain three mode of operation of the ESP8266

 

 

 

 

  1. Write the steps involved in writing code for ESP8266 as a Soft-AP.

/

 

 

 

  1. Name important libraries and functions used for ESP8266 an Access point.

 

 

 

Leave a Reply

Top
error: Content is protected !!