6. Adafruit IO - CRCibernetica/circuitpython-ideaboard GitHub Wiki

Overview

One of the easiest ways to use your IdeaBoard in IoT projects is Adafruit IO. Adafruit IO makes it easy to create store and graph your data, create dashboards and create actions that respond to your data.

Learn more about Adafruit IO

Adafruit IO Account

  1. Create a free account at Adafruit IO
  2. Find your Adafruit IO username and Active Key (password) by clicking on the yellow key. These will be used later in the code.

image

  1. Copy the adafruit_io and adafruit_minimqtt libraries to the IdeaBoard

Example Code

The IdeaBoard uses the example code named adafruit_io_simpletest_esp32s2.py. The code below comes from the Example Bundle.

The example requires two files: the main code file code.py and secrets.py where the credentials are stored.

Save this file as secrets.py

# This file is where you keep secret settings, passwords, and tokens!
# If you put them in the code you risk committing that info or sharing it

secrets = {
    'ssid' : 'ssid',
    'password' : 'password',
    'aio_username' : 'adafruit_io_username',
    'aio_key' : 'adafruit_io_key'
}

Save this file as code.py

# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT
import time
from random import randint

import ssl
import socketpool
import wifi
import adafruit_minimqtt.adafruit_minimqtt as MQTT
from adafruit_io.adafruit_io import IO_MQTT

### WiFi ###

# Add a secrets.py to your filesystem that has a dictionary called secrets with "ssid" and
# "password" keys with your WiFi credentials. DO NOT share that file or commit it into Git or other
# source control.
# pylint: disable=no-name-in-module,wrong-import-order
try:
    from secrets import secrets
except ImportError:
    print("WiFi secrets are kept in secrets.py, please add them there!")
    raise

# 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.)
aio_username = secrets["aio_username"]
aio_key = secrets["aio_key"]

print("Connecting to %s" % secrets["ssid"])
wifi.radio.connect(secrets["ssid"], secrets["password"])
print("Connected to %s!" % secrets["ssid"])

# Define callback functions which will be called when certain events happen.
# pylint: disable=unused-argument
def connected(client):
    # Connected function will be called when the client is connected to Adafruit IO.
    # This is a good place to subscribe to feed changes.  The client parameter
    # passed to this function is the Adafruit IO MQTT client so you can make
    # calls against it easily.
    print("Connected to Adafruit IO!  Listening for DemoFeed changes...")
    # Subscribe to changes on a feed named DemoFeed.
    client.subscribe("DemoFeed")


def subscribe(client, userdata, topic, granted_qos):
    # This method is called when the client subscribes to a new feed.
    print("Subscribed to {0} with QOS level {1}".format(topic, granted_qos))


def unsubscribe(client, userdata, topic, pid):
    # This method is called when the client unsubscribes from a feed.
    print("Unsubscribed from {0} with PID {1}".format(topic, pid))


# pylint: disable=unused-argument
def disconnected(client):
    # Disconnected function will be called when the client disconnects.
    print("Disconnected from Adafruit IO!")


# pylint: disable=unused-argument
def message(client, feed_id, payload):
    # Message function will be called when a subscribed feed has a new value.
    # The feed_id parameter identifies the feed, and the payload parameter has
    # the new value.
    print("Feed {0} received new value: {1}".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",
    port=1883,
    username=secrets["aio_username"],
    password=secrets["aio_key"],
    socket_pool=pool,
    ssl_context=ssl.create_default_context(),
)

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

# Connect the callback methods defined above to Adafruit IO
io.on_connect = connected
io.on_disconnect = disconnected
io.on_subscribe = subscribe
io.on_unsubscribe = unsubscribe
io.on_message = message

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

# Below is an example of manually publishing a new  value to Adafruit IO.
last = 0
print("Publishing a new message every 10 seconds...")
while True:
    # Explicitly pump the message loop.
    io.loop()
    # Send a new message every 10 seconds.
    if (time.monotonic() - last) >= 10:
        value = randint(0, 100)
        print("Publishing {0} to DemoFeed.".format(value))
        io.publish("DemoFeed", value)
        last = time.monotonic()

Output when running code.py

>>> %Run -c $EDITOR_CONTENT
Connecting to idealab
Connected to idealab!
Connecting to Adafruit IO...
Connected to Adafruit IO!  Listening for DemoFeed changes...
Subscribed to crcibernetica/f/DemoFeed2 with QOS level 0
Publishing a new message every 10 seconds...
Publishing 58 to DemoFeed.
Feed DemoFeed2 received new value: 58
Publishing 84 to DemoFeed.
Feed DemoFeed2 received new value: 84

Seeing the data

In the Adafruit account click on Feeds. DemoFeed should appear.

image

Clicking on the link will show a graph of the raw data.

image