Arduino library · Network

Adafruit IO Arduino: feeds and dashboards from an ESP32

Yes, you can build with the Adafruit IO Arduino library online in Mokxi with nothing to install. Its hardware is not simulated, so a sketch says plainly that it cannot reach the device. The example below, "An Adafruit IO counter: WiFi joins, Adafruit IO cannot", runs on the ESP32-C3-DevKitM-1.

Built in on Mokxi: #include <AdafruitIO_WiFi.h> and it builds, with nothing to install. Free, and no account needed.

What Adafruit IO Arduino does

Adafruit IO is Adafruit’s cloud for small IoT projects: a board writes numbers to named feeds, and dashboards on the website chart them or send button presses back. The Adafruit IO Arduino library is the high-level way in. One object, AdafruitIO_WiFi, takes your Adafruit IO user name and key plus the WiFi name and password, and joins both for you.

From that object you get feed pointers with io.feed("temperature"). save() writes a value; onMessage() registers a function for values arriving from the dashboard; get() asks for the latest value. io.run() has to run at the top of loop(), since it keeps the connection up and delivers messages. The official examples keep the credentials in a separate config.h tab.

In Mokxi the example’s counter sketch runs on an ESP32-C3 and shows where it stops: connect() really joins the simulated WiFi, but Adafruit IO is MQTT over TCP with TLS, which the network does not carry. io.status() stays AIO_DISCONNECTED, statusText() says why, and save() returns false. Note that the examples wait for AIO_CONNECTED forever; the sketch here gives up after four seconds.

Library Manager nameAdafruit IO Arduino
AuthorAdafruit
Include#include <AdafruitIO_WiFi.h>
#include <AdafruitIO.h>
BoardsESP32-C3, ESP32-C6, Seeed XIAO ESP32-C3, ESP32-C3 SuperMini, Seeed XIAO ESP32-C6, ESP32 DevKit, ESP32-DevKitC V4, LOLIN32 Lite, Wemos D1 R32, Cheap Yellow Display, TTGO T-Display, ESP32-S3-DevKitC-1, Seeed XIAO ESP32-S3, Arduino Nano ESP32, ESP32-S3-CAM, ESP8266 NodeMCU
In the simulatorBuilds and runs, and says plainly that the hardware behind it is not simulated.

Install it

It builds on ESP32-C3, ESP32-C6, Seeed XIAO ESP32-C3, ESP32-C3 SuperMini, Seeed XIAO ESP32-C6, ESP32 DevKit, ESP32-DevKitC V4, LOLIN32 Lite, Wemos D1 R32, Cheap Yellow Display, TTGO T-Display, ESP32-S3-DevKitC-1, Seeed XIAO ESP32-S3, Arduino Nano ESP32, ESP32-S3-CAM, ESP8266 NodeMCU.

In Mokxi it is built in: #include <AdafruitIO_WiFi.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board, install "Adafruit IO Arduino" by Adafruit from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add Adafruit IO Arduino to lib_deps in PlatformIO. Mokxi follows the calls of release 4.3.7, so a sketch written here builds there too.

What to know in the simulator: connect() really joins the WiFi network, but Adafruit IO is MQTT over TCP with TLS, which the network does not carry, so io.status() stays AIO_DISCONNECTED with a statusText() saying why, and save() returns false. The examples’ wait for AIO_CONNECTED never ends, so give it a deadline.

The calls you will use

In the order a sketch meets them.

AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASS)

The connection object, with your Adafruit IO user name and key and the WiFi network to join.

AdafruitIO_Feed *counter = io.feed("counter")

A pointer to a feed by name. The feed is created on Adafruit IO the first time a value is saved to it.

io.connect()

Starts joining WiFi and then Adafruit IO. It returns at once; watch io.status() for the result.

io.status()

Where the connection stands, from AIO_IDLE through AIO_NET_CONNECTED to AIO_CONNECTED. Compare with AIO_CONNECTED before sending.

unsigned long t = millis();
while (io.status() < AIO_CONNECTED && millis() - t < 10000) delay(500);
Serial.println(io.statusText());

io.statusText()

The status in words, for the serial monitor.

io.run()

Keeps the connection alive and delivers incoming messages. It must be the first line of loop().

counter->save(count)

Sends a value to the feed and returns true when it went out.

counter->onMessage(handleMessage)

Registers a function given an AdafruitIO_Data pointer when the feed changes, for example from a dashboard toggle. Read it with data->toInt().

Example: An Adafruit IO counter: WiFi joins, Adafruit IO cannot

On the ESP32-C3-DevKitM-1. It runs in the browser exactly as printed.

sketch.ino · ESP32-C3-DevKitM-1
// After the library's adafruitio_00_publish example.
#include "AdafruitIO_WiFi.h"

#define IO_USERNAME "your_username"
#define IO_KEY      "your_key"
#define WIFI_SSID   "Mokxi-Lab"
#define WIFI_PASS   "breadboard"

AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASS);
AdafruitIO_Feed *counter = io.feed("counter");
int count = 0;

void setup() {
  Serial.begin(115200);
  Serial.print("Connecting to Adafruit IO");
  io.connect();
  unsigned long started = millis();
  while (io.status() < AIO_CONNECTED && millis() - started < 4000) {
    Serial.print(".");
    delay(500);
  }
  Serial.println();
  Serial.println(io.statusText());
  Serial.print("WiFi: ");
  Serial.println(WiFi.status() == WL_CONNECTED ? "joined" : "not joined");
}

void loop() {
  io.run();
  Serial.print("sending -> ");
  Serial.print(count);
  Serial.println(counter->save(count) ? " saved" : " not saved");
  count++;
  delay(1000);
}

Open it in the editor, wired and ready to run

Common errors and how to fix them

The serial monitor prints dots forever after "Connecting to Adafruit IO"

The example waits for AIO_CONNECTED with no time limit. Check the WiFi details and the AIO key, and add a deadline with millis() so the sketch reports io.statusText() instead of hanging. In Mokxi it always stops at AIO_DISCONNECTED.

Values stop appearing and the dashboard shows a throttle warning

Adafruit IO limits how many data points an account may send per minute, and save() in every pass of loop() goes far over it. Send on a timer of a few seconds, or only when the value changes.

config.h: No such file or directory

The official examples put the user name, key and WiFi settings in a second tab named config.h. Copy that tab with the sketch, or replace the include with the four defines written at the top of the main file.

Questions and answers

Do I need the Adafruit MQTT Library as well?

Yes, Adafruit IO Arduino is built on it and on ArduinoHttpClient, so the Library Manager installs them together. In Mokxi they are all built in.

Try Adafruit IO Arduino without installing anything

Open the example, change the sketch, and keep your version in a free account.