Skip to content

Instantly share code, notes, and snippets.

@bitcloud
Created April 22, 2015 15:10
Show Gist options
  • Save bitcloud/b6baddff3bbb5ee7eca4 to your computer and use it in GitHub Desktop.
Save bitcloud/b6baddff3bbb5ee7eca4 to your computer and use it in GitHub Desktop.
Connect to and update arduino over wifi with esp8266
-- Code to connect to or update an arduino over wifi with an esp8266 (nodemcu lua firmware)
-- Send a single 'B' char to switch the connection to update mode
-- echo -n 'B' | nc 192.168.178.57 8080 ; avrdude -v -p atmega328p -c arduino -P net:192.168.178.57:8080 -D -U flash:w:code.hex:i
-- there are more instructions to come, but thats it for now
-- because there is no way of reaching the esp repl with this code running, there is an override.
-- currently it delays the startup for 2 secs. In that timeframe you should pull the overridepin to GND to stop booting into redirect mode
-- just using one gpio for triggering reset on the arduino and overriding the boot sequence into redirect mode of the esp
-- this code is still WIP
-- will add some wiring instructions soonisch, basicly just connect the uart to the arduino uart pins, the resetpin to the conficured gpio
resetPin=4
overridePin=4
gpio.mode(overridePin, gpio.INPUT)
bootloadermode=false
ssid='xxxxx'
pass='xxxxx'
function startServer()
sv=net.createServer(net.TCP, 180)
sv:listen(8080, function(conn)
-- wifi console connected
local newConn=true
if(bootloadermode) then
gpio.write(resetPin, gpio.LOW)
tmr.delay(10000)
gpio.write(resetPin, gpio.HIGH)
end
function s_output(str)
if (conn~=nil) then
conn:send(str)
end
end
--redirect node core stuff
node.output(s_output,0)
--redirect uart stuff
uart.on("data", 1, s_output, 0)
conn:on("receive", function(conn, pl)
--node.input(pl)
if(newConn and pl == "B") then
bootloadermode=true
s_output('bootloadermode')
pl = 'bootloadermode'
else
bootloadermode=false
end
newConn=false
uart.write(0, pl)
if (conn==nil) then
node.output(nil)
uart.on("data")
end
end)
conn:on("disconnection",function(conn)
node.output(nil)
uart.on("data")
end)
end)
end
function startRedirector()
gpio.mode(resetPin, gpio.OUTPUT)
gpio.write(resetPin, gpio.LOW)
wifi.setmode(wifi.STATION)
wifi.sta.config(ssid,pass)
wifi.sta.connect()
tmr.alarm(0, 1000, 1, function()
if wifi.sta.getip()=="0.0.0.0" then
--print("Connect AP, Waiting...")
else
startServer()
tmr.stop(0)
uart.setup( 0, 57600, 8, 0, 1, 0 )
gpio.write(resetPin, gpio.HIGH)
end
end)
end
print("READING PIN " .. overridePin .. " FOR 2 SEC TO OVERRIDE STARTUP")
tmr.alarm(0, 2000, 1, function()
tmr.stop(0)
if(gpio.read(overridePin) == 1) then
---BOOTING only if overridePin is HIGH
print("BOOTING UART REDIRECT. PIN " .. overridePin .. " IS HIGH")
startRedirector()
else
print("OVERRIDING BOOT. PIN " .. overridePin .. " IS LOW")
end
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment