Let Siri Control Your Raspberry Pi
Control your home with Raspberry Pi using voice commands through Siri in 30 minutes
Setting The Scene
At home I have two Raspberry Pi 3 installed, one is the combined web- (Apache) and printer server (CUPS) with additionally a database (MariaDB), the LAMP bundle thus. This setup is working great for staging web pages when developing or doing prototyping. Another useful feature, which I will come back to later, is hosting the “family dashboard” webpage with its dynamic content reading from the database.
The second Raspberry Pi is running solely as a media centre, a standard OSMC installation connected to the TV. It was almost a plug-and-play deal, even the existing remote control worked “out of the box” to navigate the menus. Convenient (especially for any family) is having the home NAS server as the main media library. It will make access to personal content for each member (playing their own music or watch their own pictures etc.) very easy to handle. One of the options in the OSMC settings, is having a connection to a database for storing media information (movie details and play progress etc). Perfect for this is using the database described earlier.
Included in the setup are also the iOS devices — iPads and iPhones. The common storage location of personal- and shared data (documents and media) is on a NAS server.
I could go on for hours writing articles about the above setup. With this, you have endless possibilities at your fingertips to some really useful applications and features. Document management solutions (having indexed, searchable content with OCR features), the area of IoT with all your smart devices (HUE lights, smart plugs) etc. That is for another time though.
How it Works
Controlling your home using voice commands has become really popular lately. Products from Google, Microsoft, Amazon and many more are springing up like mushrooms. Nothing wrong with getting new gadgets, something I always like doing. However, it’s entirely and only serving that one purpose. So here is an idea, why not use the existing setup on your current hardware? You might even learn something new and have some fun doing it.
Once this is running, you will be able to trigger a command through Siri from your mobile device executing a predefined command on the Raspberry Pi device. Basically, you’re talking to your Raspberry Pi.
To give you an example, I wanted my first command to reboot the Raspberry server from anywhere at any time. To verify that the command was received, I wanted a confirmation email sent to me.
Within half an hour I had it working, this includes the complete installation from scratch. That was almost a year ago, it’s still running without any issues.
As the screen-shot shows, I told Siri to, “reboot my berry”, like magic, the Raspberry Pi device rebooted. The SiriControl framework even sends the email back to me.
What You Need
As you might know, Siri can create notes on your iOS device triggered by the word “Note”. Similar to commands like “Remind” or “Set alarm”. I find it’s working better if you separate the Note command and what the note should say in two steps. For instance, my reboot command will be executed whenever a note is created saying, “reboot my berry”. I could activate Siri and say, “Note reboot my berry” all in one sentence and the note is created with the text, “reboot my berry”. However, Siri sometimes doesn’t fully “understand” that it’s a note and trigger another activity instead.
You will have to link Notes with a Gmail account. I’d suggest you create a new dedicated Gmail account for this purpose. More about the specifics later in the installation guide.
On your Raspberry, you will need to install the Python framework called SiriControl.
To conclude, you only need to install the SiriControl framework and link the default Notes to a new Gmail account.
How to Install in Three Steps
1. Create and Configure Gmail Account
As I mentioned earlier, I suggest creating a new Gmail account for this purpose only, the reasons are:
- You will have to enter the credentials in your Python code.
- You will have to enable the “allow less secure apps” option in the Gmail account. It’s done on the myaccount page. Read more about it here at Google support.
Secondly, the IMAP protocol is used by the SiriControl framework to get the new notes. You activate this in the Gmail account settings and in the Forwarding and POP/IMAP section.
Once these two modifications — allowing of less secure applications and activation of IMAP protocol — are done, the Gmail account is ready to use.
2. Configure Notes on iOS Device
You will now need to prepare Notes on your iOS device to have the default account to be the newly created Gmail account.
First, make sure the new Gmail account is added to the list of accounts (in Settings > Accounts and Passwords) on your iOS device.
On your iOS device, navigate and select the account accordingly in Settings > Notes > Default Account. Small advice, check the informational text informing about what the default account is.
To verify that all is working correctly, simply activate Siri and say, “Note”. After Siri asked for what the note should say, say for instance, “This is great”. The note should now have been created in the new Gmail account.
Important to know, the note doesn’t have to be created by the voice command. The process will also work if it’s created manually in the same location.
Now the SiriControl framework is able to get the saved note (created manually or by voice command through Siri).
3. Installing SiriControl Framework
We are almost done. As the final step, we are going to install the “engine” itself, the SiriControl framework on the Raspberry Pi. Not mandatory, but I installed it in the /opt/ folder.
Install by executing the following commands on your Raspberry Pi:
sudo apt-get update
sudo apt-get install git-core
git clone https://github.com/theraspberryguy/SiriControl-System
Once done, edit the file called siricontrol.py and enter your Gmail credentials. Save and close the file and you should have a working solution.
Execute the Python file you just edited called siricontrol.py and on your iOS device tell Siri “Note” and then “meaning of life”. Alternatively, create the same manually. If everything is working fine, the pre-installed module should give you the answer on the screen connected to your Raspberry Pi.
I prefer to have siriControl framework started (by executing the siricontrol.py script) automatically at boot time. To achieve this, edit file /etc/rc.local and add (if you’re running Python 2.7) the following line (don’t forget the ending ampersand):
python2.7 /opt/SiriControl-System/siricontrol.py &
How to Create Your Own Modules
You have completed the installation and now its time to be creative. I am sure you’re eager to create your own command, or module as it’s called in this framework. Each voice command, with its actions, creates one separate module — Python script — hence found in the modules folder.
Every module needs to include the following keywords; moduleName (name of the module), commandWords (an array of words that need to exist in the Note) and the function execute(command) (function being executed).
With basic Python knowledge, the existing modules are self-explanatory. Use the one called life.py as a template. I find it easier to work with than the provided templateModule.py. Copy and rename the file in the modules folder and start doing your magic.
In my reboot module, the smtplib Python library is used for the email functionalities. To trigger the actual reboot of the server, I call a local Bash script containing the “sudo reboot now” command using the Python subprocesses library.
Thoughts, Tips and Closing The Circle
The only limit is the imagination. Just imagine what’s possible with an architecture I explained in the beginning — accessing a database, the dynamic web pages, NAS in the network and the OSMC media centre.
You have the full option of executing any SQL statement to delete, insert or edit content in a database. It’s safe because you’re controlling the type of transaction and only the additional text is provided. The result could be displayed on a dynamic webpage, the example would be the “dashboard” page I mentioned earlier. Imagine keeping an active todo-list or a shopping-list updated by voice commands.
As you might know, connecting and controlling a camera on the Raspberry Pi is possible with the GPIO interface. I know of, but never tested it, a Python library called picamera for doing that.
I mentioned IoT before, the product called OpenHab with which you can hook up to your smart-lights or smart-plugs etc. I know for a fact that Philips HUE lights works great with it.
Executing any type of commands on your device or, using SSH remote commands etc, to any other device in your network, is rather straightforward. Sending the result by email is done safely with the Python library I mentioned earlier.
Lastly, I want to finish off the “tips and hints” session and maybe also to close the circle of “voice-controlled home” products started earlier. It’s the possibility of having speech functionality. Basically, it’s the “final touch” of getting a result — from a Google- or Wikipedia search etc — read back to you.
I let you in on something, I do not have this function yet. However, this is how I plan to go about it, instead of sending the result back in an email I will save it as a plain text file which will be the input text to the “Google Android TTS (Text To Speech) engine” called pico2wave. The audio (WAV) file is simply played with cvlc (VLC) or paplay commands directly on the Raspberry Pi device. The circle is closed.
I am going to leave you with Siri-scenarios covering the previous five “how-to tips” to get your imagination going:
All of a sudden you get a great idea while driving and need to add it to the todo-list:
“write an article about siricontrol”
You’ve recently done the shopping and forgot..:
“Add prosecco to shopping-list”
Did I forget the lawnmower out in the garden:
“Send a picture of my back garden”
Still in bed, you’re reaching for the phone and..:
“Turn on the coffee machine”
You need to know how your system is doing:
“Send status report”
Having a bet with your son on what the 5th decimal in Pi is:
“tell me the value of Pi”