Login

Set up Access point and Wifi station on NodeMCU

Objectives

  • Set up an Access Point on NodeMCU
  • Connect to a WiFi router using NodeMCU as a station

Things

  1. NodeMCU
  2. WiFi router

Let’s begin!

ESP8266 has the capability to create a Web Server on a Wireless Local Area Network (WLAN). You can then connect to the wireless network and access the webpage from your browser. ESP8266 can also be coded (Arduino IDE is commonly used for this purpose) to provide HTML links on the webpage, where you can interact with it and its connected components. ESP8266 has to be part of a wireless network in order to create a web server.

We will be using Arduino IDE to upload codes to the ESP8266. Note that we will be using NodeMCU to work with the ESP8266. So when we perform actions on ESP8266 in this unit, we are referring to the NodeMCU.

Like any other WiFi device, ESP8266 has two wifi modes – Access Point (AP) and Station (STA). To give you an understanding of AP and STA, let us use your phone as an example.

When you use your phone to connect to WiFi at home or at work, the WiFi router you are connecting to is acting as an AP, and your phone is acting as an STA. When you switch on your phone’s hotspot, your phone is acting as an AP and any other device that connects to your hotspot is acting as an STA.

ESP8266 as an Access Point (AP)

When we say that the ESP8266 is functioning as an AP, we mean that a WiFi enabled device can connect to a Wifi hotspot set up by your ESP8266. However, ESP8266 cannot connect to an external WiFi router.

You can set-up ESP8266 as an AP using the command WIFI_AP with a custom name (SSID – Service Set IDentifier) and password using the following commands.

const char *ssid = "ESP8266";
const char *password = "12345678";
WiFi.mode(WIFI_AP);
WiFi.softAP(ssid, password);

* these commands are case sensitive.

To copy the code, right click on view raw at the bottom of the code, click on open link in new tab and then copy the code.

This will set up a hotspot named ESP8266 that can be connected to, by any WiFi enabled device. The option WIFI_AP sets up the device as Access Point Only. You can use any WiFi device such as your phone to connect to this hotspot, using the password 12345678.

ESP8266 as a WiFi  Station (STA)

When we say that the ESP8266 is functioning as a Station, we mean that the ESP8266 can connect to a WiFi router in your workspace. This WiFi router can even be a hotspot on your phone. No other WiFi enabled device can connect to the ESP8266.

You can set-up ESP8266 as an STA using the command WIFI_STA with your router’s name (SSID – Service Set IDentifier) and password using the following commands.

const char *ssid = "Your_WiFi_Name";
const char *password = "Your_WiFi_Password";
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);

* these commands are case sensitive.

To copy the code, right click on view raw at the bottom of the code, click on open link in new tab and then copy the code.

This will connect the ESP8266 to your WiFi Router. The option WIFI_STA sets up the device as WiFi Station Only.

ESP8266 as an AP and an STA

This is the default mode of the ESP8266. In this mode, ESP8266 can connect to an external WiFi router and simultaneously set up an Access Point for other devices to connect to it.

In this mode, the AP and STA are isolated. In other words, say your phone is connected to the AP, and the STA is connected to a WiFi router, then only the ESP8266 can access the internet from the router, your phone cannot.

You can set-up ESP8266 as an AP and an STA using the command WIFI_AP_STA

/***For AP part***/

const char *ap_ssid = "ESP8266";
const char *ap_password = "12345678"; 

/***For STA part***/

const char *sta_ssid = "Your router name";
const char *sta_password = "Your router password";

WiFi.mode(WIFI_AP_STA);
WiFi.softAP(ap_ssid, ap_password);
WiFi.begin(sta_ssid, sta_password);

To copy the code, right click on view raw at the bottom of the code, click on open link in new tab and then copy the code.

Takeaway

Loader Loading...
EAD Logo Taking too long?
Reload Reload document
| Open Open in new tab
April 22, 2020

0 responses on "Set up Access point and Wifi station on NodeMCU"

Leave a Message

Your email address will not be published.

top

Knowledge and Content by Li2 Technologies | © 2021 NASSCOM Foundation | All rights reserved

X