Introduction: Smart Home by Raspberry Pi
There are already several products out there that makes your flat smarter, but most of them are proprietary solutions. But why do you need an internet connection to switch a light with your smartphone? That was one reason for me to build my own Smart Home solution.
I programmed a server application that runs on a Raspberry Pi. This is a java based open source project that allows you to configure your flat and connect several clients and 'controllable units'. I show a solution that handles rc power supply switches, plays music and video on the Raspberry Pi, shows the status on smart mirror and can be controlled by an android app and two pebble apps. The source is hosted on github https://github.com/dabastynator/SmartHome
Step 1: Things You Need
To set-up the Smart Home, you need the following 'ingredients'
- Raspberry Pi at least model 2 B
- 433 MHz sender, something like this http://www.ebay.de/itm/5X-433-Mhz-RF-Sender-Empfa...
- 3 jumper cables connecting the Raspberry Pi and the sender
- Some radio control sockets at 433 MHz
- Android smartphone to run the client app
Additionally, you can extend the Smart Home with more optional clients and units like this
- Pebble smartwatch
- Smart Mirror, see this project https://www.instructables.com/id/Smart-Mirror-by-...
433 MHz controlled LED strip, see this https://www.instructables.com/id/RC-controlled-LE...
Step 2: Prepare Raspberry Pi for 433 MHz
In the following steps you need access to the command line on the Raspberry Pi. To get the access you can read this instructable https://www.instructables.com/id/Remote-SSH-access...
Connect the 433 MHz sender with the Raspberry Pi as shown in the picture above
- GND (sender) <-> 6 GND (raspi)
- VCC (sender) <-> 2 +5V (raspi)
- DATA (sender) <-> 11 GPIO 17 (raspi)
Please also connect a 17cm antenna to the ANT (sender) pin. That increases the signal significant.
Since we need some libraries from other git repositories, we have to install git
sudo apt-get install git-core -y
To set up the Raspberry Pi for 433 MHz communication we need the wiring Pi library for better handling of the GPIOs.
git clone git://git.drogon.net/wiringPi cd wiringPi ./build
Then we need a library that implements typically rc power supply protocols.
git clone git://github.com/dabastynator/rcswitch-pi.git cd rcswitch-pi make cp send /usr/bin/
The 'send' executable allows you to send codes to switch most of the available power supplies.
In my Smart Home setup i also have a rc LED strip described by this instructable: https://www.instructables.com/id/RC-controlled-LED... To set colors for this LED strip you need another sending executable that allows you to send any Integer value (That encodes the color).
Therefore, compile the sendInt.cpp in the rcswitch-pi repo and move it to /usr/bin/sendInt.
sudo g++ sendInt.cpp -o /usr/bin/sendInt /home/pi/rcswitch-pi/RCSwitch.o -I/home/pi/rcswitch-pi -lwiringPi
Now you should now be able to send rc commands with the two executables /usr/bin/send and /usr/bin/sendInt
Step 3: Setup Smart Home Server
First of all you need to install several packages. The Smart Home application is java based and runs fine with openjdk-11. I am not sure about other java runtime environments. The mplayer is a minimalist command-line music player. The omxplayer uses the Raspberry Pi graphics for video encoding, so this should be used for videos. The program ant is needed to build the java application.
sudo apt-get install mplayer omxplayer openjdk-11-jdk ant -y
Setup directories for the jar file and for logs.
sudo mkdir /opt/neo sudo chown pi:pi /opt/neo mkdir /home/pi/Logs
Configure start script to start the application automatically on boot. Therefore copy the attached smart-home script into the directory /etc/init.d/ I also created a script in /usr/bin/ that pipes commands to the attached script, so i just enter smart-home to the console to execute commands.
sudo cp smart-home /etc/init.d/smart-home sudo chmod +x /etc/init.d/smart-home sudo sh -c "echo '#! /bin/bash' > /usr/bin/smart-home" sudo sh -c "echo '/etc/init.d/smart-home \$1' >> /usr/bin/smart-home" sudo chmod +x /usr/bin/smart-home sudo update-rc.d smart-home defaults
Now it is time to checkout the repository and build the application. If you don't want to compile it yourself, you can just download the attached smarthome.jar and move it to /opt/neo/
git clone git@github.com:dabastynator/SmartHome.git ant -f SmartHome/de.neo.smarthome.build/build.ant build_remote cp SmartHome/de.neo.smarthome.build/build/jar/* /opt/neo/
Try to start the smart-home and check the log file. To get access for the GPIOs, the application must be started by sudo.
sudo smart-home start cat Logs/smarthome.log
You should see the error message Configuration file does not exist that points us to the next step. The repository contains a readme that explains the configuration file. You can see this nicely rendered at github: https://github.com/dabastynator/SmartHome
Copy this xml to /home/pi/controlcenter.xml, then set the location for your media server and change the content as you need it. Once you have finished the configuration and restarted the smart-home (sudo smart-home restart) you should see the following content in the smarthome.log
24.05-08:26 REMOTE INFORMATION by de.neo.smarthome.cronjob.CronJob@15aeb7ab: Schedule cron job 24.05-08:26 REMOTE INFORMATION by [trigger.light]: Wait 79391760 ms for execution 24.05-08:26 RMI INFORMATION by Add web-handler (5061/ledstrip) 24.05-08:26 RMI INFORMATION by Add web-handler (5061/action) 24.05-08:26 RMI INFORMATION by Add web-handler (5061/mediaserver) 24.05-08:26 RMI INFORMATION by Add web-handler (5061/switch) 24.05-08:26 RMI INFORMATION by Add web-handler (5061/controlcenter) 24.05-08:26 RMI INFORMATION by Start webserver with 5 handler (localhost:5061) 24.05-08:26 REMOTE INFORMATION by Controlcenter: Add 1. control unit: MyUnit (xyz) ...
The web-server is now running :-)
Attachments
Step 4: Setup Clients
Smartphone Android client
The git repository for the smart-home application also contains the source for the android client, so you can compile it yourself. But I attached the APK for this step, that makes it easier. The first time you start the app, it asks you for a server, like in the first image above. Enter the url of the server and the security token.
That should be it. You now have access to the server and control your flat, playing music and watch videos remotely on your Raspberry Pi. Note that you can add widgets to your homescreen, that makes switches and music-control more accessible.
Smartwatch Pebble client
The source for the two pebble clients is hosted on github. One app shows the current playing music file: https://github.com/dabastynator/PebbleRemoteMusic... This also allows you to pause/play and volume up/down.
The second app triggers three actions: https://github.com/dabastynator/PebbleControl The trigger-names are: mobile.come_home mobile.leaving and mobile.go_to_bed. If you define event-rules for this trigger in your configuration-xml you trigger them by your watch.
It's all open-source, but you don't need to compile it yourself, I also attached the pebble apps. Download the PBW's with your smartphone, your phone should install them on your watch. The pebble apps need configurations to talk to the server. I attached a screenshot how my settings look like.
Smartwatch Garmin client
There is also a client available for Garmin Smartwatches. The app is available in the garmin connect app-store and can be installed here:
https://apps.garmin.com/en-US/apps/c745527d-f2af-4...
Smart Mirror client
I already created an instructable that explains how to create the Smart Mirror, see this https://www.instructables.com/id/Smart-Mirror-by-R... .The source code is also hosted on github: https://github.com/dabastynator/SmartMirror.The software of the Smart Mirror reads the configuration from the file smart_config.js that is not part of the git repository. The content of the configuration file should look like this listing:
var mOpenWeatherKey = 'your-open-wheather-key'; var mSecurity = 'your-security-token';
You also have to adjust the first two lines of the file smart_mirror.js to specify the Smart Home server ip address and the location to get the right weather.
More clients
The server application is a simple web server. This enables you to trigger actions from any client you want to by simple web-calls. In the demo-video i show the android app tasker in combination with AutoVoice. This enables me to trigger events with simple voice commands. For example "ok google, time to sleep" can trigger mobile.go_to_bed. But you can also do web-calls for example from IFTTT. How about a yellow blinking LED strip for email notification?
You can ask the server for possible web-calls like following links (replace ip, port and token by your configuration)
http://localhost:5061/controlcenter/api?token=secu...
http://localhost:5061/action/api?token=security-to...
http://localhost:5061/mediaserver/api?token=securi...
Step 5: Conclusion
There are still some features to implement: Since the server provides just a simple web-api clients do a lot of polling. To reduce the polling I want an MQTT integration for better notification. Also wifi power supplies should perform more reliable than rc power supplies since rc is just a one way communication.
It makes a lot of fun developing for this project. And it is pretty cool to control the flat by several devices, even if the internet connection breaks down.