Publishing Multiple Feeds from Various Sensors

Moderators: adafruit_support_bill, adafruit

Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
Locked
User avatar
willybezz
 
Posts: 2
Joined: Mon Sep 19, 2022 9:08 am

Publishing Multiple Feeds from Various Sensors

Post by willybezz »

Hello, I have searched around for some example code relating to publishing multiple feeds to adafruit IO from sensors attached to my ESP32-S2 TFT board. I have the BME280 and the SGP30 sensors in which I want to send more than one feed to adafruit IO. In some example code from the resources page (https://learn.adafruit.com/adafruit-esp ... ceive-data) I have tried to edit it to send temperature data to a feed on the IO and it actually works fine and is also relaying a message back to the screen with the temperature it recorded. I am stuck trying to add multiple sensor feeds in my code. Any help would be appreciated. I have already made a "Humidity1827" feed in the adafruit IO platform. My errors when this is run is: "Failed to get or send data, or connect. Error: incomplete format"

Code: Select all

import time
import ssl
from random import randint
import microcontroller
import socketpool
import wifi
import board
import neopixel
import adafruit_minimqtt.adafruit_minimqtt as MQTT
from adafruit_io.adafruit_io import IO_MQTT
import busio
import adafruit_sgp30
import math
from adafruit_bme280 import basic as adafruit_bme280

#Initialize I2C and Calculations
i2c = board.I2C()  # uses board.SCL and board.SDA
bme280 = adafruit_bme280.Adafruit_BME280_I2C(i2c)

# Set local sea level pressure in milibars
bme280.sea_level_pressure = 1013.4
# Calculate Dew Point with "Magnus" formula
b = 17.62
c = 243.12
gamma = (b * bme280.temperature /(c + bme280.temperature)) + math.log(bme280.humidity / 100.0)
dewpoint = (c * gamma) / (b - gamma)

#Secrets Imports
try:
    from secrets import secrets
except ImportError:
    print("WiFi and Adafruit IO credentials are kept in secrets.py - please add them there!")
    raise

# Add your Adafruit IO Username and Key to secrets.py
# (visit io.adafruit.com if you need to create an account,
# or if you need to obtain your Adafruit IO key.)
aio_username = secrets["aio_username"]
aio_key = secrets["aio_key"]

# WiFi
try:
    print("Connecting to %s" % secrets["ssid"])
    wifi.radio.connect(secrets["ssid"], secrets["password"])
    print("Connected to %s!" % secrets["ssid"])
# Wi-Fi connectivity fails with error messages, not specific errors, so this except is broad.
except Exception as e:  # pylint: disable=broad-except
    print("Failed to connect to WiFi. Error:", e, "\nBoard will hard reset in 30 seconds.")
    time.sleep(30)
    microcontroller.reset()

# Initialise NeoPixel
#pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.3)


# Define callback functions which will be called when certain events happen.
def connected(client):
    print("Connected to Adafruit IO!")
    # Subscribe to Adafruit IO feed called "Temperature1827"
    client.subscribe("Temperature1827","Humidity1827")


def message(client, feed_id, payload):  # pylint: disable=unused-argument
    print("Feeds Received New Values".format(feed_id, payload))
    
# Create a socket pool
pool = socketpool.SocketPool(wifi.radio)

# Initialize a new MQTT Client object
mqtt_client = MQTT.MQTT(
    broker="io.adafruit.com",
    username=secrets["aio_username"],
    password=secrets["aio_key"],
    socket_pool=pool,
    ssl_context=ssl.create_default_context(),
)

# Initialize Adafruit IO MQTT "helper"
io = IO_MQTT(mqtt_client)

# Set up the callback methods above
io.on_connect = connected
io.on_message = message

#timestamp = 0
while True:
    try:
        # If Adafruit IO is not connected...
        if not io.is_connected:
            # Connect the client to the MQTT broker.
            print("Connecting to Adafruit IO...")
            io.connect()

        # Explicitly pump the message loop.
        io.loop()
        # Obtain the "Temperature1827" value, print it and publish it to Adafruit IO every 10 seconds.
        while True: 
            temp_f = (bme280.temperature * (9/5) + 32)
            humid = bme280.humidity
            print("Temperature: %0.2f ºF" % temp_f)
            print("Humidity: %0.2f %" % humid)
            io.publish("Temperature1827", temp_f)
            io.publish("Humidity1827", humid)
            time.sleep(10)


    # Adafruit IO fails with internal error types and WiFi fails with specific messages.
    # This except is broad to handle any possible failure.
    except Exception as e:  # pylint: disable=broad-except
        print("Failed to get or send data, or connect. Error:", e,
              "\nBoard will hard reset in 30 seconds.")
        time.sleep(30)
        microcontroller.reset()
Last edited by dastels on Tue Sep 20, 2022 7:20 am, edited 1 time in total.
Reason: Add code tags

Locked
Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.

Return to “Internet of Things: Adafruit IO and Wippersnapper”