90 lines
3 KiB
Python
90 lines
3 KiB
Python
"""
|
|
Example of sending temperature
|
|
values to an Adafruit IO feed.
|
|
|
|
Dependencies:
|
|
* CircuitPython_ADT7410
|
|
https://github.com/adafruit/Adafruit_CircuitPython_ADT7410
|
|
"""
|
|
import time
|
|
import board
|
|
import busio
|
|
from digitalio import DigitalInOut
|
|
import neopixel
|
|
|
|
# ESP32 SPI
|
|
from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager
|
|
|
|
# Import Adafruit IO REST Client
|
|
from adafruit_io.adafruit_io import RESTClient, AdafruitIO_RequestError
|
|
|
|
# Import ADT7410 Library
|
|
import adafruit_adt7410
|
|
|
|
# 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
|
|
|
|
# ESP32 Setup
|
|
esp32_cs = DigitalInOut(board.D9)
|
|
esp32_ready = DigitalInOut(board.D10)
|
|
esp32_reset = DigitalInOut(board.D5)
|
|
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
|
|
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
|
|
"""Use below for Most Boards"""
|
|
status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) # Uncomment for Most Boards
|
|
"""Uncomment below for ItsyBitsy M4"""
|
|
#status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
|
|
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
|
|
|
|
"""
|
|
# PyPortal ESP32 Setup
|
|
import microcontroller
|
|
esp32_cs = DigitalInOut(microcontroller.pin.PB14)
|
|
esp32_ready = DigitalInOut(microcontroller.pin.PB16)
|
|
esp32_reset = DigitalInOut(microcontroller.pin.PB17)
|
|
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['adafruit_io_user']
|
|
ADAFRUIT_IO_KEY = secrets['adafruit_io_key']
|
|
|
|
# Create an instance of the Adafruit IO REST client
|
|
io = RESTClient(ADAFRUIT_IO_USER, ADAFRUIT_IO_KEY, wifi)
|
|
|
|
try:
|
|
# Get the 'temperature' feed from Adafruit IO
|
|
temperature_feed = io.get_feed('temperature')
|
|
except AdafruitIO_RequestError:
|
|
# If no 'temperature' feed exists, create one
|
|
temperature_feed = io.create_new_feed('temperature')
|
|
|
|
# Set up ADT7410 sensor
|
|
i2c_bus = busio.I2C(board.SCL, board.SDA)
|
|
adt = adafruit_adt7410.ADT7410(i2c_bus, address=0x48)
|
|
adt.high_resolution = True
|
|
|
|
while True:
|
|
try:
|
|
temperature = adt.temperature
|
|
# set temperature value to two precision points
|
|
temperature = '%0.2f'%(temperature)
|
|
|
|
print('Current Temperature: {0}*C'.format(temperature))
|
|
print('Sending to Adafruit IO...')
|
|
io.send_data(temperature_feed['key'], temperature)
|
|
print('Data sent!')
|
|
except (ValueError, RuntimeError) as e:
|
|
print("Failed to get data, retrying\n", e)
|
|
wifi.reset()
|
|
continue
|
|
time.sleep(0.5)
|