-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.lua
100 lines (82 loc) · 2.14 KB
/
main.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
-- GLOBAL VARIABLES
DEBUG = false
LOG2 = false -- log to secondary serial console
-- udp ports
PORT = 52381
BAUD = 38400
clientip = 0
clientport = 0
client = 0
localip = ''
dofile('helpers.lua')
dofile('credentials.lua')
-- log will print to the primary serial console
function log(s)
if DEBUG or LOG2 then
if LOG2 then
uart.write(1, s .. '\n') -- secondary, one-way console
else
print(s)
end
end
end
-- sout will print to the bidirectional uart
function sout(s)
uart.write(0, s)
end
function start_wifi()
wifi.setmode(wifi.STATION)
station_cfg={}
station_cfg.ssid=SSID
station_cfg.pwd=PWD
station_cfg.auto=true
wifi.sta.config(station_cfg)
log("\nConnecting WiFi")
-- async wait for wifi connection
tmr.create():alarm(1000, tmr.ALARM_AUTO, function(t)
blink()
if wifi.sta.getip()== nil then
log("Waiting for IP...")
else
t:stop()
localip = wifi.sta.getip() -- we store our_ip, as we need it to setup udp socket properly
log(" Your IP is ".. localip)
start_udp()
end
end)
end
function start_udp()
log('Starting UDP Socket')
udpSocket = net.createUDPSocket()
-- pass an IP here or will bind to 0.0.0.0 (all interfaces)
udpSocket:listen(PORT)
-- async wait for data
udpSocket:on("receive", function(s, data, remoteport, remoteip)
client = s
clientip = remoteip
clientport = remoteport
debug(data)
sout(data) -- echo to serial port
blink(1,5)
end)
port, ip = udpSocket:getaddr()
log(string.format("UDP socket listening on %s:%d", ip, port))
blink(3)
start_serial()
end
function start_serial()
log('Listening for Serial Data')
uart.on('data', string.char(0xff), function(data)
blink(2,5)
debug(data)
if client ~= 0 then
client:send(clientport, clientip, data);
end
end, 0)
end
-- Main Program
uart.setup(0, BAUD, 8, 0, 1, 0 ) -- echo off
gpio.mode(LEDPIN, gpio.OUTPUT)
led(off)
blink()
start_wifi() -- should give time to reload if serial crashes