Skip to content

Commit 215b5a4

Browse files
author
brentru
committed
add randomizer service
1 parent 8db9407 commit 215b5a4

File tree

1 file changed

+38
-41
lines changed

1 file changed

+38
-41
lines changed
Lines changed: 38 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,57 @@
1-
"""
2-
Example of using Adafruit IO's
3-
random data service.
4-
"""
1+
# Example for using Adafruit IO's random data (randomizer) service
2+
# adafruit_circuitpython_adafruitio with an esp32spi_socket
53
import time
64
import board
75
import busio
86
from digitalio import DigitalInOut
7+
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
8+
from adafruit_esp32spi import adafruit_esp32spi
9+
import adafruit_requests as requests
10+
from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError
911

10-
# ESP32 SPI
11-
from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager
12-
13-
# Import NeoPixel Library
14-
import neopixel
15-
16-
# Import Adafruit IO HTTP Client
17-
from adafruit_io.adafruit_io import IO_HTTP
18-
19-
# Get wifi details and more from a secrets.py file
12+
# Add a secrets.py to your filesystem that has a dictionary called secrets with "ssid" and
13+
# "password" keys with your WiFi credentials. DO NOT share that file or commit it into Git or other
14+
# source control.
15+
# pylint: disable=no-name-in-module,wrong-import-order
2016
try:
2117
from secrets import secrets
2218
except ImportError:
2319
print("WiFi secrets are kept in secrets.py, please add them there!")
2420
raise
2521

26-
# ESP32 Setup
27-
try:
28-
esp32_cs = DigitalInOut(board.ESP_CS)
29-
esp32_ready = DigitalInOut(board.ESP_BUSY)
30-
esp32_reset = DigitalInOut(board.ESP_RESET)
31-
except AttributeError:
32-
esp32_cs = DigitalInOut(board.D9)
33-
esp32_ready = DigitalInOut(board.D10)
34-
esp32_reset = DigitalInOut(board.D5)
22+
# If you are using a board with pre-defined ESP32 Pins:
23+
esp32_cs = DigitalInOut(board.ESP_CS)
24+
esp32_ready = DigitalInOut(board.ESP_BUSY)
25+
esp32_reset = DigitalInOut(board.ESP_RESET)
26+
27+
# If you have an externally connected ESP32:
28+
# esp32_cs = DigitalInOut(board.D9)
29+
# esp32_ready = DigitalInOut(board.D10)
30+
# esp32_reset = DigitalInOut(board.D5)
3531

3632
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
3733
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
38-
status_light = neopixel.NeoPixel(
39-
board.NEOPIXEL, 1, brightness=0.2
40-
) # Uncomment for Most Boards
41-
"""Uncomment below for ItsyBitsy M4"""
42-
# status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
43-
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
34+
35+
print("Connecting to AP...")
36+
while not esp.is_connected:
37+
try:
38+
esp.connect_AP(secrets["ssid"], secrets["password"])
39+
except RuntimeError as e:
40+
print("could not connect to AP, retrying: ", e)
41+
continue
42+
print("Connected to", str(esp.ssid, "utf-8"), "\tRSSI:", esp.rssi)
43+
44+
socket.set_interface(esp)
45+
requests.set_socket(socket, esp)
4446

4547
# Set your Adafruit IO Username and Key in secrets.py
4648
# (visit io.adafruit.com if you need to create an account,
4749
# or if you need your Adafruit IO key.)
4850
aio_username = secrets["aio_username"]
4951
aio_key = secrets["aio_key"]
5052

51-
# Create an instance of the Adafruit IO HTTP client
52-
io = IO_HTTP(aio_username, aio_key, wifi)
53+
# Initialize an Adafruit IO HTTP API object
54+
io = IO_HTTP(aio_username, aio_key, requests)
5355

5456
# Random Data ID
5557
# (to obtain this value, visit
@@ -58,14 +60,9 @@
5860
random_data_id = 1234
5961

6062
while True:
61-
try:
62-
print("Fetching random data from Adafruit IO...")
63-
random_data = io.receive_random_data(random_data_id)
64-
print("Random Data: ", random_data["value"])
65-
print("Data Seed: ", random_data["seed"])
66-
print("Waiting 1 minute to fetch new randomized data...")
67-
except (ValueError, RuntimeError) as e:
68-
print("Failed to get data, retrying\n", e)
69-
wifi.reset()
70-
continue
63+
print("Fetching random data from Adafruit IO...")
64+
random_data = io.receive_random_data(random_data_id)
65+
print("Random Data: ", random_data["value"])
66+
print("Data Seed: ", random_data["seed"])
67+
print("Waiting 1 minute to fetch new randomized data...")
7168
time.sleep(60)

0 commit comments

Comments
 (0)