-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.lua
151 lines (139 loc) · 3.39 KB
/
application.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
---------------- Main program
----------------------
-- Configuration Access Point WiFi
print("WiFi starting...")
ipcfg = {}
ipcfg.ip="192.168.1.1"
ipcfg.netmask="255.255.255.0"
ipcfg.gateway="192.168.1.1"
wifi.ap.setip(ipcfg)
cfg={}
cfg.ssid="pogoda"
cfg.max=1
wifi.ap.config(cfg)
wifi.setmode(wifi.SOFTAP)
print("WiFi started")
----------------------
----------------------
-- Global Variables
counter = 0
responseOK = "HTTP/1.1 200 OK \r\nContent-Type: text/html\r\n\r\n"
lastRequestWether = false
temp = 0
humi = 0
temp_dec = 0
humi_dec = 0
pin = 4
----------------------
----------------------
-- Functions
-- DHT11:
function readDataFromDHT()
status, temp, humi, temp_dec, humi_dec = dht.read(pin)
if status == dht.OK then
print( "DHT data read" )
elseif status == dht.ERROR_CHECKSUM then
print("DHT Checksum error")
elseif status == dht.ERROR_TIMEOUT then
print("DHT timed out")
end
end
-- WWW:
function readPage(fileName)
file.open(fileName)
local page=file.read()
file.close()
return page
end
function sendResponse(socket, page)
socket:on("sent", function(sck) sck:close() end)
socket:send(page)
end
function prepareMainPage(data)
local page = responseOK..readPage("index.html")
page = string.gsub(page, "#VV", counter)
counter = counter + 1
page = string.gsub(page, "#PP", data)
return page
end
function prepareWeather()
local page = readPage("pogoda.html")
local data = string.format("%d.%01d",
math.floor(temp),
temp_dec)
page = string.gsub(page, "#TI", data)
page = string.gsub(page, "#TO", "--" )
data = string.format("%d",
math.floor(humi))
page = string.gsub(page, "#HH", data)
return page
end
----------------------
----------------------
-- Server receiver
function receiver(socket, data)
local i
local j
local page = nil
if string.find(data,"favicon")==nil then
print(data)
end
i,j=string.find(data, "\n")
data=string.sub(data, 1,j)
if string.find(data, " / HTTP") then
page = prepareMainPage(prepareWeather())
lastRequestWether = true
end
if string.find(data, "index.html") then
page = prepareMainPage(prepareWeather())
lastRequestWether = true
end
if string.find(data, "weather") then
page = prepareMainPage(prepareWeather())
lastRequestWether = true
end
if string.find(data, "info") then
page = prepareMainPage(readPage("info.html"))
lastRequestWether = false
end
if string.find(data, "page") then
if lastRequestWether then
page = prepareWeather()
else
socket:close()
end
end
if string.find(data, "style.css") then
page = readPage("style.css")
end
if string.find(data, "script.js") then
page = readPage("script.js")
end
if page ~= nil then
sendResponse(socket, page)
end
collectgarbage("collect")
end
----------------------
----------------------
-- DHT cyclic timer
tmr.alarm(1, 20000, tmr.ALARM_AUTO, readDataFromDHT)
-- read DHT at startup
readDataFromDHT()
----------------------
-- Setting up a server
if sv then
print("Server is already running")
else
print("Setting up the server")
sv = net.createServer(net.TCP, 30)
end
-- listening on port 80
if sv then
sv:listen(80, function(connection)
connection:on("receive", receiver)
end)
end
print("Listening on port 80")
print("End of code")
----------------------