Fix for PMS5003 ReadTimeoutError
authorSandy Macdonald <sandyjmacdonald@gmail.com>
Thu, 13 Jun 2019 11:44:11 +0000 (12:44 +0100)
committerSandy Macdonald <sandyjmacdonald@gmail.com>
Thu, 13 Jun 2019 11:44:11 +0000 (12:44 +0100)
examples/luftdaten.py
examples/particulates.py

index 2177a56162539e755bda192a6b413e60a6949e82..d22be88f822a5db6ffdf0292d37d2b558c39cf77 100755 (executable)
@@ -5,7 +5,7 @@ import json
 import requests
 import ST7735
 from bme280 import BME280
 import requests
 import ST7735
 from bme280 import BME280
-from pms5003 import PMS5003
+from pms5003 import PMS5003, ReadTimeoutError
 from subprocess import PIPE, Popen, check_output
 from PIL import Image, ImageDraw, ImageFont
 
 from subprocess import PIPE, Popen, check_output
 from PIL import Image, ImageDraw, ImageFont
 
@@ -55,9 +55,15 @@ def read_values():
     values["temperature"] = "{:.2f}".format(comp_temp)
     values["pressure"] = "{:.2f}".format(bme280.get_pressure() * 100)
     values["humidity"] = "{:.2f}".format(bme280.get_humidity())
     values["temperature"] = "{:.2f}".format(comp_temp)
     values["pressure"] = "{:.2f}".format(bme280.get_pressure() * 100)
     values["humidity"] = "{:.2f}".format(bme280.get_humidity())
-    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))
+    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:
+        pms5003 = PMS5003()
+        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 CPU temperature to use for compensation
     return values
 
 # Get CPU temperature to use for compensation
index 99803ec05a96ff502fc440e115b5c7aa581888a3..e71ca5d96f78782f5237134e1dffd3df51dfcf7e 100755 (executable)
@@ -1,7 +1,7 @@
 #!/usr/bin/env python
 
 import time
 #!/usr/bin/env python
 
 import time
-from pms5003 import PMS5003
+from pms5003 import PMS5003, ReadTimeoutError
 
 print("""particulates.py - Print readings from the PMS5003 particulate sensor.
 
 
 print("""particulates.py - Print readings from the PMS5003 particulate sensor.
 
@@ -14,8 +14,11 @@ time.sleep(1.0)
 
 try:
     while True:
 
 try:
     while True:
-        readings = pms5003.read()
-        print(readings)
-        time.sleep(1.0)
+        try:
+            readings = pms5003.read()
+            print(readings)
+            time.sleep(1.0)
+        except ReadTimeoutError:
+            pms5003 = PMS5003()
 except KeyboardInterrupt:
     pass
 except KeyboardInterrupt:
     pass