11#!/usr/bin/env python
22
3- import time
4- import json
53import requests
64import ST7735
75from bme280 import BME280
1412except ImportError :
1513 from smbus import SMBus
1614
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.
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.
1918
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.
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
23+ Luftdaten map.
2324
2425Press Ctrl+C to exit!
2526
3031# Create BME280 instance
3132bme280 = BME280 (i2c_dev = bus )
3233
33- # Create PMS5003 instance
34- pms5003 = PMS5003 ()
35-
3634# Create LCD instance
3735disp = ST7735 .ST7735 (
3836 port = 0 ,
4644# Initialize display
4745disp .begin ()
4846
47+ # Create PMS5003 instance
48+ pms5003 = PMS5003 ()
49+
50+
4951# Read values from BME280 and PMS5003 and return as dict
5052def read_values ():
5153 values = {}
@@ -66,18 +68,21 @@ def read_values():
6668 values ["P1" ] = str (pm_values .pm_ug_per_m3 (10 ))
6769 return values
6870
71+
6972# Get CPU temperature to use for compensation
7073def get_cpu_temperature ():
7174 process = Popen (['vcgencmd' , 'measure_temp' ], stdout = PIPE )
7275 output , _error = process .communicate ()
7376 return float (output [output .index ('=' ) + 1 :output .rindex ("'" )])
7477
78+
7579# Get Raspberry Pi serial number to use as ID
7680def get_serial_number ():
77- with open ('/proc/cpuinfo' ,'r' ) as f :
81+ with open ('/proc/cpuinfo' , 'r' ) as f :
7882 for line in f :
79- if line [0 :6 ]== 'Serial' :
80- return (line .split (":" )[1 ].strip ())
83+ if line [0 :6 ] == 'Serial' :
84+ return line .split (":" )[1 ].strip ()
85+
8186
8287# Check for Wi-Fi connection
8388def check_wifi ():
@@ -86,6 +91,7 @@ def check_wifi():
8691 else :
8792 return False
8893
94+
8995# Display Raspberry Pi serial and Wi-Fi status on LCD
9096def display_status ():
9197 wifi_status = "connected" if check_wifi () else "disconnected"
@@ -102,41 +108,45 @@ def display_status():
102108 draw .text ((x , y ), message , font = font , fill = text_colour )
103109 disp .display (img )
104110
111+
105112def send_to_luftdaten (values , id ):
106113 pm_values = dict (i for i in values .items () if i [0 ].startswith ("P" ))
107114 temp_values = dict (i for i in values .items () if not i [0 ].startswith ("P" ))
108115
109116 resp_1 = requests .post ("https://api.luftdaten.info/v1/push-sensor-data/" ,
110- json = {
111- "software_version" : "enviro-plus 0.0.1" ,
112- "sensordatavalues" : [{"value_type" : key , "value" : val } for key , val in pm_values .items ()]
113- },
114- headers = {
115- "X-PIN" : "1" ,
116- "X-Sensor" : id ,
117- "Content-Type" : "application/json" ,
118- "cache-control" : "no-cache"
119- }
117+ json = {
118+ "software_version" : "enviro-plus 0.0.1" ,
119+ "sensordatavalues" : [{"value_type" : key , "value" : val } for
120+ key , val in pm_values .items ()]
121+ },
122+ headers = {
123+ "X-PIN" : "1" ,
124+ "X-Sensor" : id ,
125+ "Content-Type" : "application/json" ,
126+ "cache-control" : "no-cache"
127+ }
120128 )
121129
122130 resp_2 = requests .post ("https://api.luftdaten.info/v1/push-sensor-data/" ,
123- json = {
124- "software_version" : "enviro-plus 0.0.1" ,
125- "sensordatavalues" : [{"value_type" : key , "value" : val } for key , val in temp_values .items ()]
126- },
127- headers = {
128- "X-PIN" : "11" ,
129- "X-Sensor" : id ,
130- "Content-Type" : "application/json" ,
131- "cache-control" : "no-cache"
132- }
131+ json = {
132+ "software_version" : "enviro-plus 0.0.1" ,
133+ "sensordatavalues" : [{"value_type" : key , "value" : val } for
134+ key , val in temp_values .items ()]
135+ },
136+ headers = {
137+ "X-PIN" : "11" ,
138+ "X-Sensor" : id ,
139+ "Content-Type" : "application/json" ,
140+ "cache-control" : "no-cache"
141+ }
133142 )
134143
135144 if resp_1 .ok and resp_2 .ok :
136145 return True
137146 else :
138147 return False
139148
149+
140150# Compensation factor for temperature
141151comp_factor = 1.2
142152
0 commit comments