Make your home smart with Raspberry Pi


IoT (Internet of Things) is one of the most discussed topics in the technology community. Some experts estimate that there will be 50 billion smart devices by 2020. These devices have very different hardware and software architectures. They all have different up time and power requirements. Connecting together becomes a daunting task.

As people connects more and more devices (sensors, door locks,  switches, TV, alarms, etc.) to the Internet, security becomes a major concern. You wants a smart home, but do you really need to connect everything to the Internet? For example, you want your smart coffee machine make you a cup of coffee when you wake up in the morning. Why do the smart devices have to tell the internet that you are awake and then the internet tells your coffee maker to brew?

How about building a local IoT cloud so that you can make you home smart without all the security risks?


Raspberry Pi as local cloud

Raspberry Pi 3
Raspberry Pi 3

Raspberry Pi is a very popular single board computer for DIYers and makers. It is very powerful and versatile for home automation projects. In my previous articles, I explained how to install Ubuntu Linux and setup mySQL database and Apache Web Server on Raspberry Pi 3.

Today, I am going to show you how to use it as a MQTT broker so that smart devices can talk to each other. In short, MQTT is a message passing protocol. Clients (like smart devices) send messages to the broker (like Raspberry Pi) and the broker passes the messages to the clients who subscribe to the topic. This is completely independent of the underlying software and hardware architectures. In order words, your smart device could be running different OSes or it could be a simple embedded device running C code. The clients do not need to be on at the same time in order to communicate (because of the broker). For more information, refer to Wikipedia or

Installing mosquitto on Raspberry Pi

mosquitto is one of the implementations of MQTT broker and client. To install it, simply run the following commands on Ubuntu.

To insall:
sudo apt-get install mosquitto mosquitto-clients

To start the server, run :

By default, the broker communicates through port 1883

As a client, to subscribe to a topic (e.g. ‘temperature’), here is an example:

mosquitto_sub -t temperature
mosquitto_sub -t temperature -h -p 1883    (specify host ip and port if MQTT broker is on another machine)

On the other hand, to publish a message to a topic, here is an example:

mosquitto_pub -t temperature -m "74.8F"     ( you may run it on the same Raspberry Pi)
mosquitto_sub -t temperature -h -p 1883  (specify host ip and port if MQTT broker is on another machine)

Once a client posts a message to a topic, all other clients that subscribe to the topic will receive the message through the broker.


Connect your smart device to the broker


I posted an article (with source code) to Instructables on building a WiFi IoT thermometer. In that example, the smart device is called ‘WiThumb’. It is an Arduino compatible WiFi USB Thumb based on ESP8266. I am currently (as of July 2, 2016) running a KickStarter project to make it available to more people. In this article, I am going to show you how to program WiThumb as a MQTT client to talk to the Raspberry Pi local cloud (running mosquitto MQTT broker).

Pre-order WiThumb on KickStarter now

Source code

The source code is published on GitHub. It is very similar to the one in my previous Instructables. The difference is that the Instructables shows you how to send the temperature readings to Internet (hosted on, which this one keeps the data local.

The code relies on the ‘PubSubClient’ (a MQTT client library for Arduino), which could be found here. The temperature sensor is MCP9808 (form Microchip) and the corresponding library could be found on Arafruit.

The mains steps of my program:

  1. Upon reset, it searches for the temperature sensor and measures the temperature.
  2. It connects to the WiFi
  3. Sets up MQTT client
  4. publish the temperature reading to MQTT broker under the topic ‘temperature’.
  5. Go to deep sleep for about 2 minutes and repeat step 1

One again you may download the code here:

After compiling and uploading the code to WiThumb, other MQTT clients should see messages like these:

ESP8266 Temperature sensor MQTT client
ESP8266 Temperature sensor MQTT client



Future work

Now, we have a local IoT cloud set up. WiThumb sends the temperature reading to Raspberry Pi. We can do all sorts of creative things with it.

For example, put WiThumb in the attic to control an attic fan (with additional circuits). You could also use Raspberry Pi to host a local web server to show the temperature or data from other smart sensors. You could also run a neural network on Raspberry Pi to train it to master the control of your smart home.