How to code Adafruit IO Group Feeds

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
Tom_Henry
 
Posts: 350
Joined: Wed Aug 26, 2020 8:19 pm

How to code Adafruit IO Group Feeds

Post by Tom_Henry »

I am using the following hardware:

Metro M4 AirLift Lite
Adafruit MS8607 Sensor
Adafruit RTC PCD8523
Adafruit PMSA003I Sensor

These are all connected using i2c.

I am using CircuitPython and have attached my code.py as code8.4.21py.

I originally created all these feeds as individual feeds and you can see the results in my Dashboard screen shot.

I have created a new group of Weather feeds instead of the individual feeds.

I can’t figure out to code the group feeds in my CircuitPython code for publishing to Adafruit IO

You will see that I have uncommented some lines at the end of the code where I was trying to using the Weather group feeds: see lines 174 and 176. Those code lines didn’t work.

Can anyone help me code some lines so that my Weather group feeds will publish correctly?

Thank you,

Tom Henry
Attachments
Screen Shot 8.4.21.png
Screen Shot 8.4.21.png (487.05 KiB) Viewed 162 times
Screen Shot Feeds 8.4.21.png
Screen Shot Feeds 8.4.21.png (220.77 KiB) Viewed 162 times
code.8.4.21.py
(22.98 KiB) Downloaded 7 times

User avatar
Tom_Henry
 
Posts: 350
Joined: Wed Aug 26, 2020 8:19 pm

Re: How to code Adafruit IO Group Feeds

Post by Tom_Henry »

No reply needed.

I just figured it out using trial and error.

Tom Henry

User avatar
Franklin97355
 
Posts: 23940
Joined: Mon Apr 21, 2008 2:33 pm

Re: How to code Adafruit IO Group Feeds

Post by Franklin97355 »

Tom, if you would post your solution it will help others (myself included ;))

User avatar
Tom_Henry
 
Posts: 350
Joined: Wed Aug 26, 2020 8:19 pm

Re: How to code Adafruit IO Group Feeds

Post by Tom_Henry »

I decided to post an Adafruit IO example that uses a group of weather feeds (see attached Group Feeds Screen Shot) and a CircuitPython program (see attached code_for_group_feeds.py), and creates a dashboard display (see attached Dashboard Screen Shot).

The CircuitPython program started out with Bryan Siepert’s CircuitPython program and then I added lots of modifications.

I did this because someone asked me to do this. I did this through lots of trial and error. I really couldn’t find a good explanation anywhere on Adafruit’s website so I thought this might help others set up some group feeds.

I used the following Adafruit components: Metro M4 Airlift Lite board, PMSA003I Particle Matter sensor, Real Time Clock RTC PCF8523, and MS8607 Pressure, Temperature, and Humidity sensor.

I also found that you need to keep your refresh time to around 60 seconds otherwise your program crashes.

