-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbme680homiemqtt.py
146 lines (134 loc) · 5.46 KB
/
bme680homiemqtt.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
# -*- coding: utf-8 -*-
"""BME680 Homie MQTT
reads data (Temperature, Humidity, Pressure, Gas, Altitude) of BME680 sensor and
sends values to a MQTT Broker in Homie MQTT Convention format
You are able to set a value, when humidity alarm is fired.
"""
import config
import wificonnection
from umqtt.simple2 import MQTTClient
from time import sleep
from bme680 import *
from machine import I2C, Pin
import machine
# led declaration
if config.ledstatus:
led = Pin('LED', Pin.OUT)
# degree symbol decleration
degreecels = '\u00B0' + "C"
# Functions
def publish(topic, payload):
client.publish("homie/" + config.homieclientid + "/" + topic,
payload, retain=config.mqttretainmessage, qos=config.mqttqos)
def mqttconnect():
global client
client = MQTTClient(config.mqttclientid, config.mqttbroker, port=config.mqttport, user=config.mqttusername,
password=config.mqttpassword, keepalive=10, ssl=config.mqttssl,
ssl_params={})
client.set_last_will("homie/" + config.homieclientid + "/$state",
"lost", retain=config.mqttretainmessage, qos=config.mqttqos)
client.connect()
# homie client config
publish("$state", "init")
publish("$homie", "4.0")
publish("$name", config.homieclientname)
publish("$nodes", config.homienodes)
# homie node config
publish(config.homienodes + "/$name", "BME680 Sensor")
publish(config.homienodes + "/$properties", "temperature,humidity,humidityalarm,pressure,gas,altitude")
publish(config.homienodes + "/temperature/$name", "Temperature")
publish(config.homienodes + "/temperature/$unit", degreecels.encode('utf8'))
publish(config.homienodes + "/temperature/$datatype", "float")
publish(config.homienodes + "/temperature/$settable", "false")
publish(config.homienodes + "/humidity/$name", "Humidity")
publish(config.homienodes + "/humidity/$unit", "%")
publish(config.homienodes + "/humidity/$datatype", "float")
publish(config.homienodes + "/humidity/$settable", "false")
publish(config.homienodes + "/humidityalarm/$name", "Humidity Alarm")
publish(config.homienodes + "/humidityalarm/$datatype", "boolean")
publish(config.homienodes + "/humidityalarm/$settable", "false")
publish(config.homienodes + "/pressure/$name", "Pressure")
publish(config.homienodes + "/pressure/$unit", "hPa")
publish(config.homienodes + "/pressure/$datatype", "float")
publish(config.homienodes + "/pressure/$settable", "false")
publish(config.homienodes + "/gas/$name","Gas")
publish(config.homienodes + "/gas/$unit","ohm")
publish(config.homienodes + "/gas/$datatype","integer")
publish(config.homienodes + "/gas/$settable","false")
publish(config.homienodes + "/altitude/$name","Altitude")
publish(config.homienodes + "/altitude/$unit","m")
publish(config.homienodes + "/altitude/$datatype","float")
publish(config.homienodes + "/altitude/$settable","false")
# homie state ready
publish("$state", "ready")
def sensorpublish():
publish(config.homienodes + "/temperature", "{:.1f}".format(temperature))
publish(config.homienodes + "/humidity", "{:.1f}".format(humidity))
publish(config.homienodes + "/pressure", "{:.1f}".format(pressure))
publish(config.homienodes + "/gas", "{:.1f}".format(gas))
publish(config.homienodes + "/altitude", "{:.1f}".format(altitude))
if humidity >= config.humidityalarm:
publish(config.homienodes + "/humidityalarm", "true")
else:
publish(config.homienodes + "/humidityalarm", "false")
def bme680sensor():
global temperature
global humidity
global pressure
global gas
global altitude
# Initialize BME680 Sensor
bme680 = BME680_I2C(I2C(0, sda=Pin(config.i2c_sda),scl=Pin(config.i2c_scl)))
# set sealevel if it is conffigured
if 'config.sealevelpressure' in locals():
bme680.sea_level_pressure = config.sealevelpressure
temperature = (bme680.temperature + config.temperature_offset)
humidity = bme680.humidity
pressure = bme680.pressure
gas = bme680.gas
altitude = bme680.altitude
# do the things
try:
# connect to wifi
wificonnection.connect()
# connect to mqtt broker and initialize homie convention
mqttconnect()
while True:
if config.ledstatus:
led.value(True)
bme680sensor()
print('Temperature = ', temperature, degreecels)
print('Humidty = ', humidity, '%')
print('Pressure = ', pressure, 'hPa')
print('Gas = ', gas, 'ohm')
print('Altitude = ', altitude, 'm', '\n')
sensorpublish()
sleep(.5)
if config.usinglightsleep:
publish("$state", "sleeping")
sleep(.5)
client.disconnect()
sleep(.5)
wificonnection.disconnect()
print("done...")
#sleep(.5)
if config.ledstatus:
led.value(False)
machine.lightsleep((config.publishtime)*1000-13100)
else:
machine.lightsleep((config.publishtime)*1000-10600)
break
else:
print("just a break for %s seconds" % config.publishtime)
if config.ledstatus:
led.value(False)
sleep(config.publishtime-15)
else:
sleep(config.publishtime-10)
except RuntimeError as error:
print(error.args[0])
pass
except OSError as e:
print(e.args[0])
pass
machine.reset()