-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.py
executable file
·167 lines (137 loc) · 4.79 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env python3
import argparse
import time
import toml
import requests
import json
import numpy as np
import board
import busio
from sds011 import SDS011
import adafruit_bme280
from paho.mqtt import client as mqtt_client
# Parse command line args
parser = argparse.ArgumentParser(description='Luftdaten in Python')
parser.add_argument('-c', '--config', default='config.toml', help='path to config file')
args = parser.parse_args()
# Read config file
config = toml.load(args.config)
# Configure Logging
import logging
logging.basicConfig(level=logging.DEBUG)
# Configure BME280
print("initialize BME280")
i2c = busio.I2C(board.SCL, board.SDA)
bme280 = adafruit_bme280.Adafruit_BME280_I2C(i2c, address=0x76)
# Configure SDS011
print("initialize SDS011")
dusty = SDS011(port='/dev/ttyUSB0')
# Now we have some details about it
print("SDS011 initialized: device_id={} firmware={}".format(dusty.devid, dusty.firmware))
# Configure MQTT
mqtt_conn = None
mqtt_cfg = config["mqtt"]
if mqtt_cfg["enabled"]:
mqtt_conn = mqtt_client.Client(mqtt_cfg["client_id"])
mqtt_conn.connect(mqtt_cfg["broker"], mqtt_cfg["port"])
class Measurement:
def __init__(self):
self.pm25_value = None
self.pm10_value = None
if dusty:
pm25_values = []
pm10_values = []
dusty.wakeup()
try:
for a in range(8):
values = dusty.read_measurement()
if values is not None:
pm10_values.append(values.get("pm10"))
pm25_values.append(values.get("pm2.5"))
finally:
dusty.sleep()
self.pm25_value = np.mean(pm25_values)
self.pm10_value = np.mean(pm10_values)
self.temperature = bme280.temperature
self.humidity = bme280.relative_humidity
self.pressure = bme280.pressure
def sendMQTT(self):
mqtt_conn.publish(mqtt_cfg["topic"], json.dumps({
"dust_pm10": self.pm10_value,
"dust_pm25": self.pm25_value,
"temperature": self.temperature,
"pressure": self.pressure,
"humidity": self.humidity,
}))
def sendInflux(self):
cfg = config['influxdb']
if not cfg['enabled']:
return
data = "feinstaub,node={} SDS_P1={:0.2f},SDS_P2={:0.2f},BME280_temperature={:0.2f},BME280_pressure={:0.2f},BME280_humidity={:0.2f}".format(
cfg['node'],
self.pm10_value,
self.pm25_value,
self.temperature,
self.pressure,
self.humidity,
)
requests.post(cfg['url'],
auth=(cfg['username'], cfg['password']),
data=data,
)
def sendLuftdaten(self):
if not config['luftdaten']['enabled']:
return
self.__pushLuftdaten('https://api-rrd.madavi.de/data.php', 0, {
"SDS_P1": self.pm10_value,
"SDS_P2": self.pm25_value,
"BME280_temperature": self.temperature,
"BME280_pressure": self.pressure,
"BME280_humidity": self.humidity,
})
self.__pushLuftdaten('https://api.luftdaten.info/v1/push-sensor-data/', 1, {
"P1": self.pm10_value,
"P2": self.pm25_value,
})
self.__pushLuftdaten('https://api.luftdaten.info/v1/push-sensor-data/', 11, {
"temperature": self.temperature,
"pressure": self.pressure,
"humidity": self.humidity,
})
def __pushLuftdaten(self, url, pin, values):
requests.post(url,
json={
"software_version": "python-dusty 0.0.1",
"sensordatavalues": [{"value_type": key, "value": val} for key, val in values.items()],
},
headers={
"X-PIN": str(pin),
"X-Sensor": sensorID,
}
)
# extracts serial from cpuinfo
def getSerial():
with open('/proc/cpuinfo','r') as f:
for line in f:
if line[0:6]=='Serial':
return(line[10:26])
raise Exception('CPU serial not found')
def run():
m = Measurement()
print('pm2.5 = {:f} '.format(m.pm25_value))
print('pm10 = {:f} '.format(m.pm10_value))
print('Temp = {:0.2f} deg C'.format(m.temperature))
print('Humidity = {:0.2f} %'.format(m.humidity))
print('Pressure = {:0.2f} hPa'.format(m.pressure/100))
if mqtt_conn:
m.sendMQTT()
m.sendLuftdaten()
m.sendInflux()
sensorID = config['luftdaten'].get('sensor') or ("raspi-" + getSerial())
starttime = time.time()
if __name__ == "__main__":
while True:
print("running ...")
run()
time.sleep(60.0 - ((time.time() - starttime) % 60.0))
print("Stopped")