/ examples / adafruit_io_http / adafruit_io_weather.py
adafruit_io_weather.py
 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 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
21  try:
22      from secrets import secrets
23  except ImportError:
24      print("WiFi secrets are kept in secrets.py, please add them there!")
25      raise
26  
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)
36  
37  spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
38  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)
45  
46  # Set your Adafruit IO Username and Key in secrets.py
47  # (visit io.adafruit.com if you need to create an account,
48  # or if you need your Adafruit IO key.)
49  aio_username = secrets["aio_username"]
50  aio_key = secrets["aio_key"]
51  
52  # Create an instance of the Adafruit IO HTTP client
53  io = IO_HTTP(aio_username, aio_key, wifi)
54  
55  # Weather Location ID
56  # (to obtain this value, visit
57  # https://io.adafruit.com/services/weather
58  # and copy over the location ID)
59  location_id = 1234
60  
61  print("Getting weather record from IO...")
62  # Get the specified weather record with current weather
63  # and all available forecast information.
64  forecast = io.receive_weather(location_id)
65  
66  # Get today's forecast
67  current_forecast = forecast["current"]
68  print(
69      "It is {0} and {1}*F.".format(
70          current_forecast["summary"], current_forecast["temperature"]
71      )
72  )
73  print("with a humidity of {0}%".format(current_forecast["humidity"] * 100))
74  
75  # Get tomorrow's forecast
76  tom_forecast = forecast["forecast_days_1"]
77  print(
78      "\nTomorrow has a low of {0}*F and a high of {1}*F.".format(
79          tom_forecast["temperatureLow"], tom_forecast["temperatureHigh"]
80      )
81  )
82  print("with a humidity of {0}%".format(tom_forecast["humidity"] * 100))