-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aa9935c
commit ce8369d
Showing
6 changed files
with
79 additions
and
22 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters