Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Cheerlights - all pixels
- # Uses the Cheerlights MQTT API to switch the LED colours to the
- # current RGB value from Cheerlights
- #
- # Update via a Tweet with the content "#cheerlights <colour>"
- # see https://cheerlights.com for more information
- #
- # blog: https://dev.to/andypiper/making-a-cheerdot-with-micropython-3ocf
- # slight mods by Simon Walters @cymplecy@fosstodon.org
- # requires umqtt e.g.
- #
- # mpremote mip install umqtt.simple
- # mpremote mip install umqtt.robust
- import machine
- import network
- from umqtt.robust import MQTTClient
- from machine import Pin
- from neopixel import NeoPixel
- import time
- #from machine import WDT
- #wdt = WDT(timeout=5000) # enable it with a timeout of 2s
- #wdt.feed()
- # config network and broker
- my_ssid = "SKYEEF88"
- my_pass = "PFRNNXTYVM"
- cl_broker = "mqtt.cheerlights.com"
- neopin = Pin(8, Pin.OUT) # NeoPixel control on Pin 8
- pixels = 25 # we have 25 pixels, set as a constant here for loops
- np = NeoPixel(neopin, pixels)
- # setup the status LED
- status_led = Pin(10, Pin.OUT)
- status_led.off()
- np.fill((0,0,0))
- np.write()
- def do_connect():
- wlan = network.WLAN(network.STA_IF)
- wlan.active(True)
- wlan.config(dhcp_hostname=client_name())
- if not wlan.isconnected():
- status_led.off()
- print("connecting to network...")
- wlan.connect(my_ssid, my_pass)
- while not wlan.isconnected():
- status_led.on()
- time.sleep(0.5)
- status_led.off()
- time.sleep(0.5)
- #wdt.feed()
- status_led.on() # status LED shows connected
- print("network config:", wlan.ifconfig())
- def cl_callback(topic, payload):
- cl_rgb = hex_to_rgb(payload)
- print("message received: " + str(cl_rgb))
- light_up(cl_rgb)
- def hex_to_rgb(hex_string):
- str_len = len(hex_string)
- if hex_string.startswith("#"):
- if str_len == 7:
- r_hex = hex_string[1:3]
- g_hex = hex_string[3:5]
- b_hex = hex_string[5:7]
- elif str_len == 4:
- r_hex = hex_string[1:2] * 2
- g_hex = hex_string[2:3] * 2
- b_hex = hex_string[3:4] * 2
- elif str_len == 3:
- r_hex = hex_string[0:1] * 2
- g_hex = hex_string[1:2] * 2
- b_hex = hex_string[2:3] * 2
- else:
- r_hex = hex_string[0:2]
- g_hex = hex_string[2:4]
- b_hex = hex_string[4:6]
- return (int(r_hex, 16) >> 5), (int(g_hex, 16) >> 5), (int(b_hex, 16) >> 5)
- def client_name():
- m_id = machine.unique_id()
- client_id = (
- "{:02x}{:02x}{:02x}{:02x}".format(m_id[0], m_id[1], m_id[2], m_id[3])
- + "-cl-dot"
- )
- return client_id
- def light_up(rgb):
- # Set all Neopixels to colour
- np.fill(rgb)
- np.write()
- # the main code
- # connect to network and broker and subscribe to Cheerlights RGB topic
- do_connect()
- mqtt = MQTTClient(client_name(), cl_broker)
- mqtt.connect()
- mqtt.set_callback(cl_callback)
- mqtt.subscribe("cheerlightsRGB")
- count = 0;
- while True:
- mqtt.check_msg()
- if (count == 2000):
- status_led.on()
- #wdt.feed()
- if (count == 4000):
- status_led.off()
- #wdt.feed()
- count = 0
- count += 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement