Smart home concept is currently migrating from geek exotic to reality that can be achieved with a plethora of technology solutions available today. In the article I translated from Habrahabr the author shares own experience of converting his apartment into a smart home. Read his story below.
Let's talk about sockets
My wife always unplugs everything from the sockets before she leaves home. When I return home, I actually plug everything back in. It takes a lot of my free time. I made a lot of efforts to change my wife's mind, but nothing helped, neither my begging nor threatening. And that's when I decided it was time I build a high-tech solution to resolve this issue: I decided to automate my home sockets so that my wife wouldn't have to unplug devices and I wouldn't have to plug them in every day after work. As such, I was going to enable sockets to talk to devices and us.
When I began pondering over how to build such an IoT solution, I got a way too many questions that required answered. For instance, what micro-controller should I use for remote control? I had experience programming Atmel AVR controllers and the task didn't seem like a rocket science. I needed a flip-flop, a transistor and a pin control. As such, there appeared two tasks to solve. First, controller and relays should be somehow charged, and second, controller was to be managed. I identified 3 options of charging controller and relays: 1) battery feeding (yet, batteries will die and have to be replaced, which isn't very convenient); 2) fitting each socket with a 220 -> 5 transmitter right next to the controller (however, I didn't find any available compact transmitters or adapters and didn't feel like inventing a bicycle or burning my apartment); 3) feeding each socket from outside.
I found 2 ways of enabling remote control for the controller: by radio and by wire. While wire looked pretty simple, I still had to connect it to every single socket in house which was a bummer. And the last thing that actually talked me out of building a DIY IoT platform is troubleshooting. I'd only be able to troubleshoot architecture bugs and firmware errors at the time of system maintenance, which would definitely disturb and upset other users (my wife in the first place).
Then I started exploring available IoT systems I could use for my home automation. It was clear from the very beginning the connection was to be wireless, so I had to choose between Wi-Fi and Bluetooth technologies. They're most commonly used and support a lot of solutions. However, 2.4 Ghz Wi-Fi isn't convenient from the viewpoint of network organization. This frequency is very 'noisy' because of its high adoption and the lack of any control at setup of access points. I was looking for a more stable system that wouldn't freeze when switching between sockets or when a neighbor's router is on. Yet, it wasn't my biggest issue. A microwave oven was a big issue indeed! Its magnetron oscillator radiates at 2.45 Ghz, so when I turned on a microwave, nothing else would work in house: Bluetooth audio streaming would fade out, Wi-Fi video would stop streaming, I couldn't download any files, nor could I open any webpage. And no matter what type of a microwave oven you have - be this a built-in or a standalone one - it'll create mush anyway!
As a result, I refused to use Wi-Fi and Bluetooth.
As I kept looking for affordable and effective solutions, I came across ZigBee, a wireless standard that everyday devices use to connect to one another and that supports several standard ranges, both 2.4 Ghz and 1 Ghz. But then I bumped into Z-Wave and got really impressed! Today, there're only 2 companies in the world that manufacture basic Z-Wave units - Sigma and Mitsumi. Its size is only 0.4 x 0.4 inches (1 cm x 1 cm); basically, it's an upgradable micro-controller fitted with a transmitter / receiver unit.
All devices marked as Z-Wave compatible should undergo a strict certification. It's a closed platform which provides some benefits in terms of compatibility. For comparison, there're a lot of Bluetooth solutions for home automation available today, but they aren't compatible at all. When it comes to Z-Wave, all supporting devices are greatly compatible due to a single closed standard that keeps evolving over years. In addition, there's a paid SDK developers can take advantage of (its cost is around $2,000). Yeah, it's expensive if you're only going to automate one socket in house; but if you divide this amount by 2,000 devices you're trying to automate, the cost seems pretty reasonable, doesn't it?
Z-Wave's communication range is around 98 feet (30 m); one of its biggest advantages is that all devices mesh up with each module having a routing table. Having caught a signal, any module knows whether to transmit it further or not. As such, almost each module acts as a rebroadcasting transmitter, too. Almost - because Z-Wave includes the so-called sleeping devices that don't participate in transmission and are charged with batteries.
To prevent your IoT network from connecting to your neighbor's network, there's a special token generated for each head controller. The network is comprised of a head controller and the periphery. At first setup, the controller generates own token after which any executive device first connects to the controller, remembers its token, and then enables device's start-up.
You can find a lot of Z-Wave devices on Amazon: from modules to be built in sockets and switches to modules used for building a security system. Locks, burglar alarms, lighting, smoke detectors, water leakage gauges - whatever you need! I was even able to find automated cat food cups!
As for the working range, it's around 900 kHz. This range isn't open for all countries, so each country should undergo certification and apply for permission to use this technology. It explains why working ranges differ from country to country (in Europe - 868.42 mHz, in North America - 908.42 mHz, in Hong Kong - 919.82 mHz, in Australia - 921.42 mHz).
To build a mesh network, use an Explorer Frame mode. This mode makes all devices hush, the controller launches network scanning and devices start responding one after another (in turn). A head check occurs when each device builds own routing table. It's highly recommended to use this mode after refurnishing your apartment, because the chance is high that a moved wardrobe has started screening a signal between some devices while opening space between other devices.
To cut it short, Z-Wave standard ensures seamless and stable work outdoor within a 98 feet (30 m) range and indoor within a 32 feet (10 m) range.
Now let's talk about commercial micro-controllers. There're two famous home automation platforms: Fibaro (Home Center / Home Center Light) and Vera. They both support basic sets of rules and you can easily program devices' activation and deactivation. However, if you're looking to deploy a more complex behavior, you'll have to program from scratch using Lua.
There's also a less famous platform called Zipabox, but its controllers don't seem to support extended automation. However, Zipabox is an interesting platform because it provides extendable drag-and-drop modules.
But let's talk about Raspberry PI now. This micro-controller is very cheap (you can buy it for $50 on Amazon) and it can run a web server with UI that's enabled for automation with JS.
Raspberry PI uses an expansion card aka RaZberry that looks like this:
It's installed on Raspberry PI expansion port:
And that's how a sample dashboard interface looks like (it looks nice both in a browser and on smartphones):
Below is an example of settings. You can see all working devices at once and where they work, check connectivity and interoperability, routing tables, and so on.
For details and examples, check out Z-Wave.Me project on Github.
Home Automation System Control
My next challenge was how to enable my IoT system control so that it'd be more convenient than a web dashboard. I looked at Pebble Time and thought it was a cool gadget to control my home automation! Unlike a smartphone that I rarely carry with me about the house all the time, a smartwatch can always reside on my wrist no matter what I do at home. Unlike Android Wear or Apple Watch that are heavily dependent on the iPhone and Android-based smartphones, Pebble Time is a standalone product.
I checked other Pebble Time features and chose to build remote control for my home automation on it. First, the device can last for one week off a single charge (Apple Watch users have to re-charge their smartwatch every day!); second, Pebble Time supports both iOS and Android and is waterproof. What's more important is that Pebble Time is fitted with a hardware port.
The two biggest disadvantages of Pebble Time are the absence of a sensor screen (it has a 4-button control) and a face that's only limited to displaying 64 colors. Nothing critical, right?
Pebble works on OpenRTOS and the apps remind web rather than mobile apps. Front-end development is done in C and back-end is programmed in JS and deployed on a smartphone. The choice of languages is easy to explain: C is the most compact language for compilation and is perfectly suited for creating pre-built and built-in apps; JS is equally perfect for iOS and Android. As such, JS resides inside your smartphone and provides interface for Internet connection and GPS access. Pebble Time contains basic firmware that's enough for building an IoT network at home.
As a result of my ordeal, I built an application to control my home automation system.
When I was done with my app development, I submitted it to the app store and almost forgot about it. It was downloaded 40 times and eventually I began receiving user feedback.
I hadn't programmed in C for ages, so in the first app version I created a static array for 8 devices I had and everything worked well in my place! However, it didn't work with systems using more than 8 devices. And one of the feedbacks I got was from Denmark - Can you add more devices? I replied back with Sure, why not? and migrated my app to a dynamic array in one evening. The guy was happy, but not for a long time. It turns out he had about 54 logical devices including switches and sensors, with one physical module acting as a few logical devices. For instance, I used just one sensor for tracking motion, temperature, humidity and lighting, but in the system it displayed as 4 sensors. And a Danish guy had over 50 sensors, so to turn something off he'd have to scroll through a long list of devices on his smartphone. I came up with the following solution: using Z-Wave.Me dashboard, you can tag a device and so I tagged my Pebble and enabled a mode in the smartwatch settings that would only show devices with the same tag. That said, users choose themselves what they want to see and put a tag on respective devices. My Danish user was very happy, because now he had 5 devices he could easily control right from his Peddle Time.
What else makes Z-Wave virtual devices so powerful? Let's take a virtual HTTP-device and any other device that's not compatible with Z-Wave standard, but that can be managed with HTTP-queries.
In Z-Wave, a scene is referred to a set of states of logical devices. Scene can't be turned on or off, it can only be activated. For example, I have an Away scene that I activate each time I leave my home.
My Away scene turns off the lights in a bathroom and a toilet and deactivates all sockets in house. Activating this scene completely turns off all working devices in my apartment. Also, I have a Back scene that turns on all sockets, but not lights. At the end of the day, it's up to you to decide where to turn on the lights and where not.
As a to-do idea, I'm planning to install motorized valves that will cut off the water in one click when there's no one at home. These valves are very expensive, but creating a sinkhole in a neighborhood will cost you more! There're autonomous water leakage sensors that can be installed everywhere in house where water is used and in case of a leakage the valves will be blocked automatically.
Last but not least - it's cool to have a remote control of home automation on a Pebble smartwatch, but what if you want to have control of it outside your place? Then a good idea would be to create a Telegram bot that will alert you if something is wrong in your apartment and will ask you questions about how to fix the issue. It'll then control your home automation based on your direct commands such as "switch off light in bathroom", etc.
Home automation is a very convenient feature we're using at home. When my wife and I leave the apartment, all lights and sockets switch off, when we're back home, they're switched on automatically. However, my first experiments with home automation weren't very successful. I put a motion sensor next to a bathroom entrance, and when you went to shower and drew the shower screen, it would stop tracking your motion and lead to a black-out 15 minutes later. To get lights back on, you had to open a screen, wave your hand and draw the screen again. To solve the issue, I moved sensors to the center of the room to make sure they react no matter where you are.
And what has been your experience with DIY home automation? Please share your story as a Comment below or send us a tweet to @Intersog.
Inspired by habrahabr.ru; image sources: habrahabr.ru