Skip to content

Commit 0e122d2

Browse files
authored
Merge pull request #53 from brentru/update-for-requests-s2
BREAKING Update for ESP32S2, CPython
2 parents bf20b69 + 1ebad05 commit 0e122d2

12 files changed

+351
-414
lines changed

adafruit_io/adafruit_io.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"""
3838
import time
3939
import json
40+
4041
from adafruit_io.adafruit_io_errors import (
4142
AdafruitIO_RequestError,
4243
AdafruitIO_ThrottleError,
@@ -466,18 +467,14 @@ class IO_HTTP:
466467
467468
:param str adafruit_io_username: Adafruit IO Username
468469
:param str adafruit_io_key: Adafruit IO Key
469-
:param wifi_manager: WiFiManager object from ESPSPI_WiFiManager or ESPAT_WiFiManager
470-
470+
:param requests: A passed adafruit_requests module.
471471
"""
472472

473-
def __init__(self, adafruit_io_username, adafruit_io_key, wifi_manager):
473+
def __init__(self, adafruit_io_username, adafruit_io_key, requests):
474474
self.username = adafruit_io_username
475475
self.key = adafruit_io_key
476-
wifi_type = str(type(wifi_manager))
477-
if "ESPSPI_WiFiManager" in wifi_type or "ESPAT_WiFiManager" in wifi_type:
478-
self.wifi = wifi_manager
479-
else:
480-
raise TypeError("This library requires a WiFiManager object.")
476+
self._http = requests
477+
481478
self._aio_headers = [
482479
{"X-AIO-KEY": self.key, "Content-Type": "application/json"},
483480
{"X-AIO-KEY": self.key},
@@ -528,7 +525,7 @@ def _post(self, path, payload):
528525
:param str path: Formatted Adafruit IO URL from _compose_path
529526
:param json payload: JSON data to send to Adafruit IO
530527
"""
531-
response = self.wifi.post(
528+
response = self._http.post(
532529
path, json=payload, headers=self._create_headers(self._aio_headers[0])
533530
)
534531
self._handle_error(response)
@@ -541,7 +538,7 @@ def _get(self, path):
541538
GET data from Adafruit IO
542539
:param str path: Formatted Adafruit IO URL from _compose_path
543540
"""
544-
response = self.wifi.get(
541+
response = self._http.get(
545542
path, headers=self._create_headers(self._aio_headers[1])
546543
)
547544
self._handle_error(response)
@@ -554,7 +551,7 @@ def _delete(self, path):
554551
DELETE data from Adafruit IO.
555552
:param str path: Formatted Adafruit IO URL from _compose_path
556553
"""
557-
response = self.wifi.delete(
554+
response = self._http.delete(
558555
path, headers=self._create_headers(self._aio_headers[0])
559556
)
560557
self._handle_error(response)

examples/adafruit_io_http/adafruit_io_analog_in.py

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,58 @@
1-
"""
2-
Example of reading an analog light sensor
3-
and sending the value to Adafruit IO
4-
"""
1+
# Example of publishing the value of an ADC to Adafruit IO
2+
# adafruit_circuitpython_adafruitio with an esp32spi_socket
53
import time
64
import board
75
import busio
8-
from digitalio import DigitalInOut
96
from analogio import AnalogIn
10-
from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager
11-
12-
# Import NeoPixel Library
13-
import neopixel
14-
15-
# Import Adafruit IO HTTP Client
7+
from digitalio import DigitalInOut
8+
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
9+
from adafruit_esp32spi import adafruit_esp32spi
10+
import adafruit_requests as requests
1611
from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError
1712

18-
# Delay between polling and sending light sensor data, in seconds
19-
SENSOR_DELAY = 30
20-
21-
# Get wifi details and more from a secrets.py file
13+
# Add a secrets.py to your filesystem that has a dictionary called secrets with "ssid" and
14+
# "password" keys with your WiFi credentials. DO NOT share that file or commit it into Git or other
15+
# source control.
16+
# pylint: disable=no-name-in-module,wrong-import-order
2217
try:
2318
from secrets import secrets
2419
except ImportError:
2520
print("WiFi secrets are kept in secrets.py, please add them there!")
2621
raise
2722

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

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

4748
# Set your Adafruit IO Username and Key in secrets.py
4849
# (visit io.adafruit.com if you need to create an account,
4950
# or if you need your Adafruit IO key.)
5051
aio_username = secrets["aio_username"]
5152
aio_key = secrets["aio_key"]
5253

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

5657
try:
5758
# Get the 'light' feed from Adafruit IO
@@ -60,12 +61,13 @@
6061
# If no 'light' feed exists, create one
6162
light_feed = io.create_new_feed("light")
6263

63-
# Set up an analog light sensor on the PyPortal
64-
adc = AnalogIn(board.LIGHT)
64+
# Set up an ADC
65+
adc = AnalogIn(board.A0)
6566

67+
SENSOR_DELAY = 30
6668
while True:
6769
light_value = adc.value
68-
print("Light Level: ", light_value)
70+
print("ADC Value: ", light_value)
6971
print("Sending to Adafruit IO...")
7072
io.send_data(light_feed["key"], light_value)
7173
print("Sent!")

examples/adafruit_io_http/adafruit_io_digital_out.py

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,57 @@
1-
"""
2-
Example of turning on and off a LED
3-
from an Adafruit IO Dashboard.
4-
"""
1+
# Turn on and off a LED from your Adafruit IO Dashboard.
2+
# adafruit_circuitpython_adafruitio with an esp32spi_socket
53
import time
64
import board
75
import busio
86
from digitalio import DigitalInOut, Direction
9-
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
7+
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
8+
from adafruit_esp32spi import adafruit_esp32spi
9+
import adafruit_requests as requests
1710
from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError
1811

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
try:
5557
# Get the 'digital' feed from Adafruit IO

examples/adafruit_io_http/adafruit_io_esp_at.py

Lines changed: 0 additions & 91 deletions
This file was deleted.

0 commit comments

Comments
 (0)