Skip to content

Commit

Permalink
script formating
Browse files Browse the repository at this point in the history
On branch main
Your branch is up to date with 'origin/main'.

Changes to be committed:
	modified:   bme280-homie.py
  • Loading branch information
alaub81 committed Sep 23, 2022
1 parent a174c8c commit 4f415a2
Showing 1 changed file with 93 additions and 82 deletions.
175 changes: 93 additions & 82 deletions bme280-homie.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
#!/usr/bin/python3
import board, busio
# Import required Python libraries
import board
import busio
import adafruit_bme280
import paho.mqtt.client as mqtt
import time, ssl, systemd.daemon
import time
import ssl
import systemd.daemon

# set the variables
broker='FQDN / IP Adresse'
port=8883
broker = 'FQDN / IP Adresse'
port = 8883
mqttclientid = "clientid-bme280-homie"
clientid = "clientid-bme280"
clientname = "Clientname BME280 Sensor"
nodes="bme280"
username='mosquitto'
password='password'
insecure=True
qos=1
retain_message=True
nodes = "bme280"
username = 'mosquitto'
password = 'password'
insecure = True
qos = 1
retain_message = True
# Retry to connect to mqtt broker
mqttretry = 5
# how often should be a publish to MQTT (in Seconds)
Expand All @@ -24,40 +28,46 @@
humidityalarm = 70

# do the stuff
### Functions
# Functions


def publish(topic, payload):
client.publish("homie/" + clientid + "/" + topic,payload,qos,retain_message)
client.publish("homie/" + clientid + "/" + topic,
payload, qos, retain_message)


def on_connect(client, userdata, flags, rc):
print("MQTT Connection established, Returned code=",rc)
# homie client config
publish("$state","init")
publish("$homie","4.0")
publish("$name",clientname)
publish("$nodes",nodes)
# homie node config
publish(nodes + "/$name","BME280 Sensor")
publish(nodes + "/$properties","temperature,humidity,humidityalarm,pressure")
publish(nodes + "/temperature/$name","Temperature")
publish(nodes + "/temperature/$unit","°C")
publish(nodes + "/temperature/$datatype","float")
publish(nodes + "/temperature/$settable","false")
publish(nodes + "/humidity/$name","Humidity")
publish(nodes + "/humidity/$unit","%")
publish(nodes + "/humidity/$datatype","float")
publish(nodes + "/humidity/$settable","false")
publish(nodes + "/humidityalarm/$name", "Humidity Alarm")
publish(nodes + "/humidityalarm/$datatype", "boolean")
publish(nodes + "/humidityalarm/$settable", "false")
publish(nodes + "/pressure/$name","Pressure")
publish(nodes + "/pressure/$unit","hPa")
publish(nodes + "/pressure/$datatype","float")
publish(nodes + "/pressure/$settable","false")
# homie stae ready
publish("$state","ready")
print("MQTT Connection established, Returned code=", rc)
# homie client config
publish("$state", "init")
publish("$homie", "4.0")
publish("$name", clientname)
publish("$nodes", nodes)
# homie node config
publish(nodes + "/$name", "BME280 Sensor")
publish(nodes + "/$properties","temperature,humidity,humidityalarm,pressure")
publish(nodes + "/temperature/$name", "Temperature")
publish(nodes + "/temperature/$unit", "°C")
publish(nodes + "/temperature/$datatype", "float")
publish(nodes + "/temperature/$settable", "false")
publish(nodes + "/humidity/$name", "Humidity")
publish(nodes + "/humidity/$unit", "%")
publish(nodes + "/humidity/$datatype", "float")
publish(nodes + "/humidity/$settable", "false")
publish(nodes + "/humidityalarm/$name", "Humidity Alarm")
publish(nodes + "/humidityalarm/$datatype", "boolean")
publish(nodes + "/humidityalarm/$settable", "false")
publish(nodes + "/pressure/$name", "Pressure")
publish(nodes + "/pressure/$unit", "hPa")
publish(nodes + "/pressure/$datatype", "float")
publish(nodes + "/pressure/$settable", "false")
# homie stae ready
publish("$state", "ready")


