Tutorial: Connect to an MQTT Broker
Using Mosquitto MQTT on the FATBOX G3 to deploy an AWS Solution
DESIGNED IN AUSTRALIA. BUILT IN USA.
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).
1. Install and Run 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
2. Install and Run the Mosquitto MQTT client on FATBOX G3
You will first need to enable SSH on the FATBOX G3. You can do this in the “Management” tab of the web console.
Then SSH into your FATBOX G3 gateway, (e.g. using PuTTY SSH client [for Linux] to your device via ETH0, for instance to 192.168.1.1 port 22). Please change your default username and set a strong password to ensure security.
Then run the following:
opkg install mosquitto mosquitto-client libmosquitto
3. Run sample MQTT script to test connection to AWS MQTT broker
We have a sample Linux BASH script (/etc/MQTTclient) to get you started. 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.
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. 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
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.