Tutorials: Connect to an MQTT Broker
DESIGNED IN AUSTRALIA. BUILT IN USA.
Using Mosquitto MQTT on the FATBOX G3 to connect to a MQTT Broker running in AWS
In this tutorial, we will run through the setting up of the Mosquitto MQTT client on the FATBOX G3 side to publish remote IoT device data to a MQTT broker running in AWS. From there the JSON message stream can be ingested by a MQTT subscriber and further processed (e.g. sent to a real-time dashboard for visualization).
Prerequisite: Installing and Running Mosquitto MQTT in Amazon AWS
There are plenty of other tutorials out there about installing an instance on AWS (or you are already adept at it) so we will not be going through this in detail. For our simple demo, we created a 512MB Ubuntu (OS Only) instance and installed Mosquitto as follows:
sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa
sudo apt-get update
sudo apt-get install mosquitto
sudo apt-get install mosquitto-clients
After installing, the Mosquitto broker will automatically start and listen on port 1883. If needed you can change this using
mosquitto -p "port number"
i.e. mosquitto -p 1885
Option 1: Using the Web Configuration MQTT Client on the G3 Gateway
Log in to the FATBOX G3 web configuration menu and go to the “IoT Client” tab and enter the details of your Broker IP and Broker Port following the example configuration below.
Note that this is a basic MQTT client to broker implementation and not recommended for sensitive data unless connected over a secure VPN network if security is required.
Once this is set up, the JSON data to the test MQTT Broker running in AWS (or your own broker set up) automatically.
Option 2: Using our sample Linux BASH Script
For users who want more autonomy (i.e. to run their own security certs) we also have a sample Linux BASH script.
Make sure your IoT device data buffer file (i.e. /tmp/dataq) is being updated with data from the attached devices. See FAQ.
Then run the following
sh MQTTclient "file of data to send" "mqtt subscriber at AWS" "mqtt server IP port#"
i.e. root@FATBOX: /scripts# sh MQTTclient /tmp/dataq.txt mqtt 184.108.40.206 1883
From your AWS instance you can now test the data sent from your FATBOX G3 by starting a Mosquitto subscriber to the topic you set above. It reads 'data' JSON messages from the buffer file FIFO and send each data JSON string to the MQTT broker we have installed earlier. After each JSON line has been sent, that string will be deleted from the buffer file.
mosquitto_sub -t “topic”
i.e. mosquito_sub -t mqtt
<SAMPLE OF JSON DATA STRING RECEIVED ON AWS>
Congrats! You have successful connected your data and from here you can further process it e.g. piping the data to another application.
Send JSON data using MQTT with TLS Security to a MQTT Broker
In this tutorial, we will explain the simple steps to modify our standard Ubidots MQTT script to send your Modbus or CAN bus device data (in JSON) to a MQTT broker with TLS security. We will be using the Mosquitto test server in this article, from there you can implement the same with your own MQTT broker.
SSH over to your FATBOX and modify the following in the scripts/ubidots/ubidots_client.py file,
BROKER_ENDPOINT = "test.mosquitto.org" #mosquitto test server TLS_PORT = 8883 # Secure port
MQTT_USERNAME = ""
MQTT_PASSWORD = ""
TOPIC = "your_topic" #we use “fatbox” in our example
DEVICE_LABEL = ""
TLS_CERT_PATH = "/scripts/ubidots/mosquitto.org.crt" #you will need to copy and paste *
You can download the certificate from https://test.mosquitto.org/, please try to leave a donation to show appreciation of the nice job done by team Mosquitto!
As we are using the existing framework of our Ubidots client, you will need to ‘Enabled’ Ubidots in the ‘IoT Client’ tab of your gateway.
You can leave both Device Token and Name empty as it’s not used in this mode.
After saving the modifications to the client file and also copied over the certificate file, you are ready to go after a reboot.
If your Modbus or CAN bus settings are already working, you should be able to subscribe to your MQTT publisher topic and receive the JSON device data like in the following screen-shots.