Dopo aver compilato e collegato il nodemcu ad uno switch il pin D5, come mostrato nel precedente articolo
Esempio Lua con modulo enduser_setup(parte1)
passiamo a caricare il codice che fa uso del modulo enduser_setup.
Apriamo ESPlorer e carichiamo il seguente codice
- 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 |
Nel seguente file zip sono presenti i file elencati sopra
Qui vi è la documentazione sul modulo enduser_setup
Documentazione modulo enduser_setup
L’esempio è composto da tre file:
- init.lua: Si esegue all’avvio dell’esp8266.
- riazzerawifi.lua: Avvia il portale per definire il proprio Access Point.
- webserver.lua: Avvia il semplice webserver sul nodemcu.
Come si può notare dal codice presente in init.lua, nel caso in cui il pin D5 sia nello stato ALTO, si avvia il portale usando il modulo enduser_setup. Nel caso in cui il pin D5 è nello stato BASSO la scheda acquisisce l’IP da un Access Point definito in precedenza e si avvia il webserver di test.
Per salvare la configurazione dell’Access Point cui collegarsi posizioniamo lo switch in modo tale da avere l’ingresso HIGH sul pin D5 e riavviamo la scheda.
Da un pc possiamo osservare la presenza di un altro Access Point, definito nel file riazzerawifi.lua
1 2 |
wifi.ap.config({ssid="Mynode-"..node.chipid() , auth=wifi.OPEN}) |
composto dalla stringa Mynode+chipid del modulo
Provvediamo a collegarci a tale Access Point e mediante browser accediamo al portale, definito in genere all’indirizzo 192.168.4.1
Selezioniamo o immettiamo l’SSID di un Access Point con relativa password. Al termine della connessione riposizioniamo lo switch sulla posizione BASSO per il pin D5 e riavviamo il nodemcu dev kit.
Ora il sistema si collega automaticamente all’SSID definito mediante il portale ed avvia l’esempio del webserver.
Si rimanda alla documentazione Lua nodemcu per ulteriori approfondimenti e moduli utilizzabili