-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinit.lua
162 lines (131 loc) · 3.61 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
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
-- Set baud rate to 9600
-- WARNING:
-- By default, we switch off the echo in order to get pure result
-- If you want to use ESPlorer or other IDE upload lua script, you need to
-- manully use the following code to switch echo on first.
--
-- uart.setup(0, 9600, 8, uart.PARITY_NONE, uart.STOPBITS_1, 1)
--
uart.setup(0, 9600, 8, uart.PARITY_NONE, uart.STOPBITS_1, 0)
server = nil
data = {}
mode = nil
function connectToAP(ssid, password)
if password == nil then
password = ""
end
mode = "station"
wifi.setmode(wifi.STATIONAP)
wifi.sta.config(ssid, password, 1)
return true
end
function getIP()
if mode == "station" then
ip, netmask, gateway = wifi.sta.getip()
return ip
else
ip, netmask, gateway = wifi.ap.getip();
return ip
end
end
function getWifiStatus()
return wifi.sta.status()
end
function configAP(ssid, password)
mode = "ap"
wifi.setmode(wifi.STATIONAP)
if password == nil or password == "" then
wifi.ap.config({
ssid = ssid,
auth = wifi.OPEN
})
else
wifi.ap.config({
ssid = ssid,
pwd = password
})
end
return true
end
function getData()
return data
end
function getDataJSON()
return cjson.encode(data)
end
function setValue(key, value)
data[key] = value
return true
end
function getValue(key, value)
return data[key]
end
function sendFile(socket, filename, contentType)
if file.open(filename, "r") then
local header = "HTTP/1.1 "
header = header .. "200 OK\r\n"
header = header .. "Content-Type: "
header = header .. contentType .. "\r\n\r\n"
socket:send(header,
function()
local function sendChunk()
local line = file.read(1024)
if line then
socket:send(line, sendChunk)
else
file.close()
collectgarbage()
socket:close()
end
end
sendChunk()
end)
end
end
function processRequest(request, socket)
if request.path == "/api/data" then
local header = "HTTP/1.1 "
header = header .. "200 OK\r\n"
header = header .. "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n"
header = header .. "\r\n"
socket:send(header .. getDataJSON())
socket:close()
else
if request.path == "/" or request.path == "index.html" then
sendFile(socket, "index.html", "text/html")
else
local header = "HTTP/1.1 "
header = header .. "404 Not Found\r\n"
header = header .. "Content-Type: text/plain\r\n\r\n"
socket:send(header .. "Page not found\r\n")
socket:close()
end
end
end
function startServer()
server = net.createServer(net.TCP)
server:listen(80, function(conn)
conn:on("receive", function(socket, payload)
local _, _, method, path, vars = string.find(payload, "([A-Z]+) (.+)?(.+) HTTP")
if method == nil then
_, _, method, path = string.find(payload, "([A-Z]+) (.+) HTTP")
end
print(path)
local request = { path = path };
processRequest(request, socket);
end)
end)
return true
end
function stopServer()
if server ~= nil then
server:close()
server = nil;
end
return true
end
function restartServer()
stopServer()
startServer()
return true
end