-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.lua
More file actions
117 lines (83 loc) · 3.57 KB
/
Copy pathserver.lua
File metadata and controls
117 lines (83 loc) · 3.57 KB
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
local fan = require('fan');
local led = require('led');
local state = require('state');
local startServer = function()
wifi.setmode(wifi.STATION)
local ipConfig = {
ip = '192.168.1.7',
netmask = '255.255.255.0',
gateway = '192.168.1.1'
}
wifi.sta.setip(ipConfig)
local stationConfig = {ssid = '', pwd = ''}
wifi.sta.config(stationConfig)
local server = net.createServer(net.TCP, 30)
function onReceive(socket, data)
led.blink()
print(data)
local method = string.gmatch(data, '%S+')()
if (method == 'POST') then
local bodyIndex = data:find('\n\r\n')
if (bodyIndex) then
local bodyContent = data:sub(bodyIndex)
local iterator = string.gmatch(bodyContent, '%S+')
local setpointText = iterator()
local newState = {}
if (setpointText) then
local setpoint = tonumber(setpointText)
newState.setpoint = setpoint
end
local hysteresisText = iterator()
if (hysteresisText) then
local hysteresis = tonumber(hysteresisText)
newState.hysteresis = hysteresis
end
local mode = iterator()
if (mode == 'NORMAL' or mode == 'FORCED_FAN_ON' or mode ==
'FORCED_FAN_OFF') then newState.mode = mode end
state.setState(newState);
local message = string.format(
'{"setpoint": %.4f, "hysteresis": %.4f, "mode": "%s"}',
newState.setpoint, newState.hysteresis,
newState.mode)
socket:send('HTTP/1.1 200 OK\n')
socket:send('Server: ESP8266 (nodemcu)\n')
socket:send('Content-Type: application/json\n')
socket:send(string.format('Content-Length: %i\n\n',
message:len()))
socket:send(message)
return
end
socket:send('HTTP/1.1 400 Bad Request\n')
socket:send('Server: ESP8266 (nodemcu)\n')
socket:send('Content-Length: 0\n\n')
return
end
local freshState = state.getState()
local message = string.format(
'{"outputTemperature": %.4f, "inputTemperature": %.4f, "setpoint": %.4f, "hysteresis": %.4f, "mode": "%s", "fanOn": %s, "heap": %d}',
freshState.outputTemperature,
freshState.inputTemperature, freshState.setpoint,
freshState.hysteresis, freshState.mode,
fan.enabled and 'true' or 'false', node.heap())
socket:send('HTTP/1.1 200 OK\n')
socket:send('Server: ESP8266 (nodemcu)\n')
socket:send('Content-Type: application/json\n')
socket:send(string.format('Content-Length: %i\n\n', message:len()))
socket:send(message)
end
if server then
server:listen(80, function(socket)
socket:on('receive', onReceive)
end)
end
end
startServer()
local printWifiStatus = function()
local ssid, password = wifi.sta.getconfig()
local ip, mask, gateway = wifi.sta.getip()
local status = wifi.sta.status()
print(string.format('%s | %s | %s | %s | %s | %s', ssid, password, ip, mask,
gateway, status))
end
tmr.create():alarm(5000, tmr.ALARM_AUTO, printWifiStatus)