Skip to content

Commit

Permalink
move to mqtt
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGlauser committed Aug 9, 2023
1 parent aa9935c commit ce8369d
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .idea/airquality.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 33 additions & 13 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,66 @@
import os
import sys
from datetime import datetime

from serial import SerialException

from backoff import Backoff
from database_service import BME280Helper, LTR559Helper, connect_to_database, S8Helper, MICS6814Helper, PMS7003Helper
from database_service import (
BME280Helper,
LTR559Helper,
connect_to_database,
S8Helper,
MICS6814Helper,
PMS7003Helper,
)
from mqtt import setup_mqtt
from sensors.bme280 import BME280
from sensors.ltr559 import LTR559
from sensors.mics6814 import MICS6814
from sensors.pms7003 import PMS7003
from sensors.s8 import S8

import paho.mqtt.client as mqtt
import paho.mqtt.publish as publish

sensors = [
(BME280(), BME280Helper),
(LTR559(), LTR559Helper),
(S8(), S8Helper),
#(MICS6814(), MICS6814Helper),
(PMS7003(), PMS7003Helper)
BME280(),
LTR559(),
S8(),
# (MICS6814(), MICS6814Helper),
PMS7003(),
]

sensor_backoff = Backoff(initial_backoff=60, max_backoff=180, backoff_increment=60)
LTR559().read_data()
connect_to_database()
PMS7003().read_data()


def loop():
mqtt_client = setup_mqtt()
mqtt_client.loop_start()
try:
while True:
for sensor in sensors:
try:
sensor[1](**sensor[0].read_data())
print(f'[{datetime.utcnow()}] - reading {type(sensor[0]).__name__}')
mqtt_client.publish(
f"{os.getenv('MQTT_TOPIC_PREFIX', 'homeassistant')}/{type(sensor).__name__}",
payload=sensor.read_data(),
retain=True,
)
print(f"[{datetime.utcnow()}] - reading {type(sensor).__name__}")

except SerialException as e:
print(e)
print(f'[{datetime.utcnow()}] - Failed reading {type(sensor[0]).__name__}. SKIPPED!')
print('backoff')
print(
f"[{datetime.utcnow()}] - Failed reading {type(sensor).__name__}. SKIPPED!"
)
print("backoff")
sensor_backoff.backoff()
print('')
print("")
except KeyboardInterrupt:
sys.exit(0)


if __name__ == '__main__':
if __name__ == "__main__":
loop()
36 changes: 36 additions & 0 deletions mqtt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import os

import paho.mqtt.client as mqtt


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))


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()


def setup_mqtt() -> mqtt.Client:
device_serial_number = get_serial_number()
device_id = "raspi-" + device_serial_number
mqtt_client = mqtt.Client(client_id=device_id)

mqtt_client.on_connect = on_connect
mqtt_client.on_publish = on_publish

mqtt_client.connect(
os.getenv("MQTT_BROKER", "mosquitto"), port=os.getenv("MQTT_PORT", "1883")
)

return mqtt_client
12 changes: 6 additions & 6 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ ltr559 = "^0.1.0"
"RPi.GPIO" = "^0.7.0"
influxdb = "^5.3.0"
smbus = "^1.1.post2"
python-dotenv = "^0.14.0"
python-dotenv = "^1.0.0"
pyserial = "^3.4"
enviroplus = "^0.0.6"
"RPi.bme280" = "^0.2.3"
plantower = "^0.0.11"
paho-mqtt = "^1.6.1"

[tool.poetry.dev-dependencies]

Expand Down

0 comments on commit ce8369d

Please sign in to comment.