-
Notifications
You must be signed in to change notification settings - Fork 775
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding circuitpython code for the xmas light iot switch project
- Loading branch information
1 parent
aa4d2f8
commit 268d946
Showing
1 changed file
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# SPDX-FileCopyrightText: 2024 Liz Clark for Adafruit Industries | ||
# | ||
# SPDX-License-Identifier: MIT | ||
|
||
import time | ||
import os | ||
import ssl | ||
import wifi | ||
import socketpool | ||
import microcontroller | ||
import board | ||
import digitalio | ||
import adafruit_requests | ||
from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError | ||
|
||
aio_username = os.getenv("ADAFRUIT_AIO_USERNAME") | ||
aio_key = os.getenv("ADAFRUIT_AIO_KEY") | ||
|
||
# buttons | ||
button = digitalio.DigitalInOut(board.D5) | ||
button.direction = digitalio.Direction.INPUT | ||
button.pull = digitalio.Pull.UP | ||
button_state = False | ||
|
||
lights_on = False | ||
|
||
print() | ||
print("Connecting to WiFi...") | ||
# connect to your SSID | ||
wifi.radio.connect(os.getenv('CIRCUITPY_WIFI_SSID'), os.getenv('CIRCUITPY_WIFI_PASSWORD')) | ||
print("Connected to WiFi!") | ||
|
||
pool = socketpool.SocketPool(wifi.radio) | ||
requests = adafruit_requests.Session(pool, ssl.create_default_context()) | ||
|
||
io = IO_HTTP(aio_username, aio_key, requests) | ||
|
||
try: | ||
feed_lights = io.get_feed("lights") | ||
except AdafruitIO_RequestError: | ||
feed_lights = io.create_new_feed("lights") | ||
|
||
while True: | ||
try: | ||
if not button.value and button_state: | ||
button_state = False | ||
if button.value and not button_state: | ||
print("pressed") | ||
lights_on = not lights_on | ||
io.send_data(feed_lights["key"], int(lights_on)) | ||
button_state = True | ||
except Exception as error: # pylint: disable=broad-except | ||
print(error) | ||
time.sleep(5) | ||
microcontroller.reset() |