Building the Poor Man’s IoT
This Christmas holiday I spent some time fiddling with sensors to build a Proof of Concept on Azure IoT. In an earlier post I’ve shown how to connect your Azure IoT Hub data to a live Power BI dashboard, and as promised, here’s part two: a description – as well as working code – how to set up your home IoT appliance for monitoring all kinds of things around your house. And because it is on private budget, I’ve also put a focus on affordability of the stuff. I present to you: ‘The Poor Man’s IoT’!
The Poor Man’s Architecture
Although sensors are cheap these days, sensors with their own connectivity aren’t that cheap at all – especially in terms of home enthusiast’s budgets. But what if the sensors won’t have to connect to the internet themselves, but instead used a ‘gateway’ connecting multiple sensors to the cloud? That should be less expensive..
This isn’t exactly a new thought: many IoT sensors currently in use are too low-powered to maintain their own internet connection, so connections via gateways are in fact pretty common for IoT sensors. In the Microsoft documentation on IoT solutions for example you’ll find the following scheme (red text is my addition)
So the “poor man’s solution” of connectivity appears to be a valid architectural choice by using a so-called “field gateway”. Great!
The Poor Man’s IoT will therefor have two types of hardware:
- The field gateway managing internet and bluetooth connections
- A bunch of low-powered “devices” with Bluetooth modules doing the actual work. I’ll call these Sensor Hubs
This results in the following architectural draft:
The Field Gateway communicates with low-powered Sensor Hubs via a small JSON-based protocol. Initiative for querying is initially in the Field Gateway: it queries the sensor hubs for new values. Currently the sensor hubs don’t push any data or event.
To give you an idea, here’s an excerpt of some communication between the Field Gateway (FG) and Sensor Hub (SH), formatted for readability[ref]In case you wonder: KSN stands for Koos’ Sensor Network[/ref]:
The ‘buffersize’ mentioned in the first response is a small workaround for the fact that sensor hubs with little processing power cannot always read the bus at once – so after every n characters the Field Gateway waits (always) with sending more information until the Sensor Hub sends ‘ACK\n'[ref]Currently communication from Sensor Hub to Field Gateway doesn’t send information buffered, because it’s not strictly needed (and this is only the Proof of Concept). I know it would be better, but I don’t think it’s that hard for you to add that little bit to my code :-)[/ref]
The Poor Man’s Hardware
Having more or less figured out the architecture and communication protocol, it’s time to pick the gear. As mentioned, I’ve tried to keep an eye on budget, so it’ll be affordable to try this at home. What we need to get (buy, transform, salvage, whatever) are the:
- Sensor hubs (reading sensors and providing bluetooth access)
- An IoT Field Gateway (connecting the Sensor Hubs to the cloud)
The Sensor Hub
There are all kinds of development boards available. Because of the ‘poor man’ focus, I’ve opted for Arduino for now. Real Arduino boards are cheap – the PoC can be built with an Arduino Pro Mini 328 (only € 9.99) – , but Really Poor Men can order cheap clones (+/- € 1.90) at eBay or from vendors like Banggood (€ 9.19 for 5 boards[ref]Full disclosure: If you buy via the Banggood links provided in this post, I’m credited with “affiliate points“, which can be traded in for discounts. Don’t worry, I won’t become rich & stop blogging 🙂 [/ref]).
The sensors you’ll use depend on the stuff you need to measure. Some ideas:
- Room temperature & humidity. DHT-11 sensors are great. Limited range (0-50 C), but cheap (+/- € 1.50). Measurements precise enough for room temperature.
- More precision or sub-zero temperature / humidity measurements? DHT-22 sensors (+/- € 5). I use these for temperature & humidity inside the freezer.
- Air Pressure. Use any barometric sensor.
- Power usage of anything. Use AC current sensors (€ ??)
- Soil moisture. Build your own, they’re better than cheap pre-built ones[ref]Cheap soil moisture sensors don’t flip voltage, so electrolysis will occur[/ref]
- Light sensors (LDR). Too cheap not to buy
For the PoC I measure temperature & humidity inside my freezer, so I use a DHT-22 temperature sensor.
The IoT Field Gateway
The Field Gateway could be the most expensive part, but doesn’t have to. Any programmable computer with Bluetooth as well as internet connectivity will do: Old laptops, programmable NAS with bluetooth connectivity, old smartphones (these have better bluetooth range than most laptops), or a Raspberry Pi + bluetooth dongle as Field Gateway – which will give you the benefits of an almost full-fledged Linux / Windows 10 in a low-power, compact device.
Keep in mind you’ll need to use the AMQP protocol though, and try to build a small PoC first to prove that your Field Gateway talks to Azure IoT Hub!
For the PoC my first choice is to use an old laptop currently gathering dust.
To aid you in the re-use of the results from my mini-PoC, I’ve split the results into the physical parts:
- The Field Gateway
- The Sensor Hub
All software is on Github, under my private account (https://github.com/vstrien). Please take note that
- this is a PoC
- I’m not programming very often in C# / Arduino
The code quality, comments etc. might be below the standards you’re used to. Pull requests will be accepted though, so feel free to contribute!
The Field Gateway
I’ve built the software of the Field Gateway from a fork of the Azure IoT samples. Take a close look in particular at the Field Gateway Sample – only in C#. This is the code for the Field Gateway PoC.
The complete repository can be found at https://github.com/vstrien/azure-iot-sdks – at first sight it might look identical to Microsoft’s original samples for the IoT SDK, but take a close look at Program.cs – this is my crude & rapid implementation of a Field Gateway querying a bluetooth sensor.
The Sensor Hub
As mentioned, I’ve built the Sensor Hub inside an old cigar box. Some photos to prove that:
Finally, I’ve drawn the schematic in Fritzing. The Fritzing file can be found at https://github.com/vstrien/KSN-Sensor-Hub/blob/master/Sensorhub.fzz.
The Arduino code is also on GitHub – take a look at https://github.com/vstrien/KSN-Sensor-Hub/blob/master/KSN-Sensor-Hub.ino.
As mentioned above, I built this PoC with a home budget in mind – just to show that it’s easy to hone your skills, build cool (freezing) things and connect them to the cloud. So how cheap could it be? Well, here’s all I had to buy:
- Field Gateway: free (old smartphone, unused laptop, extended NAS)
- Arduino Pro Mini: € 10 (RPMA[ref]Real Poor Man’s Alternative[/ref]: € 2)
- DHT-22 sensor: € 5
- Bluetooth module: € 2
Total price: € 17 (official Arduino) or € 9 (RPMA)[ref]Of course you’ll need some more things, like a USB programmer (Pro Mini doesn’t include one), breadboards, a computer and some other things. As you can re-use these or they’re really cheap (resistors, soldering tin, wire), I’ve not listed them here.[/ref]
Heck, even covering my entire house with sensors will cost me around € 100 (official Arduino), or €45 (RPMA). That’s not too expensive, is it? Even if it is, the costs of the PoC (€ 17 / € 9) shouldn’t be able to hold you back trying at least the first step 🙂
Summary & Evaluation
As you can see, it’s easy to build a small (but scalable) Azure IoT appliance and connect it to Power BI – even on a limited budget. This data is live visible in Power BI (see “Azure IoT is out! Here’s how you connect with Power BI“).
Still, a few points to think about:
- Just adding sensors to your home, car or boat and start monitoring isn’t the same as the Internet of Things – in fact, it’s pretty much a one-way conversation as long as you just monitor: I see the temperature in my freezer is too high, but I still have to walk to the garage in order to turn a knob.
- All ‘sensor hubs’ need their own wall plug. You might not like that – at least I don’t.
- Bluetooth as a protocol is a blessing (energy-friendly, cheap, easy to use, available in every device), but also a limitation: whenever you’re not living in a home as tiny as mine (it’s around 60 square meters living space), it could be that bluetooth will not do as a default connection: sensors might be just too far away.
Summarized, the current state of technology makes it very affordable to start exploring things previously unthinkable: live, worldwide available, scalable server farms available for free (the Azure plan I use here is free), connecting sensors around the home for a few bucks… I’m honestly amazed about the possibilities without needing heavy investments first.
In terms of budget, there are very few reasons not set up a Proof of Concept and start measuring things important for your home – or business. All that’s in the way is the choice to actually start doing it!