Skip to content

Instantly share code, notes, and snippets.

@skyrocknroll
Last active January 10, 2021 21:51
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skyrocknroll/fb7184f4a6e0025e5d33443553a995db to your computer and use it in GitHub Desktop.
Save skyrocknroll/fb7184f4a6e0025e5d33443553a995db to your computer and use it in GitHub Desktop.
nodemcu led blink lua script nodemcu ESP12E
wifi.setmode(wifi.SOFTAP)
wifi.ap.config({ssid="test",pwd="12345678"})
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)

Install node flasher

  • apt-get install python3-pip
  • pip3 install -U -f https://extras.wxpython.org/wxPython4/extras/linux/gtk3/ubuntu-16.04 wxPython
  • pip3 install esptool
  • git clone https://github.com/marcelstoer/nodemcu-pyflasher
  • cd /nodemcu-pyflasher && python3 nodemcu-pyflasher.py
  • Generate a build and build will be delivered tto email download and burn https://nodemcu-build.com/trigger-build.php
  • Nodemcu devkit and pin details can be found here https://github.com/nodemcu/nodemcu-devkit-v1.0
wifi.setmode(wifi.STATION)
wifi.sta.config("xxxx", "xxxxxx")
-- register event callbacks for WiFi events
wifi.sta.eventMonReg(wifi.STA_CONNECTING, function(previous_state)
if(previous_state==wifi.STA_GOTIP) then
print("Station lost connection with access point. Attempting to reconnect...")
else
print("STATION_CONNECTING")
end
end)
print(wifi.sta.getip())
dofile("blink.lua")
srv=net.createServer(net.TCP,3)
srv:listen(80,function(conn)
conn:on("receive",function(conn,payload)
print(payload)
conn:send("<h1> Hello, NodeMcu.</h1>")
end)
conn:on("sent", function (c) c:close() end) --close the socket after succesfully flushed data
end)
  • Connect the nodemcu to backside of the cpu. Front panel is completely flicky.
  • Once connected Open ESPlorer as root or add the current user to dialout group using sudo usermod -a -G dialout $USER . After this no more need to launch as usod. Please logout and login for the change to effect.
  • Select /dev/ttyUSBx and click open.
  • Click the rest button in nodemcu.
  • send =node.heap() and wait for sometime. Send the same command again if you get garbage output till you get the right numbers. ** use nodemcu uploader which stable and better than ESPLorer
  • Also set the baud rate to 115200bps ( 921600bps never workes 115200bps worked better) . Latest firmware changed the default baud rate to 115200bps so use that.
  • ESPlorer for some reason not uploading the files correctly.
  • https://github.com/kmpm/nodemcu-uploader install it pip install nodemcu-uploader
  • nodemcu-uploader upload init.lua
  • Whatever put under print() will can be monitored through terminal port. use nodemcu-uploader terminal You should see whatever print statements you have put in terminal port. Also you can send data simultaneously. l = file.list(); for k,v in pairs(l) do print("name:"..k..", size:"..v) end paste this to get the list of files.
  • use cutecom apt-get install cutecom which has seperate window for read and write which will decluter.
  • Find the port of the nodemcu . Usually it will under ls -l /dev/ttyUSB*
  • In the device drop down paste the terminal /dev/ttyUSB0
  • Make sure you are connection 2.4Ghz SSID not the 5GHZ because nodemcu only supports 2.4Ghz
  • Install this vscode plugin https://github.com/fduman/vscode-nodemcu which will make life easier.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment