|
4 | 4 | NOTE: This example is for Adafruit IO
|
5 | 5 | Plus subscribers only.
|
6 | 6 | """
|
| 7 | +import time |
7 | 8 | import board
|
8 | 9 | import busio
|
9 | 10 | from digitalio import DigitalInOut
|
| 11 | +import adafruit_esp32spi.adafruit_esp32spi_socket as socket |
| 12 | +from adafruit_esp32spi import adafruit_esp32spi |
| 13 | +import adafruit_requests as requests |
| 14 | +from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError |
10 | 15 |
|
11 |
| -# ESP32 SPI |
12 |
| -from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager |
13 |
| - |
14 |
| -# Import NeoPixel Library |
15 |
| -import neopixel |
16 |
| - |
17 |
| -# Import Adafruit IO HTTP Client |
18 |
| -from adafruit_io.adafruit_io import IO_HTTP |
19 |
| - |
20 |
| -# Get wifi details and more from a secrets.py file |
| 16 | +# Add a secrets.py to your filesystem that has a dictionary called secrets with "ssid" and |
| 17 | +# "password" keys with your WiFi credentials. DO NOT share that file or commit it into Git or other |
| 18 | +# source control. |
| 19 | +# pylint: disable=no-name-in-module,wrong-import-order |
21 | 20 | try:
|
22 | 21 | from secrets import secrets
|
23 | 22 | except ImportError:
|
24 | 23 | print("WiFi secrets are kept in secrets.py, please add them there!")
|
25 | 24 | raise
|
26 | 25 |
|
27 |
| -# ESP32 Setup |
28 |
| -try: |
29 |
| - esp32_cs = DigitalInOut(board.ESP_CS) |
30 |
| - esp32_ready = DigitalInOut(board.ESP_BUSY) |
31 |
| - esp32_reset = DigitalInOut(board.ESP_RESET) |
32 |
| -except AttributeError: |
33 |
| - esp32_cs = DigitalInOut(board.D9) |
34 |
| - esp32_ready = DigitalInOut(board.D10) |
35 |
| - esp32_reset = DigitalInOut(board.D5) |
| 26 | +# If you are using a board with pre-defined ESP32 Pins: |
| 27 | +esp32_cs = DigitalInOut(board.ESP_CS) |
| 28 | +esp32_ready = DigitalInOut(board.ESP_BUSY) |
| 29 | +esp32_reset = DigitalInOut(board.ESP_RESET) |
| 30 | + |
| 31 | +# If you have an externally connected ESP32: |
| 32 | +# esp32_cs = DigitalInOut(board.D9) |
| 33 | +# esp32_ready = DigitalInOut(board.D10) |
| 34 | +# esp32_reset = DigitalInOut(board.D5) |
36 | 35 |
|
37 | 36 | spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
|
38 | 37 | esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
|
39 |
| -status_light = neopixel.NeoPixel( |
40 |
| - board.NEOPIXEL, 1, brightness=0.2 |
41 |
| -) # Uncomment for Most Boards |
42 |
| -"""Uncomment below for ItsyBitsy M4""" |
43 |
| -# status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2) |
44 |
| -wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light) |
| 38 | + |
| 39 | +print("Connecting to AP...") |
| 40 | +while not esp.is_connected: |
| 41 | + try: |
| 42 | + esp.connect_AP(secrets["ssid"], secrets["password"]) |
| 43 | + except RuntimeError as e: |
| 44 | + print("could not connect to AP, retrying: ", e) |
| 45 | + continue |
| 46 | +print("Connected to", str(esp.ssid, "utf-8"), "\tRSSI:", esp.rssi) |
| 47 | + |
| 48 | +socket.set_interface(esp) |
| 49 | +requests.set_socket(socket, esp) |
45 | 50 |
|
46 | 51 | # Set your Adafruit IO Username and Key in secrets.py
|
47 | 52 | # (visit io.adafruit.com if you need to create an account,
|
48 | 53 | # or if you need your Adafruit IO key.)
|
49 | 54 | aio_username = secrets["aio_username"]
|
50 | 55 | aio_key = secrets["aio_key"]
|
51 | 56 |
|
52 |
| -# Create an instance of the Adafruit IO HTTP client |
53 |
| -io = IO_HTTP(aio_username, aio_key, wifi) |
| 57 | +# Initialize an Adafruit IO HTTP API object |
| 58 | +io = IO_HTTP(aio_username, aio_key, requests) |
54 | 59 |
|
55 | 60 | # Weather Location ID
|
56 | 61 | # (to obtain this value, visit
|
57 | 62 | # https://io.adafruit.com/services/weather
|
58 | 63 | # and copy over the location ID)
|
59 |
| -location_id = 1234 |
| 64 | +location_id = 2127 |
60 | 65 |
|
61 |
| -print("Getting weather record from IO...") |
62 |
| -# Get the specified weather record with current weather |
| 66 | +print("Getting forecast from IO...") |
| 67 | +# Fetch the specified record with current weather |
63 | 68 | # and all available forecast information.
|
64 | 69 | forecast = io.receive_weather(location_id)
|
65 | 70 |
|
|
0 commit comments