-
1Installing MQTT broker on Raspberry Pi
For this project we use an open source Mosquitto MQTT broker. It is lightweight and is suitable for use on all devices from low power single board computers to full servers.
Before we start installing, it's a good practice to update the system components first:
$ sudo apt-get update $ sudo apt-get upgrade
Install Mosquitto broker. Open a terminal and type the following command:
$ sudo apt-get install mosquitto -y
Configure Mosquitto broker. Edit config file:
$ sudo vi /etc/mosquitto/mosquitto.conf
and add the following lines at the top:
port 1883allow_anonymous true
Restart RPi to apply changes:
$ sudo reboot
That's it! Our MQTT broker is up and running now!
NOTE: for the sake of simplicity of this project we are not creating accounts. So anyone within our local network can connect to this MQTT broker without credentials. If you want to add user authentication and make it more secure, there are plenty of tutorials on the internet how to do it.
Now, we just need to obtain IP address so we can send messages to our Mosquitto broker from other devices in the network:
Obtain IP address:
$ hostname -I
your_RPi_IP_address (e.g. 192.168.1.10)
-
2Connect IOT Cricket to RaspberryPi over MQTT
In this project we use the IOT Cricket WiFi module for a simple sensor to report a temperature every 30 seconds to our system. It will run on a battery so we can stick it anywhere at home or a garden. Later you can use IOT Cricket to build various battery powered sensors, alarms, buttons, switches and connect them too to our RPi MQTT broker out of the box.
In the first step connect batteries to Cricket.
Cricket comes with a built-in temperature sensor. We just need to configure it to send temperature value to our MQTT broker by setting the RPi IP address.
In order to do that open Cricket's configuration panel (see the steps here) and apply the following settings (as shown on the image below, please adjust IP address in "url" box to your RPi)
Now we can exit from the configuration mode.
The device is ready! Cricket is already sending data to our MQTT broker every 30 seconds.
-
3Inspect MQTT messages
To view / receive messages sent to our MQTT broker we can use various tools.
The easiest one might be a command line tool mosquitto_sub. We can install it either on any computer in our network or on our RPi with this command:
$ sudo apt-get install mosquitto-clients -y
Now we can execute the following command to listen to ALL topics and messages send via our MQTT broker:
$ mosquitto_sub -v -h your_RPi_IP_address -p 1883 -t '#' ... /59A98F494C/device_name MyTemperatureDev /59A98F494C/device_sn 59A98F494C /59A98F494C/hwc_wake_up 3794 /59A98F494C/hwc_wifi_enabled 3763 /59A98F494C/hwc_message_sent 3664 /59A98F494C/temp 26.0 /59A98F494C/io1_wake_up 0 /59A98F494C/rtc_wake_up 1 ...
The above is an output example of what IOT Cricket sends to our broker. Among the other data we can see there is a temperature: /59A98F494C/temp 26.0
The beauty of MQTT is that it allows us to subscribe to only topics which we are interested in. If we want to receive just a temperature, we can subscribe to /59A98F494C/temp topic by using the following command:
$ mosquitto_sub -h your_RPi_IP_address -t '/59A98F494C/temp' ... 26.1 26.5 27.2 27.6 ...
-
4Summary
We have shown in this project an essential hardware and software to start building low power, energy efficient, home automation systems based on WiFi. MQTT is the essence to build upon for more sophisticated systems.
The ecosystem of software and services which can be integrated with MQTT is HUGE! There are a lot of great systems such as Home Assistant, Node RED, Grafana, etc. to let you come up with a great user experience of your own system. Not only can we do a simple print outs of a temperature, but we can have great dashboards visualising the data and managing your devices.
Now as we have this basic system infrastructure in place, the sky is our limit of what other WiFi devices we can build using IOT Cricket modules and add to our home automation system.
Thanks for getting up to here. We hope you enjoyed this tutorial!
Things On Edge team
About us
Things On Edge is a company based in Cambridge, UK. We design ultra-low battery powered Cricket Wi-Fi moduleto enable you to connect various electronic devices to smartphones or other internet services literally in minutes. It doesn't require any programming and coding. It allows you to integrate your devices to a huge IOT ecosystem services over MQTT and HTTP apis.
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.