IoT Bytes

Bits and Bytes of IoT

NodeMCU Wifi Configuration

Pradeep Singh | 1st April 2016

wifi

ESP8266 with NodeMCU Firmware can be configured as an Access Point, Wifi Client (Host / Station) or both as Client and AP at the same time. It has capability work with 802.11b, 802.11b and 802.11n networks. In this article we will check some of the WiFi configuration methods with sample code.

WiFi Device Mode:

We can configure the role (Client, AP or Both)of ESP8266 Wifi chip from any of following options –

  1. wifi.STATION
  2. wifi.SOFTAP
  3. wifi.STATIONAP

We can configure NodeMCU for role using “wifi.setmode()”. Following is an example to configure the module as WiFi Endpoint –

wifi.setmode(wifi.STATION)

WiFi Signal Standard:

The WiFi networking standard (802.11b, g or n) can be configured from any of the following options –

  1. wifi.PHYMODE_B
  2. wifi.PHYMODE_G
  3. wifi.PHYMODE_N

We can configure NodeMCU to work with any of these WiFi Standards using “wifi.setphymode()”. Following is an example to configure module with 802.11n –

wifi.setphymode(wifi.PHYMODE_N)

Config Validation:

You can check WiFi Module related configuration using following methods –

print(wifi.getmode())
print(wifi.getphymode())
print(wifi.getchannel())

You can check WiFi Station related configuration using following methods –

print(wifi.sta.getmac())
print(wifi.sta.getip())

You can check WiFi SoftAP related configuration using following methods –

print(wifi.ap.getmac())
print(wifi.ap.getip())
print(wifi.ap.getbroadcast())
--- This block will print list of all the Clients connected to AP ---
for mac,ip in pairs(wifi.ap.getclient()) do
    print(mac,ip)
end

 

Sample Code to List All Available Access Points:


Following is an example to list all available WiFi Networks (GitHub Link for Code File)-

-----------------------------------------------
--- Set Variables ---
-----------------------------------------------
--- AP Search Params ---
ap_list_cfg={}
--- Enter SSID, use nil to list all SSIDs ---
ap_list_cfg.ssid=nil
--- Enter BSSID, use nil to list all SSIDs ---
ap_list_cfg.bssid=nil
--- Enter Channel ID, use 0 to list all channels ---
ap_list_cfg.channel=0
--- Use 0 to skip hidden networks, use 1 to list them ---
ap_list_cfg.show_hidden=1

--- AP Table Format ---
--- 1 for Output table format - (BSSID : SSID, RSSI, AUTHMODE, CHANNEL)
--- 0 for Output table format - (SSID : AUTHMODE, RSSI, BSSID, CHANNEL)
ap_list_table_format = 1

-----------------------------------------------

--- Print Output ---
function print_AP_List(ap_table)
 for p,q in pairs(ap_table) do
 print(p.." : "..q)
 end
end

--- Call Get AP Method ---
wifi.sta.getap(ap_list_cfg,ap_list_table_format, print_AP_List)
-----------------------------------------------

 

Sample Code to Configure NodeMCU as WiFi Client:


Following is an example to connect NodeMCU to existing WiFi Network (GitHub Link for Code File)-

-----------------------------------------------
--- Set Variables ---
-----------------------------------------------
--- WIFI CONFIGURATION ---
WIFI_SSID = "yourSSID"
WIFI_PASSWORD = "yourPassword"
WIFI_SIGNAL_MODE = wifi.PHYMODE_N

--- IP CONFIG (Leave blank to use DHCP) ---
ESP8266_IP=""
ESP8266_NETMASK=""
ESP8266_GATEWAY=""
-----------------------------------------------

--- Connect to the wifi network ---
wifi.setmode(wifi.STATION) 
wifi.setphymode(WIFI_SIGNAL_MODE)
wifi.sta.config(WIFI_SSID, WIFI_PASSWORD) 
wifi.sta.connect()

if ESP8266_IP ~= "" then
 wifi.sta.setip({ip=ESP8266_IP,netmask=ESP8266_NETMASK,gateway=ESP8266_GATEWAY})
end
-----------------------------------------------

--- Check the IP Address ---
print(wifi.sta.getip())

