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
174
175
|
#!/usr/bin/lua
--
-- weatherstation.lua - a simple web server intended to be run from inetd to
-- expose sensors from the BeagleBone Weather cape and serve the web app.
--
-- List of files shared by this service.
distfiles={}
distfiles["/index.html"] = true
distfiles["/style.css"] = true
distfiles["/jquery.js"] = true
distfiles["/processing.js"] = true
distfiles["/spin.js"] = true
distfiles["/weatherstation.js"] = true
-- Base path for distfiles
prefix="/usr/share/beaglebone/weather"
-- Check that the filename is part of this demo.
function filename_is_valid(filename)
return distfiles[filename] ~= nil
end
-- Check if the string 's' starts with 'starting'
function starts_with(s, starting)
return string.sub(s, 1, string.len(starting)) == starting
end
-- Check if the string 's' ends with 'ending'
function ends_with(s, ending)
return string.sub(s, -string.len(ending)) == ending
end
-- Return the content type of the file based only on the file extension.
function get_content_type(filename)
if ends_with(filename, ".js") then
return "application/javascript"
elseif ends_with(filename, ".css") then
return "text/css"
elseif ends_with(filename, ".html") then
return "text/html"
elseif ends_with(filename, ".json") then
return "application/json"
else
return "text/plain"
end
end
-- Reads from STDIN until an empty or nil line is received.
-- Returns the first line of the request.
function read_request()
local i = 0
local lines = {}
repeat
local line = io.read("*line")
if line == nil or line == "\r" then
break
end
lines[i] = line
i = i + 1
until false
return lines[0]
end
-- Return the entire contents of a file
function read_file(filename)
local f = io.open(filename, "rb")
if f == nil then
return nil
end
local content = f:read("*all")
f:close()
return content
end
-- Extract the value of the LABEL:VALUE pairs read from the device files.
function extract_value(data, pat)
local x = string.match(data, pat)
if x == nil then
return "0"
end
return x
end
-- Read the sensor values and generate json output
function generate_json()
local json = ""
local tsl2550 = read_file("/dev/tsl2550b3s39")
if tsl2550 == nil then
return nil
end
local illuminance = extract_value(tsl2550, "ILLUMINANCE: (%d+)")
local sht21 = read_file("/dev/sht21b3s40")
if sht21 == nil then
return nil
end
local temperature = extract_value(sht21, "TEMPERATURE: (%d+.%d+)")
local humidity = extract_value(sht21, "HUMIDITY: (%d+.%d+)")
local bmp085 = read_file("/dev/bmp085b3s77")
if bmp085 == nil then
return nil
end
local pressure = extract_value(bmp085, "PRESSURE: (%d+)")
json = json .. "{\n"
json = json .. "\"temperature\": " .. temperature .. ",\n"
json = json .. "\"humidity\": " .. humidity .. ",\n"
json = json .. "\"illuminance\": " .. illuminance .. ",\n"
json = json .. "\"pressure\": " .. pressure .. "\n"
json = json .. "}\n"
return json
end
function handle_request(req)
if req == nil then
response("404 Not Found", "text/plain", "Unknown Request")
return
end
-- Parse filename out of HTTP request
local filename = (string.match(req, " %S+ "):gsub("^%s+", ""):gsub("%s+$", ""))
if filename == "" or filename == "/" then
filename = "/index.html"
end
-- Check if the filename is known (i.e. it's a file in the web app)
if filename_is_valid(filename) then
local contents = read_file(prefix .. filename)
if contents ~= nil then
response("200 OK", get_content_type(filename), contents)
else
response("404 Not Found", "text/plain", "Failed to load known file")
end
-- Else maybe the user is requesting the dynamic json data
elseif starts_with(filename, "/weather.json") then
local json = generate_json()
if json == nil then
response("500 Internal Server Error", "text/plain", "Could not get sensor values")
else
response("200 OK", get_content_type("/weather.json"), json)
end
-- Else, the user is requesting something not part of the app
else
response("403 Forbidden", "text/plain", "File not allowed.")
end
return
end
-- Send the response to the HTTP request.
function response(err_code, content_type, content)
io.write("HTTP/1.1 " .. err_code .. "\r\n")
io.write("Content-Type: " .. content_type .. "\r\n")
io.write("\r\n")
io.write(content)
io.write("\r\n")
end
-- Read the request and then handle it.
handle_request(read_request())
|