Skip to content

Commit

Permalink
Merge pull request #53 from brentru/update-for-requests-s2
Browse files Browse the repository at this point in the history
BREAKING Update for ESP32S2, CPython
  • Loading branch information
brentru committed Nov 20, 2020
2 parents bf20b69 + 1ebad05 commit 0e122d2
Show file tree
Hide file tree
Showing 12 changed files with 351 additions and 414 deletions.
19 changes: 8 additions & 11 deletions adafruit_io/adafruit_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"""
import time
import json

from adafruit_io.adafruit_io_errors import (
AdafruitIO_RequestError,
AdafruitIO_ThrottleError,
Expand Down Expand Up @@ -466,18 +467,14 @@ class IO_HTTP:
:param str adafruit_io_username: Adafruit IO Username
:param str adafruit_io_key: Adafruit IO Key
:param wifi_manager: WiFiManager object from ESPSPI_WiFiManager or ESPAT_WiFiManager
:param requests: A passed adafruit_requests module.
"""

def __init__(self, adafruit_io_username, adafruit_io_key, wifi_manager):
def __init__(self, adafruit_io_username, adafruit_io_key, requests):
self.username = adafruit_io_username
self.key = adafruit_io_key
wifi_type = str(type(wifi_manager))
if "ESPSPI_WiFiManager" in wifi_type or "ESPAT_WiFiManager" in wifi_type:
self.wifi = wifi_manager
else:
raise TypeError("This library requires a WiFiManager object.")
self._http = requests

