Raspberry Pi, Arduino and MQTT

Now that I'm living on a 7 acre spread, I need to start figuring out a way to provide security, remote sensors for temperature, humidity, power, water pressure, etc. and methods for controlling remote lights, relays, valves, etc.

My Arduino Temperature and Humidity Logger has been working flawlessly Since June 2014, but the sensors are hard-wired to an Arduino in the data closet and adding more sensors would be quite a chore. What if I wanted to add a PIR sensor at the beginning of my driveway? Since it's over 100 yards away, I would need a wireless solution.

After weeks of research, I decided to create a network of sensors and actuators using an Arduino with an Xbee Coordinator as the controller and remote Xbee radios for the sensors and actuators. The Xbee Series 2 radios can be programmed to join a mesh network controlled by the Coordinator Xbee. Each remote Xbee has numerous digital I/O and analog input pins. If a remote Xbee can't talk directly to the Coordinator, other Xbees can act as routers.

I can't expect a lowly Arduino to handle all the remote Xbee polling, all the logic necessary for the decision making and the human interface. Instead, the Arduino will only do the remote polling and sending the results to a separate message store. When I add another Xbee to the network, it will be a simple matter to add the polling and send the results to the message store.

The message store is a server that runs software called a MQTT Broker. MQTT is an acronym for "Message Queuing Telemetry Transport". The protocol is very lightweight and is designed for small, distributed sensors. In my environment, the Arduino with its attached Xbee Coordinator and Ethernet shield will act as a MQTT Client to the MQTT Broker. It will poll the remote Xbees, store the data on the Broker, then look on the broker for data to act on.

Here is an example of one of the things I want to do: I have a detached unheated workshop with a large cabinet. The cabinet contains cans of paint, lubricant, cleaners and other liquids. During the Winter, without heat in the cabinet, the contents would likely be destroyed by the freezing temperatures. The workshop Xbee will have a temperature sensor tied to an analog input line, a relay connected to a heater driven by a digital output line and a door sensor wired to a digital input pin. Example data posted on the MQTT broker could be "45" (temperature in fahrenheit) and "workshop door open" indicating the shop door is open. The Arduino will make no decisions based on this data. As part of the main Arduino software loop, the Arduino will look on the broker for data concerning the relay. If it sees a message like "Shop relay on", the Arduino will instruct the Coordinator to send a packet to the the workshop Xbee turning the relay on.

So what sends the "Shop relay on" message to the MQTT Broker? Another MQTT Client will send the message. In this case, the Client will be a web page looking at the paint cabinet temperature. When the temperature is lower than a user defined set point, the Client will send "Shop relay on", otherwise the message will be "Shop relay off".

Another MQTT Client, in this case a security client, would be looking at the "workshop door open" message. When it sees "workshop door open", a Twitter message could be sent to me stating that someone had opened my workshop door.

There are many types of MQTT Clients and the MQTT Broker can be scaled up to handle more processes than I will ever need.

So far I've accomplished the following:

I created a Debian virtual machine running under VMware Player and installed the HiveMQ MQTT Broker. I installed an Android app called MyMQTT to send and receive test messages to the Broker. HiveMQ is an enterprise software package, but if you keep the number of concurrent MQTT Clients under 25, it's free to use.

I have two 2 milliwatt Xbee Series 2 radios, an Xbee USB interface board, an Xbee prototype board,  an Arduino Uno, an Xbee shield and an Ethernet shield. So far I have been able to use Digi's XCTU software to flash the firmware on the Xbees and send and receive packets between the two radios.

So far, so good.


I received a Raspberry Pi for Christmas and have so far created images for a LAMP server and a Mosquitto MQTT Broker. Looks like VMware is trying to get rid of their free VMware Player so I think I will transition from Virtual Machines to Raspberry Pi machines. Since Windows 10 seems to get updates quite often and require re-boots, I got tired of taking down the Virtual Machines to install Windows patches. Now my environment can run continuously.


The Raspberry Pi turned out to be a surprisingly powerful machine so I installed the LAMP server and MQTT Broker on the same machine. I re-programmed the Arduino in the data closet to simply gather temperature and humidity data and continuously update topics on the MQTT Broker. I have a PHP script on the LAMP / MQTT Server that is triggered every 4 minutes by Cron. The script fetches the latest temperature and humidity data from the MQTT Broker and updates a MySQL database. The scripts used to generate the graphs remained the same.

When I am ready to add more functionality, I can build more Arduino or Raspberry Pi sensor machines and have them publish data to new topics. On the Server end, a few more scripts to manage the data and Voilà, additional nifty functions.


A couple of weeks ago, our little wine cooler crapped out and I was tasked with the job of finding another use for the otherwise nice cabinet. I'm an avid gardener and like to start plants from seed, so I decided to create a seed starting box, AKA a growbox. I ordered the latest version Raspberry Pi from Amazon and started gathering parts from the junk box.

Check out the Grow Box page for construction details.