Skip to content

Commit 8a8428e

Browse files
authored
Merge branch 'master' into pb-moon-clock
2 parents 84d279d + 342671c commit 8a8428e

File tree

6 files changed

+867
-4
lines changed

6 files changed

+867
-4
lines changed

Adafruit_IO_Schedule_Trigger/code.py

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
import time
2+
import board
3+
import busio
4+
from digitalio import DigitalInOut
5+
from adafruit_esp32spi import adafruit_esp32spi
6+
from adafruit_esp32spi import adafruit_esp32spi_wifimanager
7+
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
8+
import neopixel
9+
import adafruit_minimqtt.adafruit_minimqtt as MQTT
10+
from adafruit_io.adafruit_io import IO_MQTT
11+
12+
### WiFi ###
13+
14+
# Get wifi details and more from a secrets.py file
15+
try:
16+
from secrets import secrets
17+
except ImportError:
18+
print("WiFi secrets are kept in secrets.py, please add them there!")
19+
raise
20+
21+
# If you are using a board with pre-defined ESP32 Pins:
22+
esp32_cs = DigitalInOut(board.ESP_CS)
23+
esp32_ready = DigitalInOut(board.ESP_BUSY)
24+
esp32_reset = DigitalInOut(board.ESP_RESET)
25+
26+
# If you have an externally connected ESP32:
27+
# esp32_cs = DigitalInOut(board.D9)
28+
# esp32_ready = DigitalInOut(board.D10)
29+
# esp32_reset = DigitalInOut(board.D5)
30+
31+
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
32+
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
33+
"""Use below for Most Boards"""
34+
status_light = neopixel.NeoPixel(
35+
board.NEOPIXEL, 1, brightness=0.2
36+
) # Uncomment for Most Boards
37+
"""Uncomment below for ItsyBitsy M4"""
38+
# status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
39+
# Uncomment below for an externally defined RGB LED
40+
# import adafruit_rgbled
41+
# from adafruit_esp32spi import PWMOut
42+
# RED_LED = PWMOut.PWMOut(esp, 26)
43+
# GREEN_LED = PWMOut.PWMOut(esp, 27)
44+
# BLUE_LED = PWMOut.PWMOut(esp, 25)
45+
# status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
46+
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
47+
48+
# Set up a pin for controlling the relay
49+
power_pin = DigitalInOut(board.D3)
50+
power_pin.switch_to_output()
51+
52+
# Define callback functions which will be called when certain events happen.
53+
# pylint: disable=unused-argument
54+
def connected(client):
55+
# Connected function will be called when the client is connected to Adafruit IO.
56+
# This is a good place to subscribe to feed changes. The client parameter
57+
# passed to this function is the Adafruit IO MQTT client so you can make
58+
# calls against it easily.
59+
print("Connected to Adafruit IO!")
60+
61+
62+
def subscribe(client, userdata, topic, granted_qos):
63+
# This method is called when the client subscribes to a new feed.
64+
print("Listening for changes on relay feed...")
65+
66+
67+
def unsubscribe(client, userdata, topic, pid):
68+
# This method is called when the client unsubscribes from a feed.
69+
print("Unsubscribed from {0} with PID {1}".format(topic, pid))
70+
71+
72+
# pylint: disable=unused-argument
73+
def disconnected(client):
74+
# Disconnected function will be called when the client disconnects.
75+
print("Disconnected from Adafruit IO!")
76+
77+
78+
# pylint: disable=unused-argument
79+
def on_message(client, feed_id, payload):
80+
# Message function will be called when a subscribed feed has a new value.
81+
# The feed_id parameter identifies the feed, and the payload parameter has
82+
# the new value.
83+
print("Feed {0} received new value: {1}".format(feed_id, payload))
84+
85+
86+
def on_relay_msg(client, topic, message):
87+
# Method called whenever user/feeds/relay has a new value
88+
if message == "morning":
89+
print("Morning - turning outlet ON")
90+
power_pin.value = True
91+
elif message == "night":
92+
print("Night - turning outlet OFF")
93+
power_pin.value = False
94+
else:
95+
print("Unexpected value received on relay feed.")
96+
97+
98+
# Connect to WiFi
99+
print("Connecting to WiFi...")
100+
wifi.connect()
101+
print("Connected!")
102+
103+
# Initialize MQTT interface with the esp interface
104+
MQTT.set_socket(socket, esp)
105+
106+
# Initialize a new MQTT Client object
107+
mqtt_client = MQTT.MQTT(
108+
broker="io.adafruit.com",
109+
username=secrets["aio_username"],
110+
password=secrets["aio_key"],
111+
)
112+
113+
# Initialize an Adafruit IO MQTT Client
114+
io = IO_MQTT(mqtt_client)
115+
116+
# Connect the callback methods defined above to Adafruit IO
117+
io.on_connect = connected
118+
io.on_disconnect = disconnected
119+
io.on_subscribe = subscribe
120+
io.on_unsubscribe = unsubscribe
121+
io.on_message = on_message
122+
123+
# Connect to Adafruit IO
124+
print("Connecting to Adafruit IO...")
125+
io.connect()
126+
127+
# Set up a message handler for the relay feed
128+
io.add_feed_callback("relay", on_relay_msg)
129+
130+
# Subscribe to all messages on the relay feed
131+
io.subscribe("relay")
132+
133+
# Get the most recent value on the relay feed
134+
io.get("relay")
135+
136+
# Start a blocking loop to check for new messages
137+
while True:
138+
try:
139+
io.loop()
140+
except (ValueError, RuntimeError) as e:
141+
print("Failed to get data, retrying\n", e)
142+
wifi.reset()
143+
io.reconnect()
144+
continue
145+
time.sleep(0.5)

