|
| 1 | +import network |
| 2 | +import requests |
| 3 | +from pimoroni_yukon import Yukon |
| 4 | +from pimoroni_yukon import SLOT2 as STRIP_SLOT |
| 5 | +from pimoroni_yukon import SLOT5 as RM2_SLOT |
| 6 | +from pimoroni_yukon.modules import LEDStripModule, RM2WirelessModule |
| 7 | + |
| 8 | + |
| 9 | +""" |
| 10 | +Obtain the current CheerLights colour from the internet and show it on an LED Strip connected to Yukon. |
| 11 | +For more information about CheerLights, visit: https://cheerlights.com/ |
| 12 | +
|
| 13 | +This example requires a secrets.py file to be on your board's file system with the credentials of your WiFi network. |
| 14 | +
|
| 15 | +Hold "Boot" to exit the program (can take up to 5 seconds). |
| 16 | +""" |
| 17 | + |
| 18 | +try: |
| 19 | + from secrets import WIFI_SSID, WIFI_PASSWORD |
| 20 | + if len(WIFI_SSID) == 0: |
| 21 | + raise ValueError("no WiFi network set. Open the 'secrets.py' file on your device to add your WiFi credentials") |
| 22 | +except ImportError: |
| 23 | + raise ImportError("no module named 'secrets'. Create a 'secrets.py' file on your device with your WiFi credentials") |
| 24 | + |
| 25 | + |
| 26 | +# Constants |
| 27 | +COLOUR_NAMES = ("R", "G", "B") |
| 28 | +CONNECTION_INTERVAL = 1.0 # The time to sleep between each connection check |
| 29 | +REQUEST_INTERVAL = 5.0 # The time to sleep between each internet request |
| 30 | +STRIP_TYPE = LEDStripModule.NEOPIXEL # Change to LEDStripModule.DOTSTAR for APA102 style strips |
| 31 | + # Two Neopixel strips can be driven too, by using LEDStripModule.DUAL_NEOPIXEL |
| 32 | +STRIP_PIO = 0 # The PIO system to use (0 or 1) to drive the strip(s) |
| 33 | +STRIP_SM = 0 # The State Machines (SM) to use to drive the strip(s) |
| 34 | +LEDS_PER_STRIP = 60 # How many LEDs are on the strip. If using DUAL_NEOPIXEL this can be a single value or a list or tuple |
| 35 | +BRIGHTNESS = 1.0 # The max brightness of the LEDs (only supported by APA102s) |
| 36 | + |
| 37 | +# Variables |
| 38 | +yukon = Yukon() # Create a new Yukon object |
| 39 | +leds = LEDStripModule(STRIP_TYPE, # Create a LEDStripModule object, with the details of the attached strip(s) |
| 40 | + STRIP_PIO, |
| 41 | + STRIP_SM, |
| 42 | + LEDS_PER_STRIP, |
| 43 | + BRIGHTNESS) |
| 44 | +wireless = RM2WirelessModule() # Create a RM2WirelessModule object |
| 45 | + |
| 46 | + |
| 47 | +# Wrap the code in a try block, to catch any exceptions (including KeyboardInterrupt) |
| 48 | +try: |
| 49 | + yukon.register_with_slot(leds, STRIP_SLOT) # Register the LEDStripModule object with the slot |
| 50 | + yukon.register_with_slot(wireless, RM2_SLOT) # Register the RM2WirelessModule object with the slot |
| 51 | + yukon.verify_and_initialise() # Verify that the modules are attached to Yukon, and initialise them |
| 52 | + |
| 53 | + wlan = network.WLAN(network.STA_IF) # Create a new network object for interacting with WiFi |
| 54 | + wlan.active(True) # Turn on WLAN communications |
| 55 | + |
| 56 | + # Connect to WLAN |
| 57 | + print(f"Connecting to network '{WIFI_SSID}'") |
| 58 | + wlan.connect(WIFI_SSID, WIFI_PASSWORD) |
| 59 | + |
| 60 | + # Wait until the connection is established |
| 61 | + while not wlan.isconnected(): |
| 62 | + print('Waiting for connection...') |
| 63 | + yukon.monitored_sleep(CONNECTION_INTERVAL) |
| 64 | + |
| 65 | + # Print out our IP address |
| 66 | + print(f'Connected on {wlan.ifconfig()[0]}') |
| 67 | + |
| 68 | + # Turn on power to the module slots and the LED strip |
| 69 | + yukon.enable_main_output() |
| 70 | + leds.enable() |
| 71 | + |
| 72 | + # Loop until the BOOT/USER button is pressed |
| 73 | + while not yukon.is_boot_pressed(): |
| 74 | + |
| 75 | + # Get the current CheerLights colour from the internet |
| 76 | + req = requests.get("http://api.thingspeak.com/channels/1417/field/2/last.json") |
| 77 | + json = req.json() |
| 78 | + req.close() |
| 79 | + |
| 80 | + # Use the second to get the colour components for the RGB output |
| 81 | + colour = tuple(int(json['field2'][i:i + 2], 16) for i in (1, 3, 5)) |
| 82 | + |
| 83 | + # Print out the Cheerlights colour |
| 84 | + for i in range(len(colour)): |
| 85 | + print(f"{COLOUR_NAMES[i]} = {colour[i]}", end=", ") |
| 86 | + print() |
| 87 | + |
| 88 | + # Apply the colour to all the LEDs and send them to the strip |
| 89 | + for led in range(leds.strip.num_leds()): |
| 90 | + leds.strip.set_rgb(led, *colour) |
| 91 | + leds.strip.update() |
| 92 | + |
| 93 | + # Monitor sensors for a number of seconds, recording the min, max, and average for each |
| 94 | + yukon.monitored_sleep(REQUEST_INTERVAL) |
| 95 | + |
| 96 | + |
| 97 | +finally: |
| 98 | + # Put the board back into a safe state, regardless of how the program may have ended |
| 99 | + yukon.reset() |
| 100 | + |
| 101 | + # Attempt to disconnect from WiFi |
| 102 | + try: |
| 103 | + wlan.disconnect() |
| 104 | + except Exception: |
| 105 | + pass |
0 commit comments