Skip to content

Commit

Permalink
Update Luftdaten examples to Sensor.Community
Browse files Browse the repository at this point in the history
Co-authored-by: Phil Howard <[email protected]>
  • Loading branch information
michaeldufault and Gadgetoid committed Nov 15, 2023
1 parent 2cfed37 commit f6ee18b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 34 deletions.
40 changes: 20 additions & 20 deletions examples/luftdaten.py → examples/sensorcommunity.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
level=logging.INFO,
datefmt="%Y-%m-%d %H:%M:%S")

logging.info("""luftdaten.py - Reads temperature, pressure, humidity,
PM2.5, and PM10 from Enviro plus and sends data to Luftdaten,
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 Luftdaten at:
https://meine.luftdaten.info/ and enter your Raspberry Pi
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
Luftdaten map.
Sensor.Community map.
Press Ctrl+C to exit!
Expand Down Expand Up @@ -118,7 +118,7 @@ def display_status():
disp.display(img)


def send_to_luftdaten(values, id):
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"))

Expand All @@ -132,7 +132,7 @@ def send_to_luftdaten(values, id):
resp_pm = requests.post(
"https://api.sensor.community/v1/push-sensor-data/",
json={
"software_version": "enviro-plus 0.0.1",
"software_version": "enviro-plus 1.0.0",
"sensordatavalues": pm_values_json
},
headers={
Expand All @@ -144,17 +144,17 @@ def send_to_luftdaten(values, id):
timeout=5
)
except requests.exceptions.ConnectionError as e:
logging.warning(f"Sensor.Community (Luftdaten) PM Connection Error: {e}")
logging.warning(f"Sensor.Community PM Connection Error: {e}")
except requests.exceptions.Timeout as e:
logging.warning(f"Sensor.Community (Luftdaten) PM Timeout Error: {e}")
logging.warning(f"Sensor.Community PM Timeout Error: {e}")
except requests.exceptions.RequestException as e:
logging.warning(f"Sensor.Community (Luftdaten) PM Request Error: {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 0.0.1",
"software_version": "enviro-plus 1.0.0",
"sensordatavalues": temp_values_json
},
headers={
Expand All @@ -166,17 +166,17 @@ def send_to_luftdaten(values, id):
timeout=5
)
except requests.exceptions.ConnectionError as e:
logging.warning(f"Sensor.Community (Luftdaten) Climate Connection Error: {e}")
logging.warning(f"Sensor.Community Climate Connection Error: {e}")
except requests.exceptions.Timeout as e:
logging.warning(f"Sensor.Community (Luftdaten) Climate Timeout Error: {e}")
logging.warning(f"Sensor.Community Climate Timeout Error: {e}")
except requests.exceptions.RequestException as e:
logging.warning(f"Sensor.Community (Luftdaten) Climate Request Error: {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"Luftdaten Error. PM: {resp_pm.reason}, Climate: {resp_bmp.reason}")
logging.warning(f"Sensor.Community Error. PM: {resp_pm.reason}, Climate: {resp_bmp.reason}")
return False
else:
return False
Expand All @@ -185,7 +185,7 @@ def send_to_luftdaten(values, id):
# Compensation factor for temperature
comp_factor = 2.25

# Raspberry Pi ID to send to Luftdaten
# Raspberry Pi ID to send to Sensor.Community
id = "raspi-" + get_serial_number()

# Width and height to calculate text position
Expand All @@ -204,18 +204,18 @@ def send_to_luftdaten(values, id):
time_since_update = 0
update_time = time.time()

# Main loop to read data, display, and send to Luftdaten
# 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_luftdaten(values, id):
logging.info("Luftdaten Response: OK")
if send_to_sensorcommunity(values, id):
logging.info("Sensor.Community Response: OK")
else:
logging.warning("Luftdaten Response: Failed")
logging.warning("Sensor.Community Response: Failed")
display_status()
except Exception as e:
logging.warning(f"Main Loop Exception: {e}")
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@
except ImportError:
import ltr559

print("""luftdaten_combined.py - This combines the functionality of luftdaten.py and combined.py
print("""sensorcommunity_combined.py - This combines the functionality of sensorcommunity.py and combined.py
================================================================================================
Luftdaten INFO
Sensor.Community INFO
Reads temperature, pressure, humidity,
PM2.5, and PM10 from Enviro plus and sends data to Luftdaten,
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 Luftdaten at:
https://meine.luftdaten.info/ and enter your Raspberry Pi
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
Luftdaten map.
Sensor.Community map.
Press Ctrl+C to exit!
Expand Down Expand Up @@ -242,7 +242,7 @@ def display_everything():
st7735.display(img)


def send_to_luftdaten(values, id):
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"))

Expand All @@ -252,9 +252,9 @@ def send_to_luftdaten(values, id):
for key, val in temp_values.items()]

resp_1 = requests.post(
"https://api.luftdaten.info/v1/push-sensor-data/",
"https://api.sensor.community/v1/push-sensor-data/",
json={
"software_version": "enviro-plus 0.0.1",
"software_version": "enviro-plus 1.0.0",
"sensordatavalues": pm_values_json
},
headers={
Expand All @@ -266,9 +266,9 @@ def send_to_luftdaten(values, id):
)

resp_2 = requests.post(
"https://api.luftdaten.info/v1/push-sensor-data/",
"https://api.sensor.community/v1/push-sensor-data/",
json={
"software_version": "enviro-plus 0.0.1",
"software_version": "enviro-plus 1.0.0",
"sensordatavalues": temp_values_json
},
headers={
Expand All @@ -288,7 +288,7 @@ def send_to_luftdaten(values, id):
# Compensation factor for temperature
comp_factor = 1

# Raspberry Pi ID to send to Luftdaten
# Raspberry Pi ID to send to Sensor.Community
id = "raspi-" + get_serial_number()


Expand Down Expand Up @@ -317,7 +317,7 @@ def send_to_luftdaten(values, id):
update_time = time.time()
cpu_temps_len = float(len(cpu_temps))

# Main loop to read data, display, and send to Luftdaten
# Main loop to read data, display, and send to Sensor.Community
while True:
try:
curtime = time.time()
Expand Down Expand Up @@ -346,7 +346,7 @@ def send_to_luftdaten(values, id):
if time_since_update > 145:
values = read_values(comp_temp, raw_press*100,
raw_humid, raw_pm25, raw_pm10)
resp = send_to_luftdaten(values, id)
resp = send_to_sensorcommunity(values, id)
update_time = curtime
status = "ok" if resp else "failed"
print(f"Response: {status}\n")
Expand Down

0 comments on commit f6ee18b

Please sign in to comment.