self._aio_headers = [
{"X-AIO-KEY": self.key, "Content-Type": "application/json"},
{"X-AIO-KEY": self.key},
Expand Down Expand Up @@ -528,7 +525,7 @@ def _post(self, path, payload):
:param str path: Formatted Adafruit IO URL from _compose_path
:param json payload: JSON data to send to Adafruit IO
"""
response = self.wifi.post(
response = self._http.post(
path, json=payload, headers=self._create_headers(self._aio_headers[0])
)
self._handle_error(response)
Expand All @@ -541,7 +538,7 @@ def _get(self, path):
GET data from Adafruit IO
:param str path: Formatted Adafruit IO URL from _compose_path
"""
response = self.wifi.get(
response = self._http.get(
path, headers=self._create_headers(self._aio_headers[1])
)
self._handle_error(response)
Expand All @@ -554,7 +551,7 @@ def _delete(self, path):
DELETE data from Adafruit IO.
:param str path: Formatted Adafruit IO URL from _compose_path
"""
response = self.wifi.delete(
response = self._http.delete(
path, headers=self._create_headers(self._aio_headers[0])
)
self._handle_error(response)
Expand Down
72 changes: 37 additions & 35 deletions examples/adafruit_io_http/adafruit_io_analog_in.py
Original file line number Diff line number Diff line change
@@ -1,57 +1,58 @@
"""
Example of reading an analog light sensor
and sending the value to Adafruit IO
"""
# Example of publishing the value of an ADC to Adafruit IO
# adafruit_circuitpython_adafruitio with an esp32spi_socket
import time
import board
import busio
from digitalio import DigitalInOut
from analogio import AnalogIn
from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager

# Import NeoPixel Library
import neopixel

# Import Adafruit IO HTTP Client
from digitalio import DigitalInOut
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
from adafruit_esp32spi import adafruit_esp32spi
import adafruit_requests as requests
from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError

# Delay between polling and sending light sensor data, in seconds
SENSOR_DELAY = 30

# Get wifi details and more from a secrets.py file
# Add a secrets.py to your filesystem that has a dictionary called secrets with "ssid" and
# "password" keys with your WiFi credentials. DO NOT share that file or commit it into Git or other
# source control.
# pylint: disable=no-name-in-module,wrong-import-order
try:
from secrets import secrets
except ImportError:
print("WiFi secrets are kept in secrets.py, please add them there!")
raise

# ESP32 Setup
try:
esp32_cs = DigitalInOut(board.ESP_CS)
esp32_ready = DigitalInOut(board.ESP_BUSY)
esp32_reset = DigitalInOut(board.ESP_RESET)
except AttributeError:
esp32_cs = DigitalInOut(board.D9)
esp32_ready = DigitalInOut(board.D10)
esp32_reset = DigitalInOut(board.D5)
# If you are using a board with pre-defined ESP32 Pins:
esp32_cs = DigitalInOut(board.ESP_CS)
esp32_ready = DigitalInOut(board.ESP_BUSY)
esp32_reset = DigitalInOut(board.ESP_RESET)

# If you have an externally connected ESP32:
# esp32_cs = DigitalInOut(board.D9)
# esp32_ready = DigitalInOut(board.D10)
# esp32_reset = DigitalInOut(board.D5)

spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
status_light = neopixel.NeoPixel(
board.NEOPIXEL, 1, brightness=0.2
) # Uncomment for Most Boards
"""Uncomment below for ItsyBitsy M4"""
# status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)

print("Connecting to AP...")
while not esp.is_connected:
try:
esp.connect_AP(secrets["ssid"], secrets["password"])
except RuntimeError as e:
print("could not connect to AP, retrying: ", e)
continue
print("Connected to", str(esp.ssid, "utf-8"), "\tRSSI:", esp.rssi)

socket.set_interface(esp)
requests.set_socket(socket, esp)

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

# Create an instance of the Adafruit IO HTTP client
io = IO_HTTP(aio_username, aio_key, wifi)
# Initialize an Adafruit IO HTTP API object
io = IO_HTTP(aio_username, aio_key, requests)

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

# Set up an analog light sensor on the PyPortal
adc = AnalogIn(board.LIGHT)
# Set up an ADC
adc = AnalogIn(board.A0)

SENSOR_DELAY = 30
while True:
light_value = adc.value
print("Light Level: ", light_value)
print("ADC Value: ", light_value)
print("Sending to Adafruit IO...")
io.send_data(light_feed["key"], light_value)
print("Sent!")
Expand Down
62 changes: 32 additions & 30 deletions examples/adafruit_io_http/adafruit_io_digital_out.py
Original file line number Diff line number Diff line change
@@ -1,55 +1,57 @@
"""
Example of turning on and off a LED
from an Adafruit IO Dashboard.
"""
# Turn on and off a LED from your Adafruit IO Dashboard.
# adafruit_circuitpython_adafruitio with an esp32spi_socket
import time
import board
import busio
from digitalio import DigitalInOut, Direction

# ESP32 SPI
from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager

# Import NeoPixel Library
import neopixel

# Import Adafruit IO HTTP Client
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
from adafruit_esp32spi import adafruit_esp32spi
import adafruit_requests as requests
from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError

# Get wifi details and more from a secrets.py file
# Add a secrets.py to your filesystem that has a dictionary called secrets with "ssid" and
# "password" keys with your WiFi credentials. DO NOT share that file or commit it into Git or other
# source control.
# pylint: disable=no-name-in-module,wrong-import-order
try:
from secrets import secrets
except ImportError:
print("WiFi secrets are kept in secrets.py, please add them there!")
raise

# ESP32 Setup
try:
esp32_cs = DigitalInOut(board.ESP_CS)
esp32_ready = DigitalInOut(board.ESP_BUSY)
esp32_reset = DigitalInOut(board.ESP_RESET)
except AttributeError:
esp32_cs = DigitalInOut(board.D9)
esp32_ready = DigitalInOut(board.D10)
esp32_reset = DigitalInOut(board.D5)
# If you are using a board with pre-defined ESP32 Pins:
esp32_cs = DigitalInOut(board.ESP_CS)
esp32_ready = DigitalInOut(board.ESP_BUSY)
esp32_reset = DigitalInOut(board.ESP_RESET)

# If you have an externally connected ESP32:
# esp32_cs = DigitalInOut(board.D9)
# esp32_ready = DigitalInOut(board.D10)
# esp32_reset = DigitalInOut(board.D5)

spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
status_light = neopixel.NeoPixel(
board.NEOPIXEL, 1, brightness=0.2
) # Uncomment for Most Boards
"""Uncomment below for ItsyBitsy M4"""
# status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)

print("Connecting to AP...")
while not esp.is_connected:
try:
esp.connect_AP(secrets["ssid"], secrets["password"])
except RuntimeError as e:
print("could not connect to AP, retrying: ", e)
continue
print("Connected to", str(esp.ssid, "utf-8"), "\tRSSI:", esp.rssi)

socket.set_interface(esp)
requests.set_socket(socket, esp)

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

# Create an instance of the Adafruit IO HTTP client
io = IO_HTTP(aio_username, aio_key, wifi)
# Initialize an Adafruit IO HTTP API object
io = IO_HTTP(aio_username, aio_key, requests)

try:
# Get the 'digital' feed from Adafruit IO
Expand Down
91 changes: 0 additions & 91 deletions examples/adafruit_io_http/adafruit_io_esp_at.py

This file was deleted.

0 comments on commit 0e122d2

Please sign in to comment.