-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.lua
130 lines (87 loc) · 3.16 KB
/
init.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
-- -------------------------------------
-- Begin WiFi configuration
-- -------------------------------------
wifiConf = {}
-- wifi.STATION -- station: join a WiFi network
-- wifi.SOFTAP -- access point: create a WiFi network
-- wifi.wifi.STATIONAP -- both station and access point
wifiConf.mode = wifi.STATIONAP -- both station and access point
wifiConf.accessPoint = {}
wifiConf.accessPoint.ssid = "Arctic-"..node.chipid() -- Name of the SSID you want to create
wifiConf.accessPoint.pwd = "password" -- WiFi password - at least 8 characters
wifiConf.accessPointIp = {}
wifiConf.accessPointIp.ip = "192.168.111.1"
wifiConf.accessPointIp.netmask = "255.255.255.0"
wifiConf.accessPointIp.gateway = "192.168.111.1"
-- wifiConf = nil
collectgarbage()
-- End WiFi configuration
-- ---------------------------------------------------------------------
-- Compile server code and remove original .lua files.
-- This only happens the first time afer the .lua files are uploaded.
-- ---------------------------------------------------------------------
local compileAndRemoveIfNeeded = function(f)
if file.open(f) then
file.close()
print('Compiling:', f)
node.compile(f)
file.remove(f)
collectgarbage()
end
end
local serverFiles = {
'network.lua',
'scanap.lua',
'commands.lua',
'httpserver.lua',
'httpserver-b64decode.lua',
'httpserver-basicauth.lua',
'httpserver-conf.lua',
'httpserver-connection.lua',
'httpserver-error.lua',
'httpserver-header.lua',
'httpserver-request.lua',
'httpserver-static.lua',
}
for i, f in ipairs(serverFiles) do compileAndRemoveIfNeeded(f) end
compileAndRemoveIfNeeded = nil
serverFiles = nil
collectgarbage()
dofile("commands.lc")()
-- -------------------------------------------------------------------------
-- Configure WIFI AP and tell the chip to connect to the access point
-- -------------------------------------------------------------------------
wifi.setmode(wifiConf.mode)
tmr.alarm(1, 3000, tmr.ALARM_SINGLE, function ()
dofile("scanap.lc")()
tmr.alarm(1, 30000, tmr.ALARM_AUTO, function()
dofile("scanap.lc")
end)
end )
start_softap = function(ssid, passwd)
if (wifiConf.mode == wifi.SOFTAP) or (wifiConf.mode == wifi.STATIONAP) then
if not (ssid == "Arctic-NOCALL") then
wifiConf.accessPoint.ssid = ssid
end
wifiConf.accessPoint.pwd = passwd
wifi.ap.config(wifiConf.accessPoint)
wifi.ap.setip(wifiConf.accessPointIp)
end
start_softap = nil
end
-- -------------------------------------------------------------------
-- Function to start the web server on port 80
-- It is called only once so it will be deleted after use
-- -------------------------------------------------------------------
start_http_server = function(uname, passwd)
tmr.alarm(2, 10000, tmr.ALARM_AUTO, function()
if (not not wifi.sta.getip()) or (not not wifi.ap.getip()) then
dofile("httpserver.lc")(80, uname, passwd)
tmr.unregister(2)
start_http_server = nil
end
end )
end
collectgarbage()
-- Tell main MCU that we are booted and ready
uart.write(0, "$__BOOT__\r\n")