ESP8266 NodeMCU As WebServer Basic Code

Posted on

ESP8266 NodeMCU Webserver

How To Make NodeMCU ESP8266 As WebServer – In the previous article, we have learned how to make this nodemcu a setpoint, where we can control a led on the nodemcu board via the wifi network provided by the NodeMCU ESP8266 itself.

In that tutorial, even though we have used the wifi network by the ESP8266 microcontroller, we can only control the device in close proximity. If we want to get a wider reach, then we have to use the internet network.

Therefore, the NodeMCU ESP8266 must be connected to the internet via a hotspot/router. At this point, NodeMCU is said to be a webserver. For illustration you can see in the following image:

ESP8266 NodeMCU As WebServer Basic Code

Conditions:

  1. Available wifi network from router/hotspot
  2. NodeMCU must be connected to router’s wifi

In this test, I want to tell you that I am using a wifi network from a router/hotspot with parameters:

  1. Network name (SSID) is “HUAWEI_HyJK”
  2. Password is “sore2021”

Program Code

To understand how this happens, let’s dive into the code of the NodeMCU Webserver program that uses the Arduino IDE as a program text editor.

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>

// The led used is led_builtin
// This led is active-low
#define led D4

// This is the WiFi & Pass the one i'm using
//you can change it based on your router
const char* ssid     = "HUAWEI-HyJK";
const char* password = "sore2021";

ESP8266WebServer server(80);

// This block is the html code
String html = R"***(
<!DOCTYPE html>
<html>
  <head>
  <style>
  .center {
    text-align:center;
  }
  .button {
    border: none;
    color: white;
    padding: 15px 32px;
    text-align: center;
    text-decoration: none;
    display: inline-block;
    font-size: 16px;
    margin: 4px 10px;
    cursor: pointer;
  }

  .button1 {background-color: #4CAF50;} 
  .button2 {background-color: #e60000;} 
  </style>     
  </head>
  <body>
    <h1 class="center">NodeMCU ESP8266 LED</h1>
    <div class="center">
    <input class="button button1" type="button" onclick="location.href='/on';" value="ON">
    <input class="button button2" type="button" onclick="location.href='/off';" value="OFF">
    </div>
  </body>
</html>
)***";

//==============================================================
void setup()
{
  pinMode(led, OUTPUT); 
  
  Serial.begin(115200);
  WiFi.begin(ssid, password);
            
  Serial.print("\n\r \n\rWorking to connect");
  
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500); Serial.print(".");
  }
  
  Serial.println("Connected to WiFi");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  
  server.on("/", handleRoot);           
  server.on("/on", led_on);
  server.on("/off", led_off);
  
  server.begin();
  Serial.println("HTTP server started");
}

void loop()
{
  server.handleClient();                
}


void handleRoot()                      
{
  server.send(200, "text/html", html);  
}

void led_on()
{
  // because led_builtin is active-low
  // so I added "!" before HIGH
  //"!" is NOT operator
  digitalWrite(led, !HIGH); 
  server.send(200, "text/html", html);
}

void led_off()
{
  // Likewise LOW, I added "!"
  digitalWrite(led, !LOW);
  server.send(200, "text/html", html);
}

After the webserver program above is uploaded to the NodeMCU ESP8266, open Serial Monitor. When this program was created, the generated IP address was 192.168.100.124 (yours might be different). Then the display will be like this:

Please you can click the button and look into the NodeMCU LED. Hope this is useful. Done.