Tilmandel

analog_led_strip_mqtt_control

Oct 20th, 2020
903
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import paho.mqtt.client as mqtt
  2. import time
  3. import pigpio
  4. from PIL import ImageColor
  5.  
  6.  
  7. def convert_to_rgb(color = "#000000"):
  8.         pi = pigpio.pi()
  9.         r,g,b = ImageColor.getrgb(color)
  10.         pi.set_PWM_dutycycle(18,r)
  11.         pi.set_PWM_dutycycle(27,g)
  12.         pi.set_PWM_dutycycle(4,b)
  13.  
  14.  
  15. def on_message(client,userdata,msg):
  16.         if msg.topic == 'led1':
  17.                 hex_color = msg.payload.decode('utf-8')
  18.                 #print(hex_color)
  19.                 convert_to_rgb(hex_color)
  20.  
  21.  
  22. broker_address = "192.168.1.21"
  23. client = mqtt.Client('ledzio_sub')
  24. client.on_message = on_message
  25. client.connect(broker_address)
  26. pi = pigpio.pi()
  27.  
  28. while True:
  29.         try:
  30.                 client.loop_start()
  31.                 topic = 'led1'
  32.                 client.subscribe(topic)
  33.                 client.loop_stop()
  34.         except:
  35.                 print("Exiting Application")
  36.                 exit(0)
  37.  
  38.  
RAW Paste Data