code.py
  1  # SPDX-FileCopyrightText: 2019 Brent Rubell for Adafruit Industries
  2  #
  3  # SPDX-License-Identifier: MIT
  4  
  5  """
  6  PyPortal Smart Thermometer
  7  ==============================================
  8  Turn your PyPortal into an internet-connected
  9  thermometer with Adafruit IO
 10  
 11  Author: Brent Rubell for Adafruit Industries, 2019
 12  """
 13  import time
 14  import board
 15  import neopixel
 16  import busio
 17  from digitalio import DigitalInOut
 18  from analogio import AnalogIn
 19  import adafruit_adt7410
 20  
 21  from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager
 22  from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError
 23  
 24  # thermometer graphics helper
 25  import thermometer_helper
 26  
 27  # rate at which to refresh the pyportal screen, in seconds
 28  PYPORTAL_REFRESH = 2
 29  
 30  # Get wifi details and more from a secrets.py file
 31  try:
 32      from secrets import secrets
 33  except ImportError:
 34      print("WiFi secrets are kept in secrets.py, please add them there!")
 35      raise
 36  
 37  # PyPortal ESP32 Setup
 38  esp32_cs = DigitalInOut(board.ESP_CS)
 39  esp32_ready = DigitalInOut(board.ESP_BUSY)
 40  esp32_reset = DigitalInOut(board.ESP_RESET)
 41  spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
 42  esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
 43  status_light = neopixel.NeoPixel(board.NEOPIXEL, 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  try:
 50      ADAFRUIT_IO_USER = secrets['aio_username']
 51      ADAFRUIT_IO_KEY = secrets['aio_key']
 52  except KeyError:
 53      raise KeyError('To use this code, you need to include your Adafruit IO username \
 54  and password in a secrets.py file on the CIRCUITPY drive.')
 55  
 56  # Create an instance of the IO_HTTP client
 57  io = IO_HTTP(ADAFRUIT_IO_USER, ADAFRUIT_IO_KEY, wifi)
 58  
 59  # Get the temperature feed from Adafruit IO
 60  temperature_feed = io.get_feed('temperature')
 61  
 62  # init. graphics helper
 63  gfx = thermometer_helper.Thermometer_GFX(celsius=False)
 64  
 65  # init. adt7410
 66  i2c_bus = busio.I2C(board.SCL, board.SDA)
 67  adt = adafruit_adt7410.ADT7410(i2c_bus, address=0x48)
 68  adt.high_resolution = True
 69  
 70  # init. the light sensor
 71  light_sensor = AnalogIn(board.LIGHT)
 72  
 73  def set_backlight(val):
 74      """Adjust the TFT backlight.
 75      :param val: The backlight brightness. Use a value between ``0`` and ``1``, where ``0`` is
 76                  off, and ``1`` is 100% brightness.
 77      """
 78      val = max(0, min(1.0, val))
 79      try:
 80          board.DISPLAY.auto_brightness = False
 81      except AttributeError:
 82          pass
 83      board.DISPLAY.brightness = val
 84  
 85  while True:
 86      # read the light sensor
 87      light_value = light_sensor.value
 88      print('Light Value: ', light_value)
 89      # read the temperature sensor
 90      temperature = adt.temperature
 91      try: # WiFi Connection
 92          if light_value < 1000: # turn on the backlight
 93              set_backlight(1)
 94              print('displaying temperature...')
 95              gfx.display_temp(temperature)
 96              # Get and display date and time form Adafruit IO
 97              print('Getting time from Adafruit IO...')
 98              datetime = io.receive_time()
 99              print('displaying time...')
100              gfx.display_date_time(datetime)
101          else: # turn off the backlight
102              set_backlight(0)
103          try: # send temperature data to IO
104              gfx.display_io_status('Sending data...')
105              print('Sending data to Adafruit IO...')
106              io.send_data(temperature_feed['key'], temperature)
107              print('Data sent!')
108              gfx.display_io_status('Data sent!')
109          except AdafruitIO_RequestError as e:
110              raise AdafruitIO_RequestError('IO Error: ', e)
111      except (ValueError, RuntimeError, ConnectionError, OSError) as e:
112          print("Failed to get data, retrying\n", e)
113          wifi.reset()
114          continue
115      time.sleep(PYPORTAL_REFRESH)