Development of NodeMCU

Subsection 4.2: WiFi Mode

WiFi Mode

As above metioned, NodeMCU has three types of WiFi modes: AP (Access Point), STA (Station), and AP+STA. The code is listed as follows.

AP mode

file name is “ap.lua” in the documents.

the "fileName.lua" in "init.lua" is revised as "ap.lua", "fileName.lc" is revised as "ap.lc", and then download to NodeMCU.

1     print("Ready to start soft ap")
2     
3     local str=wifi.ap.getmac();
4     local ssidTemp=string.format("%s%s%s",string.sub(str,10,11),string.sub(str,13,14),string.sub(str,16,17));
5     
6     cfg={}
7     cfg.ssid="ESP8266_"..ssidTemp;
8     cfg.pwd="12345678"
9     wifi.ap.config(cfg)
10     
11     cfg={}
12     cfg.ip="192.168.1.1";
13     cfg.netmask="255.255.255.0";
14     cfg.gateway="192.168.1.1";
15     wifi.ap.setip(cfg);
16     wifi.setmode(wifi.SOFTAP)
17     
18     str=nil;
19     ssidTemp=nil;
20     collectgarbage();
21     
22     print("Soft AP started")
23     print("Heep:(bytes)"..node.heap());
24     print("MAC:"..wifi.ap.getmac().."\r\nIP:"..wifi.ap.getip());

line 1: print the character;

line 3-4: get MAC address with AP mode, and the SSID from 6bits at the end of MAC. Certainly, you can use others name as ID as well. For example, node.chipid() can get the ID of ESP8266;

line 6-9: set SSID under AP mode. The format of SSID is "ESP8266_XXXXXX", where "XXXXXX" is the 6bits of the end of MAC address;

line 11-15: set the IP address, subnet mask, and gateway address for the module;

line 16: call wifi.setmode to run;

line 23: print the current memory.

Download this program, after run, the wireless devices can search the AP signal from NodeMCU. The following Figure is shown the connection with computer.

Log after run

1     NodeMCU 0.9.6 build 20150406  powered by Lua 5.1.4
2     
3     
4     ESP8266 Started
5     Compile File:ap.lua
6     Remove File:ap.lua
7     Readly to start soft ap
8     Soft AP started
9     Heep:(bytes)15328
10     MAC:1A-FE-34-A1-14-A7
11     IP:192.168.1.1
12     >

From the above log, ap.lua is deleted after compile.

If restart NodeMCU, the log is as follows.

1     NodeMCU 0.9.6 build 20150406  powered by Lua 5.1.4
2     
3     
4     ESP8266 Started
5     Ready to start soft ap
6     Soft AP started
7     Heep:(bytes)15048
8     MAC:1A-FE-34-A1-14-A7
9     IP:192.168.1.1
10     >

For more information, please visit  https://github.com/nodemcu/nodemcu-firmware/wiki/nodemcu_api_cn.

STA Mode

The file name is "sta.lua".

The "fileName.lua" in init.lu is revised into "sta.lua", and "fileName.lc" is revised into "sta.lc". After revised, can download into the NodeMCU. The code is as follows.

1     print("Ready to Set up wifi mode")
2     wifi.setmode(wifi.STATION)
3     
4     wifi.sta.config("MERCURY_1013","123456789")
5     wifi.sta.connect()
6     local cnt = 0
7     tmr.alarm(3, 1000, 1, function() 
8         if (wifi.sta.getip() == nil) and (cnt < 20) then 
9         print("Trying Connect to Router, Waiting...")
10         cnt = cnt + 1 
11         else 
12         tmr.stop(3)
13         if (cnt < 20) then print("Config done, IP is "..wifi.sta.getip())
14         else print("Wifi setup time more than 20s, Please verify wifi.sta.config() function. Then re-download the file.")
15         end
16             cnt = nil;
17             collectgarbage();
18         end 
19          end)

line 1: print the character;

line 2: set WiFi as STA mode;

line 4: set the wireless router SSID and password under the STA mode;

line 5: send a connection;

line 6-19: start the 3rd timer, check the connection after each 1000ms. If don't connect the wireless router within 20s, there is a connection failure. If successfully connect the wireless router within 20ms, the 3rd timer stops, and print the IP address. The log is as follows.

1     NodeMCU 0.9.6 build 20150406  powered by Lua 5.1.4
2     
3     
4     ESP8266 Started
5     Compile File:sta.lua
6     Remove File:sta.lua
7     Ready to Set up wifi mode
8     > Trying Connect to Router, Waiting...
9     Trying Connect to Router, Waiting...
10     Config done, IP is 192.168.1.100

It shows that 192.168.1.100 is gotten by NodeMCU.

AP+STA Mode

The file name is "apsta.lua".

In the file "init.lua", "fileName.lua: is revised as "apsta.lua", "fileName.lc" is revised as "apsta.lc". After revised, can download into NodeMCU. The code is as follows.

1     print("Ready to start soft ap AND station")
2     local str=wifi.ap.getmac();
3     local ssidTemp=string.format("%s%s%s",string.sub(str,10,11),string.sub(str,13,14),string.sub(str,16,17));
4     wifi.setmode(wifi.STATIONAP)
5     
6     local cfg={}
7     cfg.ssid="ESP8266_"..ssidTemp;
8     cfg.pwd="12345678"
9     wifi.ap.config(cfg)
10     cfg={}
11     cfg.ip="192.168.2.1";
12     cfg.netmask="255.255.255.0";
13     cfg.gateway="192.168.2.1";
14     wifi.ap.setip(cfg);
15     
16     wifi.sta.config("MERCURY_1013","123456789")
17     wifi.sta.connect()
18     
19     local cnt = 0
20     gpio.mode(0,gpio.OUTPUT);
21     tmr.alarm(0, 1000, 1, function() 
22         if (wifi.sta.getip() == nil) and (cnt < 20) then 
23             print("Trying Connect to Router, Waiting...")
24             cnt = cnt + 1 
25                  if cnt%2==1 then gpio.write(0,gpio.LOW);
26                  else gpio.write(0,gpio.HIGH); end
27         else 
28             tmr.stop(0);
29             print("Soft AP started")
30             print("Heep:(bytes)"..node.heap());
31             print("MAC:"..wifi.ap.getmac().."\r\nIP:"..wifi.ap.getip());
32             if (cnt < 20) then print("Conected to Router\r\nMAC:"..wifi.sta.getmac().."\r\nIP:"..wifi.sta.getip())
33                 else print("Conected to Router Timeout")
34             End
35     gpio.write(0,gpio.LOW);
36             cnt = nil;cfg=nil;str=nil;ssidTemp=nil;
37             collectgarbage()
38         end 
39     end)

line 1: print the character;

line 2-14: set AP;

line 16-17: set STA parameters;

line 19-39: set timer 0 to check whether it is connected to the wireless router after each 1s;

line 20: set D0 (i.e., GPIO16) as output. Revise the port to connect the LED light on NodeMCU development board.

line 25-26: shows the state of WiFi connection state.

The log is as follows.

1     NodeMCU 0.9.6 build 20150406  powered by Lua 5.1.4
2     
3     
4     ESP8266 Started
5     Compile File:apsta.lua
6     Remove File:apsta.lua
7     Ready to start soft ap AND station
8     > Trying Connect to Router, Waiting...
9     Trying Connect to Router, Waiting...
10     Trying Connect to Router, Waiting...
11     Soft AP started
12     Heep:(bytes)16192
13     MAC:1A-FE-34-A1-14-A7
14     IP:192.168.2.1
15     Conected to Router
16     MAC:18-FE-34-A1-14-A7
17     IP:192.168.1.100