Sample Code to Configure NodeMCU as WiFi Access Point:


Following is an example to to use NodeMCU as an AP (GitHub Link for Code File)-

---------------------------------------
--- Set Variables ---
---------------------------------------
--- Set AP Configuration Variables ---
AP_CFG={}
--- SSID: 1-32 chars
AP_CFG.ssid="pradeep"
--- Password: 8-64 chars. Minimum 8 Chars
AP_CFG.pwd="wifipassword"
--- Authentication: AUTH_OPEN, AUTH_WPA_PSK, AUTH_WPA2_PSK, AUTH_WPA_WPA2_PSK
AP_CFG.auth=AUTH_OPEN
--- Channel: Range 1-14
AP_CFG.channel = 6
--- Hidden Network? True: 1, False: 0
AP_CFG.hidden = 0
--- Max Connections: Range 1-4
AP_CFG.max=4
--- WiFi Beacon: Range 100-60000
AP_CFG.beacon=100

--- Set AP IP Configuration Variables ---
AP_IP_CFG={}
AP_IP_CFG.ip="192.168.10.1"
AP_IP_CFG.netmask="255.255.255.0"
AP_IP_CFG.gateway="192.168.10.1"

--- Set AP DHCP Configuration Variables ---
--- There is no support for defining last DHCP IP ---
AP_DHCP_CFG ={}
AP_DHCP_CFG.start = "192.168.10.2"
---------------------------------------

--- Configure ESP8266 into AP Mode ---
wifi.setmode(wifi.SOFTAP)
--- Configure 802.11n Standard ---
wifi.setphymode(wifi.PHYMODE_N)

--- Configure WiFi Network Settings ---
wifi.ap.config(AP_CFG)
--- Configure AP IP Address ---
wifi.ap.setip(AP_IP_CFG)

--- Configure DHCP Service ---
wifi.ap.dhcp.config(AP_DHCP_CFG)
--- Start DHCP Service ---
wifi.ap.dhcp.start()
---------------------------------------

Sample Code to Configure NodeMCU in Hybrid Mode (AP + Client):


Following is an example to to use NodeMCU in Hybrid Mode  (GitHub Link for Code File)-

-----------------------------------------------
--- Set Variables ---
-----------------------------------------------
--- Set Client Configuration Variables ---

WIFI_SSID = "yourssid"
WIFI_PASSWORD = "yourpassword"

--- IP CONFIG (Leave blank to use DHCP) ---
ESP8266_IP=""
ESP8266_NETMASK=""
ESP8266_GATEWAY=""
-----------------------------------------------
--- Set AP Configuration Variables ---

AP_CFG={}
--- SSID: 1-32 chars
AP_CFG.ssid="pradeep"
--- Password: 8-64 chars. Minimum 8 Chars
AP_CFG.pwd="wifipassword"
--- Authentication: AUTH_OPEN, AUTH_WPA_PSK, AUTH_WPA2_PSK, AUTH_WPA_WPA2_PSK
AP_CFG.auth=AUTH_OPEN
--- Channel: 1-14
AP_CFG.channel = 6
--- Hidden Network? True: 1, False: 0
AP_CFG.hidden = 0
--- Max Connections 1-4
AP_CFG.max=4
--- WiFi Beacon 100-60000
AP_CFG.beacon=100

--- Set AP IP Configuration Variables ---
AP_IP_CFG={}
AP_IP_CFG.ip="192.168.10.1"
AP_IP_CFG.netmask="255.255.255.0"
AP_IP_CFG.gateway="192.168.10.1"

--- Set AP DHCP Configuration Variables ---
--- There is no support for defining last DHCP IP
AP_DHCP_CFG ={}
AP_DHCP_CFG.start = "192.168.10.2"
---------------------------------------

--- Configure ESP8266 into Hybrid Mode (AP and Client Mode) ---
wifi.setmode(wifi.STATIONAP)
--- Configure 802.11n Standard ---
wifi.setphymode(wifi.PHYMODE_N)

-----------------------------------------------
--- WiFi Client Setup ---

--- Configure Client with Router SSID and Password ---
wifi.sta.config(WIFI_SSID, WIFI_PASSWORD) 
wifi.sta.connect()

