7 from bme280
import BME280
8 from pms5003
import PMS5003
, ReadTimeoutError
9 from subprocess
import PIPE
, Popen
, check_output
10 from PIL
import Image
, ImageDraw
, ImageFont
13 from smbus2
import SMBus
15 from smbus
import SMBus
17 print("""luftdaten.py - Reads temperature, pressure, humidity, PM2.5, and PM10 from
18 Enviro plus and sends data to Luftdaten, the citizen science air quality project.
20 Note: you'll need to register with Luftdaten at: https://meine.luftdaten.info/ and
21 enter your Raspberry Pi serial number that's displayed on the Enviro plus LCD
22 along with the other details before the data appears on the Luftdaten map.
30 # Create BME280 instance
31 bme280
= BME280(i2c_dev
=bus
)
33 # Create PMS5003 instance
49 # Read values from BME280 and PMS5003 and return as dict
52 cpu_temp
= get_cpu_temperature()
53 raw_temp
= bme280
.get_temperature()
54 comp_temp
= raw_temp
- ((cpu_temp
- raw_temp
) / comp_factor
)
55 values
["temperature"] = "{:.2f}".format(comp_temp
)
56 values
["pressure"] = "{:.2f}".format(bme280
.get_pressure() * 100)
57 values
["humidity"] = "{:.2f}".format(bme280
.get_humidity())
59 pm_values
= pms5003
.read()
60 values
["P2"] = str(pm_values
.pm_ug_per_m3(2.5))
61 values
["P1"] = str(pm_values
.pm_ug_per_m3(10))
62 except ReadTimeoutError
:
64 pm_values
= pms5003
.read()
65 values
["P2"] = str(pm_values
.pm_ug_per_m3(2.5))
66 values
["P1"] = str(pm_values
.pm_ug_per_m3(10))
69 # Get CPU temperature to use for compensation
70 def get_cpu_temperature():
71 process
= Popen(['vcgencmd', 'measure_temp'], stdout
=PIPE
)
72 output
, _error
= process
.communicate()
73 return float(output
[output
.index('=') + 1:output
.rindex("'")])
75 # Get Raspberry Pi serial number to use as ID
76 def get_serial_number():
77 with
open('/proc/cpuinfo','r') as f
:
79 if line
[0:6]=='Serial':
80 return(line
.split(":")[1].strip())
82 # Check for Wi-Fi connection
84 if check_output(['hostname', '-I']):
89 # Display Raspberry Pi serial and Wi-Fi status on LCD
91 wifi_status
= "connected" if check_wifi() else "disconnected"
92 text_colour
= (255, 255, 255)
93 back_colour
= (0, 170, 170) if check_wifi() else (85, 15, 15)
94 id = get_serial_number()
95 message
= "{}\nWi-Fi: {}".format(id, wifi_status
)
96 img
= Image
.new('RGB', (WIDTH
, HEIGHT
), color
=(0, 0, 0))
97 draw
= ImageDraw
.Draw(img
)
98 size_x
, size_y
= draw
.textsize(message
, font
)
99 x
= (WIDTH
- size_x
) / 2
100 y
= (HEIGHT
/ 2) - (size_y
/ 2)
101 draw
.rectangle((0, 0, 160, 80), back_colour
)
102 draw
.text((x
, y
), message
, font
=font
, fill
=text_colour
)
105 def send_to_luftdaten(values
, id):
106 pm_values
= dict(i
for i
in values
.items() if i
[0].startswith("P"))
107 temp_values
= dict(i
for i
in values
.items() if not i
[0].startswith("P"))
109 resp_1
= requests
.post("https://api.luftdaten.info/v1/push-sensor-data/",
111 "software_version": "enviro-plus 0.0.1",
112 "sensordatavalues": [{"value_type": key
, "value": val
} for key
, val
in pm_values
.items()]
117 "Content-Type": "application/json",
118 "cache-control": "no-cache"
122 resp_2
= requests
.post("https://api.luftdaten.info/v1/push-sensor-data/",
124 "software_version": "enviro-plus 0.0.1",
125 "sensordatavalues": [{"value_type": key
, "value": val
} for key
, val
in temp_values
.items()]
130 "Content-Type": "application/json",
131 "cache-control": "no-cache"
135 if resp_1
.ok
and resp_2
.ok
:
140 # Compensation factor for temperature
143 # Raspberry Pi ID to send to Luftdaten
144 id = "raspi-" + get_serial_number()
146 # Width and height to calculate text position
152 font
= ImageFont
.truetype("fonts/Asap/Asap-Bold.ttf", font_size
)
154 # Display Raspberry Pi serial and Wi-Fi status
155 print("Raspberry Pi serial: {}".format(get_serial_number()))
156 print("Wi-Fi: {}\n".format("connected" if check_wifi() else "disconnected"))
158 # Main loop to read data, display, and send to Luftdaten
161 values
= read_values()
163 resp
= send_to_luftdaten(values
, id)
164 print("Response: {}\n".format("ok" if resp
else "failed"))
166 except Exception as e
: