As mentioned in the previous article
how to upload nodemcu firmware with esptool
we are now ready to upload some example code on ESP8266 system.
We follow the LUA Nodemcu documentation
and upload a simple http server on the system.
Using ESPlorer we upload the following code, a modified version of the code available on the github. Modify the fields “SSID” and “password” with the the values of your Wi-fi Router.
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 |
wifi.setmode(wifi.STATION) wifi.sta.config("SSID", "password") 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) |
Save the file as ipstatico.lua. With ESPlorer upload the file to nodemcu devkit with “Save to ESP” button .
Create a second file named init.lua
1 |
tmr.alarm(0, 5000, 0, function() dofile("ipstatico.lua") end) |
Save this file on the device.
Reboot the device with the reset command on ESPlorer tool and from the ESPlorer command bar execute
1 |
print(wifi.sta.getip()) |
This command provides us the ip acquired by the device. We are able now to access with a browser to the assigned ip address, discovered above, and interact with a small interface that allows us to activate or not the PIN1 output
With this method, however, we need to know the Access Point to which you must connect. To overcome this limitation, we can use the enduser_sertup module
With this module we start the ESP8266 device as an access point with a portal on which we can enter the credentials of the access point which we want to connect.
In the next article we’ll build the nodemcu firmware by adding the enduser setup module, not available in the standard firmware, and we will do the same example using this module