commit a8bfba6a77f58efa1363bd6126593bfa4b8d3821 Author: biqu Date: Tue Sep 24 16:30:31 2024 -0400 Sheller MQTT app configuration diff --git a/mqtt_listener.py b/mqtt_listener.py new file mode 100644 index 0000000..cf06f29 --- /dev/null +++ b/mqtt_listener.py @@ -0,0 +1,122 @@ +import paho.mqtt.client as mqtt +import serial +import time + + +class ShellerRPMDisplay: + mqtt_host = "172.22.112.94" + + COM_PORTS = [ "/dev/ttyACM0", "/dev/ttyUSB0" ] + buffer = "" + def init(self): + self.drum = 0 + self.paddle = 0 + try: + self.serials = [ + serial.Serial( + self.COM_PORTS[1], + 9600, + timeout=1, + stopbits=serial.STOPBITS_ONE, + bytesize=serial.EIGHTBITS + ), + serial.Serial( + self.COM_PORTS[0], + 9600, + timeout=1, + stopbits=serial.STOPBITS_ONE, + bytesize=serial.EIGHTBITS + ) + ] + self.mqtt_inform() + for s in self.serials: + s.write(bytearray("@D1\r\n", encoding='ascii')) + return True + except Exception as e: + print(e) + time.sleep(1) + if hasattr(self, "serials"): + del self.serials + return False + + def start(self): + self.isrunning = True + self.mqtt_inform() + + def stop(self): + self.isrunning = False + if hasattr(self, "serials"): + try: + for serial in self.serials: + serial.close() + except: + pass + del self.serials + + def main(self): + while self.isrunning: + if not hasattr(self, "serials"): + self.init() + try: + for i, serial in enumerate(self.serials): + if serial.in_waiting > 0: + self.buffer += serial.read(serial.in_waiting).decode( + "utf-8" + ) + print(self.buffer) + self.process_text(self.buffer[: self.buffer.index("\r")], i) + self.buffer = "" + except Exception as e: + print(e) + time.sleep(1) + + def mqtt_inform(self): + # Publish MQTT messages for Moisture Percentage + self.client = mqtt.Client() + self.client.connect(self.mqtt_host, 1883) + self.client.publish( + "homeassistant/sensor/sheller-paddle-rpm/config", + '{\ + "name": "Paddle RPM", \ + "state_topic": "homeassistant/sensor/sheller/state", \ + "unit_of_measurement": "RPM", \ + "value_template": "{{ value_json.paddle }}", \ + "unique_id": "sheller-paddle-rpm", \ + "device":{ \ + "identifiers": ["shelling-machine"], \ + "name": "Shelling Machine", \ + "manufacturer": "ME&E" \ + } \ + }', retain=True + ) + self.client.publish( + "homeassistant/sensor/sheller-drum-rpm/config", + '{\ + "name": "Drum RPM", \ + "state_topic": "homeassistant/sensor/sheller/state", \ + "unit_of_measurement": "RPM", \ + "value_template": "{{ value_json.drum }}", \ + "unique_id": "sheller-drum-rpm", \ + "device":{ \ + "identifiers": ["shelling-machine"] \ + } \ + }', retain=True + ) + + def process_text(self, text, index): + print(text) + if index == 0: + self.paddle = text + elif index == 1: + self.drum = text + + self.client.publish( + "homeassistant/sensor/sheller/state", + '{"paddle": "%s", "drum": "%s"}' + % (self.paddle, self.drum), retain=True + ) + +if __name__ == "__main__": + obj = ShellerRPMDisplay() + obj.start() + obj.main() diff --git a/mqtt_listener.service b/mqtt_listener.service new file mode 100644 index 0000000..20a9946 --- /dev/null +++ b/mqtt_listener.service @@ -0,0 +1,15 @@ +[Unit] +Description=MQTT Listener Service +After=network.target + +[Service] +ExecStart=/home/biqu/mqtt_start.sh +WorkingDirectory=/home/biqu +StandardOutput=inherit +StandardError=inherit +Restart=always +User=biqu + +[Install] +WantedBy=multi-user.target + diff --git a/mqtt_start.sh b/mqtt_start.sh new file mode 100755 index 0000000..dbfe86b --- /dev/null +++ b/mqtt_start.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +# navigate to the directory of the script +cd /home/biqu/screw_control/ + +# activate the python environment, if you're using one +# source /path_to_your_venv/bin/activate + +# run the script +/usr/bin/python3 mqtt_listener.py