-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcarbon_intensity.py
120 lines (96 loc) · 3.79 KB
/
carbon_intensity.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
"""
This example connects to the Carbon Intensity API to give you a regional
forecast of how your (UK) electricity is being generated and its carbon impact.
Carbon Intensity API only reports data from the UK National Grid.
Find out more about what the numbers mean at:
https://carbonintensity.org.uk/
Make sure to uncomment the correct size for your display!
"""
import urequests
import inky_frame
graphics = None
WIDTH = None
HEIGHT = None
# Length of time between updates in minutes.
# Frequent updates will reduce battery life!
UPDATE_INTERVAL = 240
# Set (the first half) of your UK postcode here
POSTCODE = "S9"
URL = "https://api.carbonintensity.org.uk/regional/postcode/" + str(POSTCODE)
def update():
global region, forecast, index, power_list, datetime_to, datetime_from
print(f"Requesting URL: {URL}")
r = urequests.get(URL)
# open the json data
j = r.json()
print("Data obtained!")
print(j)
# parse the json data
region = j["data"][0]["shortname"]
forecast = j["data"][0]["data"][0]["intensity"]["forecast"]
index = j["data"][0]["data"][0]["intensity"]["index"]
power_list = []
for power in j["data"][0]["data"][0]["generationmix"]:
power_list.append(power['perc'])
datetime_to = j["data"][0]["data"][0]["to"].split("T")
datetime_from = j["data"][0]["data"][0]["from"].split("T")
# close the socket
r.close()
def draw():
# we're setting up our PicoGraphics buffer after we've made our RAM intensive https request
w, h = WIDTH, HEIGHT
graphics.set_pen(inky_frame.WHITE)
graphics.clear()
# draw lines
graphics.set_pen(inky_frame.BLACK)
graphics.line(0, int((h / 100) * 0), w, int((h / 100) * 0))
graphics.line(0, int((h / 100) * 50), w, int((h / 100) * 50))
graphics.set_font("bitmap8")
graphics.text('100%', w - 40, 10, scale=2)
graphics.text('50%', w - 40, int((h / 100) * 50 + 10), scale=2)
# draw bars
bar_colours = [
inky_frame.ORANGE,
inky_frame.RED,
inky_frame.ORANGE,
inky_frame.RED,
inky_frame.BLUE,
inky_frame.ORANGE,
inky_frame.GREEN,
inky_frame.GREEN,
inky_frame.GREEN
]
for p in power_list:
graphics.set_pen(bar_colours[power_list.index(p)])
graphics.rectangle(int(power_list.index(p) * w / 9), int(h - p * (h / 100)),
int(w / 9), int(h / 100 * p))
# draw labels
graphics.set_font('sans')
# once in white for a background
graphics.set_pen(inky_frame.WHITE)
labels = ['biomass', 'coal', 'imports', 'gas', 'nuclear', 'other', 'hydro', 'solar', 'wind']
graphics.set_thickness(4)
for label in labels:
graphics.text(f'{label}', int((labels.index(label) * w / 9) + (w / 9) / 2), h - 10, angle=270, scale=1)
# again in black
graphics.set_pen(inky_frame.BLACK)
labels = ['biomass', 'coal', 'imports', 'gas', 'nuclear', 'other', 'hydro', 'solar', 'wind']
graphics.set_thickness(2)
for label in labels:
graphics.text(f'{label}', int((labels.index(label) * w / 9) + (w / 9) / 2), h - 10, angle=270, scale=1)
# draw header
graphics.set_thickness(3)
graphics.set_pen(inky_frame.GREEN)
if index in ['high', 'very high']:
graphics.set_pen(inky_frame.RED)
if index in ['moderate']:
graphics.set_pen(inky_frame.ORANGE)
graphics.set_font("sans")
graphics.text('Carbon Intensity', 10, 35, scale=1.2, angle=0)
# draw small text
graphics.set_pen(inky_frame.BLACK)
graphics.set_font("bitmap8")
graphics.text(f'Region: {region}', int((w / 2) + 30), 10, scale=2)
graphics.text(f'{forecast} gCO2/kWh ({index})', int((w / 2) + 30), 30, scale=2)
graphics.text(f'{datetime_from[0]} {datetime_from[1]} to {datetime_to[1]}', int((w / 2) + 30), 50, scale=2)
graphics.update()