Advertisement
cymplecy

cheerlightsDemo for ESP32-C3FH4-RGB

Dec 28th, 2022 (edited)
1,795
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.20 KB | Software | 0 0
  1. # Cheerlights - all pixels
  2. # Uses the Cheerlights MQTT API to switch the LED colours to the
  3. # current RGB value from Cheerlights
  4. #
  5. # Update via a Tweet with the content "#cheerlights <colour>"
  6. # see https://cheerlights.com for more information
  7. #
  8. # blog: https://dev.to/andypiper/making-a-cheerdot-with-micropython-3ocf
  9. #  slight mods by Simon Walters @cymplecy@fosstodon.org
  10. # requires umqtt e.g.
  11. #
  12. # mpremote mip install umqtt.simple
  13. # mpremote mip install umqtt.robust
  14.  
  15. import machine
  16. import network
  17. from umqtt.robust import MQTTClient
  18. from machine import Pin
  19. from neopixel import NeoPixel
  20. import time
  21.  
  22. #from machine import WDT
  23. #wdt = WDT(timeout=5000)  # enable it with a timeout of 2s
  24. #wdt.feed()
  25.  
  26. # config network and broker
  27. my_ssid = "SKYEEF88"
  28. my_pass = "PFRNNXTYVM"
  29. cl_broker = "mqtt.cheerlights.com"
  30.  
  31. neopin = Pin(8, Pin.OUT)  # NeoPixel control on Pin 8
  32. pixels = 25  # we have 25 pixels, set as a constant here for loops
  33. np = NeoPixel(neopin, pixels)
  34.  
  35. # setup the status LED
  36. status_led = Pin(10, Pin.OUT)
  37. status_led.off()
  38. np.fill((0,0,0))
  39. np.write()
  40. def do_connect():
  41.     wlan = network.WLAN(network.STA_IF)
  42.     wlan.active(True)
  43.     wlan.config(dhcp_hostname=client_name())
  44.     if not wlan.isconnected():
  45.         status_led.off()
  46.         print("connecting to network...")
  47.         wlan.connect(my_ssid, my_pass)
  48.         while not wlan.isconnected():
  49.             status_led.on()
  50.             time.sleep(0.5)
  51.             status_led.off()
  52.             time.sleep(0.5)
  53.             #wdt.feed()
  54.     status_led.on()  # status LED shows connected
  55.     print("network config:", wlan.ifconfig())
  56.  
  57.  
  58. def cl_callback(topic, payload):
  59.     cl_rgb = hex_to_rgb(payload)
  60.     print("message received: " + str(cl_rgb))
  61.     light_up(cl_rgb)
  62.  
  63.  
  64. def hex_to_rgb(hex_string):
  65.     str_len = len(hex_string)
  66.     if hex_string.startswith("#"):
  67.         if str_len == 7:
  68.             r_hex = hex_string[1:3]
  69.             g_hex = hex_string[3:5]
  70.             b_hex = hex_string[5:7]
  71.         elif str_len == 4:
  72.             r_hex = hex_string[1:2] * 2
  73.             g_hex = hex_string[2:3] * 2
  74.             b_hex = hex_string[3:4] * 2
  75.     elif str_len == 3:
  76.         r_hex = hex_string[0:1] * 2
  77.         g_hex = hex_string[1:2] * 2
  78.         b_hex = hex_string[2:3] * 2
  79.     else:
  80.         r_hex = hex_string[0:2]
  81.         g_hex = hex_string[2:4]
  82.         b_hex = hex_string[4:6]
  83.  
  84.     return (int(r_hex, 16) >> 5), (int(g_hex, 16) >> 5), (int(b_hex, 16) >> 5)
  85.  
  86.  
  87. def client_name():
  88.     m_id = machine.unique_id()
  89.     client_id = (
  90.         "{:02x}{:02x}{:02x}{:02x}".format(m_id[0], m_id[1], m_id[2], m_id[3])
  91.         + "-cl-dot"
  92.     )
  93.     return client_id
  94.  
  95.  
  96. def light_up(rgb):
  97.     # Set all Neopixels to colour
  98.     np.fill(rgb)
  99.     np.write()
  100.  
  101.  
  102. # the main code
  103. # connect to network and broker and subscribe to Cheerlights RGB topic
  104.  
  105. do_connect()
  106. mqtt = MQTTClient(client_name(), cl_broker)
  107. mqtt.connect()
  108. mqtt.set_callback(cl_callback)
  109. mqtt.subscribe("cheerlightsRGB")
  110.  
  111. count = 0;
  112. while True:
  113.     mqtt.check_msg()
  114.     if (count == 2000):
  115.         status_led.on()
  116.         #wdt.feed()
  117.     if (count == 4000):
  118.         status_led.off()
  119.         #wdt.feed()
  120.         count = 0
  121.     count += 1
  122.    
  123.    
  124.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement