In today's era, technology can enhance human life. Technology is evolving decade by decade. Automation was a science fiction earlier but not today. By combining latest technology with home, we can build an awesome home. With the Raspberry Pi and Windows 10, we can build a home automation system that is capable of operating home devices automatically.
Getting StartedBefore starting the project, lets understand basics first. Consider the following image (Overall Configuration):
Now considering room scenario, an Arduino UNO will control devices and reads sensor data. Periodically, Raspberry Pi requests for the sensor data collected by Arduino UNO. The figure "Room Architecture" depicts how the Arduino UNO will connects with the devices and sensors. Each room have multiple controllable devices(i.e. Light(s), Fan, Wall Socket(s), etc.), one PassiveIR (to detect human presence in the room), one temperature sensor (LM35 to collect room temperature) and LDR (to detect light intensity near room window).
The most important part is how we will identify devices? Its simple. We will make device address by combining room number with device number.
For example:
Room Number 1 having three devices. Device address will be:R1\Dev0 R1\Dev1 R1\Dev2
So up till now we know the overall configuration. In short, a Raspberry Pi will act as primary controller. Each room have its own one Arduino UNO which act as slave of Raspberry Pi. Communication between Raspberry Pi and Arduino UNO will be done using I2C.
SchematicsTo make it more easy, lets start implementing for first room. Refer schematic to connect components in proper way.
Note: I2C address for Room 1's Arduino is 0x40. You can change address but make sure to change it back in Aruino's sketch and room's slave address in the app provided below, too. Note that each Arduino resides on the bus must have unique slave address.
I2C Bus Stripe: To attach more than one Arduino you can use I2C Bus Stripe as show below.
The relay shown in the schematic are operated by 5VDC. You may have different rating of coil to drive relay. In such situation you need logic level converter circuit.
WARNING: Please make sure that the device is disconnected from the wall outlet and outlet is switched-off. Parent observation is required for small children.
WARNING: IN CASE OF ANY PROBLEM, SHUT DOWN HOUSE'S MAIN LINE OR ROOM'S TRIPPER LIKE MCB OR ELCB AND CALL ELECTRICIAN. PARENT OBSERVATION IS REQUIRED.
All done at hardware side. Now its time to build software.
SoftwareOur project consists of a Raspberry Pi 2 and Arduino UNO. Raspberry Pi 2 software developed in Visual Studio 2015. I am assuming that reader is aware with project creation for Raspberry Pi 2 in Visual Studio 2015. I am also assuming that user have intermediate knowledge of Arduino framework, Visual C# and Windows Universal XAML.
If Arduino is new for you, please refer this link. For Raspberry Pi 2 beginner, please refer this link.
Let's understand software in following formation:
- Protocol (How Raspberry Pi 2 and Arduino talks)
- Class Structure (How Raspberry Pi 2 maintains such complicate devices and rooms)
- User Interface (Wire-frame)
Before going further, lets first decide how Raspberry Pi and Arduino talks. To make a reliable protocol, we must first have clear objectives or goals for the communication. In consideration of this project, goals are:
- Read sensors
- Read device's state
- Set device state
Lets start defining protocol first. Protocol define rules to communicate over the bus. Protocol is nothing more than byte sequence.
I have defined protocol for sending and receiving bytes. Sending bytes are fixed of three while receiving byte array is of fourteen bytes.
Refer following schematics to understand protocol defined for this project ('X' denotes random value or '0', it will be ignored while communicating):
Class named 'I2C_Helper' contains implementation of protocol in PRi2's Universal app project. This class is available in Universal Windows Project. To open it: Goto Solution Explorer > Library > Communication folder. On Arduino side, it is easy to understand I2C library.
If you are newer to I2C of RPi2 (Windows IoT Core), refer this link. If you are newer to Arduino's I2C, refer this link. There is an article available for Arduino I2C Communication with RPi2 (Windows IoT Core) written by Christiano Faig.
Now, we have defined a proper communication scheme that is able to give status and values of sensors, devices and also provides accessibility to set device state.
Class Structure (How Raspberry Pi 2 Maintain Objects)As discussed first, this project considered whole home. Home consists of multiple rooms and room consists of multiple devices. Thus the OOP structure of such configuration is shown below:
Primary class "Home" consists of multiple room objects as generic list of room (List<Room>). Home class provides static method to load and save home object on the Pi to use them later.
Room consists of devices as generic list of devices (List<Device>) and sensor structure to encapsulate all sensors into one.
Device consists of details of them and functions to turn on and off them using protocol Mode - 2. AmbientLight, PassiveIR and Temperature class provides access to the room's environmental data. Room object maintain sensor data and periodically refresh them using protocol Mode - 0.
Note: Class diagram is provided in project. To view it, goto 'Solution Explorer' and double click on 'ClassDiagram.cd'.User Interface
Any application must be user-friendly. To make user-friendly application, start with wire-frame. Assume that you are going to use your own application and find out the main objectives and how to integrate them in such way that they will be most easy to end-user.
In this application's scenario, our main objectives are:
- Room Management
- Device Management
- External Services like GSM Communication, Internet Communication, etc
To do so, I have splitted problem into:
- Home Page: Provides basic device status information, date-time and lock
- Favorite Devices: Direct access to favorite devices
- Room Page: Provides access to configured rooms and their devices
- Common Water Devices: Water-pump and Geyser
- Settings/Configuration: Provides management of rooms and their devices
Based on the wire-frame, I have developed UI which is shown next. You can download complete source code. Link to the download is provided at the end of the article.
I am assuming that you are familiar with XAML(people having WPF background will have advantage). XAML Source code of the project is well commented so I am not going to explain it here as its not feasible. If you are newer to WPF, refer this link for getting started with WPF and MSDN link as a reference.
So, have already developed UI. Hope you can modify it upto your needs. Feel free to comment for the help.
How to configure?I have tried to make this software as easy as possible. With minor configuration, you can operate devices directly using this Raspberry Pi 2. Step-by-step configuration is shown below:
You can attach water-pump and geyser same way you add device. But you can't attach water-pump or geyser directly to the relay board to power them up. It will blow-up relay board as well as hazardous.
WARNING:
DO NOT CONNECT WATER-PUMP OR GEYSER DIRECTLY TO RELAY-BOARD. IT IS HAZARDOUS. YOU NEED CONTACTOR TO OPERATE HIGH CURRENT DEVICES LIKE WATER-PUMP OR GEYSER. IF YOU DO NOT KNOW, PLEASE CONSULT ELECTRICIAN. YOU CAN FIND DETAILS ABOUT CONTACTOR BELOW IN PART LIST.
To operate water-pump or geyser, connect water-pump or geyser with contactor and connect contactor's coil with relay board. So now, when you operate device, relay activates contactor's coil and thus pump or geyser starts/stop. Before purchasing contactor, please make sure its coil rating and contactor's rating. Contactor are available in so many variety. So verify it with your water-pump's rating. It is advisable to purchase slightly higher rated contactor than your water-pump's rating.
WARNING:
DO NOT TOGGLE DEVICES QUICKLY. IT MAY FRY DEVICE AND MAY LEAD TO ELECTRONIC SHORT-CIRCUIT. IN CASE OF ANY PROBLEM, SHUT DOWN HOUSE'S MAIN LINE OR ROOM'S TRIPPER LIKE MCB OR ELCB.
How do I deploy this solution to Raspberry Pi 2?You can refer this link to understand deployment process.
How do I register this app as startup app?Lot's of hobbyist wants their application start right after the Raspberry Pi 2 boots. To do so, read my article: Windows 10 IoT Core : Setting Startup App
Known IssuesArduino BootupWhen Arduio boots, it flickers D13 pin. It is good to do not connect any relay pin on D13 because when Arduino boots or reboots, D13 will be flickerd once and if any device connected with Relay (controlled via D13) will flicker, too.
Solution: There are two options: first one, simply do not connect realy with D13. Second one is bit complicated. You can rewrite Aruino's OptiBoot firmware that do not use D13 while booting and burn that bootloader to Arduino.
Clock IssueRaspberry Pi 2 does not have on-board Real Time Clock chip. Thus it is not possible to maintain time after power-failure or fresh boot. Thus external RTC must be attached and programmed to maintain date time.
Solution: NTP can be used but needs Internet connection or even Arduino can deals with RTC and Raspberry Pi requests for the date time at boot. It is also good to attach external RTC directly with RPi 2.
I had faced many issues while developing Universal App in C#. Universal app is superset of WinRT and thus so much asynchronous operation needed to be performed. If error arises in asynchronous operation, it becomes headache to solve. Whenever any exception or error occurred, most of time I faced following screen:
No point to the error prone line, no programmer friendly message, nothing. I have to place so many breakpoint to find out the cause of the error. In debugging, sometimes universal app just shuts-down without any prior notice and suddenly my watch window becomes useless. The cause of the behavior is described at this link.
Solution: Place breakpoint before the expected error prone line.
In short, at this moment (while this article was published) universal app is great but lacks of debugging features as conventional Windows application have.
FutureThere is no limitation when it comes to features, new ideas or even to overcome existing limitation. But it is not feasible to add each and every feature at once. At this release, this article lets you understand real power of Raspberry Pi 2 and Windows IoT. Windows Universal XAML is great GUI framework for Windows 10 IoT & Raspberry Pi 2 have good graphics processor on board. By combining these two, an extraordinary looking GUI solution can be created. In addition, Raspberry Pi 2 have 900MHz quad-core CPU, which is quite awesome for multi-threaded (in UWP, Task) solutions. This is the basic to intermediate project for those who wants to learn fundamentals of embedded and software system (i.e. I2C bus communication, how to operate appliances, custom protocol design, OOP design for real-world application and wire-frame).
In future, we can add capability to communicate over remote devices using RadioFrequency or InfraRed instead of I2C bus. Web-management portal using Azure can be integrated for mobile devices. Further the real automation will be integrated like event based operation, timed operation with RTC chip. For example, turn on backyard lights at 7:00 P.M. and turn back them off at 10:00 P.M.; a good example for event based is to turn on garden's lights when ambient light intensity decreased below specific intensity and so on. So there is no limitation for this new Windows 10 IoT Core platform for Raspberry Pi 2.
Good Luck & Be Safe.
Feel free to ask for help or questions.
Comments