# SPDX-FileCopyrightText: 2019 Brent Rubell for Adafruit Industries # # SPDX-License-Identifier: MIT """ PyPortal IOT Data Logger for Adafruit IO Dependencies: * CircuitPython_ADT7410 https://github.com/adafruit/Adafruit_CircuitPython_ADT7410 * CircuitPython_AdafruitIO https://github.com/adafruit/Adafruit_CircuitPython_AdafruitIO """ import time import board import busio from digitalio import DigitalInOut from analogio import AnalogIn # ESP32 SPI from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager # Import NeoPixel Library import neopixel # Import Adafruit IO HTTP Client from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError # Import ADT7410 Library import adafruit_adt7410 # Timeout between sending data to Adafruit IO, in seconds IO_DELAY = 30 # Get wifi details and more from a secrets.py file try: from secrets import secrets except ImportError: print("WiFi secrets are kept in secrets.py, please add them there!") raise # PyPortal ESP32 Setup esp32_cs = DigitalInOut(board.ESP_CS) esp32_ready = DigitalInOut(board.ESP_BUSY) esp32_reset = DigitalInOut(board.ESP_RESET) spi = busio.SPI(board.SCK, board.MOSI, board.MISO) esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light) # Set your Adafruit IO Username and Key in secrets.py # (visit io.adafruit.com if you need to create an account, # or if you need your Adafruit IO key.) ADAFRUIT_IO_USER = secrets['aio_username'] ADAFRUIT_IO_KEY = secrets['aio_key'] # Create an instance of the Adafruit IO HTTP client io = IO_HTTP(ADAFRUIT_IO_USER, ADAFRUIT_IO_KEY, wifi) try: # Get the 'temperature' feed from Adafruit IO temperature_feed = io.get_feed('temperature') light_feed = io.get_feed('light') except AdafruitIO_RequestError: # If no 'temperature' feed exists, create one temperature_feed = io.create_new_feed('temperature') light_feed = io.create_new_feed('light') # Set up ADT7410 sensor i2c_bus = busio.I2C(board.SCL, board.SDA) adt = adafruit_adt7410.ADT7410(i2c_bus, address=0x48) adt.high_resolution = True # Set up an analog light sensor on the PyPortal adc = AnalogIn(board.LIGHT) while True: try: light_value = adc.value print('Light Level: ', light_value) temperature = adt.temperature print('Temperature: %0.2f C'%(temperature)) print('Sending to Adafruit IO...') io.send_data(light_feed['key'], light_value) io.send_data(temperature_feed['key'], temperature, precision=2) print('Sent to Adafruit IO!') except (ValueError, RuntimeError, ConnectionError, OSError) as e: print("Failed to get data, retrying\n", e) wifi.reset() continue print('Delaying {0} seconds...'.format(IO_DELAY)) time.sleep(IO_DELAY)