Is it possible to upload a Lua script to a NodeMCU using the Wifi interface instead of serial?
The tutorials and examples that I have found all use the serial interface, i.e. a cable, to program the NodeMCU, but I would like to change the program without connecting anything (using a smartphone or a browser)
I upload all the modules over wifi. I first upload a bootstrap.lua
program in the usual way (over USB). This program can then be used to upload the real (larger) payload. Here is the bootstrap program:
ip, mask, host = wifi.sta.getip()
port, path, pgm = 80, "/upload", "u.lc"
file.remove(pgm) ; file.open(pgm, "w+") payloadFound = false
local conn = net.createConnection(net.TCP, 0)
conn:on("connection", function(conn)
conn:send("GET "..path.."/"..pgm.." HTTP/1.0\r\n".."Host: "..host.."\r\nConnection: close\r\nAccept: */*\r\n\r\n") end)
conn:on("receive", function(conn, payload)
if (payloadFound) then file.write(payload) file.flush()
else payloadOffset = string.find(payload, "\r\n\r\n")
if (payloadOffset) then
file.write(string.sub(payload, payloadOffset + 4)) file.flush() payloadFound = true
end end end)
conn:on("disconnection", function(conn) file.close() dofile(pgm) end) conn:connect(port,host)
The first line uses the gateway server as the web server from which programs are uploaded.
The second line sets the port (80
), path (/upload
) and name (u.lc
) of the program to upload.
It then GETs the file and finally runs it (last line).
You must have your wireless connection active before running this, and your web server should be active of course, with your payload in /upload/u.lc
.
Naturally you can change the hardwired values, or even make them dynamic.
BTW, the condensed format is there to make the initial upload fast, I upload with luatool.py
using the --dofile
option.
Updating your program (u.lc
) later is a simple repeat of dofile("bootstrap.lua")
.
My u.lc
is a stage 2 bootstrap that uploads a long list of files (mostly .lc
). Probably too involved for this short answer.
Finally, I should mention that this is loosely based on https://github.com/Manawyrm/ESP8266-HTTP/
HTH
Yes it is possible. It is kind of a home-brewed option but it works to a certain extent. The only limitation is in size, of course but other than that it works pretty well. Take a look at:
http://www.instructables.com/id/ESP8266-WiFi-File-Management/
You need to have a way to write the PHP program (I wrote it in C#) if you can't write the code in another language you can download and reuse what this user wrote and use your own PHP server and you should be good to go.
If you have questions, please ask.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With