The code:
(Other people helped me)
#
import time
import sys
import paho.mqtt.client as mqtt
import blinkt
#blinkt.set_clear_on_exit()
def on_connect(client, userdata, flags, rc):
print("Connected:", rc)
client.subscribe("TELEPI_LOCAL/tele_led/#", 0)
def on_subscribe(client, userdata, mid, granted_qos):
print ('Subscribed:', userdata, mid, granted_qos)
print ('We are here, waiting for commands...')
def on_message(client, userdata, msg):
global abort
global blinkt
print ("===============================")
print ("New loop invocation")
my_string = (msg.payload.decode("utf-8"))
#my_string = (message.payload.decode("utf-8"))
print ("my_String")
print (my_string)
dummy = my_string.split(",")
print ("Dummy ",dummy)
# [int(x) & 0xff for x in data]
command = dummy[0]
print ("Command ",command)
if command == 'Heartbeat':
print ("I am alive")
client.publish("TELEPI_LOCAL/tele_led/led-response", "Script is alive")
if command == 'Stop':
print ("Script terminates..")
client.publish("TELEPI_LOCAL/tele_led/led-response", "Script is stopping")
blinkt.set_all(0, 0, 0)
blinkt.show()
abort = True
print (len(dummy))
#
if command == 'rgb' and len(dummy) == 5:
pixel = dummy[1]
red = dummy[2]
green = dummy[3]
blue = dummy[4]
pixel = int(pixel)
if pixel > 8: pixel = 0
red = int(red)
green = int(green)
blue = int(blue)
print("--== values ==--")
print ("Command >",command,"<")
print ("LED ", pixel)
print ("Red ", red)
print ("Green ",green)
print ("Blue ",blue)
print ("Data length ",len(dummy))
if pixel == 8:
print ("Oops")
blinkt.set_all(red,green,blue)
blinkt.show()
# for x in range(blinkt.NUM_PIXELS):
# blinkt.set_pixel(pixel,red,greed,blue)
else:
print ("setting pixel")
print ("Led ",pixel," Red ",red," Green ",green," Blue ",blue)
blinkt.set_pixel(pixel,red,green,blue)
print("Done")
blinkt.show()
return
#
# if command == "dim":
# inits
#mqtt_broker = "192.168.0.99"
mqtt_broker = "127.0.0.1"
mqtt_port = 1883
abort = False
#client = mqtt.Mosquitto()
client = mqtt.Client("LED blink routine")
client.on_connect = on_connect
client.on_message = on_message
client.on_subscribe = on_subscribe
resp = client.connect(mqtt_broker, mqtt_port, 60)
client.loop_start()
while abort == False:
time.sleep(1)
client.loop_stop()
client.disconnect()
del client
print ('Script has terminated')
exit(0)
Let me know if you need help on it.
SORRY, I posted the wrong code.
Changed.
Look again!