import paho.mqtt.client as mqtt import random class RgbLamp: def __init__(self, host, port = 1883): self.mqttc = mqtt.Client() self.mqttc.on_message = self.onMessage self.mqttc.on_connect = self.onConnect self.host = host self.port = port self.mqttc.connect_async(self.host, self.port, 60) self.mqttc.loop_start() self._nBeat = 0 def onMessage(self, mqttc, obj, msg): print(msg) def onConnect(self, client, userdata, flags, rc): print("Connected with result code "+str(rc)) self.mqttc.subscribe("Timer/#", 2) self.mqttc.subscribe("Pc/#", 2) def onBeat(self, fft): json = "{\"spot\":[{\"color\":" + str(0) + ",\"radius\":" + str(int(fft[1])*3+2) + ",\"pos\":" + str(10 + self._nBeat%4 * 10) + ",\"keep\":0}],\"speed\":40}" self._nBeat += 1 #print(" ------------------ " + json) self.mqttc.publish("RgbLamp/spot", json)