Main Content

MQTT Basics

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 URLmqtt3.thingspeak.comand port1883. The ThingSpeak broker supports both MQTT publish and MQTT subscribe as shown in the following diagrams.

MQTT Publish

This figure describes the topic structure. The broker acknowledges a correctCONNECTrequest withCONNACK.

MQTT Subscribe

This figure describes the topic structure. The broker acknowledges a correctSUBSCRIBErequest withSUBACK.

Create a ThingSpeak MQTT Device

MQTT access to your channels, including credentials, is handled by a ThingSpeak MQTT device. Your device is configured with the credentials necessary for your MQTT client to communicate with ThingSpeak, and for authorizing specific channels. Use the following steps to create an MQTT device.

  1. In the ThingSpeak menu clickDevices>MQTT.

    Device menu

  2. On the MQTT Devices page, clickAdd Device>Add a new device.

    Add device option

  3. Fill in the Add a new device dialog:

    New device dialog

    1. Provide a device name.

    2. Provide an optional description.

    3. In the Authorize Channels section, choose a channel from the list menu, then clickAdd Channel. Repeat this until you have all the channels you want connected to the device in the Authorized Channel list. Set each to allow publish and allow subscribe as needed.

    4. ClickAdd Device.

    5. At this point, ThingSpeak generates a list of credentials for your device that includes client ID, username, and password. You can view and copy these items from this page, or click下载证书to save the credentials in a local file.Important: Record or save your credentials now, as you will not get another opportunity to view or save the password.

    6. ClickDoneto complete the device creation.

Your new device now appears on the MQTT Devices page. You can alter your device settings and add channels to it by clicking编辑. You can use the client ID, username, and password in your MQTT client to access your ThingSpeak MQTT device for publishing and subscribing to its channels.

Note

You can have up to 100 MQTT devices per user, with a maximum of 10 authorized channels per device. If you require more devices or channels,Contact Support.

For information on using a desktop MQTT client, seePublish and Subscribe to a Channel Using Desktop MQTT Client.

Server Response for Connections

If the connection is successful, the ThingSpeak MQTT broker responds with a connection acknowledgment,CONNACK. The MQTT broker responds to a successful subscription request with aSUBACKmessage and relays any new data posted to the subscribed channel or field to the client. To learn more about these messages, seeMQTT v3.1.1specification. You can then publish messages to topics using the formPublish to a Channel FeedorPublish to a Channel Field Feed.

MQTT Client Configuration

ThingSpeak has an MQTT broker at the URLmqtt3.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

When configuring your client connection, consider the following:

  • 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 withQoS=0. This "fire-and-forget" protocol means that your client does not receive an acknowledgment when you publish to a ThingSpeak channel.

  • MQTT connections time out after one hour of idleness.

  • When using a WebSocket connection the required path is/mqtt.

    • Using the Paho library with Python®on Raspberry Pi™, the default MQTT path is"/mqtt", but you can also set this with the code line:

      client.ws_set_options("/mqtt")
    • HiveMQ MQTT CLI,设置路径与the command option:

      mqtt pub … -ws -ws:path /mqtt …

See Also

|||

Related Topics

External Websites