MQTT (Message Queue Telemetry Transport) is a machine-to-machine (M2M) data transfer protocol and the leading messaging protocol for the Industrial Internet of Things (IIoT). Although there are currently a number of competing IoT technologies and protocols in play, MQTT's extremely lightweight overhead (2-byte header), publish/subscribe model and bidirectional capabilities are ideally suited to meet the requirements of industrial control systems.
MQTT is a publish/subscribe protocol that allows edge-of-network devices (e.g. our CO2 sensor) to publish to a broker. Clients (e.g. a web application) connect to this broker, which then mediates between the communication of the two devices. Each device can subscribe or register to certain topics.
Unlike the usual poll/response models of many protocols, which tend to unnecessarily saturate data connections with unchanging data, publish/subscribe models actually maximise the available bandwidth.
MQTT was created with the aim of collecting data from as many devices as possible and then transporting this data to an IT infrastructure. It is lightweight and therefore ideal for remote monitoring, especially in M2M connections where a small footprint code is required or where network bandwidth is limited.
As there can be many data generating sensors and there are usually also many 'customers', the number of connections would be enormous if they were connected 1:1. To prevent this, MQTT has a middle layer: the broker, indicated above by the round shape.
Simply put, it comes down to the following:
- A sensor (from now on we will call him the Publisher Client) reports to the broker and says 'under this name I am going to offer data for the coming period, good luck with that'. The name under which he will submit his data is called "Topic". We also note that the Publisher Client only has one connection to maintain (i.e. with the broker) regardless of where the data eventually comes from or where it is needed.
- One or more clients (dashboard, logging database etc), which we call 'Subscriber client', report to the broker and say 'I would like to have a 'subscription' to this specific 'Topic'. From that moment on, every time there is new data within this Topic, the broker will communicate it to all 'Subscribers'.
We also note that with the addition of the broker, both the Publisher client and the 'subscriber client' only have to maintain one connection (i.e. with the broker) regardless of the number of sensors or the number of locations where the data is ultimately needed.
How to create a web dashboard
From the explanation above we see that we need three components. A Public Client (our CO2 sensor of course), a Broker and a Subscribing Client (the dashboard).
As a dashboard and broker we will use Tago.io.
If you do not have a Tago.io account yet, create one first (free account possible). Once it is created, create a new Device.
Name your device, set Payload as JSON and click Create my Device.
Under General Information tab you can create a Token. You will need this token to set up the CO2 sensor.
Enter a Token Name and click on Generate.
Click on the eye icon to view the Token.
We will need this token to set up our CO2 sensor.
The CO2 sensor has Wi-Fi on board and MQTT support (both are off by default). So we need to activate both. To activate them, press the button on the right of the USB connector. The CO2 sensor will now create a new Wi-Fi network with the name (SSID) operame-xxxx (xxxx is a unique number). This name is also shown on the display.
Now connect to this network (e.g. via your smartphone or computer). Then open a web browser and surf to 192.168.4.1. You will see the following screen.
Check Wi-Fi and enter SSID and password of your Wi-Fi network (the Wi-Fi network you want to connect your CO2 sensor to).
Also tick MQTT and fill in Tago.io Broker information.
- Broker: mqtt.tago.io
- Broker TCP port: 1883
- Username MQTT: Token
- Password MQTT: your device token created in Tago.io
All we need to do is enter the topics. You are free to give any name to a topic. But because you need to use it later in Tago.io, it is best to keep it simple.
- Topic for CO2
- Topic for temperature: temp
- Topic for humidity: hum
If you want to send temperature and humidity as well, you have to check the box for Activate MQTT for temperature and humidity as well.
Then click on Save (bottom right) and then Restart (top left). The CO2 sensor will reboot and send data to the Broker.
Data is now being sent by the CO2 sensor via MQTT. To visualise this data in Tago.io you can use the Live Inspector.
Select your device and select the Live Inspector tab. Press the green Play button.
Then wait until you see the data coming in.
If you see the messages coming in and the data looks correct, then you know the connection is working. But nothing is saved yet. We now need to create an Action.
To do this, go to Actions and click on Add Action.
Give your action a name. Set the trigger to MQTT Topic. Choose Insert to Device Bucket as Action Type. Then click on Create my Action.
Then select your device and add the MQTT topics. Do not forget to press Save to save your changes.
Now go back to your device and to the Live Inspector. Click on the green Play button again and wait for the data to come in.
You can now see that the data is kept in a Bucket. Notice also that we get the three different variables (CO2, temperature and humidity) with their value and unit.
Because the CO2 sensor already transmits the data in Tago.Io format, we do not need to normalise the data.
We can now create a Dashboard.
To add a Dashboard, click on the plus symbol next to Dashboards.
Name your Dashboard and choose a Normal dashboard. Click on Create my Dashboard to create your dashboard.
Click on Add widget to add a widget.
As an example, we will choose Display as the widget.
Select your Device and Variable. If necessary, give it an appropriate title and click on Create.
Your widget is now created and displays the CO2 value sent by your CO2 sensor.
We can now do the same for temperature and humidity.
Click on the plus icon at the top right.
This time we choose a Solid widget.
Enter a suitable title, select your device and variable and adjust the range if necessary. Click on Create to create the widget.
Temperature has now been added to our dashboard.
And finally, we add the humidity.
From now on you can let yourself go 🕺