diff --git a/examples/luftdaten.py b/examples/luftdaten.py index 84f1117..d72e718 100755 --- a/examples/luftdaten.py +++ b/examples/luftdaten.py @@ -8,12 +8,31 @@ from subprocess import PIPE, Popen, check_output from PIL import Image, ImageDraw, ImageFont from fonts.ttf import RobotoMedium as UserFont +from datetime import date + +def get_filename_datetime(): + # Use current date to get a text file name. + return "file-" + str(date.today()) + ".txt" + +try: + # Transitional fix for breaking change in LTR559 + from ltr559 import LTR559 + ltr559 = LTR559() +except ImportError: + import ltr559 +from enviroplus import gas +from enviroplus.noise import Noise try: from smbus2 import SMBus except ImportError: from smbus import SMBus +import json + +gas.enable_adc() +gas.set_adc_gain(4.096) + print("""luftdaten.py - Reads temperature, pressure, humidity, PM2.5, and PM10 from Enviro plus and sends data to Luftdaten, the citizen science air quality project. @@ -56,9 +75,34 @@ def read_values(): cpu_temp = get_cpu_temperature() raw_temp = bme280.get_temperature() comp_temp = raw_temp - ((cpu_temp - raw_temp) / comp_factor) + lux = ltr559.get_lux() + proximity = ltr559.get_proximity() + readings = gas.read_all() + oxidising = readings.oxidising + nh3 = readings.nh3 + reducing = readings.reducing + adc = readings.adc + noise = Noise() + amps = noise.get_amplitudes_at_frequency_ranges([ + (100, 200), + (500, 600), + (1000, 1200) + ]) + amps = [n * 32 for n in amps] + values["temperature"] = "{:.2f}".format(comp_temp) + values["cpu_temp"] = "{:.2f}".format(cpu_temp) values["pressure"] = "{:.2f}".format(bme280.get_pressure() * 100) values["humidity"] = "{:.2f}".format(bme280.get_humidity()) + values["lux"] = "{:05.02f}".format(lux) + values["proximity"] = "{:05.02f}".format(proximity) + values["nh3"] = "{:05.02f}".format(nh3) + values["oxidising"] = "{:05.02f}".format(oxidising) + values["reducing"] = "{:05.02f}".format(reducing) + values["adc"] = "{:05.02f}".format(adc) + values["amp_100_200"] = "{:05.02f}".format(amps[0]) + values["amp_500_600"] = "{:05.02f}".format(amps[1]) + values["amp_1000_1200"] = "{:05.02f}".format(amps[2]) try: pm_values = pms5003.read() values["P2"] = str(pm_values.pm_ug_per_m3(2.5)) @@ -110,45 +154,56 @@ def display_status(): draw.text((x, y), message, font=font, fill=text_colour) disp.display(img) +def log_values(values): + json_data = json.dumps(values) + fname = get_filename_datetime() + path = "/home/pi/enviroplus-python/logs/" + fname + with open(path, "a+") as f: + # Write data to file. + f.write(json_data) + f.write("\n") def send_to_luftdaten(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")) - + temp_values = {'pressure': values["pressure"], 'temperature' : values['temperature'], 'humidity' : values['humidity']} 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()] - - resp_1 = requests.post( - "https://api.luftdaten.info/v1/push-sensor-data/", - json={ - "software_version": "enviro-plus 0.0.1", - "sensordatavalues": pm_values_json - }, - headers={ - "X-PIN": "1", - "X-Sensor": id, - "Content-Type": "application/json", - "cache-control": "no-cache" - } - ) - - resp_2 = requests.post( - "https://api.luftdaten.info/v1/push-sensor-data/", - json={ - "software_version": "enviro-plus 0.0.1", - "sensordatavalues": temp_values_json - }, - headers={ - "X-PIN": "11", - "X-Sensor": id, - "Content-Type": "application/json", - "cache-control": "no-cache" - } - ) - - if resp_1.ok and resp_2.ok: - return True - else: + try: + resp_1 = requests.post( + "https://api.luftdaten.info/v1/push-sensor-data/", + json={ + "software_version": "enviro-plus 0.0.1", + "sensordatavalues": pm_values_json + }, + headers={ + "X-PIN": "1", + "X-Sensor": id, + "Content-Type": "application/json", + "cache-control": "no-cache" + }, + timeout = 30 + ) + + resp_2 = requests.post( + "https://api.luftdaten.info/v1/push-sensor-data/", + json={ + "software_version": "enviro-plus 0.0.1", + "sensordatavalues": temp_values_json + }, + headers={ + "X-PIN": "11", + "X-Sensor": id, + "Content-Type": "application/json", + "cache-control": "no-cache" + }, + timeout = 30 + ) + + if resp_1.ok and resp_2.ok: + return True + else: + return False + except: return False @@ -178,11 +233,12 @@ def send_to_luftdaten(values, id): try: time_since_update = time.time() - update_time values = read_values() - print(values) - if time_since_update > 145: + # print(values) + if time_since_update > 120 and check_wifi() : + log_values(values) resp = send_to_luftdaten(values, id) update_time = time.time() - print("Response: {}\n".format("ok" if resp else "failed")) - display_status() + print("Response: {}\n".format("ok luftdaten" if resp else "failed luftdaten")) + # display_status() except Exception as e: print(e)