After the firmware build and its upload to the nodemcu dev kit, we connected a switch to pin D5, as shown in the previous article
Nodemcu Enduser_setup module lua sample(part1)
We finally can upload some sample code in which we use the enduser_setup module
Open ESPlorer and load the following sample code
- webserver.lua
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
-- webserver.lua --webserver sample from the nodemcu github if srv~=nil then srv:close() end gpio.mode(1, gpio.OUTPUT) srv=net.createServer(net.TCP) srv:listen(80,function(conn) conn:on("receive", function(client,request) local buf = "" local _, _, method, path, vars = string.find(request, "([A-Z]+) (.+)?(.+) HTTP") if(method == nil)then _, _, method, path = string.find(request, "([A-Z]+) (.+) HTTP") end local _GET = {} if (vars ~= nil)then for k, v in string.gmatch(vars, "(%w+)=(%w+)&*") do _GET[k] = v end end buf = buf.."<h1> Hello, NodeMcu.</h1><form src=\"/\">Turn PIN1 <select name=\"pin\" onchange=\"form.submit()\">" local _on,_off = "","" if(_GET.pin == "ON")then _on = " selected=true" gpio.write(1, gpio.HIGH) elseif(_GET.pin == "OFF")then _off = " selected=\"true\"" gpio.write(1, gpio.LOW) end buf = buf.."<option".._on..">ON</opton><option".._off..">OFF</option></select></form>" client:send(buf) end) conn:on("sent", function (c) c:close() end) end) |
- riasserawifi.lua
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
--riazzerawifi.lua -- Starts the portal to choose the wi-fi router to which we have -- to associate wifi.sta.disconnect() wifi.setmode(wifi.STATIONAP) --ESP SSID generated wiht its chipid wifi.ap.config({ssid="Mynode-"..node.chipid() , auth=wifi.OPEN}) enduser_setup.manual(true) enduser_setup.start( function() print("Connected to wifi as:" .. wifi.sta.getip()) end, function(err, str) print("enduser_setup: Err #" .. err .. ": " .. str) end ); |
- init.lua
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
-- init.lua --Access Point management pin = 5 --Input PIN Resetta=0 --Variable used to manage AP reset gpio.mode(pin,gpio.INPUT) --Pin 5 in input mode --If pin input is High let's start the nodemcu portal if (gpio.read(pin)==gpio.HIGH ) then Resetta=1 end -- Check if reset the AP credential if Resetta==1 then -- Start the nodemcu portal tmr.alarm(0, 5000, 0, function() dofile("riazzerawifi.lua") end) else -- Start the webserver wiht ip defined by Wi-Fi router tmr.alarm(0, 5000, 0, function() dofile("webserver.lua") end) end |
It is possible to download the above samples at the following link
Here is the documentation about the enduser_setup module
enduser_setup module documentation
The example consists of three files:
- init.lua: It is executed at the startup of the esp8266.
- riazzerawifi.lua: It starts the portal to connect and save the configuration to your Access Point.
- webserver.lua: It starts the webserver sample on the nodemcu.
As you can see from the code in init.lua, when the pin D5 is in the state HIGH, we start the portal using enduser_setup module. In the case in which the pin D5 is in the state LOW the esp8266 acquires the IP from an Access Point defined previously after which the test webserver starts.
To save the access point confguration to which we want to connect we place the switch in order to have the HIGH input on pin D5 and reboot the card.
From a PC we can observe the presence of another access point, defined in the file riazzerawifi.lua
1 2 |
wifi.ap.config({ssid="Mynode-"..node.chipid() , auth=wifi.OPEN}) |
composed by Mynode string + ChipID module
We can connect to this access point and via browser we access to the portal at the ip address 192.168.4.1
Select or enter the SSID of an access point providing the correct password. After the connection reposition the switch to have the LOW state for pin D5 and reboot the nodemcu dev kit.
Now the system will automatically connect to the SSID defined previously and starts the example of the web server.
Please refer to the Lua documentation nodemcu for further study and usable modules