Skip to content

Commit f95ce6a

Browse files
author
brentru
committed
add weather service
1 parent 215b5a4 commit f95ce6a

File tree

1 file changed

+35
-30
lines changed

1 file changed

+35
-30
lines changed

examples/adafruit_io_http/adafruit_io_weather.py

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,62 +4,67 @@
44
NOTE: This example is for Adafruit IO
55
Plus subscribers only.
66
"""
7+
import time
78
import board
89
import busio
910
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
1015

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
2120
try:
2221
from secrets import secrets
2322
except ImportError:
2423
print("WiFi secrets are kept in secrets.py, please add them there!")
2524
raise
2625

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)
3635

3736
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
3837
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)
4550

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

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)
5459

5560
# Weather Location ID
5661
# (to obtain this value, visit
5762
# https://io.adafruit.com/services/weather
5863
# and copy over the location ID)
59-
location_id = 1234
64+
location_id = 2127
6065

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
6368
# and all available forecast information.
6469
forecast = io.receive_weather(location_id)
6570

0 commit comments

Comments
 (0)