MQTT with Adafruit

Adafruit is an open-source hardware company that is popular among the maker community. One of their services is Adafruit IO, which is a MQTT broker.

The broker has a free tier with some restrictions. It is perfectly usable for makers with few devices.


We assume that you have set up your development environment as described in the IDE tutorial.

We also assume that you have flashed your device with Jaguar and that you are familiar with running Toit programs on it. If not, have a look at the Hello world tutorial.

While not necessary, we recommend to do the MQTT tutorial first.

Note that you can do this tutorial without a device. In that case, you need to use the -d host option whenever you invoke jag run. The program will then run on your computer instead of on a device.


The MQTT functionality is not part of the core libraries and must be imported as a package. See the packages tutorial for details.

We are using the mqtt package. To install it, run the following command:

jag pkg install

When connecting to TLS secured services we will also use the certificate-roots package:

jag pkg install

Adafruit IO

To use Adafruit IO, you need to create an account. Go to and create an account.

Once you have created an account, you need to create a feed. Feeds are topics that you can publish to and subscribe from. Go to and click on the "+ New Feed" button to create a new feed. For this tutorial we will use the feed temperature.

Click on the "AIO Key" button on the top right of the page (next to "+ New Device"). This opens a dialog with your AIO key.

Screenshot of the AIO key button
Screenshot of the AIO key button

Copy the username and key. You will need them later.


The following code connects to Adafruit IO and publishes a message every 2.5 seconds. The message goes to the feed temperature and will thus be a simple number. The number is a simulated temperature reading, and will randomly fluctuate (starting at 25°C).

Don't forget to replace <YOUR_USERNAME> and <YOUR_KEY> with your username and key. If you created a feed with a different name, you also need to change the ADAFRUIT-IO-FEEDNAME constant.

We will leave the CLIENT-ID empty. This means that the broker will generate a random client ID for us. It also resets the session with every connection. If you want to take full advantage of the broker, you should use a persistent client ID. See the MQTT tutorial for details.

import certificate-roots
import mqtt


ADAFRUIT-IO-FEEDNAME ::= "temperature"


CLIENT-ID ::= ""


  client := mqtt.Client.tls --host=ADAFRUIT-IO-HOST
  // Alternatively, you can also connect without TLS, by using the
  // following client constructor:
  client := mqtt.Client --host=ADAFRUIT-IO-HOST
  // In that case you can remove the `certificate-roots` import and
  // avoid calling `certificate-roots.install-common-trusted-roots`.

  options := mqtt.SessionOptions
    --client-id = CLIENT-ID
    --username = ADAFRUIT-IO-USERNAME
    --password = ADAFRUIT-IO-KEY

  client.start --options=options

  print "Connected to broker"


  // Simulates a temperature sensor.
  temperature := 25.0
    temperature += ((random 100) - 50) / 100.0
    client.publish topic "$temperature"
    // Don't publish too often to avoid rate limiting.
    sleep --ms=2_500


You can see the published values on<ADAFRUIT_IO_USERNAME>/feeds/temperature.

It is not good practice to hard-code credentials in your program. See the secrets tutorial for details on how to handle secrets in Toit projects.

Adafruit IO features

In this tutorial we only publish messages to Adafruit IO. However, Adafruit IO also supports many other features: You can, obviously, also subscribe to feeds. You can also create dashboards and visualize your data. You can also create triggers that send you emails or text messages when certain conditions are met.

The Adafruit IO documentation contains a lot of information about the service.

For MQTT specific information, see Adafruit IO MQTT API.