4d3d49acd87a86dae17d79f51981f67d75fc261a
5 from bme280
import BME280
6 from pms5003
import PMS5003
, ReadTimeoutError
7 from subprocess
import PIPE
, Popen
, check_output
8 from PIL
import Image
, ImageDraw
, ImageFont
11 from smbus2
import SMBus
13 from smbus
import SMBus
15 print("""luftdaten.py - Reads temperature, pressure, humidity,
16 PM2.5, and PM10 from Enviro plus and sends data to Luftdaten,
17 the citizen science air quality project.
19 Note: you'll need to register with Luftdaten at:
20 https://meine.luftdaten.info/ and enter your Raspberry Pi
21 serial number that's displayed on the Enviro plus LCD along
22 with the other details before the data appears on the
31 # Create BME280 instance
32 bme280
= BME280(i2c_dev
=bus
)
47 # Create PMS5003 instance
51 # Read values from BME280 and PMS5003 and return as dict
54 cpu_temp
= get_cpu_temperature()
55 raw_temp
= bme280
.get_temperature()
56 comp_temp
= raw_temp
- ((cpu_temp
- raw_temp
) / comp_factor
)
57 values
["temperature"] = "{:.2f}".format(comp_temp
)
58 values
["pressure"] = "{:.2f}".format(bme280
.get_pressure() * 100)
59 values
["humidity"] = "{:.2f}".format(bme280
.get_humidity())
61 pm_values
= pms5003
.read()
62 values
["P2"] = str(pm_values
.pm_ug_per_m3(2.5))
63 values
["P1"] = str(pm_values
.pm_ug_per_m3(10))
64 except ReadTimeoutError
:
66 pm_values
= pms5003
.read()
67 values
["P2"] = str(pm_values
.pm_ug_per_m3(2.5))
68 values
["P1"] = str(pm_values
.pm_ug_per_m3(10))
72 # Get CPU temperature to use for compensation
73 def get_cpu_temperature():
74 process
= Popen(['vcgencmd', 'measure_temp'], stdout
=PIPE
)
75 output
, _error
= process
.communicate()
76 return float(output
[output
.index('=') + 1:output
.rindex("'")])
79 # Get Raspberry Pi serial number to use as ID
80 def get_serial_number():
81 with
open('/proc/cpuinfo', 'r') as f
:
83 if line
[0:6] == 'Serial':
84 return line
.split(":")[1].strip()
87 # Check for Wi-Fi connection
89 if check_output(['hostname', '-I']):
95 # Display Raspberry Pi serial and Wi-Fi status on LCD
97 wifi_status
= "connected" if check_wifi() else "disconnected"
98 text_colour
= (255, 255, 255)
99 back_colour
= (0, 170, 170) if check_wifi() else (85, 15, 15)
100 id = get_serial_number()
101 message
= "{}\nWi-Fi: {}".format(id, wifi_status
)
102 img
= Image
.new('RGB', (WIDTH
, HEIGHT
), color
=(0, 0, 0))
103 draw
= ImageDraw
.Draw(img
)
104 size_x
, size_y
= draw
.textsize(message
, font
)
105 x
= (WIDTH
- size_x
) / 2
106 y
= (HEIGHT
/ 2) - (size_y
/ 2)
107 draw
.rectangle((0, 0, 160, 80), back_colour
)
108 draw
.text((x
, y
), message
, font
=font
, fill
=text_colour
)
112 def send_to_luftdaten(values
, id):
113 pm_values
= dict(i
for i
in values
.items() if i
[0].startswith("P"))
114 temp_values
= dict(i
for i
in values
.items() if not i
[0].startswith("P"))
116 resp_1
= requests
.post("https://api.luftdaten.info/v1/push-sensor-data/",
118 "software_version": "enviro-plus 0.0.1",
119 "sensordatavalues": [{"value_type": key
, "value": val
} for
120 key
, val
in pm_values
.items()]
125 "Content-Type": "application/json",
126 "cache-control": "no-cache"
130 resp_2
= requests
.post("https://api.luftdaten.info/v1/push-sensor-data/",
132 "software_version": "enviro-plus 0.0.1",
133 "sensordatavalues": [{"value_type": key
, "value": val
} for
134 key
, val
in temp_values
.items()]
139 "Content-Type": "application/json",
140 "cache-control": "no-cache"
144 if resp_1
.ok
and resp_2
.ok
:
150 # Compensation factor for temperature
153 # Raspberry Pi ID to send to Luftdaten
154 id = "raspi-" + get_serial_number()
156 # Width and height to calculate text position
162 font
= ImageFont
.truetype("fonts/Asap/Asap-Bold.ttf", font_size
)
164 # Display Raspberry Pi serial and Wi-Fi status
165 print("Raspberry Pi serial: {}".format(get_serial_number()))
166 print("Wi-Fi: {}\n".format("connected" if check_wifi() else "disconnected"))
168 # Main loop to read data, display, and send to Luftdaten
171 values
= read_values()
173 resp
= send_to_luftdaten(values
, id)
174 print("Response: {}\n".format("ok" if resp
else "failed"))
176 except Exception as e
: