Skip to content

Commit 124f49b

Browse files
committed
Update luftdaten.py
Add Logging, PMS5003 Checksum exception and Luftdaten exception handling as per Issue #81
1 parent 654986f commit 124f49b

File tree

1 file changed

+83
-50
lines changed

1 file changed

+83
-50
lines changed

examples/luftdaten.py

Lines changed: 83 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import ST7735
55
import time
66
from bme280 import BME280
7-
from pms5003 import PMS5003, ReadTimeoutError
7+
from pms5003 import PMS5003, ReadTimeoutError, ChecksumMismatchError
88
from subprocess import PIPE, Popen, check_output
99
from PIL import Image, ImageDraw, ImageFont
1010
from fonts.ttf import RobotoMedium as UserFont
@@ -13,20 +13,26 @@
1313
from smbus2 import SMBus
1414
except ImportError:
1515
from smbus import SMBus
16+
import logging
1617

17-
print("""luftdaten.py - Reads temperature, pressure, humidity,
18-
PM2.5, and PM10 from Enviro plus and sends data to Luftdaten,
19-
the citizen science air quality project.
18+
logging.basicConfig(
19+
format='%(asctime)s.%(msecs)03d %(levelname)-8s %(message)s',
20+
level=logging.INFO,
21+
datefmt='%Y-%m-%d %H:%M:%S')
2022

21-
Note: you'll need to register with Luftdaten at:
22-
https://meine.luftdaten.info/ and enter your Raspberry Pi
23-
serial number that's displayed on the Enviro plus LCD along
24-
with the other details before the data appears on the
25-
Luftdaten map.
23+
logging.info("""luftdaten.py - Reads temperature, pressure, humidity,
24+
#PM2.5, and PM10 from Enviro plus and sends data to Luftdaten,
25+
#the citizen science air quality project.
2626
27-
Press Ctrl+C to exit!
27+
#Note: you'll need to register with Luftdaten at:
28+
#https://meine.luftdaten.info/ and enter your Raspberry Pi
29+
#serial number that's displayed on the Enviro plus LCD along
30+
#with the other details before the data appears on the
31+
#Luftdaten map.
2832
29-
""")
33+
#Press Ctrl+C to exit!
34+
35+
#""")
3036

3137
bus = SMBus(1)
3238

@@ -63,7 +69,8 @@ def read_values():
6369
pm_values = pms5003.read()
6470
values["P2"] = str(pm_values.pm_ug_per_m3(2.5))
6571
values["P1"] = str(pm_values.pm_ug_per_m3(10))
66-
except ReadTimeoutError:
72+
except(ReadTimeoutError, ChecksumMismatchError):
73+
logging.info("Failed to read PMS5003. Reseting and retrying.")
6774
pms5003.reset()
6875
pm_values = pms5003.read()
6976
values["P2"] = str(pm_values.pm_ug_per_m3(2.5))
@@ -117,37 +124,63 @@ def send_to_luftdaten(values, id):
117124

