NodeMCU as Access Point Controlling LED

Posted on

NodeMCU as Access Point

How to make NodeMCU an Access Point for Controlling LED, 2nd – In the previous tutorial, we have learned the basics of NodeMCU V3 including the install library, IO pin access, Blink a LED, and others.

In this tutorial, we will learn how to use the ESP8266 NodeMCU to communicate with our gadgets (smartphone, laptop, tablet, etc) via WiFi communication.
Before continuing, I will say that this article will teach you how to use a private WiFi network.

I mean, we will make the ESP8266 an access point that can communicate only with our gadgets, without being connected to a router or Wi-Fi hotspot.

ESP8266 NodeMCU as Access Point Controlling LED

Advantages & Disadvantages of Access Points

The advantage of this mode is that we can directly access our devices without having a router or wi-fi hotspot.

The disadvantage is that we cannot access our devices over long distances.

This mode is best used for devices at close range. Because this NodeMCU uses a PCB antenna, the distance is about 90 meters if there are no obstacles.

However, it will decrease along with the many obstacles such as walls, room space and so on.

This range has been tested by Andreas Spiess in his video with the title ESP8266 Range Test with and without External Antenna.

Program Code

Now, I have provided a program that we can use to communicate our gadget to nodemcu via Wi-Fi transmitted by NodeMCU ESP8266.

#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 name that will appear
const char* ssid = "Testing-NodeMCU";

// Set IP address, can be replaced with other numbers
IPAddress IPaddr (192, 168, 168, 168);
IPAddress IPmask(255, 255, 255, 0);
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">TES 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);
  Serial.println();
  Serial.print("Access point configuration...");
  WiFi.softAP(ssid);
  WiFi.softAPConfig(IPaddr, IPaddr, IPmask); 
  IPAddress myIP = WiFi.softAPIP();
  Serial.print("IP address is: ");
  Serial.println(myIP);
  
  server.on("/", handleRoot);
  server.on("/on", led_on);
  server.on("/off", led_off);
  server.begin();
  
  Serial.println("HTTP server starting.");
}

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);
}

Before you upload the program above, it’s a good idea to read the explanation of the program below.

EXPLANATION

The LED used is the led-builtin nodemcu v3 which is connected to pin D4.

define led D4

This led is active-low, meaning that when we apply LOW voltage, the LED will light up and vice versa.

If you are familiar with the Arduino Uno, then you will find that the led will be activated in Active-HIGH mode.

Because it is nodeMcu that transmits the wifi signal itself, we must specify the name of the wifi network.

const char * ssid = “Testing-NodeMCU”;

In the program above, the name of my wifi gave the name “Testing-NodeMCU”. You can change this name as you like.

Set the IP address to our liking with the numbers on this line:

IPAddress IPaddr (192, 168, 168, 168);

After that, I insert the HTML code, which will display the On / OFF button in the browser, when accessed.

Steps

Now let’s test it. The steps are as follows:

  • Upload the program above to NodeMCU
  • After the upload is complete, open the Wi-Fi connection on the gadget, we use a smartphone, then select and connect to the “Testing-NodeMCU” network
  • Once connected, open the browser.
  • Type the IP address
  • Then it will appear like this
ESP8266 NodeMCU as Access Point Controlling LED

Click the button, you will see the led on the NodeMCU will turn on or off.

That is all. I hope “How to make NodeMCU as Access Point for Controlling LED” is useful. Thanks to ArduinoKode for this tutorial.

Leave a Reply