Main Content

Publish and Subscribe to a Channel Using Desktop MQTT Client

This example shows how to use a desktop MQTT client to publish and subcribe to a channel. The example uses MQTT X, an MQTT 5.0 desktop client open-sourced by EMQ. If you have more than one sensor value that you want to send to ThingSpeak™, you can choose to publish multiple values to a channel feed. If you have only one sensor, you can publish a single value to a channel field.

Prepare Software

1) Create a new channel, as shown in Collect Data in a New Channel.

2) Create a new MQTT device by clicking Devices > MQTT at the top of the ThingSpeak page, then Add Device. When you set up the device, add your new channel. For details, see Create a ThingSpeak MQTT Device.

3) While adding the new device, click Download Credentials > Plain Text. Keep this downloaded file for access in the Configure section below.

4) Download and install MQTT X.

Configure MQTT X Client

Configure the connection profile for the desktop MQTT client.

1) Click New Connection, or the plus (+) icon in the laft pane.

2) In the General configuration box, enter the following information:

Name: MyChannel (Any name you want to use for this MQTT X connection)

Client ID: ENTER_YOUR_MQTT_CLIENT_ID (from saved credentials file)

Host: mqtt:// mqtt3.thingspeak.com

Port: 1883

Username: ENTER_YOUR_MQTT_USERNAME (from saved credentials file, might be the same as Client ID)

Password: ENTER_YOUR_MQTT_PASSWORD (from saved credentials file)

3) In the Advanced configuration box:

Clean Session: true (for best performance)

MQTT Version: 3.1.1

4) Click Connect.

Subscribe to a Channel Feed

To subscribe to a channel:

1) Click + New Subscription.

2) Set the following fields in the New Subscription dialog:

Topic: channels/<channelID>/subscribe (use the channel number created in the Prepare section)

QoS: 0

3) Click Confirm.

Publish to Channel Feed

At the bottom of the right pane is the area for publishing to a channel.

1) Enter the following fields:

Payload: Plaintext

QoS: 0

Topic: channels/<channelID>/publish (replace <channelID> with the channel ID)

Data field: field1=45&field2=60&status=MQTTPUBLISH

2) Click the send arrow.

The log shows both the publish and subscription results:

                Topic: channels/33301/publishQoS: 0

                field1=45&field2=60&status=MQTTPUBLISH


Topic: channels/33301/subscribeQoS: 0

{"channel_id":33301,"created_at":"2021-04-29T19:45:43Z","entry_id":23,
"field1":"45","field2":"60","field3":null,"field4":null,"field5":null,
"field6":null,"field7":null,"field8":null,"latitude":null,"longitude":null,
"elevation":null,"status":"MQTTPUBLISH"}

Publish to Channel Field

Alternatively, you can enter the following to publish to a single channel field.

Topic: channels/<channelID>/publish/fields/field1

Data field: 45

Replace <channelID> with the channel ID. This PUBLISH message publishes a value of 45 to field 1 of the specified channel.

See Also

|

Related Examples

More About