#!/usr/bin/env python3 import configparser import sys import time import paho.mqtt.client as mqtt def on_connect(client, userdata, flags, rc): print("Connected with result code " + str(rc)) client.subscribe("bigclown/node/lcd-thermostat:0/led/#") # The callback for when a PUBLISH message is received from the server. def on_message(client, userdata, msg): ptopic = msg.topic.split("/") topic = "/".join(ptopic[ptopic.index("led") + 1 :]) if topic == "effect/set": # Well for now just this way client.publish( "bigclown/node/power-controller:0/led-strip/-/effect/set", '{"type": "rainbow", "wait":50}', ) else: client.publish( "bigclown/node/power-controller:0/led-strip/-/" + topic, payload=msg.payload ) # def on_log(mqttc, obj, level, string): # print(string) def main(): cnf = configparser.ConfigParser() cnf.read(sys.argv[1]) cnf_mqtt = cnf["mqtt"] client = mqtt.Client() client.on_connect = on_connect client.on_message = on_message # client.on_log = on_log client.username_pw_set(cnf_mqtt["username"], cnf_mqtt["password"]) while True: try: client.connect("127.0.0.1", 1883, 60) break except ConnectionRefusedError: time.sleep(1) client.loop_forever() if __name__ == "__main__": main()