code.py
 1  # SPDX-FileCopyrightText: 2019 Brent Rubell for Adafruit Industries
 2  #
 3  # SPDX-License-Identifier: MIT
 4  
 5  """
 6  PyPortal IOT Data Logger for Adafruit IO
 7  
 8  Dependencies:
 9      * CircuitPython_ADT7410
10          https://github.com/adafruit/Adafruit_CircuitPython_ADT7410
11  
12      * CircuitPython_AdafruitIO
13          https://github.com/adafruit/Adafruit_CircuitPython_AdafruitIO
14  """
15  import time
16  import board
17  import busio
18  from digitalio import DigitalInOut
19  from analogio import AnalogIn
20  
21  # ESP32 SPI
22  from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager
23  
24  # Import NeoPixel Library
25  import neopixel
26  
27  # Import Adafruit IO HTTP Client
28  from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError
29  
30  # Import ADT7410 Library
31  import adafruit_adt7410
32  
33  # Timeout between sending data to Adafruit IO, in seconds
34  IO_DELAY = 30
35  
36  # Get wifi details and more from a secrets.py file
37  try:
38      from secrets import secrets
39  except ImportError:
40      print("WiFi secrets are kept in secrets.py, please add them there!")
41      raise
42  
43  # PyPortal ESP32 Setup
44  esp32_cs = DigitalInOut(board.ESP_CS)
45  esp32_ready = DigitalInOut(board.ESP_BUSY)
46  esp32_reset = DigitalInOut(board.ESP_RESET)
47  spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
48  esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
49  status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2)
50  wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
51  
52  # Set your Adafruit IO Username and Key in secrets.py
53  # (visit io.adafruit.com if you need to create an account,
54  # or if you need your Adafruit IO key.)
55  ADAFRUIT_IO_USER = secrets['aio_username']
56  ADAFRUIT_IO_KEY = secrets['aio_key']
57  
58  # Create an instance of the Adafruit IO HTTP client
59  io = IO_HTTP(ADAFRUIT_IO_USER, ADAFRUIT_IO_KEY, wifi)
60  
61  try:
62      # Get the 'temperature' feed from Adafruit IO
63      temperature_feed = io.get_feed('temperature')
64      light_feed = io.get_feed('light')
65  except AdafruitIO_RequestError:
66      # If no 'temperature' feed exists, create one
67      temperature_feed = io.create_new_feed('temperature')
68      light_feed = io.create_new_feed('light')
69  
70  # Set up ADT7410 sensor
71  i2c_bus = busio.I2C(board.SCL, board.SDA)
72  adt = adafruit_adt7410.ADT7410(i2c_bus, address=0x48)
73  adt.high_resolution = True
74  
75  # Set up an analog light sensor on the PyPortal
76  adc = AnalogIn(board.LIGHT)
77  
78  while True:
79      try:
80          light_value = adc.value
81          print('Light Level: ', light_value)
82  
83          temperature = adt.temperature
84          print('Temperature: %0.2f C'%(temperature))
85  
86          print('Sending to Adafruit IO...')
87  
88          io.send_data(light_feed['key'], light_value)
89          io.send_data(temperature_feed['key'], temperature, precision=2)
90          print('Sent to Adafruit IO!')
91      except (ValueError, RuntimeError, ConnectionError, OSError) as e:
92          print("Failed to get data, retrying\n", e)
93          wifi.reset()
94          continue
95      print('Delaying {0} seconds...'.format(IO_DELAY))
96      time.sleep(IO_DELAY)