diff --git a/adafruit_io/adafruit_io.py b/adafruit_io/adafruit_io.py index e270a4a..b645743 100755 --- a/adafruit_io/adafruit_io.py +++ b/adafruit_io/adafruit_io.py @@ -37,6 +37,7 @@ """ import time import json + from adafruit_io.adafruit_io_errors import ( AdafruitIO_RequestError, AdafruitIO_ThrottleError, @@ -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}, @@ -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) @@ -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) @@ -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) diff --git a/examples/adafruit_io_http/adafruit_io_analog_in.py b/examples/adafruit_io_http/adafruit_io_analog_in.py index a1c4c53..6acdbc3 100644 --- a/examples/adafruit_io_http/adafruit_io_analog_in.py +++ b/examples/adafruit_io_http/adafruit_io_analog_in.py @@ -1,48 +1,49 @@ -""" -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, @@ -50,8 +51,8 @@ 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 @@ -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!") diff --git a/examples/adafruit_io_http/adafruit_io_digital_out.py b/examples/adafruit_io_http/adafruit_io_digital_out.py index 1f57b12..58418e9 100644 --- a/examples/adafruit_io_http/adafruit_io_digital_out.py +++ b/examples/adafruit_io_http/adafruit_io_digital_out.py @@ -1,46 +1,48 @@ -""" -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, @@ -48,8 +50,8 @@ 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 diff --git a/examples/adafruit_io_http/adafruit_io_esp_at.py b/examples/adafruit_io_http/adafruit_io_esp_at.py deleted file mode 100644 index cf703b3..0000000 --- a/examples/adafruit_io_http/adafruit_io_esp_at.py +++ /dev/null @@ -1,91 +0,0 @@ -""" -Usage example of the ESP32 over UART -using the CircuitPython ESP_ATControl library. - -Dependencies: - * https://github.com/adafruit/Adafruit_CircuitPython_ESP_ATcontrol -""" -from random import randint -import board -import busio -from digitalio import DigitalInOut - -# ESP32 AT -from adafruit_espatcontrol import ( - adafruit_espatcontrol, - adafruit_espatcontrol_wifimanager, -) - -# Use below for Most Boards -import neopixel - -# Import Adafruit IO HTTP Client -from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError - -status_light = neopixel.NeoPixel( - board.NEOPIXEL, 1, brightness=0.2 -) # Uncomment for Most Boards - -# Uncomment below for ItsyBitsy M4# -# import adafruit_dotstar as dotstar -# status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2) - -# Uncomment below for Particle Argon# -# status_light = None - -# Get wifi details and more from a secrets.py file -try: - from secrets import secrets -except ImportError: - print("WiFi secrets are kept in secrets.py, please add them there!") - raise - -# With a Metro or Feather M4 -uart = busio.UART(board.TX, board.RX, timeout=0.1) -resetpin = DigitalInOut(board.D5) -rtspin = DigitalInOut(board.D6) - -# With a Particle Argon -""" -RX = board.ESP_TX -TX = board.ESP_RX -resetpin = DigitalInOut(board.ESP_WIFI_EN) -rtspin = DigitalInOut(board.ESP_CTS) -uart = busio.UART(TX, RX, timeout=0.1) -esp_boot = DigitalInOut(board.ESP_BOOT_MODE) -from digitalio import Direction -esp_boot.direction = Direction.OUTPUT -esp_boot.value = True -""" - -esp = adafruit_espatcontrol.ESP_ATcontrol( - uart, 115200, reset_pin=resetpin, rts_pin=rtspin, debug=False -) -wifi = adafruit_espatcontrol_wifimanager.ESPAT_WiFiManager(esp, secrets, status_light) - -# 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) - -try: - # Get the 'temperature' feed from Adafruit IO - temperature_feed = io.get_feed("temperature") -except AdafruitIO_RequestError: - # If no 'temperature' feed exists, create one - temperature_feed = io.create_new_feed("temperature") - -# Send random integer values to the feed -random_value = randint(0, 50) -print("Sending {0} to temperature feed...".format(random_value)) -io.send_data(temperature_feed["key"], random_value) -print("Data sent!") - -# Retrieve data value from the feed -print("Retrieving data from temperature feed...") -received_data = io.receive_data(temperature_feed["key"]) -print("Data from temperature feed: ", received_data["value"]) diff --git a/examples/adafruit_io_http/adafruit_io_feeds.py b/examples/adafruit_io_http/adafruit_io_feeds.py index 5240957..d70819e 100644 --- a/examples/adafruit_io_http/adafruit_io_feeds.py +++ b/examples/adafruit_io_http/adafruit_io_feeds.py @@ -1,44 +1,48 @@ -""" -Example of interacting with Adafruit IO feeds -""" +# Adafruit IO HTTP API - Feed Interactions +# Documentation: https://io.adafruit.com/api/docs/#feeds +# adafruit_circuitpython_adafruitio with an esp32spi_socket import board import busio from digitalio import DigitalInOut - -# 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 -# 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, @@ -46,8 +50,8 @@ 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) # Create a new 'circuitpython' feed with a description print("Creating new Adafruit IO feed...") diff --git a/examples/adafruit_io_http/adafruit_io_groups.py b/examples/adafruit_io_http/adafruit_io_groups.py index 436d321..e894dff 100644 --- a/examples/adafruit_io_http/adafruit_io_groups.py +++ b/examples/adafruit_io_http/adafruit_io_groups.py @@ -1,52 +1,58 @@ -""" -Example of performing group operations -""" +# Adafruit IO HTTP API - Group Interactions +# Documentation: https://io.adafruit.com/api/docs/#groups +# adafruit_circuitpython_adafruitio with an esp32spi_socket import board import busio from digitalio import DigitalInOut - -# 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 -# 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) # Create a new group print("Creating a new Adafruit IO Group...") diff --git a/examples/adafruit_io_http/adafruit_io_metadata.py b/examples/adafruit_io_http/adafruit_io_metadata.py index dcabe10..00aa3bc 100644 --- a/examples/adafruit_io_http/adafruit_io_metadata.py +++ b/examples/adafruit_io_http/adafruit_io_metadata.py @@ -1,45 +1,47 @@ -""" -Example of attaching metadata -to data sent to Adafruit IO. -""" +# Adafruit IO HTTP API - Sending values with optional metadata +# adafruit_circuitpython_adafruitio with an esp32spi_socket import board import busio from digitalio import DigitalInOut - -# 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, @@ -47,8 +49,8 @@ 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 'location' feed from Adafruit IO diff --git a/examples/adafruit_io_http/adafruit_io_randomizer.py b/examples/adafruit_io_http/adafruit_io_randomizer.py index b2889ed..96e4039 100644 --- a/examples/adafruit_io_http/adafruit_io_randomizer.py +++ b/examples/adafruit_io_http/adafruit_io_randomizer.py @@ -1,46 +1,48 @@ -""" -Example of using Adafruit IO's -random data service. -""" +# Example for using Adafruit IO's random data (randomizer) service +# adafruit_circuitpython_adafruitio with an esp32spi_socket import time import board import busio from digitalio import DigitalInOut - -# 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 -# 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, @@ -48,8 +50,8 @@ 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) # Random Data ID # (to obtain this value, visit @@ -58,14 +60,9 @@ random_data_id = 1234 while True: - try: - print("Fetching random data from Adafruit IO...") - random_data = io.receive_random_data(random_data_id) - print("Random Data: ", random_data["value"]) - print("Data Seed: ", random_data["seed"]) - print("Waiting 1 minute to fetch new randomized data...") - except (ValueError, RuntimeError) as e: - print("Failed to get data, retrying\n", e) - wifi.reset() - continue + print("Fetching random data from Adafruit IO...") + random_data = io.receive_random_data(random_data_id) + print("Random Data: ", random_data["value"]) + print("Data Seed: ", random_data["seed"]) + print("Waiting 1 minute to fetch new randomized data...") time.sleep(60) diff --git a/examples/adafruit_io_http/adafruit_io_simpletest.py b/examples/adafruit_io_http/adafruit_io_simpletest.py index a347757..26749d1 100644 --- a/examples/adafruit_io_http/adafruit_io_simpletest.py +++ b/examples/adafruit_io_http/adafruit_io_simpletest.py @@ -1,54 +1,36 @@ -""" -Sending data to Adafruit IO and receiving it. -""" +# adafruit_circuitpython_adafruitio usage with native wifi networking +import ssl from random import randint -import board -import busio -from digitalio import DigitalInOut - -# ESP32 SPI -from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager - -# Import NeoPixel Library -import neopixel - -# Import Adafruit IO HTTP Client +import adafruit_requests +import socketpool +import wifi 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) - -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) - # 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) +print("Connecting to %s" % secrets["ssid"]) +wifi.radio.connect(secrets["ssid"], secrets["password"]) +print("Connected to %s!" % secrets["ssid"]) + + +pool = socketpool.SocketPool(wifi.radio) +requests = adafruit_requests.Session(pool, ssl.create_default_context()) +# Initialize an Adafruit IO HTTP API object +io = IO_HTTP(aio_username, aio_key, requests) try: # Get the 'temperature' feed from Adafruit IO diff --git a/examples/adafruit_io_http/adafruit_io_simpletest_cpython.py b/examples/adafruit_io_http/adafruit_io_simpletest_cpython.py new file mode 100644 index 0000000..e2cc823 --- /dev/null +++ b/examples/adafruit_io_http/adafruit_io_simpletest_cpython.py @@ -0,0 +1,45 @@ +# adafruit_circuitpython_adafruitio usage with a CPython socket +import socket +import ssl +from random import randint +import adafruit_requests +from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError + +# 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 + +# 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"] + + +requests = adafruit_requests.Session(socket, ssl.create_default_context()) +# Initialize an Adafruit IO HTTP API object +io = IO_HTTP(aio_username, aio_key, requests) + +try: + # Get the 'temperature' feed from Adafruit IO + temperature_feed = io.get_feed("temperature") +except AdafruitIO_RequestError: + # If no 'temperature' feed exists, create one + temperature_feed = io.create_new_feed("temperature") + +# Send random integer values to the feed +random_value = randint(0, 50) +print("Sending {0} to temperature feed...".format(random_value)) +io.send_data(temperature_feed["key"], random_value) +print("Data sent!") + +# Retrieve data value from the feed +print("Retrieving data from temperature feed...") +received_data = io.receive_data(temperature_feed["key"]) +print("Data from temperature feed: ", received_data["value"]) diff --git a/examples/adafruit_io_http/adafruit_io_temperature.py b/examples/adafruit_io_http/adafruit_io_temperature.py index f124b48..0d25f05 100644 --- a/examples/adafruit_io_http/adafruit_io_temperature.py +++ b/examples/adafruit_io_http/adafruit_io_temperature.py @@ -1,53 +1,49 @@ -""" -Example of sending temperature -values to an Adafruit IO feed. - -Dependencies: - * CircuitPython_ADT7410 - https://github.com/adafruit/Adafruit_CircuitPython_ADT7410 -""" +# Example of sending ADT7410 sensor temperature values to IO +# adafruit_circuitpython_adafruitio with an esp32spi_socket import time import board import busio from digitalio import DigitalInOut - -# ESP32 SPI -from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager - -# Import NeoPixel Library -import neopixel - -# Import ADT7410 Library +import adafruit_esp32spi.adafruit_esp32spi_socket as socket +from adafruit_esp32spi import adafruit_esp32spi +import adafruit_requests as requests import adafruit_adt7410 - -# Import Adafruit IO HTTP Client 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, @@ -55,8 +51,8 @@ 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 'temperature' feed from Adafruit IO @@ -71,17 +67,12 @@ adt.high_resolution = True while True: - try: - temperature = adt.temperature - # set temperature value to two precision points - temperature = "%0.2f" % (temperature) + temperature = adt.temperature + # set temperature value to two precision points + temperature = "%0.2f" % (temperature) - print("Current Temperature: {0}*C".format(temperature)) - print("Sending to Adafruit IO...") - io.send_data(temperature_feed["key"], temperature) - print("Data sent!") - except (ValueError, RuntimeError) as e: - print("Failed to get data, retrying\n", e) - wifi.reset() - continue + print("Current Temperature: {0}*C".format(temperature)) + print("Sending to Adafruit IO...") + io.send_data(temperature_feed["key"], temperature) + print("Data sent!") time.sleep(0.5) diff --git a/examples/adafruit_io_http/adafruit_io_weather.py b/examples/adafruit_io_http/adafruit_io_weather.py index 33fc0f4..6239d24 100644 --- a/examples/adafruit_io_http/adafruit_io_weather.py +++ b/examples/adafruit_io_http/adafruit_io_weather.py @@ -1,47 +1,47 @@ -""" -Example of getting weather -from the Adafruit IO Weather Service -NOTE: This example is for Adafruit IO -Plus subscribers only. -""" +# Example of using the Adafruit IO+ Weather Service +# adafruit_circuitpython_adafruitio with an esp32spi_socket import board import busio from digitalio import DigitalInOut - -# 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 -# 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, @@ -49,17 +49,17 @@ 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) # Weather Location ID # (to obtain this value, visit # https://io.adafruit.com/services/weather # and copy over the location ID) -location_id = 1234 +location_id = 2127 -print("Getting weather record from IO...") -# Get the specified weather record with current weather +print("Getting forecast from IO...") +# Fetch the specified record with current weather # and all available forecast information. forecast = io.receive_weather(location_id)