Skip to content

Instantly share code, notes, and snippets.

@robertfoss
Last active December 25, 2015 16:48
Show Gist options
  • Save robertfoss/2291a9beb9bb889acc8b to your computer and use it in GitHub Desktop.
Save robertfoss/2291a9beb9bb889acc8b to your computer and use it in GitHub Desktop.
NodeMCU - IoT on the Cheap

Linux

Windows

ESP8266 Flasher
LUA Loader 

Mac / Linux

esptool
UART Driver
esplorer 

Command-line to flash firmware

esptool.py -p /dev/ttyUSB0 write_flash 0x000000 Downloads/nodemcu.bin

NodeMCU Firmware Binary

nodemcu-dev-14-modules-2015-11-02-23-45-27-float.bin 

Sample LUA Scripts

Rename either one of these files to init.lua and upload it to the NodeMCU board.

Other Resources

-- Set up WiFI
wifi.setmode(wifi.STATION)
wifi.sta.config("", "")
-- Tell us when we've gotten an IP
tmr.alarm(0, 500, 1, function()
if wifi.sta.getip()==nil
then
print("Connecting to AP...")
else
print("Connected as: " .. wifi.sta.getip())
tmr.stop(0)
end
end)
print("STARTING UP")
wifi.setmode(wifi.STATION)
wifi.sta.config("Hacklab-Guests","")
led1 = 5
gpio.mode(led1, 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+)=([^&]+)&*") do
_GET[k] = v
end
end
buf = buf.."<h1>ESP8266 Web Server</h1>";
buf = buf.."<p>Output 5 <a href=\"?set1=on\"><button>ON</button></a>&nbsp;<a href=\"?set1=off\"><button>OFF</button></a></p>";
local _on,_off = "",""
if(_GET.set1 == "on") then gpio.write(led1, gpio.HIGH); end
if(_GET.set1 == "off") then gpio.write(led1, gpio.LOW); end
WebResponse(client, buf);
collectgarbage();
end)
end)
function WebResponse(client, buf)
local hd = "HTTP/1.1 200 OK\r\n"
hd=hd.."Server: AvESP 0.1\r\n"
hd=hd.."Content-Length: "..string.len(buf).."\r\n"
hd=hd.."Content-Type: text/html\r\n"
hd=hd.."\r\n"
client:send(hd..buf)
client:close()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment