Skip to content

Instantly share code, notes, and snippets.

@helgibbons
Last active November 4, 2022 15:16
Show Gist options
  • Save helgibbons/725710e28ab6523379092303f1834a70 to your computer and use it in GitHub Desktop.
Save helgibbons/725710e28ab6523379092303f1834a70 to your computer and use it in GitHub Desktop.
Enviro Luftdaten/Sensor Community example
from enviro.helpers import get_config, connect_to_wifi
from enviro import logging
import urequests, ujson, os
import machine
import ubinascii
def upload_readings():
if not connect_to_wifi():
logging.error(f" - cannot upload readings, wifi connection failed")
return False
url = get_config("custom_http_url")
logging.info(f"> uploading cached readings to {url}")
auth = None
if get_config("custom_http_username"):
auth = (get_config("custom_http_username"), get_config("custom_http_password"))
id = "raspi-" + ubinascii.hexlify(machine.unique_id()).decode()
for cache_file in os.ilistdir("uploads"):
cache_file = cache_file[0]
try:
with open(f"uploads/{cache_file}", "r") as f:
loaded_json = ujson.load(f)
headers={
"X-PIN": "1",
"X-Sensor": id,
"contentType": "application/json",
}
payload = {
"software_version": "Enviro Urban",
"sensordatavalues":[
{"value_type":"P1","value":loaded_json["pm10"]},
{"value_type":"P2","value":loaded_json["pm2_5"]}
]
}
result = urequests.post(url, headers=headers, json=payload)
headers={
"X-PIN": "11",
"X-Sensor": id,
"contentType": "application/json",
}
payload = {
"software_version": "Enviro Urban",
"sensordatavalues":[
{"value_type":"temperature","value":loaded_json["temperature"]},
{"value_type":"humidity","value":loaded_json["humidity"]},
{"value_type":"pressure","value":loaded_json["pressure"]}
]
}
result = urequests.post(url, headers=headers, json=payload)
if result.status_code not in [200, 201, 202]:
logging.error(f" - failed to upload '{cache_file}' ({result.status_code} {result.reason})", cache_file)
else:
logging.info(f" - uploaded {cache_file}")
os.remove(f"uploads/{cache_file}")
result.close()
except OSError as e:
logging.error(f" - failed to upload '{cache_file}'")
@helgibbons
Copy link
Author

helgibbons commented Aug 21, 2022

I think pressure needs to be provided in Pa not hPa - it is currently showing as 10.04 hPa on the sensor.community site.

@helgibbons
Copy link
Author

image

Readings from the Enviro Urban's particulate sensor (Garden) seem to be tracking quite a bit higher (and with more variance) than the ones from my Raspberry Pi/Enviro+ combo (Street), and other sensors in the surrounding area.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment