Open azidanit opened 1 year ago
import paho.mqtt.client as mqttClient import time import os import argparse
NUM_DEVICES = None
def execute_command(device, command): print(f"EXEC {device}") full_str_cmd = f"mpp-solar -p {device} -P PI30 -c {command}" os.system(full_str_cmd) time.sleep(0.1) os.system(full_str_cmd)
def on_message(client, userdata, message): print("Message received: ", message.payload) print("Message Topic: ", message.topic)
if message.topic.startswith("homeassistant/sensor/inverter"):
device_num = int(message.topic.split("_")[-1]) - 1
if 0 <= device_num < NUM_DEVICES:
device_name = DEVICE_NAMES[device_num]
execute_command(device_name, message.payload.decode())
def on_connect(client, userdata, flags, rc): if rc == 0: print("Connected to broker") global Connected Connected = True client.on_message = on_message for device_num in range(1, NUMDEVICES + 1): client.subscribe(f"homeassistant/sensor/inverter{device_num}") else: print("Connection failed")
Connected = False
parser = argparse.ArgumentParser(description="MQTT client script") parser.add_argument("--broker", required=True, help="Broker address") parser.add_argument("--port", required=True, type=int, help="Broker port") parser.add_argument("--user", required=True, help="Username") parser.add_argument("--password", required=True, help="Password") parser.add_argument("--devices", required=True, nargs="+", help="Device names")
args = parser.parse_args()
broker_address = args.broker port = args.port user = args.user password = args.password DEVICE_NAMES = args.devices NUM_DEVICES = len(DEVICE_NAMES)
client = mqttClient.Client("Python", clean_session=False) client.username_pw_set(user, password=password) client.on_connect = on_connect client.on_message = on_message
is_mqtt_connected = False
while not is_mqtt_connected: try: client.connect(broker_address, port=port) is_mqtt_connected = True except: print("Cannot connect to MQTT broker") time.sleep(1)
client.loop_forever()
while not Connected: time.sleep(0.1)
try: while True: time.sleep(1) except KeyboardInterrupt: print("Exiting") client.disconnect() client.loop_stop()
[Unit] Description=MQTT Client Service After=network.target
[Service] User=your_username Group=your_group WorkingDirectory=/path/to/script/directory ExecStart=/usr/bin/python \ /path/to/your/script.py \ --broker broker_address \ --port port \ --user username \ --password password \ --devices "/dev/hidraw0" "/dev/hidraw1" "/dev/ttyUSB0" Restart=always
[Install] WantedBy=multi-user.target
/etc/systemd/system/mqtt_client.service sudo systemctl daemon-reload sudo systemctl enable mqtt_client.service
import paho.mqtt.client as mqttClient import time import os
Define the devices (assuming device and port names are the same)
DEVICE_NAMES = [ "/dev/hidraw0", "/dev/hidraw1", "/dev/ttyUSB0" ]
NUM_DEVICES = len(DEVICE_NAMES)
def execute_command(device, command): print(f"EXEC {device}") full_str_cmd = f"mpp-solar -p {device} -P PI30 -c {command}" os.system(full_str_cmd) time.sleep(0.1) os.system(full_str_cmd)
def on_message(client, userdata, message): print("Message received: ", message.payload) print("Message Topic: ", message.topic)
def on_connect(client, userdata, flags, rc): if rc == 0: print("Connected to broker") global Connected Connected = True client.on_message = on_message for device_num in range(1, NUMDEVICES + 1): client.subscribe(f"homeassistant/sensor/inverter{device_num}") else: print("Connection failed")
Connected = False
broker_address = "192.168.1.102" port = 1883 user = "sonoff" password = "sonoff"
client = mqttClient.Client("Python", clean_session=False) client.username_pw_set(user, password=password) client.on_connect = on_connect client.on_message = on_message
is_mqtt_connected = False
while not is_mqtt_connected: try: client.connect(broker_address, port=port) is_mqtt_connected = True except: print("Cannot connect to MQTT broker") time.sleep(1)
client.loop_forever()
while not Connected: time.sleep(0.1)
try: while True: time.sleep(1) except KeyboardInterrupt: print("Exiting") client.disconnect() client.loop_stop()