RoboRally/nodemcu_firmware/init.lua

47 lines
936 B
Lua

dofile('motor.lua')
cfg={}
cfg.ssid="robot-1"
cfg.pwd="roborally"
wifi.ap.config(cfg)
sv = net.createServer()
Peer = {}
function Peer:create(sck)
local peer = {}
setmetatable(peer, Peer)
peer.buffer = {}
return peer
end
function Peer:receive(sck, data)
self.buffer = self.buffer .. data
if #self.buffer >= 4 then
dir = string.byte(self.buffer,1)
if dir > 127 then dir = -256+dir end
motor_dir(1, dir)
dir = string.byte(self.buffer,2)
if dir > 127 then dir = -256+dir end
motor_dir(2, dir)
delay = string.byte(self.buffer,3) * 256 + string.byte(self.buffer, 4)
tmr.delay(delay)
motor_dir(1, 0)
motor_dir(2, 0)
self.buffer = {}
sck:close()
end
end
if sv then
-- listen to port 80 and call callback function if data is received
sv:listen(80,
function(conn)
peer = Peer:create(conn)
conn:on("receive", peer:receive(conn))
conn:send("robot-1 at your service")
end)
end