MQTT is a publish/subscribe architecture that is developed primarily to connect bandwidth and power-constrained devices over wireless networks. It is a simple and lightweight protocol that runs over TCP/IP sockets or WebSockets. MQTT over WebSockets can be secured with SSL. The publish/subscribe architecture enables messages to be pushed to the client devices without the device needing to continuously poll the server.
The MQTT broker is the central point of communication, and it is in charge of dispatching all messages between the senders and the rightful receivers. A client is any device that connects to the broker and can publish or subscribe to topics to access the information. A topic contains the routing information for the broker. Each client that wants to send messages publishes them to a certain topic, and each client that wants to receive messages subscribes to a certain topic. The broker delivers all messages with the matching topic to the appropriate clients.
ThingSpeak™ has an MQTT broker at the URL mqtt.thingspeak.com
and
port 1883
. The ThingSpeak broker supports both MQTT publish and MQTT subscribe as shown in the
diagrams.
The figure describes the topic structure. The Write API Key is required to
publish. The broker acknowledges a correct CONNECT
request with
CONNACK
.
The figure describes the topic structure. To subscribe to a public or private
channel, you must also supply a user name and your MQTT API key as password when you
connect. The broker acknowledges a correct SUBSCRIBE
request with
SUBACK
.
To use MQTT subscribe, supply any unique user name and the MQTT API Key as your password when you connect. MQTT publish does not require the API key. To see your MQTT API key, choose Account > My Profile.
If the connection is successful, the ThingSpeak MQTT broker responds with a CONNACK
, a connection
acknowledgment. The MQTT broker responds to a successful subscription request with a
SUBACK
message and relays any new data posted to the
subscribed channel or field to the client. To learn more about these messages, see
MQTT v3.1.1 specification. You can then publish messages to topics using
the form Publish to a Channel Feed, or Publish to a Channel Field Feed.
ThingSpeak has an MQTT broker at the URL mqtt.thingspeak.com
.
Configure your MQTT client to communicate with the ThingSpeak MQTT broker based on one of the following options:
Port |
Connection Type |
Encryption |
---|---|---|
1883 |
TCP |
None |
8883 |
TCP |
TLS/SSL |
80 |
WebSocket |
None |
443 |
WebSocket |
TLS/SSL |
Note
If your hardware supports secure SSL socket connection mode, use that mode to communicate with the ThingSpeak MQTT broker whenever possible.
ThingSpeak implements MQTT publish and subscribe functionality with
QoS
0
. This "fire-and-forget" protocol means that your
client does not get any acknowledgment when you publish to a ThingSpeak channel.
There is a one hour timeout on idle MQTT connections
Publish to a Channel Feed | Publish to a Channel Field Feed | Subscribe to a Channel Feed | Subscribe to a Channel Field Feed