All of the items are connected on a breadboard via I2C using the ground, 3.3 volts, SDA, SCL connectors on each component. I also ended up using some JST SH 4-Pin Cables (part # 4399) that worked rather well. I would have added a photo of the layout but you are limited to only 3 attachments for a Forum post.

Here is the output coming from the Mu editor:

CircuitPython Output from Mu on my MacBook

(This is refreshed every 60 seconds.)

Concentration Units (standard)
---------------------------------------
PM 1.0: 0 PM2.5: 0 PM10: 0

Particles 0.3: 27
Particles 0.5: 9
Particles 1.0: 0
Particles 2.5: 0
Particles 5.0: 0
Particles 10.0: 0

Concentration Units (environmental)
---------------------------------------
PM 1.0: 0 PM2.5: 0 PM10: 0
---------------------------------------
Particles > 0.3um / 0.1L air: 27
Particles > 0.5um / 0.1L air: 9
Particles > 1.0um / 0.1L air: 0
Particles > 2.5um / 0.1L air: 0
Particles > 5.0um / 0.1L air: 0
Particles > 10 um / 0.1L air: 0
---------------------------------------

Tom Henry's MS8607 Sensor Data:

Today is Wednesday: 8/11/2021
The time is 15:26:22

Pressure: 1005.22 hPa
Pressure: 14.58 psi
Temperature: 27.66 C
Temperature: 77.07 F
Humidity: 37.31 % rH

Here is the part of the CircuitPython program that publishes the group feeds.

The title of my group is simply Weather. You can see that the format is:

io.publish(“weather.feed name", name related to the feed in your CircuitPython program)

# publish to adafruit io
io.publish("weather.date", todays_date)
io.publish("weather.time", todays_time)
io.publish("weather.humidity", sensor.relative_humidity)
io.publish("weather.pressure", psi)
io.publish("weather.f", F)

io.publish("weather.p1", p1)
io.publish("weather.p2", p2)
io.publish("weather.p3", p3)
io.publish("weather.p4", p4)
io.publish("weather.p5", p5)
io.publish("weather.p6", p6)

sleep(60)

Hope this works for you.

Tom Henry
Attachments
Dashboard Screen Shot.png
Dashboard Screen Shot.png (512.82 KiB) Viewed 106 times
Group Feeds Screen Shot.png
Group Feeds Screen Shot.png (193.94 KiB) Viewed 106 times
code_for _group_feeds.py
(5.38 KiB) Downloaded 5 times

User avatar
Franklin97355
 
Posts: 23940
Joined: Mon Apr 21, 2008 2:33 pm

Re: How to code Adafruit IO Group Feeds

Post by Franklin97355 »

Thank you

Code: Select all

# SPDX-FileCopyrightText: 2020 Bryan Siepert, written for Adafruit Industries
# SPDX-License-Identifier: MIT
# Modified by Tom Henry, 8.12.21
# Metro M4 AirLift Lite: MS8607, PCF8523, PMSA003I

import time
from time import sleep
import board
import busio
from digitalio import DigitalInOut, Direction, Pull
from adafruit_pm25.i2c import PM25_I2C
from adafruit_ms8607 import MS8607
import adafruit_pcf8523

reset_pin = None

from digitalio import DigitalInOut
from adafruit_esp32spi import adafruit_esp32spi
from adafruit_esp32spi import adafruit_esp32spi_wifimanager
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
import neopixel
import adafruit_minimqtt.adafruit_minimqtt as MQTT
from adafruit_io.adafruit_io import IO_MQTT

# Create library object, use 'slow' 100KHz frequency!
i2c = busio.I2C(board.SCL, board.SDA, frequency=100000)

# Connect to a PM2.5 sensor over I2C
pm25 = PM25_I2C(i2c, reset_pin)

# i2c = board.I2C()
sensor = MS8607(i2c)

rtc = adafruit_pcf8523.PCF8523(i2c)

days = ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")

if False:   # change to True if you want to write the time!
    # year, mon, date, hour, min, sec, wday, yday, isdst
    t = time.struct_time((2021,  8,   2,   16,  20,  0,    1,   -1,    -1))
    print("Setting time to:", t) # uncomment for debugging
    rtc.datetime = t

# 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_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)

F = ((sensor.temperature * 1.8) + 32.0)-4.0
psi = (sensor.pressure * 0.0145038)

# Connect to WiFi
print("Connecting to WiFi...")
wifi.connect()
print("Connected!")

# Initialize MQTT interface with the esp interface
MQTT.set_socket(socket, esp)

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

# Initialize an Adafruit IO MQTT Client
io = IO_MQTT(mqtt_client)

# Connect to Adafruit IO
print("Connecting to Adafruit IO...")
io.connect()

print("Found PM2.5 sensor, reading data...")

while True:

    try:
        aqdata = pm25.read()
        # print(aqdata)
    except RuntimeError:
        print("Unable to read from sensor, retrying...")
        continue

    print()
    print("Concentration Units (standard)")
    print("---------------------------------------")
    print(
        "PM 1.0: %d\tPM2.5: %d\tPM10: %d"
        % (aqdata["pm10 standard"], aqdata["pm25 standard"], aqdata["pm100 standard"])
    )


    print()
    print()
    p1 = "%d" % (aqdata["particles 03um"])
    print ("Particles 0.3: ", p1)

    p2 = "%d" % (aqdata["particles 05um"])
    print ("Particles 0.5: ", p2)

    p3 = "%d" % (aqdata["particles 10um"])
    print ("Particles 1.0: ", p3)

    p4 = "%d" % (aqdata["particles 25um"])
    print ("Particles 2.5: ",p4)

    p5 = "%d" % (aqdata["particles 50um"])
    print ("Particles 5.0: ", p5)

    p6 = "%d" % (aqdata["particles 100um"])
    print ("Particles 10.0: ", p6)

    print()
    print()

    print("Concentration Units (environmental)")
    print("---------------------------------------")
    print(
        "PM 1.0: %d\tPM2.5: %d\tPM10: %d"
        % (aqdata["pm10 env"], aqdata["pm25 env"], aqdata["pm100 env"])
    )
    print("---------------------------------------")
    print("Particles > 0.3um / 0.1L air:", aqdata["particles 03um"])
    print("Particles > 0.5um / 0.1L air:", aqdata["particles 05um"])
    print("Particles > 1.0um / 0.1L air:", aqdata["particles 10um"])
    print("Particles > 2.5um / 0.1L air:", aqdata["particles 25um"])
    print("Particles > 5.0um / 0.1L air:", aqdata["particles 50um"])
    print("Particles > 10 um / 0.1L air:", aqdata["particles 100um"])
    print("---------------------------------------")


    t = rtc.datetime
    # print(t) # uncomment for debugging
    print()
    print("Tom Henry's MS8607 Sensor Data:")
    print()
    print("Today is %s: %d/%d/%d" % (days[t.tm_wday], t.tm_mon, t.tm_mday, t.tm_year))
    todays_date = "%s: %d/%d/%d" % (days[t.tm_wday], t.tm_mon, t.tm_mday, t.tm_year)
    print("The time is %d:%02d:%02d" % (t.tm_hour, t.tm_min, t.tm_sec))
    todays_time = "%d:%02d:%02d" % (t.tm_hour, t.tm_min, t.tm_sec)
    print()

    print("Pressure: %.2f hPa" % sensor.pressure)
    print("Pressure: %.2f psi" % psi)
    print("Temperature: %.2f C" % sensor.temperature)
    print("Temperature: %.2f F" % F)
    print("Humidity: %.2f %% rH" % sensor.relative_humidity)
    print("\n------------------------------------------------\n")


    # publish to adafruit io
    io.publish("weather.date", todays_date)
    io.publish("weather.time", todays_time)
    io.publish("weather.humidity", sensor.relative_humidity)
    io.publish("weather.pressure", psi)
    io.publish("weather.f", F)

    io.publish("weather.p1", p1)
    io.publish("weather.p2", p2)
    io.publish("weather.p3", p3)
    io.publish("weather.p4", p4)
    io.publish("weather.p5", p5)
    io.publish("weather.p6", p6)

    sleep(60)








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”