MQTT, or message queue telemetry transport, is a protocol for device communication that Adafruit IO supports. Using a MQTT library or client you can publish and subscribe to a feed to send and receive feed data.
To use the MQTT API that Adafruit IO exposes you'll need a MQTT client library. For Python, Node.js, and Arduino you can use Adafruit's IO client libraries as they include support for MQTT (see the client libraries section). For other languages or platforms look for a MQTT library that ideally supports the MQTT 3.1.1 protocol.
You will want to use the following details to connect a MQTT client to Adafruit IO:
- Host: io.adafruit.com
- Port: 1883 or 8883 (for SSL encrypted connection)
- Username: your Adafruit account username (see the accounts.adafruit.com page here to find yours)
- Password: your Adafruit IO key (click the AIO Key button on a dashboard to find the key)
We strongly recommend using SSL if your MQTT client allows it.
If the MQTT library requires that you set a client ID then use a unique value like a random GUID. Most MQTT libraries handle setting the client ID to a random value automatically though.
Adafruit IO's MQTT API exposes feed data using special topics. You can publish a new value for a feed to its topic, or you can subscribe to a feed's topic to be notified when the feed has a new value. Any one of the following topic forms is valid for a feed:
- (username)/feeds/(feed name or key)
- (username)/f/(feed name or key)
Where (username) is your Adafruit IO username (the same as specified when connecting to the MQTT server) and (feed name or key) is the feed's name or key. The smaller '/f/' path is provided as a convenience for small embedded clients that need to save memory.
For example if your username is mosfet and you're accessing a feed called Photocell One (which has a Key of photocell-one) you can use any of these paths:
- mosfet/feeds/Photocell One
- mosfet/f/Photocell One
To append a new value to a feed perform a MQTT publish against the feed path and provide the new feed value as the payload of the request.
To be notified of a change in a feed perform a MQTT subscribe against the feed path. When a new value is added to the feed then the Adafruit IO MQTT server will send a notification with the new feed value in the payload.
You can also subscribe to the parent 'feeds' path to be notified when any owned feed changes using MQTT's
# wildcard character. For example the mosfet user could subscribe to either:
Once subscribed to the path above any change to a feed owned by mosfet will be sent to the MQTT client. The topic will specify the feed that was updated, and the payload will have the new value.
Be aware the MQTT server sends feed updates on all possible paths for a specific feed. For example, subscribing to
mosfet/f/# and publishing to
mosfet/f/photocell-one would produce messages from: mosfet/f/photocell-one, mosfet/f/photocell-one/json, and mosfet/f/photocell-one/csv; each referring to the same updated value. To reduce noise, make sure to grab the specific topic of the feed / format you're interested in and change your subscription to that.
PLEASE NOTE: as we adjust which identifiers we use for Feeds internally, the feed updates you receive when using a wildcard will include but may not be limited to the ones shown above.
If you'd like to avoid the formatted feeds ("/json" and "/csv" topics) but still see all the feeds you're publishing to, you can use MQTT's
+ wildcard in place of
#. In this case, subscribing to
mosfet/f/+ would produce output on
mosfet/f/photocell-one, but not
One feature of MQTT is the ability to specify a QoS, or quality of service, level when publishing feed data. This allows an application to confirm that its data has been sucessfully published. If you aren't familiar with MQTT QoS levels be sure to read this great blog post explaining their meaning.
For publishing feed values the Adafruit IO MQTT API supports QoS level 0 (at most once) and 1 (at least once) only. QoS level 2 (exactly once) is not currently supported.
Adafruit IO's MQTT server imposes a rate limit to prevent excessive load on the service. If a user performs too many publish actions in a short period of time then some of the publish requests might be rejected. The current rate limit is at most 2 requests per second (or 120 requests within 60 seconds).
If you exceed this limit, a notice will be sent to the (username)/throttle topic. You can subscribe to the topic if you wish to know when the Adafruit IO rate limit has been exceeded for your user account.
This limit applies to all connections so if you have multiple devices or clients publishing data be sure to delay their updates enough that the total rate is below 2 requests/second.