Bluetooth_Room_Lights/code.py

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
"""
2+
Bluetooth Controlled Room Lights using a Circuit Playground Bluetooth
3+
Scroll between 7 modes and control brightness with your smartphone via Bluetooth
4+
Full tutorial: https://learn.adafruit.com/easy-no-solder-bluetooth-controlled-room-lights/overview
5+
Code by Kattni Rembor & Erin St Blaine for Adafruit Industries
6+
Adafruit invests time and resources to bring you this code! Please support our shop!
7+
"""
8+
9+
# pylint: disable=attribute-defined-outside-init
10+
# pylint: disable=too-few-public-methods
11+
12+
import board
13+
import neopixel
14+
from adafruit_led_animation.animation.solid import Solid
15+
from adafruit_led_animation.animation.comet import Comet
16+
from adafruit_led_animation.animation.rainbow import Rainbow
17+
from adafruit_led_animation.animation.rainbowcomet import RainbowComet
18+
from adafruit_led_animation.animation.sparkle import Sparkle
19+
from adafruit_led_animation.animation.sparklepulse import SparklePulse
20+
from adafruit_led_animation.sequence import AnimationSequence
21+
from adafruit_led_animation.group import AnimationGroup
22+
from adafruit_led_animation.animation import Animation
23+
from adafruit_led_animation.sequence import AnimateOnce
24+
from adafruit_led_animation.color import (
25+
AMBER,
26+
ORANGE,
27+
WHITE,
28+
RED,
29+
BLACK,
30+
colorwheel,
31+
)
32+
33+
34+
from adafruit_ble import BLERadio
35+
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
36+
from adafruit_ble.services.nordic import UARTService
37+
38+
from adafruit_bluefruit_connect.packet import Packet
39+
from adafruit_bluefruit_connect.button_packet import ButtonPacket
40+
from adafruit_bluefruit_connect.color_packet import ColorPacket
41+
42+
NUM_LEDS = 240 # change to reflect your LED strip
43+
NEOPIXEL_PIN = board.A1 # change to reflect your wiring
44+
45+
# Declare a NeoPixel object on NEOPIXEL_PIN with NUM_LEDS pixels,
46+
# no auto-write.
47+
# Set brightness to max, we'll control it later in the code
48+
pixels = neopixel.NeoPixel(NEOPIXEL_PIN, NUM_LEDS, brightness=1.0,
49+
auto_write=False,
50+
#pixel_order=(1,0,2,3) #uncomment if using RGBW NeoPixels
51+
)
52+
53+
54+
ble = BLERadio()
55+
uart_service = UARTService()
56+
advertisement = ProvideServicesAdvertisement(uart_service)
57+
58+
class RainbowFade(Animation):
59+
''' fades the entire strip through the whole spectrum '''
60+
_color_index = 150 # choose start color (0-255)
61+
def __init__(self, pixel_object, speed, name): # define animation
62+
super().__init__(pixel_object, speed=speed, color=WHITE, name=name)
63+
64+
def draw(self): # draw the animation
65+
''' fades the entire strip through the whole spectrum '''
66+
self.color = colorwheel(self._color_index + 1)
67+
self._color_index = (self._color_index + 1) % 256
68+
self.fill(self.color)
69+
70+
# ANIMATION DEFINITIONS --
71+
# create as many animations as you'd like and define their attributes here.
72+
# They can be a single line or a group of animations - the groups will play
73+
# at the same time, overlaid on top of each other.
74+
75+
76+
readingLight = Solid(pixels, color=0xFF7D13) #warm white color HEX code
77+
brightWhite = Solid(pixels, color=(150, 150, 150))
78+
rainbow = Rainbow(pixels, speed=0.1, period=10, step=0.5)
79+
rainbowfade = RainbowFade(pixels, speed=0.4, name="rainbowfade")
80+
powerup = RainbowComet(pixels, speed=0, tail_length=50, bounce=False)
81+
off = Solid(pixels, color=BLACK)
82+
83+
#startup animation will play just once
84+
startup = AnimateOnce(powerup)
85+
86+
#starrynight and fire are animation groups with layered effects.
87+
starrynight = AnimationGroup(
88+
SparklePulse(pixels, speed=0.01, color=(0, 0, 150), period=1),
89+
Comet(pixels, speed=0, tail_length=8, color=(150, 150, 150), bounce=False),)
90+
91+
fire = AnimationGroup(
92+
Comet(pixels, speed=0, tail_length=1, color=BLACK),
93+
Sparkle(pixels, speed=0.05, num_sparkles=10, color=AMBER),
94+
Sparkle(pixels, speed=0.05, num_sparkles=10, color=RED),
95+
Sparkle(pixels, speed=0.05, num_sparkles=20, color=ORANGE),
96+
Sparkle(pixels, speed=0.05, num_sparkles=5, color=0xFF7D13),
97+
Sparkle(pixels, speed=0.05, num_sparkles=10, color=BLACK),
98+
)
99+
100+
# Here is the animation playlist where you set the order of modes
101+
102+
animations = AnimationSequence(
103+
readingLight,
104+
fire,
105+
rainbow,
106+
starrynight,
107+
rainbowfade,
108+
brightWhite,
109+
auto_clear=True,
110+
)
111+
112+
113+
114+
MODE = 0
115+
116+
while True:
117+
if MODE == 0: # If currently off...
118+
startup.animate()
119+
while startup.animate():
120+
pass
121+
MODE = 1
122+
# Advertise when not connected
123+
124+
elif MODE >= 1: # If not OFF MODE...
125+
ble.start_advertising(advertisement)
126+
while not ble.connected:
127+
if MODE == 2:
128+
pass
129+
elif MODE == 1:
130+
animations.animate()
131+
# Now we're connected
132+
133+
while ble.connected:
134+
if uart_service.in_waiting:
135+
packet = Packet.from_stream(uart_service)
136+
# Color Picker Functionality
137+
if isinstance(packet, ColorPacket):
138+
MODE = 2
139+
# Set all the pixels to one color and stay there.
140+
pixels.fill(packet.color)
141+
pixels.show()
142+
# Control Pad Functionality
143+
elif isinstance(packet, ButtonPacket):
144+
if packet.pressed:
145+
if packet.button == ButtonPacket.BUTTON_1:
146+
MODE = 1
147+
animations.activate(1)
148+
elif packet.button == ButtonPacket.BUTTON_2:
149+
MODE = 1
150+
animations.activate(2)
151+
elif packet.button == ButtonPacket.BUTTON_3:
152+
MODE = 1
153+
animations.activate(3)
154+
elif packet.button == ButtonPacket.BUTTON_4:
155+
MODE = 1
156+
animations.activate(4)
157+
# change the mode with right arrow
158+
elif packet.button == ButtonPacket.RIGHT:
159+
MODE = 1
160+
animations.next()
161+
elif packet.button == ButtonPacket.LEFT:
162+
MODE = 4
163+
off.animate()
164+
#change the brightness with up and down arrows
165+
elif packet.button == ButtonPacket.UP:
166+
pixels.brightness = pixels.brightness + 0.1
167+
pixels.show()
168+
if pixels.brightness > 1:
169+
pixels.brightness = 1
170+
elif packet.button == ButtonPacket.DOWN:
171+
pixels.brightness = pixels.brightness - 0.1
172+
pixels.show()
173+
if pixels.brightness < 0.1:
174+
pixels.brightness = 0.1
175+
if MODE == 1:
176+
animations.animate()
177+
if MODE == 4:
178+
animations.freeze()

CLUE_Egg_Drop/clue_egg_drop.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import board
44
from digitalio import DigitalInOut, Direction, Pull
55
import pulseio
6-
from adafruit_lsm6ds import LSM6DS33, AccelRange, AccelHPF, Rate
6+
from adafruit_lsm6ds.lsm6ds33 import LSM6DS33
7+
from adafruit_lsm6ds import AccelRange, AccelHPF, Rate
78
from adafruit_display_text import label
89
import displayio
910
import terminalio

0 commit comments

Comments
 (0)