def on_disconnect(client, userdata, rc):
print("MQTT Connection disconnected, Returned code=",rc)
print("MQTT Connection disconnected, Returned code=", rc)


# running the Script
# define BME280 Sensor
Expand All @@ -68,25 +78,26 @@ def on_disconnect(client, userdata, rc):
bme280.mode = adafruit_bme280.MODE_NORMAL
time.sleep(1)

#MQTT Connection
# MQTT Connection
mqttattempts = 0
while mqttattempts < mqttretry:
try:
client=mqtt.Client(mqttclientid)
client.username_pw_set(username, password)
client.tls_set(cert_reqs=ssl.CERT_NONE) #no client certificate needed
client.tls_insecure_set(insecure)
client.will_set("homie/" + clientid + "/$state","lost",qos,retain_message)
client.connect(broker, port)
client.loop_start()
mqttattempts = mqttretry
except :
print("Could not establish MQTT Connection! Try again " + str(mqttretry - mqttattempts) + "x times")
mqttattempts += 1
if mqttattempts == mqttretry:
print("Could not connect to MQTT Broker! exit...")
exit (0)
time.sleep(5)
try:
client = mqtt.Client(mqttclientid)
client.username_pw_set(username, password)
client.tls_set(cert_reqs=ssl.CERT_NONE) # no client certificate needed
client.tls_insecure_set(insecure)
client.will_set("homie/" + clientid + "/$state","lost", qos, retain_message)
client.connect(broker, port)
client.loop_start()
mqttattempts = mqttretry
except:
print("Could not establish MQTT Connection! Try again " +
str(mqttretry - mqttattempts) + "x times")
mqttattempts += 1
if mqttattempts == mqttretry:
print("Could not connect to MQTT Broker! exit...")
exit(0)
time.sleep(5)

# Tell systemd that our service is ready
systemd.daemon.notify('READY=1')
Expand All @@ -96,34 +107,34 @@ def on_disconnect(client, userdata, rc):

# finaly the loop
while True:
try:
humidity = bme280.humidity
publish(nodes + "/temperature","{:.2f}".format(bme280.temperature))
publish(nodes + "/humidity","{:.2f}".format(humidity))
publish(nodes + "/pressure","{:.2f}".format(bme280.pressure))
if humidity >= humidityalarm:
publish(nodes + "/humidityalarm", "true")
else:
publish(nodes + "/humidityalarm", "false")
time.sleep(publishtime)

except KeyboardInterrupt:
print("Goodbye!")
# At least close MQTT Connection
publish("$state","disconnected")
time.sleep(1)
client.disconnect()
client.loop_stop()
exit (0)

except :
print("An Error accured ... ")
time.sleep(3)
continue
try:
humidity = bme280.humidity
publish(nodes + "/temperature", "{:.2f}".format(bme280.temperature))
publish(nodes + "/humidity", "{:.2f}".format(humidity))
publish(nodes + "/pressure", "{:.2f}".format(bme280.pressure))
if humidity >= humidityalarm:
publish(nodes + "/humidityalarm", "true")
else:
publish(nodes + "/humidityalarm", "false")
time.sleep(publishtime)

except KeyboardInterrupt:
print("Goodbye!")
# At least close MQTT Connection
publish("$state", "disconnected")
time.sleep(1)
client.disconnect()
client.loop_stop()
exit(0)

except:
print("An Error accured ... ")
time.sleep(3)
continue

# At least close MQTT Connection
print("Script stopped")
publish("$state","disconnected")
publish("$state", "disconnected")
time.sleep(1)
client.disconnect()
client.loop_stop()
client.loop_stop()

0 comments on commit 4f415a2

Please sign in to comment.