MQTT to ThingSpeak API

From Bright Things Wiki
Jump to: navigation, search

This page documents how to set up a bridge between MQTT messages and a server running the ThingSpeak API.


  1. Access to MQTT live data
  2. Access to a server using the ThingSpeak API
  3. Access to a docker engine

MQTT Live Data

In our case, we've got a TM8110 sensor node reporting temperature and the voltage of it's internal battery once a minute. The sensor node has been configured to use the mqtt server and the mqtt topic of bt/gw/sn0.

First, we can use mosquitto_sub to have a look at the data (CTRL-C will stop the execution):

lth@ncpws04:~$  mosquitto_sub -v -h -u demo -P demo -t bt/gw/sn0/#
bt/gw/sn0/v {"description":"tm8110","value":3706,"unit":"mV"}
bt/gw/sn0/t {"description":"tm8110","value":46.88,"unit":"°C"}
bt/gw/sn0/v {"description":"tm8110","value":3704,"unit":"mV"}
bt/gw/sn0/t {"description":"tm8110","value":47.44,"unit":"°C"}

In other words, we've got two values - the temperature reported with topic bt/gw/sn0/t and the voltage reported as bt/gw?sn0/v. In both cases, the message contains JSON data and it is the "value" field we are interested in.

If you don't have access to a system with mosquitto_sub installed, the docker container, which we will use later, can be used instead:

docker run --entrypoint=mosquitto_sub -ti thingmesh/mqtt2ts -q 2 -h -u demo2 -P demo -t bt/gw/sn0/#

ThingSpeak Server Setup

First we go to and select Sign Up.


Once signed in, we click on the button New Channel.

Channel 4 ThingSpeak.png

The final step is to acquire a write API key. Click on the API Keys tab, and then click on the Generate Write API key button.

API Key.png

Running MQTT2TS

First step is to ensure you have got the latest version of the thingmesh/mqtt2ts image. Simply run:

lth@ncpws04:~$ docker pull thingmesh/mqtt2ts
Using default tag: latest
latest: Pulling from thingmesh/mqtt2ts
398b35933f7c: Already exists 
542604722136: Already exists 
1eb692479468: Already exists 
3f824549dff1: Already exists 
5649599dfae1: Already exists 
9aed159f0c4d: Already exists 
91a9e750f573: Already exists 
ca5482868514: Already exists 
134a7bac7dc1: Already exists 
Digest: sha256:1ec9195bece4e60ae2cd8a345f28482052c3689d410b55e1e5bec83fdbcc1f68
Status: Image is up to date for thingmesh/mqtt2ts:latest

You can now run the container without any arguments:

lth@ncpws04:~$ docker run -ti thingmesh/mqtt2ts
Mqtt2TS version 1.1
Copyright (C) 2015 Bright Things UN Ltd. <>

Mqtt2TS will subscribe to MQTT topics and push the received data to a server running the ThingSpeak API.

Usage: mqtt2ts [option [option]...]


    -h --help          Display this help text
    -v --verbose       Verbose output
    -i --interval      Minimum interval between updates in seconds (default: 10)
    --mqtthost         MQTT server hostname or IP address (default:
    --mqttuser         MQTT user name
    --mqttpwd          MQTT password
    --tsurl            ThingSpeak base URL (default:
    --tsapikey         ThingSpeak API key
    --topic1           MQTT topic for field1
    --topic2           MQTT topic for field2
    --topic3           MQTT topic for field3
    --topic4           MQTT topic for field4
    --topic5           MQTT topic for field5
    --topic6           MQTT topic for field6
    --topic7           MQTT topic for field7
    --topic8           MQTT topic for field8
    --value1           JSON value for field1
    --value2           JSON value for field2
    --value3           JSON value for field3
    --value4           JSON value for field4
    --value5           JSON value for field5
    --value6           JSON value for field6
    --value7           JSON value for field7
    --value8           JSON value for field8

This will show you how to use the Docker image. Based on the information gathered in the previous sections of this document, the following command line can be created:

lth@ncpws04:~$ docker run -ti thingmesh/mqtt2ts --verbose --interval=60 --mqttuser=demo --mqttpwd=demo --tsapikey=PFOWIBNJ2WVK0LAQ --topic1=bt/gw/sn0/t --value1=value --topic2=bt/gw/sn0/v --value2=value
Interval: 60
Subscribe: mosquitto_sub -v -i ede10d7fe6db -h -u demo3 -P demo -t bt/gw/sn0/t -t bt/gw/sn0/v
Got MQTT topic=bt/gw/sn0/v message={"description":"tm8110","value":4013,"unit":"mV"}
31 seconds since last update
Got MQTT topic=bt/gw/sn0/t message={"description":"tm8110","value":28.44,"unit":"°C"}
31 seconds since last update
Got MQTT topic=bt/gw/sn0/v message={"description":"tm8110","value":4007,"unit":"mV"}
91 seconds since last update
Running: curl --silent
Got MQTT topic=bt/gw/sn0/t message={"description":"tm8110","value":28.50,"unit":"°C"}
0 seconds since last update

Notice that I have omitted --mqtthost and --tsurl since the values needed for this example are the default values for the Docker image.

If any of the arguments need changing, the container can be stopped by pressing CTRL-C. Once all the arguments are correct and updates are received on the ThingSpeak server, the container can be started in the background by replacing the -ti in the above example with -d.

The result should look like this:

MQTT2TS Demo ThingSpeak.png

Or click to see the live version.