Skip to content

Commit 119c818

Browse files
author
brentru
committed
add example for accessing weather service
1 parent 3ced1e3 commit 119c818

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
"""
2+
Example of getting weather
3+
from the Adafruit IO Weather Service
4+
NOTE: This example is for Adafruit IO
5+
Plus subscribers only.
6+
"""
7+
import board
8+
import busio
9+
from digitalio import DigitalInOut
10+
11+
# ESP32 SPI
12+
from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager
13+
14+
# Import Adafruit IO REST Client
15+
from Adafruit_IO import RESTClient
16+
17+
# Get wifi details and more from a wifi_settings.py.py file
18+
try:
19+
from wifi_settings import settings
20+
except ImportError:
21+
print("WiFi settings are kept in wifi_settings.py.py, please add them there!")
22+
raise
23+
24+
25+
# ESP32 Setup
26+
esp32_cs = DigitalInOut(board.D9)
27+
esp32_ready = DigitalInOut(board.D10)
28+
esp32_reset = DigitalInOut(board.D5)
29+
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
30+
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
31+
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, settings, board.NEOPIXEL)
32+
33+
"""
34+
# PyPortal ESP32 Setup
35+
import microcontroller
36+
esp32_cs = DigitalInOut(microcontroller.pin.PB14)
37+
esp32_ready = DigitalInOut(microcontroller.pin.PB16)
38+
esp32_reset = DigitalInOut(microcontroller.pin.PB17)
39+
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
40+
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
41+
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, settings, board.NEOPIXEL)
42+
"""
43+
44+
# Set your Adafruit IO Username and Key in wifi_settings.py
45+
# (visit io.adafruit.com if you need to create an account,
46+
# or if you need your Adafruit IO key.)
47+
ADAFRUIT_IO_USER = settings['adafruit_io_user']
48+
ADAFRUIT_IO_KEY = settings['adafruit_io_key']
49+
50+
# Create an instance of the Adafruit IO REST client
51+
io = RESTClient(ADAFRUIT_IO_USER, ADAFRUIT_IO_KEY, wifi)
52+
53+
# Weather Location ID
54+
location_id = 2127
55+
56+
print('Getting weather record from IO...')
57+
# Get the specified weather record with current weather
58+
# and all available forecast information.
59+
forecast = io.receive_weather(location_id)
60+
61+
# Get today's forecast
62+
current_forecast = forecast['current']
63+
print('It is {0} and {1}*F.'.format(current_forecast['summary'], current_forecast['temperature']))
64+
print('with a humidity of {0}%'.format(current_forecast['humidity']))
65+
66+
# Get tomorrow's forecast
67+
tom_forecast = forecast['forecast_days_1']
68+
print('\nTomorrow has a low of {0}*F and a high of {1}*F.'.format(
69+
tom_forecast['temperatureLow'], tom_forecast['temperatureHigh']))
70+
print('with a humidity of {0}%'.format(tom_forecast['humidity']))

0 commit comments

Comments
 (0)