Subscribe to Arduino Academy and start learning today for FREE!

Setting up a Mosquitto MQTT IOT Broker

Ever want to view / control devices at home while on the road?

As part of our IoT initiative, I have set up a broker based on Mosquitto. Mosquitto uses MQTT, a publish and subscribe mechanism that avoids the necessity of special router configuration on your home network. A sensor node (arduino with ethernet or wifi, esp8266, etc.) publishes data to the public broker, and a client (cell phone, another arduino, etc.) subscribes to that topic to display information or receive instructions. Since the device that subscribes, or publishes to the broker initiates the conversation, your home router does not need network address translation or ports opened up. To secure the data transmission, usernames, passwords, client id’s and SSL are used.

Our first step was to install Mosquitto on the server. We used the excellent tutorial at https://www.baldengineer.com/mqtt-tutorial.html to get the broker up and running, and tested. Since we used a windows 10 machine as the server, we had to enclose the message “Hello World” in double quotes instead of single quotes as stated in the tutorial.

Become the Maker you were born to be. Try Arduino Academy for FREE!

There are two dependencies listed during the installation, so also download those, and copy the resulting dll’s to the mosquitto directory.

Since I use MySQL and PHP to collect data from our sensors and log to the database, I created a php page that acts as a publisher to the broker. The sensor could just as easily publish to the broker directly, but I needed the database and visual graphing capability of PHP, CSS3, HTML5 and Javascript. I can also easily email alerts.

I may set up php as a subscriber, and have the sensors publish to the broker.

To use MQTT with PHP you need a MQTT Library for PHP.

Stay tuned for more articles using ESP8266’s, relays and other sensors, with MQTT transport. I’ll have a Mosquitto sandbox for readers to play with so you can publish and subscribe your sensors.

PHP Example Code:

https://pastebin.com/X2iewi60

Android viewing and control:

There is an very good Android dashboard for publishing and subscribing to the broker. You can see your sensor values, and send commands to your devices from anywhere in the world.

Become the Maker you were born to be. Try Arduino Academy for FREE!

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

Archives

0
Would love your thoughts, please comment.x
()
x