I want to run mosquitto sub command in a python script instead of typing it in raspberry pi terminal but I don't know how to do it. I need your help please
Below is a python script that prints the battery voltage and exits. You need to change the mqtt_broker for your installation. See at the bottom of this page on how to find the correct one:
from time import sleep import ssl import json import os from paho.mqtt.client import Client username = "your VRM email" password = "your VRM pasword" portal_id = "your VRM portal ID" mqtt_broker = "mqtt67.victronenergy.com" def on_message(client, userdata, message): val = json.loads(message.payload) print(val["value"]) client.loop_stop() client.disconnect() os._exit(0) def on_connect(client, userdata, rc, *args): client.subscribe("N/%s/system/0/Dc/Battery/Voltage" % portal_id) client = Client("P1") client.tls_set(cert_reqs=ssl.CERT_NONE) client.tls_insecure_set(True) client.username_pw_set(username, password=password) client.connect(mqtt_broker, port=8883) client.on_connect = on_connect client.on_message = on_message client.loop_start() while True: sleep(1)
31 People are following this question.