Sheller MQTT app configuration

This commit is contained in:
biqu 2024-09-24 16:30:31 -04:00
commit a8bfba6a77
3 changed files with 147 additions and 0 deletions

122
mqtt_listener.py Normal file
View File

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

15
mqtt_listener.service Normal file
View File

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

10
mqtt_start.sh Executable file
View File

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