-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.lua
173 lines (130 loc) · 4.94 KB
/
commands.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
return function ()
-- -----------------------------------------------------------
-- Send response to main mcu
-- -----------------------------------------------------------
function mcu_sendResponse(r)
if (not r) then
uart.write(0, "@-\r\n")
else
uart.write(0, "@" .. r .. "\r\n")
end
end
-- --------------------------------------------------------------
-- Do a command on the main MCU (Teensy)
-- Read and return a parameter value
-- Write a parameter value
-- --------------------------------------------------------------
function mcu_doCommand(c)
uart.write(0, "#" .. c .. "\r\n")
cr = coroutine.running()
client = true
return coroutine.yield()
end
function mcu_getParam(p)
return mcu_doCommand("R " .. p)
end
function mcu_setParam(p, val)
return mcu_doCommand("W " .. p .. " " .. val)
end
function mcu_sendData(d)
uart.write(0, ":" .. d .. "\r\n")
end
-- --------------------------------------------------------------
-- Check if access point (ssid) is approved and return password
-- nil if not found
-- Empty string if access point is open
-- --------------------------------------------------------------
function mcu_checkAp(ssid)
return mcu_doCommand("A " .. ssid .. " ")
end
-- -----------------------------------------
-- Listen for data on serial port
-- -----------------------------------------
function mcu_listen(func)
uart.on("data", "\r", func, 0)
end
-- ----------------------------------------
-- Open a socket and start a connection
-- ----------------------------------------
srv = nil
sock = nil
function net_connect(host, port)
dofile("network.lc")(host, port)
end
function net_data(d)
sock:send(d, function()
mcu_sendResponse("OK");
end)
end
-- ------------------------------------------------------------------------------
-- Start a listener for events from the serial port.
-- Can listen for commands (from main MCU) or responses to commands (from ESP)
-- ------------------------------------------------------------------------------
client = false
listener = coroutine.create( function()
while true do
shell = false
mcu_listen(function(data)
data = string.sub(data,0,-2)
if (client) then
-- CLIENT MODE: Get responses from main MCU. Return result to
-- coroutine that initiated the command.
client = false
coroutine.resume(cr, data)
else
-- SERVER MODE: Get and respond to commands from main MCU
-- Return to shell
if string.match(data, "^SHELL=1") then
-- unregister callback function
uart.on("data")
shell = true
print("Returning to NODEMCU shell")
-- Show status
elseif string.match(data, "^STAT") then
mcu_sendResponse(wifi.sta.status())
-- Show config
elseif string.match(data, "^CONF") then
ssid, password, bssid_set, bssid = wifi.sta.getconfig()
if not (wifi.sta.status() == 5) then
mcu_sendResponse("-")
else
mcu_sendResponse(ssid .. " (" .. bssid .. ")")
end
-- Show IP address
elseif string.match(data, "^IP") then
mcu_sendResponse(wifi.sta.getip())
-- Show mac address
elseif string.match(data, "^MAC") then
mcu_sendResponse(wifi.sta.getmac())
-- Show AP IP address
elseif string.match(data, "^AP.IP") then
mcu_sendResponse(wifi.ap.getip())
elseif string.match(data, "^AP.SSID") then
mcu_sendResponse(wifiConf.accessPoint.ssid);
elseif string.match(data, "^NET.OPEN .*") then
local port,host = string.match(data, "NET.OPEN ([0-9]+) (.*)")
net_connect(host, port)
elseif string.match(data, "^NET.DATA .*") then
print("'"..data.."'")
if (not not sock) then
local dt = string.match(data, "NET.DATA (.*)")
if (not dt) then
net_data("\r\n")
else
net_data(dt.."\r\n")
end
end
elseif string.match(data, "^NET.CLOSE") then
if (not not sock) then
sock:close()
end
mcu_sendResponse("OK");
else
print("Unknown command: ", data)
end
end
end )
coroutine.yield()
end
end )
end