-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathluftdaten-influxdb.py
executable file
·136 lines (107 loc) · 4.49 KB
/
luftdaten-influxdb.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
#!/usr/bin/env python
import logging
import time
from logging.handlers import MemoryHandler, TimedRotatingFileHandler
from subprocess import check_output
import yaml
from PIL import ImageFont
import enviroplus_lcd
import enviroplus_reader
import influxdb_local_weather_client
import luftdaten_client
import opensensemap_client
config = yaml.safe_load(open("config.yml"))
print("""luftdaten-influxdb.py - Reads temperature, pressure, humidity,
PM 1.0, PM2.5, and PM10 from Enviro plus and sends data to InfluxDB and Luftdaten,
the citizen science air quality project.
Note: you'll need to register with Luftdaten at:
https://meine.luftdaten.info/ and enter your Raspberry Pi
serial number that's displayed on the Enviro plus LCD along
with the other details before the data appears on the
Luftdaten map.
Press Ctrl+C to exit!
""")
# Create logger
def get_logger(path):
rotating_handler = TimedRotatingFileHandler(path, when='d', backupCount=7)
formatter = logging.Formatter('%(asctime)s [%(levelname)s] %(message)s')
rotating_handler.setFormatter(formatter)
memory_handler = MemoryHandler(capacity=512 * 1024, target=rotating_handler)
console_handler = logging.StreamHandler()
console_handler.setFormatter(formatter)
logger = logging.getLogger("luftdaten")
logger.setLevel(logging.DEBUG)
logger.addHandler(memory_handler)
logger.addHandler(console_handler)
return logger
# Get Raspberry Pi serial number to use as ID
def get_serial_number():
with open('/proc/cpuinfo', 'r') as f:
for line in f:
if line[0:6] == 'Serial':
return line.split(":")[1].strip()
# Check for Wi-Fi connection
def check_wifi():
if check_output(['hostname', '-I']):
return True
else:
return False
# Raspberry Pi ID to send to Luftdaten
id = "raspi-" + get_serial_number()
# Logger
logger = get_logger('/var/log/luftdaten.log')
# Text settings
font_size = 16
font = ImageFont.truetype("fonts/Asap/Asap-Bold.ttf", font_size)
# Display Raspberry Pi serial and Wi-Fi status
logger.info("Raspberry Pi serial: {}".format(get_serial_number()))
logger.info(
"Wi-Fi: {}\n".format('connected' if check_wifi() else 'disconnected'))
time_since_update = 0
update_time = time.time()
# Enviroplus measures reader
reader = enviroplus_reader.EnviroPlusReader()
# Luftdaten Client
luftdaten_client = luftdaten_client.LuftdatenClient(id)
# InfluxDB client
influxdb_cfg = config['influxdb']
influxdb_weather = influxdb_local_weather_client.InfluxDbWeather(
influxdb_cfg['host'], influxdb_cfg['port'], influxdb_cfg['database'],
influxdb_cfg['username'], influxdb_cfg['password'], logger)
enviroplus_lcd = enviroplus_lcd.EnviroplusLCD(font)
# OpenSenseMap client
opensensemap_cfg = config['opensensemap']
opensensemap_client = opensensemap_client.OpenSenseMapClient(opensensemap_cfg['sensebox_id'],
opensensemap_cfg['temperature_sensor_id'],
opensensemap_cfg['humidity_sensor_id'],
opensensemap_cfg['pressure_sensor_id'],
opensensemap_cfg['pm_1_0_sensor_id'],
opensensemap_cfg['pm_2_5_sensor_id'],
opensensemap_cfg['pm_10_sensor_id'], logger)
wifi_status = 'connected' if check_wifi() else 'disconnected'
enviroplus_lcd.display_status(wifi_status, 'waiting')
# Main loop to read data, display, and send to Luftdaten
while True:
try:
time_since_update = time.time() - update_time
# Read measures
values = reader.read_values()
logger.debug(values)
# Sen measures to influxDb
influxdb_weather.send_to_influxdb(values)
# Send measures to OpenSenseMap
opensensemap_client.send_to_opensensemap(values)
if time_since_update > 120:
wifi_status = 'connected' if check_wifi() else 'disconnected'
# Send to Luftdaten
resp = luftdaten_client.send_to_luftdaten(values)
response = 'ok' if resp else 'failed'
update_time = time.time()
logger.info("Response: {}".format(response))
enviroplus_lcd.display_status(wifi_status, response)
time.sleep(2)
except KeyboardInterrupt:
enviroplus_lcd.turnoff()
raise
except Exception as e:
logger.exception(e)