-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalendar.py
165 lines (149 loc) · 4.83 KB
/
calendar.py
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
#!/usr/bin/env python
#
# Minimal weather calendar for the Waveshare 4.3 inch e-ink display
# This runs on the WiPy 2.0; floats will not work on the WiPy 1.0.
# May work on other mPython boards.
#
# Gets the weather data from various weather services.
# TBD: get calendar data from Google.
#
from eInk import *
from time import sleep
from weathericons import interpret_icons
import machine
import ujson
import urequests as requests
CONFIG = 'config.json'
service = 'weatherunderground'
Tx='G12'
Rx='G13'
uartnum=1
def parseFloat(f):
return(int(f.split('.')[0]))
#def urlopen(url, data=None, method="GET"):
if data is not None and method == "GET":
method = "POST"
try:
proto, dummy, host, path = url.split("/", 3)
except ValueError:
proto, dummy, host = url.split("/", 2)
path = ""
if proto == "http:":
port = 80
elif proto == "https:":
import ussl
port = 443
else:
raise ValueError("Unsupported protocol: " + proto)
if ":" in host:
host, port = host.split(":", 1)
port = int(port)
ai = usocket.getaddrinfo(host, port)
addr = ai[0][4]
s = usocket.socket()
s.connect(addr)
if proto == "https:":
s = ussl.wrap_socket(s)
s.write(method)
s.write(b" /")
s.write(path)
s.write(b" HTTP/1.0\r\nHost: ")
s.write(host)
s.write(b"\r\n")
if data:
s.write(b"Content-Length: ")
s.write(str(len(data)))
s.write(b"\r\n")
s.write(b"\r\n")
if data:
s.write(data)
l = s.readline()
protover, status, msg = l.split(None, 2)
status = int(status)
#print(protover, status, msg)
while True:
l = s.readline()
if not l or l == b"\r\n":
break
#print(line)
if l.startswith(b"Transfer-Encoding:"):
if b"chunked" in line:
raise ValueError("Unsupported " + l)
elif l.startswith(b"Location:"):
raise NotImplementedError("Redirects not yet supported")
return s
def mungWeather(url,text,stop="."):
index = url.find(text)
index += len(text)
index2 = url.find(stop, index)
return(url[index:index2])
def setup():
global cfg
f = open(CONFIG, 'r')
x = f.readall()
cfg = ujson.loads(x)
eink_init()
eink_clear()
def draw_structure():
eink_draw_line(300,0,300,599)
eink_draw_line(300,200,799,200)
eink_draw_line(300,400,799,400)
eink_update()
def get_weather(service):
#
# Get weather conditions and forecast using API call and REST call.
# Returned formats are different between services, hence this routine.
#
if service == "openweathermap":
pt1 = "http://api.openweathermap.org/data/2.5/weather?id="
pt2 = "&units=metric&cnt=3&appid="
url = pt1+cfg["Openweathercity"]+pt2+cfg["OpenWeatherAPI"]
r = requests.get(url)
weather_json = r.json()
r.close()
temp = round(weather_json["main"]["temp"])
wind = round(weather_json["wind"]["speed"])
humidity = weather_json["main"]["humidity"]
description = weather_json["weather"][0]["description"]
pressure = weather_json["main"]["pressure"]
iconid = weather_json["weather"][0]["id"]
elif service == "weatherunderground":
pt1 = "http://api.wunderground.com/api/"
pt2 = "/forecast/conditions/q/"
url = pt1+cfg["WundergroundAPI"]+pt2+cfg["WundergroundCity"]+".json"
r = requests.get(url)
weather_json = r.json()
r.close()
temperature = weather_json["current_observation"]["temp_c"]
wind = weather_json["current_observation"]["wind_kph"]
humidity = weather_json["current_observation"]["relative_humidity"]
description = weather_json["current_observation"]["weather"]
pressure = weather_json["current_observation"]["pressure_mb"]
iconid = weather_json["current_observation"]["icon"]
forecast = {}
for i in range(1,4):
forecast[i] = {}
forecast[i]["high"] = weather_json["forecast"]["simpleforecast"]["forecastday"][i]["high"]["celsius"]
forecast[i]["low"] = weather_json["forecast"]["simpleforecast"]["forecastday"][i]["low"]["celsius"]
else:
print("Error: invalid service selected")
return(temperature, wind, humidity, description, pressure, iconid, forecast)
def get_calendar():
pass
def main():
setup()
draw_structure()
w=get_weather(service)
v=interpret_icons(service,str(w[5]))
print(w)
eink_set_en_font(ASCII32)
eink_disp_string(v["label"], 50, 250)
eink_disp_bitmap(v["icon"]+'.BMP', 100, 100)
eink_set_en_font(ASCII64)
eink_disp_string(str(w[0]), 100, 350)
for i in range(1,4):
y = ((i*2)-1)*100
eink_disp_string(w[6][i]["low"],400,y)
eink_disp_string(w[6][i]["high"],600,y)
eink_update()
main()