# Play nodeMCU ###### tags: `RPI` ## good tutorial https://blog.alexellis.io/iot-nodemcu-sensor-bme280/ https://nodemcu.readthedocs.io/ ## Build firmware https://nodemcu.readthedocs.io/en/master/build/#integer-build ``` shell git clone --recurse-submodules https://github.com/nodemcu/nodemcu-firmware.git cd nodemcu-firmware vim app/include/user_config.h vim app/include/user_module.h docker run --rm -it -v $PWD:/opt/nodemcu-firmware marcelstoer/nodemcu-build build ``` note: You need to ensure the below module/features are enabled. * http * ssl * net * GPIO * wifi * MQTT * PMSLEEP_ENABLE ## Flash firmware https://nodemcu.readthedocs.io/en/master/flash/ https://github.com/espressif/esptool ``` shell pip3 install esptool # use this when your program is in infinite loop sudo esptool.py erase_flash sudo esptool.py -p /dev/ttyUSB0 write_flash 0x00000 nodemcu-firmware/bin/nodemcu_float_master_20200210-0431.bin ``` ## some lua example A webserver that toggle gpio https://gist.github.com/linnil1/e754589d52e387f14290ea16e1151468 Send data to server every seconds https://gist.github.com/linnil1/d5ec89f84090f8279bbb2038924abd25 ### wifi connection ``` lua ssid = "" key = "" function connect_wifi(func) func = func or function() end print("Get wifi") wifi.setmode(wifi.STATION) station_cfg={} station_cfg.ssid = ssid station_cfg.pwd = key station_cfg.save = true wifi.eventmon.register(wifi.eventmon.STA_CONNECTED, function(T) print("STA - CONNECTED\nSSID: "..T.SSID.."\nBSSID: "..T.BSSID.."\nChannel: "..T.channel.."\n") end) wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, function(T) print("IP: "..T.IP.."\nMASK: "..T.netmask.."\nGW: "..T.gateway.."\nStation ready!\n") func() end) wifi.sta.config(station_cfg) end ``` ### requests ``` lua http.get("https://ntu.edu.tw:443", nil, function(code, data) print(code, data) end ) http.post(url, 'Content-Type: application/json\r\n', '{"status": "..s.."}', function(code, data) if (code < 0) then fail_func() else print(code, data) end end ) ``` ### file read write ``` lua -- list l = file.list(); for k,v in pairs(l) do print("name:"..k..", size:"..v) end -- read if file.open("txt") then print(file.read()) file.close() end -- write if file.open("txt", "w") then file.write('foo bar') file.close() end ``` ### gpio https://www.electronicwings.com/nodemcu/nodemcu-gpio-with-esplorer-ide https://nodemcu.readthedocs.io/en/master/modules/gpio/ ``` lua LEDpin = 8 -- Declare LED pin no. gpio.mode(LEDpin, gpio.OUTPUT) gpio.write(LEDpin, gpio.HIGH) gpio.write(LEDpin, gpio.LOW) -- blink once in 300ms gpio.write(LEDpin, gpio.LOW) tmr.create():alarm(300, tmr.ALARM_SINGLE, function() gpio.write(LEDpin, gpio.LOW) end) ``` ### http server ``` lua sv = net.createServer(net.TCP, 30) -- response data for request function send(sck, message) print(message) sck:send("HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n") sck:send('{"status":"'..message..'"}', function() sck:close() end) end -- Create a web server function web_server() function receiver(sck, data) print(data) send(sck, "Hello World") end if sv then sv:listen(80, function(conn) conn:on("receive", receiver) end) end end ``` ### other ``` lua node.restart() file.rename(a, b) file.remove(a) ``` ### Note: Don't use tmr.delay https://nodemcu.readthedocs.io/en/master/modules/tmr use alarm instead. ``` lua tmr.create():alarm(1000, tmr.ALARM_AUTO, function(mytmr) -- 100ms if (something) then mytmr.unregister(mytmr) else do_something() end end ``` ### deep sleep https://nodemcu.readthedocs.io/en/master/modules/node/#nodedsleep `node.dsleep(1 * 1000000)`(1second) and connect D0(pin16) to RST(reset) ## upload code https://nodemcu.readthedocs.io/en/latest/upload/ https://github.com/kmpm/nodemcu-uploader If your file name is `init.lua`, it will automatically execute it after booting. ``` shell pip3 install nodemcu-uploader sudo nodemcu-uploader upload ./myinit.lua ``` testing: you will see interaction mode. ``` sudo screen -L /dev/ttyUSB0 115200 ``` You can run your code like this ``` > dofile("myinit.lua") ``` ## Install lua on local https://www.lua.org/start.html ``` curl -R -O http://www.lua.org/ftp/lua-5.3.5.tar.gz tar zxf lua-5.3.5.tar.gz cd lua-5.3.5 sudo make linux install ``` and tutorial https://www.tutorialspoint.com/lua/index.htm