forked from pimoroni/enviroplus-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsensorcommunity.py
More file actions
executable file
·221 lines (184 loc) · 6.78 KB
/
sensorcommunity.py
File metadata and controls
executable file
·221 lines (184 loc) · 6.78 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/usr/bin/env python3
import logging
import time
from subprocess import check_output
import requests
import st7735
from bme280 import BME280
from fonts.ttf import RobotoMedium as UserFont
from PIL import Image, ImageDraw, ImageFont
from pms5003 import PMS5003, ChecksumMismatchError, ReadTimeoutError
from smbus2 import SMBus
logging.basicConfig(
format="%(asctime)s.%(msecs)03d %(levelname)-8s %(message)s",
level=logging.INFO,
datefmt="%Y-%m-%d %H:%M:%S")
logging.info("""sensorcommunity.py - Reads temperature, pressure, humidity,
PM2.5, and PM10 from Enviro plus and sends data to Sensor.Community,
the citizen science air quality project.
Note: you'll need to register with Sensor.Community at:
https://devices.sensor.community/ and enter your Raspberry Pi
serial number that's displayed on the Enviro plus LCD along
with the other details before the data appears on the
Sensor.Community map.
Press Ctrl+C to exit!
""")
bus = SMBus(1)
# Create BME280 instance
bme280 = BME280(i2c_dev=bus)
# Create LCD instance
disp = st7735.ST7735(
port=0,
cs=1,
dc="GPIO9",
backlight="GPIO12",
rotation=270,
spi_speed_hz=10000000
)
# Initialize display
disp.begin()
# Create PMS5003 instance
pms5003 = PMS5003()
# Read values from BME280 and PMS5003 and return as dict
def read_values():
values = {}
cpu_temp = get_cpu_temperature()
raw_temp = bme280.get_temperature()
comp_temp = raw_temp - ((cpu_temp - raw_temp) / comp_factor)
values["temperature"] = f"{comp_temp:.2f}"
values["pressure"] = f"{bme280.get_pressure() * 100:.2f}"
values["humidity"] = f"{bme280.get_humidity():.2f}"
try:
pm_values = pms5003.read()
values["P2"] = str(pm_values.pm_ug_per_m3(2.5))
values["P1"] = str(pm_values.pm_ug_per_m3(10))
except(ReadTimeoutError, ChecksumMismatchError):
logging.info("Failed to read PMS5003. Resetting and retrying.")
pms5003.reset()
pm_values = pms5003.read()
values["P2"] = str(pm_values.pm_ug_per_m3(2.5))
values["P1"] = str(pm_values.pm_ug_per_m3(10))
return values
# Get the temperature of the CPU for compensation
def get_cpu_temperature():
with open("/sys/class/thermal/thermal_zone0/temp", "r") as f:
temp = f.read()
temp = int(temp) / 1000.0
return temp
# Get Raspberry Pi serial number to use as ID
def get_serial_number():
with open("/proc/cpuinfo", "r") as f:
for line in f:
if line.startswith("Serial"):
return line.split(":")[1].strip()
# Check for Wi-Fi connection
def check_wifi():
if check_output(["hostname", "-I"]):
return True
else:
return False
# Display Raspberry Pi serial and Wi-Fi status on LCD
def display_status():
wifi_status = "connected" if check_wifi() else "disconnected"
text_colour = (255, 255, 255)
back_colour = (0, 170, 170) if check_wifi() else (85, 15, 15)
id = get_serial_number()
message = f"{id}\nWi-Fi: {wifi_status}"
img = Image.new("RGB", (WIDTH, HEIGHT), color=(0, 0, 0))
draw = ImageDraw.Draw(img)
x1, y1, x2, y2 = font.getbbox(message)
size_x = x2 - x1
size_y = y2 - y1
x = (WIDTH - size_x) / 2
y = (HEIGHT / 2) - (size_y / 2)
draw.rectangle((0, 0, 160, 80), back_colour)
draw.text((x, y), message, font=font, fill=text_colour)
disp.display(img)
def send_to_sensorcommunity(values, id):
pm_values = dict(i for i in values.items() if i[0].startswith("P"))
temp_values = dict(i for i in values.items() if not i[0].startswith("P"))
pm_values_json = [{"value_type": key, "value": val} for key, val in pm_values.items()]
temp_values_json = [{"value_type": key, "value": val} for key, val in temp_values.items()]
resp_pm = None
resp_bmp = None
try:
resp_pm = requests.post(
"https://api.sensor.community/v1/push-sensor-data/",
json={
"software_version": "enviro-plus 1.0.0",
"sensordatavalues": pm_values_json
},
headers={
"X-PIN": "1",
"X-Sensor": id,
"Content-Type": "application/json",
"cache-control": "no-cache"
},
timeout=5
)
except requests.exceptions.ConnectionError as e:
logging.warning(f"Sensor.Community PM Connection Error: {e}")
except requests.exceptions.Timeout as e:
logging.warning(f"Sensor.Community PM Timeout Error: {e}")
except requests.exceptions.RequestException as e:
logging.warning(f"Sensor.Community PM Request Error: {e}")
try:
resp_bmp = requests.post(
"https://api.sensor.community/v1/push-sensor-data/",
json={
"software_version": "enviro-plus 1.0.0",
"sensordatavalues": temp_values_json
},
headers={
"X-PIN": "11",
"X-Sensor": id,
"Content-Type": "application/json",
"cache-control": "no-cache"
},
timeout=5
)
except requests.exceptions.ConnectionError as e:
logging.warning(f"Sensor.Community Climate Connection Error: {e}")
except requests.exceptions.Timeout as e:
logging.warning(f"Sensor.Community Climate Timeout Error: {e}")
except requests.exceptions.RequestException as e:
logging.warning(f"Sensor.Community Climate Request Error: {e}")
if resp_pm is not None and resp_bmp is not None:
if resp_pm.ok and resp_bmp.ok:
return True
else:
logging.warning(f"Sensor.Community Error. PM: {resp_pm.reason}, Climate: {resp_bmp.reason}")
return False
else:
return False
# Compensation factor for temperature
comp_factor = 2.25
# Raspberry Pi ID to send to Sensor.Community
id = "raspi-" + get_serial_number()
# Width and height to calculate text position
WIDTH = disp.width
HEIGHT = disp.height
# Text settings
font_size = 16
font = ImageFont.truetype(UserFont, font_size)
# Log Raspberry Pi serial and Wi-Fi status
logging.info(f"Raspberry Pi serial: {get_serial_number()}")
wifi_status = "connected" if check_wifi() else "disconnected"
logging.info(f"Wi-Fi: {wifi_status}\n")
time_since_update = 0
update_time = time.time()
# Main loop to read data, display, and send to Sensor.Community
while True:
try:
values = read_values()
time_since_update = time.time() - update_time
if time_since_update > 145:
logging.info(values)
update_time = time.time()
if send_to_sensorcommunity(values, id):
logging.info("Sensor.Community Response: OK")
else:
logging.warning("Sensor.Community Response: Failed")
display_status()
except Exception as e:
logging.warning(f"Main Loop Exception: {e}")