How to use MQTT with PiDome


Level:
Beginner
Author:
published:
Modified:

MQTT is an extreme lightweight protocol used for machine-2-machine communications. It exists for a very long time and is now very hot in the IOT community and proffesional IOT implementations


Enable the MQTT Broker

To enable the broker you have to add the broker plugin. It already is within the default server distribution so this would be quite easy to do. Go to “Plugin management > Device plugins” and click the button “Add a new plugin”. In the drop down list which is shown select “PiDome MQTT Broker”. That part of the page will refresh and you will have the option to enter a name and description identifying this plugin. Also you need to fill in the port used.

 add-mqtt-broker

This are currently the only options. More options will follow as the project grows. By clicking “Save devices plugin”. the plugin will be saved and started. Initially it can possibly show that the plugin ain’t running, refresh the listing by clicking on “Devices plugin” on the left. This is because it can take some time before it is started.

Use the broker

Take any MQTT client and connect to the broker using the Raspberry Pi’s ip address and the port just entered. An example simple client would be MQTT.fx created by Jens Deters. Below a screenshot of this app as i have used it to test the server’s broker.

mqttclient

 

Use your own sensors as entities within the server

as said in the beginning of the post it is possible to use your own sensors within the server as devices. For this a topic namespace is reserved in the MQTT broker which is: “/hooks/devices/”. To start using your own sensors within the server as a legitimate entity follow these steps:

Create a custom device

First you need to create a device skeleton. This is done with the “Custom devices editor”. When you have the custom device opened create groups and controls by drag and drop these groups and controls in the device editor. Keep the group and controls id’s as short as possible but descriptive enough for your own identification. Take a look at the below screenshots for an example:

mqttgroup

Here you see i used the group id “mqttgroup”, remember this one.

datacontrol

Here above you see a data control added with control id “control1”. Datatype is float, this is how the data coming from your sensor will be handled internally in the server. A data control only shows data coming from the sensor.

togglecontrol

Above the toggle control is shown with id toggle0. A toggle button is an bidirectional control. This means it can receive the toggle status from your device, but can also send a toggle status to your device!

You do not need to enter any options. Also the address is not used and can be set to not used.

Add the custom device to the server

With the custom device created, you need to add it to the server. Remember, this custom device is a skeleton, so it can be added multiple times. Go to “Devices (Add/Remove/Settings)” and click “Add device” look at the screenshot below I have named my custom device (device skeleton) MQTT test.

addnewdevice-mqtt

When this device is selected you can now enter the device details:

adddevicedetails-mqtt

Now that this is done, the device is added to the server. Now, how do you know how to publish your sensor data to the device?
Well, go to “Plugin management > Device plugins” and click on View. Take a look at the screenshot below:

mqtt-broker-device-view

This page will contain a list of devices you have added to the server. When you click on the device name you just have added you will see the reserved namespace with added an /device identifier/groupid/controlid which in this case would be “/hooks/devices/23/mqttgroup/control1”. When you publish FLOAT data to this topic it will be shown in the web interface, clients, etc… if you would publish a BOOLEAN “true” or “false” to toggle0 you will see the button switch state in the web interface and clients etc…

This setup with hooked devices also makes it possible that your own device can be published in a semantic way in the /Home/ topic namespace with the group and control names you have given it.

The image below shows how the flow goes with the above setup.

mqttflow

I would say try it out and have fun with it!

A real time broker example

In the clip below we have created a link between a 2048 JavaFX clone game created by Jose Pereda and Bruno borges. We took the source code and added some code so it can communicate with the broker included in PiDome. I won’t tell what happens but take a close look at the tablet when the player looses the game. If you want to know how it’s done keep on watching the video clip.

Latest news/blog

Friends of PiDome

Atlassian
Products for teams, from startup to enterprise
UniPi.technology
Home-automation hardware manufacturer
Hubbit.nl
Webshop of the most prominent dutch tech magazines
RFXCOM
Affordable 433Mhz RF transceivers

Latest added technology

  • SMS
  • Z-Wave
  • PushBullet
  • PlugWise
  • MQTT

Some project stats

Open Hub project report for PiDome Platform

Other

Official NLJUG Member