--- Configure Static IP (If Available) ---
if ESP8266_IP ~= "" then
 wifi.sta.setip({ip=ESP8266_IP,netmask=ESP8266_NETMASK,gateway=ESP8266_GATEWAY})
end

-----------------------------------------------
--- WiFi Access Point Setup ---

--- Configure WiFi Network Settings ---
wifi.ap.config(AP_CFG)
--- Configure AP IP Address ---
wifi.ap.setip(AP_IP_CFG)

--- Configure DHCP Service ---
wifi.ap.dhcp.config(AP_DHCP_CFG)
--- Start DHCP Service ---
wifi.ap.dhcp.start()
---------------------------------------
Advertisements

20 thoughts on “NodeMCU Wifi Configuration

  1. Pradeep – I need one help.

    I am trying to configure NodeMCU using the firmware (similar to C language). I have been successful in a basic project of triggering LEDs using browser. In my current code i have hard coded the SSID and Pwd.

    I would like to know how could i setup, so that i donot have to hardcode the wifi SSID and Pwd. It could give me a prompt first time to set the SSID and pwd. This would help me to use the device in my friends home as well.

    Request your feedback.

    Like

    1. You may consider connecting a push button with one of the NodeMCU’s GPIO Pins and write code to delete wifi config from your device when this button is pressed. After deleting the WiFi config, your code should configure NodeMCU with an open WiFi SSID and put it in AP Mode. This setup will act as “reset” button found on most WiFi routers. You may also need to write additional code to generate a simple HTML page that can accept new WiFi Config and change NodeMCU’s mode to AP or Client.

      If this seems too much, try exploring ESPWebFramework at https://github.com/fdivitto/ESPWebFramework

      Like

  2. hello i made esp8266 to SoftAp mode and connected my mobile phone to the WiFi network .
    what i want to do is sending data from phone(Browser) to ESP & vise-versa. what to for this..
    I want to make a device which will send the data to Phone an phone will display it. also using that we can change same parameter to ESP using Browser on phone

    ThankYou

    Like

    1. Hi Ismail,

      You can create a simple Web Server on ESP8266 which can listen to GET requests from HTTP clients (Web Browsers) and respond with some value, For Ex some sensor data in HTML Body.

      Same Web Server can be extended to listen to POST requests from HTTP clients (Web Browsers). You can post data in Json or XML format and based on that make changes on ESP8266.

      You can easily find many examples for your use case on google, simple search with topic “nodemcu web server example”. Let me know in case you need additional details on this.

      Like

  3. Thanks for very nice article!

    I would like to detect new wifi client in AP and start/redirect browser in client device (mobile phone) into to APs page. It would make easier to use 8266 as web server.

    The respinse string for redirecting is like
    startup = “GET /read HTTP/1.0”
    startup += “Host: ” + WiFi.localIP()
    startup += “User-Agent: CaptiveNetworkSupport/1.0 wispr”
    startup += “Connection: close”

    Like

  4. Hi!
    I’m a beginner to nodeMCU, I just wanted to ask which IDE is being used to run the scripts mentioned here?

    Can we do it using the Arduino IDE? I’m quite comfortable with arduino programming

    Like

  5. Thanks for a well thought out and complete summary. I appreciate the you have written this in Lau, but do you have a companion site or Git which repeats this in Arduino C, my preferred module. I have just configured a NodeMCU with an Arduino for a ‘simple’ blink project to start. Now I want to go further and direct from computer or smartphone. Thanks in Advance.

    Like

  6. Hi Pradeep, do you have a link to or a sample code ?

    “You may consider connecting a push button with one of the NodeMCU’s GPIO Pins and write code to delete wifi config from your device when this button is pressed. After deleting the WiFi config, your code should configure NodeMCU with an open WiFi SSID and put it in AP Mode. This setup will act as “reset” button found on most WiFi routers. You may also need to write additional code to generate a simple HTML page that can accept new WiFi Config and change NodeMCU’s mode to AP or Client.”

    Like

  7. Hi Pradeep,

    i want to know how to connect to wifi on nodemcu with touchsceen. like first it scan all available networks and then keyboard will open. just fill the password and connect to it.. How is this Possible. please guide me if possible make a tutorial for that.

    thanks

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: