-
Notifications
You must be signed in to change notification settings - Fork 181
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Address Issue #81 #82
Conversation
Merge from pimoroni/master
Add Logging, PMS5003 Checksum exception and Luftdaten exception handling as per Issue pimoroni#81
Oh wow! This is brilliant, thank you. I'd raised this to pick up... eventually... but I'm stretched thin and really appreciate you giving it a shot. I'll try and test this today! |
Seems to work okay. (although the screen isn't working for me, I think that's a new Pi 4 kernel problem or a hardware problem) But I'd recommend a few changes:
Here's an attempt to put those changes into a patch: diff --git a/examples/luftdaten.py b/examples/luftdaten.py
index 7701a4a..38c47aa 100755
--- a/examples/luftdaten.py
+++ b/examples/luftdaten.py
@@ -124,10 +124,12 @@ def send_to_luftdaten(values, id):
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()]
- resp1_exception = False
- resp2_exception = False
+
+ resp_pm = None
+ resp_bmp = None
+
try:
- resp_1 = requests.post(
+ resp_pm = requests.post(
"https://api.luftdaten.info/v1/push-sensor-data/",
json={
"software_version": "enviro-plus 0.0.1",
@@ -142,17 +144,14 @@ def send_to_luftdaten(values, id):
timeout=5
)
except requests.exceptions.ConnectionError as e:
- resp1_exception = True
- logging.info('Luftdaten PM Connection Error: ' + str(e))
+ logging.warning('Luftdaten PM Connection Error: ' + str(e))
except requests.exceptions.Timeout as e:
- resp1_exception = True
- logging.info('Luftdaten PM Timeout Error: ' + str(e))
+ logging.warning('Luftdaten PM Timeout Error: ' + str(e))
except requests.exceptions.RequestException as e:
- resp1_exception = True
- logging.info('Luftdaten PM Request Error: ' + str(e))
+ logging.warning('Luftdaten PM Request Error: ' + str(e))
try:
- resp_2 = requests.post(
+ resp_bmp = requests.post(
"https://api.luftdaten.info/v1/push-sensor-data/",
json={
"software_version": "enviro-plus 0.0.1",
@@ -167,19 +166,18 @@ def send_to_luftdaten(values, id):
timeout=5
)
except requests.exceptions.ConnectionError as e:
- resp2_exception = True
- logging.info('Luftdaten Climate Connection Error: ' + str(e))
+ logging.warning('Luftdaten Climate Connection Error: {}'.format(e))
except requests.exceptions.Timeout as e:
- resp2_exception = True
- logging.info('Luftdaten Climate Timeout Error: ' + str(e))
+ logging.warning('Luftdaten Climate Timeout Error: {}'.format(e))
except requests.exceptions.RequestException as e:
- resp2_exception = True
- logging.info('Luftdaten Climate Request Error: ' + str(e))
+ logging.warning('Luftdaten Climate Request Error: {}'.format(e))
- if not resp1_exception and not resp2_exception:
- if resp_1.ok and resp_2.ok:
+ if resp_pm is not None and resp_bmp is not None:
+ if resp_pm.ok and resp_bmp.ok:
+ logging.info('Luftdaten Climate Success', values)
return True
else:
+ logging.warning('Luftdaten Error PM: {}, Climate: {}'.format(resp_pm.reason, resp_bmp.reason))
return False
else:
return False
@@ -210,12 +208,14 @@ update_time = time.time()
while True:
try:
values = read_values()
- logging.info(values)
time_since_update = time.time() - update_time
- if time_since_update > 145:
- resp = send_to_luftdaten(values, id)
+ if time_since_update > 145:
+ logging.info(values)
update_time = time.time()
- logging.info("Luftdaten Response: {}\n".format("ok" if resp else "failed"))
+ if send_to_luftdaten(values, id):
+ logging.info("Luftdaten Response: OK")
+ else:
+ logging.warning("Luftdaten Response: Failed")
display_status()
except Exception as e:
logging.info("Main Loop Exception: " + str(e)) |
Your changes make it much better. I've had problems applying the patch to my PR and it doesn't seem to apply the @@ -124,10 +124,12 @@ changes. I suspect that it's a line numbering problem and I'll try again tomorrow when I have some more time. I also think that it might be better to use your "{}'.format(e)" Climate Error logging changes to the PM Errors and Main Loop Exception. They still have "+ str(e)". To be consistent, the Main Loop Exception should probably also be a warning, rather that an info. |
Added suggested changes but removed "logging.info('Luftdaten Climate Success', values)", since I found it redundant, given the use of "logging.info("Luftdaten Response: OK")" in Line 215.
Still couldn't apply the patch but I've now updated the code with your suggestions and my previous comment. Testing has shown that it's working OK but that "logging.info('Luftdaten Climate Success', values)" was redundant. Happy to put that back in if you think it's essential. |
Sorry this has flown somewhat under my radar due to juggling so many projects! It has not been forgotten though. |
@sandyjmacdonald is merging this going to cause a major catastrophe with any Luftdaten setup guides or anthing? I can't see any reason why it might. |
Thanks @helgibbons for taking this for a spin so I could merge. Thank you @roscoe81 for all your effort here, much appreciated! |
I've raised this PR to partially address Issue #81 with the following comments:
Hope that helps