I have a problem with adafruitIO. With the following code i get an error message.
AdafruitIO_RequestError: Adafruit IO Error 400: ['Name must be unique within the selected group', 'Key must be unique within the selected group', 'Name must not match an existing key within the selected group', 'Group feeds is invalid']
Code: Select all
import board
import busio
import neopixel
import time
import adafruit_ahtx0
from digitalio import DigitalInOut
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
from adafruit_esp32spi import adafruit_esp32spi
from adafruit_esp32spi import adafruit_esp32spi_wifimanager
import adafruit_requests as requests
from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError
from adafruit_bus_device.i2c_device import I2CDevice
# Timeout between sending data to Adafruit IO, in seconds
IO_DELAY = 10
# Initialize STEMMA_I2C
i2c = board.STEMMA_I2C()
# aht 20
aht = adafruit_ahtx0.AHTx0(i2c)
# co2
sensor = I2CDevice(i2c, 0x69)
# import secret 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.D7)
esp32_ready = DigitalInOut(board.D6)
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)
# WIFI
status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2)
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
print("Connecting to AP...")
while not esp.is_connected:
try:
esp.connect_AP(secrets["ssid"], secrets["password"])
except RuntimeError as e:
print("could not connect to AP, retrying: ", e)
continue
print("Connected to", str(esp.ssid, "utf-8"), "\tRSSI:", esp.rssi)
socket.set_interface(esp)
requests.set_socket(socket, esp)
# AdafruitIO
aio_username = secrets["aio_username"]
aio_key = secrets["aio_key"]
# Initialize an Adafruit IO HTTP API object
io = IO_HTTP(aio_username, aio_key, requests)
# IO Feed
try:
# Get the feed from Adafruit IO
temperature_feed = io.get_feed("temperature")
humidity_feed = io.get_feed("humidity")
carbon_feed = io.get_feed("carbon")
except AdafruitIO_RequestError:
# If no feed exists, create one
temperature_feed = io.create_new_feed("temperature")
humidity_feed = io.create_new_feed("humidity")
carbon_feed = io.create_new_feed("carbon")
while True:
with sensor:
sensor.write(bytes([ord('R')]))
time.sleep(0.6)
result = bytearray(5)
sensor.readinto(result)
try:
if result[4] != 0:
test = result[1:5]
co2 = int(test.decode('ascii'))
else:
test = result[1:4]
co2 = int(test.decode('ascii'))
temp = aht.temperature
hum = aht.relative_humidity
print("\nTemperature: %0.1f C" % temp)
print("Humidity: %0.1f %%" % hum)
print("Co2: %i ppm" % co2)
print(co2)
print('Sending to Adafruit IO...')
io.send_data(temperature_feed['key'], temp)
io.send_data(humidity_feed['key'], hum)
io.send_data(carbon_feed['key'], co2)
print('Sent to Adafruit IO!')
except (ValueError, RuntimeError, ConnectionError, OSError) as e:
print("Failed to get data, retrying\n", e)
wifi.reset()
wifi.connect()
continue
print('Delaying {0} seconds...'.format(IO_DELAY))
time.sleep(IO_DELAY)
Unfortunately I have no idea what I am doing wrong. If I comment out the three carbonfeed lines, then it works.
Thanks for your help!