We spoke to Bhavana Srinivas and Geremy Cohen from PubNub about their LEGO Smart Home model, a proof of concept project that shows how you can use the Raspberry Pi with communication platform PubNub in order to automate your household electronics and other Internet of Things devices. You can read the full piece in the latest issue.
Tell us about PubNub and your LEGO Smart Home.
PubNub is a global data stream network. What this means is that, using the PubNub software, you can talk between devices in real-time. And, going beyond that, we provide an infrastructure as well, so you can scale and build real-time applications. The simplest case is when you have two mobile phones talking to each other – using any kind of instant messaging or a commercial app that requires stocks to be updated in real-time, for example, or any kind of home automation – that kind of real-time messaging is brought to you PubNub. Since we have 14 data centres all over the world, your data gets replicated and we’re able to communicate between devices in less than a quarter of a second.
The Internet of Things was picking up at PubNub last year, so we decided to have some cool demos to show how you can integrate PubNub as a software for any kind of home automation that you build out. We were building two projects at the same time – one with the Arduino Uno and the other with the Raspberry Pi, both to show home automation itself. So with this project, the Raspberry Pi Model B+ would be the brain behind the whole project, powering the different embedded sensors, the stepper motor to control the door, and we used PubNub as a glue to talk between these different devices.
I didn’t build this myself – it was actually another engineer here called Geremy who built it over Christmas. We were going to have the software component, which is PubNub, and then on a mobile phone you could control all the devices in your house; the devices being the seven embedded LEDs – used to simulate things like a stove, a fireplace, a barbecue station and lights within the house – and then we had a stepper motor to open and close the door, and a couple of sensors like temperature, humidity, etc, to measure those different values within the house. So what you can do with this demo is open this dashboard on your phone, typically an app, and the moment you instruct it to switch on the light or open the door, it will reflect immediately on the house. You can get the state of the house continuously, in terms of the voltage or the temperature, the pressure, so that’s a typical home automation system.
How does messaging between different PubNub-connected devices work?
So PubNub provides real-time communication between any two devices. What happens is that one device is publishing on a particular channel and then another device is subscribing to that same channel, and that’s how the message is transmitted from the first device to the second. All you need with PubNub is the publisher key, the subscriber key and then the channel name, and when two devices are using the same set of parameters – the same pub and sub keys – and when one publisher is on that channel, the other receives it on the same channel. On a very basic level, that’s how PubNub works.
How much of the software setup was specific to this project – was there a lot of work involved to get PubNub talking to the Pi?
So how it works with PubNub is that we have 70+ SDKs, which means we support that many platforms or devices – so if you’re using an iOS device then you can use our iOS SDK, or there’s our Python and Java SDKs, and so on. For the Raspberry Pi we chose to use our Python SDK. PubNub provides very easy-to-use APIs, so if you wanted to send out a message then it would be as simple as pubnub.publish and to receive a message it would be pubnub.subscribe, so the SDKs are already built out, but there was a little bit of tweaking for it to fit this particular home automation model.
How easy would it be for people to recreate your LEGO Smart Home model for themselves?
It’s super easy. We are actually creating tutorials as well – we’re done with the temperature sensor, the humidity sensor, etc, and we just have a little bit of tweaking to do for the lights – so they’re going to be smaller follow-ups to the same video and blog that we’ve posted already. So it’s super easy to create and is meant for the hacker community, like a weekend or holiday project where you can just sit and build this whole thing in a couple of days. There’s a free tier in PubNub where we provide a sandbox account you can use for this, wherein you’re given publish and subscribe keys and you can go crazy. There’s a limit on the number of messages you can send but that’s way more than what you would need for a free tier. Everything’s open, so you can see it on our website, and it’s free up to 20 devices.
And how straightforward would it be to scale up this project and use it for real home automation, with things like Philips Hues and Sonos speakers?
We have an intern who recently built out the Hue light bulbs with the Raspberry Pi in about half an hour; it wasn’t a big deal at all. PubNub is just plug-and-play – you build out all this software, put it on your hardware, and you don’t have to worry about which network you’re on, you don’t have to configure the routers or firewalls, any of that stuff. It’s very easy to scale with PubNub itself because we’re global – even if you get up to millions of users, irrespective of where you are in the world you still receive the message in less than a quarter of a second.
Interested in building a full-size home automation system with the Raspberry Pi? Grab a copy of the new issue – Liam Fraser explains how to remotely control lights and plug sockets, monitor the temperature and set up a video doorbell for your home, all connected through a tweaked Heimcontrol interface.