element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Pi IoT
  • Challenges & Projects
  • Design Challenges
  • Pi IoT
  • More
  • Cancel
Pi IoT
Blog [PiIot#04]: Freeboarding with MQTT
  • Blog
  • Forum
  • Documents
  • Polls
  • Files
  • Events
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: vish
  • Date Created: 17 Jul 2016 1:51 PM Date Created
  • Views 2940 views
  • Likes 4 likes
  • Comments 0 comments
  • mqtt
  • vish_piiot
  • freeboard
  • piiot
  • nodejs
  • raspberry pi projects
  • piiot challenge
Related
Recommended

[PiIot#04]: Freeboarding with MQTT

vish
vish
17 Jul 2016

Previously I discussed about designing a dashboard using Freeboard and setting up a MQTT Broker with Websockets enabled. In this post, I'm going to use both of them and create a dashboard for visualizing data coming through an MQTT stream. If you don't want to set up your on MQTT broker, I found a open broker from HiveMQ handy. You can connect to it using broker.hivemq.com at port 8000 for websockets connection and port 1883 for normal connection. More info can be found here. Since the dashboard will be running at client end, we'll be using as JS MQTT client with websockets connection.

image

 

Setting up freeboard for receiving  MQTT packets was a little pain. There is no official plugins available. I found a descent plugin at https://github.com/alsm/freeboard-mqtt but it was not really what I want. I has quite a few draw backs:

  1. It was not working for me. (I still don't know why image - might be the way they use connect to broker )
  2. It can support only one topic subscription. This is was a little annoying to me as for each topic I need to create a datasource - which in turn creates one client per topic image
  3. Not easy to select the fields in data source

So I decided to create my own plugin using it as a starting point. I wanted to be able to subscribe to multiple topics from one client, easiness in selecting datasources for widgets and with possibility of wildcard subscriptions. The new plugin is used in this blog post for all MQTT subscriptions. Currently I resides inside my main dashboard repository but I'm looking forward to spin it off this project and make it itself a fully fledged project by the end of this challenge. In this post, I'm planning to show only how to use the plugin. I'm not planning to cover the technicalities of writing freeboard plugins or how to set up them. If you want to use this plugin in your project, simple copy the plugin under 'www/freeboard/plugins/thirdparty/freeboard-mqtt-paho' of the git repo and follow the instructions here.

 

Setting up Freeboard

A whole working setup of the dashboard along with nodejs sever is available in my github. Clone it and it will setup everything. You can run this in your pc or raspberry pi. I'm running this from my Pi3 as this will in future serve as the hub for my network.

To clone the repository, go to some location in your computer/raspberry pi and

$ git clone https://github.com/v-i-s-h/PiIoT-dashboard.git

Then navigate to the repo directory and install the dependencies using

$ cd PiIoT-dashboard
$ npm install

This will install the dependencies required to host the dashboard. Inside this directory, you will find a modified version of Freeboard at /www/freeboard/ and my mqtt-plugin inside www/freeboard/plugins/thirdparty/freeboard-mqtt-paho/mqtt.paho.plugin.js

Now you can start the dashboard by

$ node server.js 8080

You can get something similar to:

image

Now you can go to your browser and type <Pi's IP:8080> to load freeboard:

image

 

Adding MQTT Client Data source

Now we can go on to create our own MQTT Datasource. Go to [ADD] and Select 'Paho MQTT Client'.

Now fill in the details such as broker address and topics to subscribe. You can subscribe to multiple topics separated by semi-column. No way for user authentication is  available in plugin right now. Also enable 'JSON ENABLED' if your messages are json encoded. For now, I'm dealing with simple text messages. In this dashboard, I wish to monitor the data from three sensors. Each sensor will publish data in topic '/user/vish/test/sensorX' where X can be 0/1/2. That are the topics I'm subscribing to in this screenshot, seperated by semi column.
image

Click 'Save' once you are finished. Now you will be able to see it in your Datasources.image

image

 

Adding widgets

Now I'll go to add three guage meters and three sparklines for values from three sensors. Video below shows the procedure for adding these widgets.

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

 

Testing

We are now ready to go. In order to test the setup, I wrote a small shell script to mock the sensors. It will publish random data on the topics mentioned above. This will be enough to test whether our dashboard is getting updated correctly. Shell script is given below:

# Script to publish random sensor values to MQTT broker
BROKER=<insert broker address>
PORT=1883   # Default port is 1883
for i in {1..5}
do
    mosquitto_pub -t "/user/vish/test/sensor0" -m $((RANDOM%101)) -h $BROKER -p $PORT;    sleep 1;
    mosquitto_pub -t "/user/vish/test/sensor1" -m $((RANDOM%101)) -h $BROKER -p $PORT;    sleep 1;
    mosquitto_pub -t "/user/vish/test/sensor2" -m $((RANDOM%101)) -h $BROKER -p $PORT;    sleep 1;
done

 

Now save this as mock_sensors.sh, make them executable and run:

$ chmod mock_sensors.sh
$ ./mock_sensors.sh

 

Below is a video of the test.

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

That's it. Now we are able to visualize data streaming through MQTT broker in freeboard.

 

Happy hacking,

vish

 

<< Prev | Index | Next >>

  • Sign in to reply
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube