-
Notifications
You must be signed in to change notification settings - Fork 0
/
Weather.lua
58 lines (43 loc) · 1.77 KB
/
Weather.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
-- Get The Weather from Wunderground
-- tested on NodeMCU 0.9.5 build 20150108
conn=net.createConnection(net.TCP, 0)
-- Once connected to wensite send request
conn:on("connection",function(conn, payload)
conn:send("GET /api/yourAPIKeyHere/astronomy/q/UK/Gravesend.json"
.." HTTP/1.1\r\n"
.."Host: api.wunderground.com\r\n"
.."Connection: close\r\n"
.."Accept: */*\r\n"
.."User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; )\r\n"
.."\r\n"
)
end)
-- What to do once data is received back from the website
conn:on("receive", function(conn, payload)
success = true
process(payload)
conn:send("ok\r\n")
end)
--t = tmr.now()
conn:connect(80,'api.wunderground.com')
function process(payload)
s = payload
s=(string.gsub(s, "%c+", "")) -- strip out the newline and carriage return characters
print()
-- TODO check valid APi return here
-- sunrise
sunriseHour, sunriseMinute = string.match(s, [["sunrise": {"hour":"(%d+)","minute":"(%d+)"]])
print("Sunrise:\t"..sunriseHour..":"..sunriseMinute)
-- sunset
sunsetHour, sunsetMinute = string.match(s, [["sunset": {"hour":"(%d+)","minute":"(%d+)"]])
print("Sunset:\t"..sunsetHour..":"..sunsetMinute)
-- percentage of moon illuminated
percentIlluminated = string.match(s, [["percentIlluminated":"(%d+)"]])
print("Percent:\t"..percentIlluminated.."%")
-- age of moon since last new moon
age = string.match(s, [["ageOfMoon":"(%d+)"]])
print("Age:\t\t"..age)
-- Phase of the moon
moonPhase = string.match(s, [["phaseofMoon":"(.-)"]])
print("Phase:\t"..moonPhase)
end