code.py
 1  # SPDX-FileCopyrightText: 2022 Liz Clark for Adafruit Industries
 2  #
 3  # SPDX-License-Identifier: MIT
 4  
 5  import os
 6  import time
 7  import ssl
 8  import wifi
 9  import socketpool
10  import microcontroller
11  import adafruit_requests
12  
13  #  adafruit quotes URL
14  quotes_url = "https://www.adafruit.com/api/quotes.php"
15  
16  #  connect to SSID
17  wifi.radio.connect(os.getenv('WIFI_SSID'), os.getenv('WIFI_PASSWORD'))
18  
19  pool = socketpool.SocketPool(wifi.radio)
20  requests = adafruit_requests.Session(pool, ssl.create_default_context())
21  
22  while True:
23      try:
24          #  pings adafruit quotes
25          print("Fetching text from %s" % quotes_url)
26          #  gets the quote from adafruit quotes
27          response = requests.get(quotes_url)
28          print("-" * 40)
29          #  prints the response to the REPL
30          print("Text Response: ", response.text)
31          print("-" * 40)
32          response.close()
33          #  delays for 1 minute
34          time.sleep(60)
35      # pylint: disable=broad-except
36      except Exception as e:
37          print("Error:\n", str(e))
38          print("Resetting microcontroller in 10 seconds")
39          time.sleep(10)
40          microcontroller.reset()