-
Notifications
You must be signed in to change notification settings - Fork 184
/
mqtt-all.py
executable file
·285 lines (236 loc) · 7.61 KB
/
mqtt-all.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#!/usr/bin/env python3
"""
Run mqtt broker on localhost: sudo apt-get install mosquitto mosquitto-clients
Example run: python3 mqtt-all.py --broker 192.168.1.164 --topic enviro --username xxx --password xxxx
"""
import argparse
import ssl
import time
import st7735
from bme280 import BME280
from pms5003 import PMS5003, ReadTimeoutError, SerialTimeoutError
from enviroplus import gas
try:
# Transitional fix for breaking change in LTR559
from ltr559 import LTR559
ltr559 = LTR559()
except ImportError:
import ltr559
import json
from subprocess import PIPE, Popen, check_output
import paho.mqtt.client as mqtt
from fonts.ttf import RobotoMedium as UserFont
from PIL import Image, ImageDraw, ImageFont
try:
from smbus2 import SMBus
except ImportError:
from smbus import SMBus
DEFAULT_MQTT_BROKER_IP = "localhost"
DEFAULT_MQTT_BROKER_PORT = 1883
DEFAULT_MQTT_TOPIC = "enviroplus"
DEFAULT_READ_INTERVAL = 5
DEFAULT_TLS_MODE = False
DEFAULT_USERNAME = None
DEFAULT_PASSWORD = None
# mqtt callbacks
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("connected OK")
else:
print("Bad connection Returned code=", rc)
def on_publish(client, userdata, mid):
print("mid: " + str(mid))
# Read values from BME280 and return as dict
def read_bme280(bme280):
# Compensation factor for temperature
comp_factor = 2.25
values = {}
cpu_temp = get_cpu_temperature()
raw_temp = bme280.get_temperature() # float
comp_temp = raw_temp - ((cpu_temp - raw_temp) / comp_factor)
values["temperature"] = int(comp_temp)
values["pressure"] = round(
int(bme280.get_pressure() * 100), -1
) # round to nearest 10
values["humidity"] = int(bme280.get_humidity())
data = gas.read_all()
values["oxidised"] = int(data.oxidising / 1000)
values["reduced"] = int(data.reducing / 1000)
values["nh3"] = int(data.nh3 / 1000)
values["lux"] = int(ltr559.get_lux())
return values
# Read values PMS5003 and return as dict
def read_pms5003(pms5003):
values = {}
try:
pm_values = pms5003.read() # int
values["pm1"] = pm_values.pm_ug_per_m3(1)
values["pm25"] = pm_values.pm_ug_per_m3(2.5)
values["pm10"] = pm_values.pm_ug_per_m3(10)
except ReadTimeoutError:
pms5003.reset()
pm_values = pms5003.read()
values["pm1"] = pm_values.pm_ug_per_m3(1)
values["pm25"] = pm_values.pm_ug_per_m3(2.5)
values["pm10"] = pm_values.pm_ug_per_m3(10)
return values
# Get CPU temperature to use for compensation
def get_cpu_temperature():
process = Popen(
["vcgencmd", "measure_temp"], stdout=PIPE, universal_newlines=True
)
output, _error = process.communicate()
return float(output[output.index("=") + 1:output.rindex("'")])
# 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
# Display Raspberry Pi serial and Wi-Fi status on LCD
def display_status(disp, mqtt_broker):
# Width and height to calculate text position
WIDTH = disp.width
HEIGHT = disp.height
# Text settings
font_size = 12
font = ImageFont.truetype(UserFont, font_size)
wifi_status = "connected" if check_wifi() else "disconnected"
text_colour = (255, 255, 255)
back_colour = (0, 170, 170) if check_wifi() else (85, 15, 15)
device_serial_number = get_serial_number()
message = f"{device_serial_number}\nWi-Fi: {wifi_status}\nmqtt-broker: {mqtt_broker}"
img = Image.new("RGB", (WIDTH, HEIGHT), color=(0, 0, 0))
draw = ImageDraw.Draw(img)
x1, y1, x2, y2 = font.getbbox(message)
size_x = x2 - x1
size_y = y2 - y1
x = (WIDTH - size_x) / 2
y = (HEIGHT / 2) - (size_y / 2)
draw.rectangle((0, 0, 160, 80), back_colour)
draw.text((x, y), message, font=font, fill=text_colour)
disp.display(img)
def main():
parser = argparse.ArgumentParser(
description="Publish enviroplus values over mqtt"
)
parser.add_argument(
"--broker",
default=DEFAULT_MQTT_BROKER_IP,
type=str,
help="mqtt broker IP",
)
parser.add_argument(
"--port",
default=DEFAULT_MQTT_BROKER_PORT,
type=int,
help="mqtt broker port",
)
parser.add_argument(
"--topic", default=DEFAULT_MQTT_TOPIC, type=str, help="mqtt topic"
)
parser.add_argument(
"--interval",
default=DEFAULT_READ_INTERVAL,
type=int,
help="the read interval in seconds",
)
parser.add_argument(
"--tls",
default=DEFAULT_TLS_MODE,
action="store_true",
help="enable TLS"
)
parser.add_argument(
"--username",
default=DEFAULT_USERNAME,
type=str,
help="mqtt username"
)
parser.add_argument(
"--password",
default=DEFAULT_PASSWORD,
type=str,
help="mqtt password"
)
args = parser.parse_args()
# Raspberry Pi ID
device_serial_number = get_serial_number()
device_id = "raspi-" + device_serial_number
print(
f"""mqtt-all.py - Reads Enviro plus data and sends over mqtt.
broker: {args.broker}
client_id: {device_id}
port: {args.port}
topic: {args.topic}
tls: {args.tls}
username: {args.username}
password: {args.password}
Press Ctrl+C to exit!
"""
)
mqtt_client = mqtt.Client(client_id=device_id)
if args.username and args.password:
mqtt_client.username_pw_set(args.username, args.password)
mqtt_client.on_connect = on_connect
mqtt_client.on_publish = on_publish
if args.tls is True:
mqtt_client.tls_set(tls_version=ssl.PROTOCOL_TLSv1_2)
if args.username is not None:
mqtt_client.username_pw_set(args.username, password=args.password)
mqtt_client.connect(args.broker, port=args.port)
bus = SMBus(1)
# Create BME280 instance
bme280 = BME280(i2c_dev=bus)
# Create LCD instance
disp = st7735.ST7735(
port=0,
cs=1,
dc="GPIO9",
backlight="GPIO12",
rotation=270,
spi_speed_hz=10000000
)
# Initialize display
disp.begin()
# Try to create PMS5003 instance
HAS_PMS = False
try:
pms5003 = PMS5003()
_ = pms5003.read()
HAS_PMS = True
print("PMS5003 sensor is connected")
except SerialTimeoutError:
print("No PMS5003 sensor connected")
# Display Raspberry Pi serial and Wi-Fi status
print(f"RPi serial: {device_serial_number}")
wifi_status = "connected" if check_wifi() else "disconnected"
print(f"Wi-Fi: {wifi_status}\n")
print(f"MQTT broker IP: {args.broker}")
# Set an initial update time
update_time = time.time()
# Main loop to read data, display, and send over mqtt
mqtt_client.loop_start()
while True:
try:
values = read_bme280(bme280)
if HAS_PMS:
pms_values = read_pms5003(pms5003)
values.update(pms_values)
time_since_update = time.time() - update_time
if time_since_update >= args.interval:
update_time = time.time()
values["serial"] = device_serial_number
print(values)
mqtt_client.publish(args.topic, json.dumps(values), retain=True)
display_status(disp, args.broker)
except Exception as e:
print(e)
if __name__ == "__main__":
main()