HOW TO MAKE A NODE MCU A WEB SERVER

Introduction

Once the wifi connection is established in our nodemcu our primary task is completed but the most important thing is how can we communicate our nodemcu with other different clients. we are going to see all the detail about how can we request a data and send a data to any server / client in this section

Components Required

  • Nodemcu
  • USB cable
  • led
  • jumper wires

Connection Diagram

connection diagram

Coding

SERVER READING A DATA FROM A CLIENT

<pre>#include <ESP8266WiFi.h>
const char *ssid =  "black hat";  
const char *pass =  "nepal@12345";
WiFiClient client;
WiFiServer server(80);
void setup() 
{
       Serial.begin(9600);
       delay(10);
       Serial.println("Connecting to ");
       Serial.println(ssid); 
       WiFi.begin(ssid, pass); 
       while (WiFi.status() != WL_CONNECTED) 
          {
            delay(500);
            Serial.print(".");
          }
  Serial.println("");
  Serial.println("WiFi connected"); 
  Serial.println('n');
  Serial.println("Connection established!");  
  Serial.print("IP address:t");
  Serial.println(WiFi.localIP());
  server.begin();
}
void loop() 
{      
     // put your main code here, to run repeatedly:
  client = server.available();  //Gets a client that is connected to the server and has data available for reading.    
  if (client == 1)
  {  
    String request =  client.readStringUntil('n');
    Serial.println(request);
    request.trim();
  }
}</pre>

READING A DATA AND PERFORMING A OUTPUT ACCORDINGLY

<pre>#include <ESP8266WiFi.h>
const char *ssid =  "Your wifi Network name";  
const char *pass =  "Network password";
WiFiClient client;
WiFiServer server(80);
#define led D2
void setup() 
{
       Serial.begin(9600);
       delay(10);
       Serial.println("Connecting to ");
       Serial.println(ssid); 
       WiFi.begin(ssid, pass); 
       while (WiFi.status() != WL_CONNECTED) 
          {
            delay(500);
            Serial.print(".");
          }
  Serial.println("");
  Serial.println("WiFi connected"); 
  Serial.println('n');
  Serial.println("Connection established!");  
  Serial.print("IP address:t");
  Serial.println(WiFi.localIP());
  server.begin();
  pinMode(led, OUTPUT);
}
void loop() 
{      
     // put your main code here, to run repeatedly:
  client = server.available();  //Gets a client that is connected to the server and has data available for reading.    
  if (client == 1)
  {  
    String request =  client.readStringUntil('n');
    Serial.println(request);
    request.trim();
    if(request == "GET /ledon HTTP/1.1")
    {
      digitalWrite(led, HIGH);
    }
    if(request == "GET /ledoff HTTP/1.1")
    {
      digitalWrite(led, LOW);
    }
  }
}</pre>

Important Term TO Understand

Client

client is a computer hardware device or software that accesses a service made available by a server. The server is often (but not always) located on a separate physical computer.

We must create one variable named client of type WiFiClient. We can give any name of the variable like client1,client2 and so on but it must be of type WiFiClient.

<pre>WiFiClient client;</pre>

Server

server is a physical computer dedicated to run services to serve the needs of other computers. Depending on the service that is running, it could be a file server, database server, home media server, print server, or web server.

We must create one variable named server of type WiFiServer. We can give any name of the variable like server1,server2 and so on but it must be of type WiFiServer.

On a Web server or HTTPS, port 80 is the port that the server “listens to” or expects to receive from a Web client, assuming that the default was taken when the server was configured or set up.

<pre>WiFiServer server(80);</pre>

server.begin();

By using this command in set up section we are starting our node mcu as server.

stream.readStringUntil(terminator)

readStringUntil() reads characters from a stream into a String.  The function terminates if the terminator character is detected.

terminator

The character to search for. Allowed data types: char.

<pre> String request =  client.readStringUntil('n');</pre>

In this command there is a string variable called request which stores the value from client.readStringUntil(‘n’). The client.readStringUntil(‘n’) generally have the value that the client have requested.

request.trim();

This function is used to remove the garbage value that may be present in client.readStringUntil(‘n’).

YOUTUBE LINK

Recommended For You

About the Author: admin

Leave a Reply

Your email address will not be published.