118125
pm_values_json = [{"value_type": key, "value": val} for key, val in pm_values.items()]
119126
temp_values_json = [{"value_type": key, "value": val} for key, val in temp_values.items()]
120-
121-
resp_1 = requests.post(
122-
"https://api.luftdaten.info/v1/push-sensor-data/",
123-
json={
124-
"software_version": "enviro-plus 0.0.1",
125-
"sensordatavalues": pm_values_json
126-
},
127-
headers={
128-
"X-PIN": "1",
129-
"X-Sensor": id,
130-
"Content-Type": "application/json",
131-
"cache-control": "no-cache"
132-
}
133-
)
134-
135-
resp_2 = requests.post(
136-
"https://api.luftdaten.info/v1/push-sensor-data/",
137-
json={
138-
"software_version": "enviro-plus 0.0.1",
139-
"sensordatavalues": temp_values_json
140-
},
141-
headers={
142-
"X-PIN": "11",
143-
"X-Sensor": id,
144-
"Content-Type": "application/json",
145-
"cache-control": "no-cache"
146-
}
147-
)
148-
149-
if resp_1.ok and resp_2.ok:
150-
return True
127+
resp1_exception = False
128+
resp2_exception = False
129+
try:
130+
resp_1 = requests.post(
131+
"https://api.luftdaten.info/v1/push-sensor-data/",
132+
json={
133+
"software_version": "enviro-plus 0.0.1",
134+
"sensordatavalues": pm_values_json
135+
},
136+
headers={
137+
"X-PIN": "1",
138+
"X-Sensor": id,
139+
"Content-Type": "application/json",
140+
"cache-control": "no-cache"
141+
},
142+
timeout=5
143+
)
144+
except requests.exceptions.ConnectionError as e:
145+
resp1_exception = True
146+
logging.info('Luftdaten PM Connection Error: ' + str(e))
147+
except requests.exceptions.Timeout as e:
148+
resp1_exception = True
149+
logging.info('Luftdaten PM Timeout Error: ' + str(e))
150+
except requests.exceptions.RequestException as e:
151+
resp1_exception = True
152+
logging.info('Luftdaten PM Request Error: ' + str(e))
153+
154+
try:
155+
resp_2 = requests.post(
156+
"https://api.luftdaten.info/v1/push-sensor-data/",
157+
json={
158+
"software_version": "enviro-plus 0.0.1",
159+
"sensordatavalues": temp_values_json
160+
},
161+
headers={
162+
"X-PIN": "11",
163+
"X-Sensor": id,
164+
"Content-Type": "application/json",
165+
"cache-control": "no-cache"
166+
},
167+
timeout=5
168+
)
169+
except requests.exceptions.ConnectionError as e:
170+
resp2_exception = True
171+
logging.info('Luftdaten Climate Connection Error: ' + str(e))
172+
except requests.exceptions.Timeout as e:
173+
resp2_exception = True
174+
logging.info('Luftdaten Climate Timeout Error: ' + str(e))
175+
except requests.exceptions.RequestException as e:
176+
resp2_exception = True
177+
logging.info('Luftdaten Climate Request Error: ' + str(e))
178+
179+
if not resp1_exception and not resp2_exception:
180+
if resp_1.ok and resp_2.ok:
181+
return True
182+
else:
183+
return False
151184
else:
152185
return False
153186

@@ -166,23 +199,23 @@ def send_to_luftdaten(values, id):
166199
font_size = 16
167200
font = ImageFont.truetype(UserFont, font_size)
168201

169-
# Display Raspberry Pi serial and Wi-Fi status
170-
print("Raspberry Pi serial: {}".format(get_serial_number()))
171-
print("Wi-Fi: {}\n".format("connected" if check_wifi() else "disconnected"))
202+
# Log Raspberry Pi serial and Wi-Fi status
203+
logging.info("Raspberry Pi serial: {}".format(get_serial_number()))
204+
logging.info("Wi-Fi: {}\n".format("connected" if check_wifi() else "disconnected"))
172205

173206
time_since_update = 0
174207
update_time = time.time()
175208

176209
# Main loop to read data, display, and send to Luftdaten
177210
while True:
178211
try:
179-
time_since_update = time.time() - update_time
180212
values = read_values()
181-
print(values)
213+
logging.info(values)
214+
time_since_update = time.time() - update_time
182215
if time_since_update > 145:
183216
resp = send_to_luftdaten(values, id)
184217
update_time = time.time()
185-
print("Response: {}\n".format("ok" if resp else "failed"))
218+
logging.info("Luftdaten Response: {}\n".format("ok" if resp else "failed"))
186219
display_status()
187220
except Exception as e:
188-
print(e)
221+
logging.info("Main Loop Exception: " + str(e))

0 commit comments

Comments
 (0)