This project is to get a Python script to control Smartthings with the Kano coding wand as an input.
My daughters (8 and 12) are big Harry Potter fans and got the Kano Coding Wand for Christmas. The Kano coding app is cool and they're having fun with it. Great tool to introduce coding concepts.
We also have a bit of home automation sprinkled throughout the house with SmartThings, Phillips Hue, Logitech Harmony hub, etc... They inevitably got to the point where they wanted to turn the Christmas Tree Lights on with the wand and started throwing terms like Lumos and Nox at me. Sounded like a fun challenge so I took it on.
Had to go with a Linux OS since a critical element (Bluepy python library for connecting to the wand Bluetooth) is only available on Linux platforms. Convenient anyway from the standpoint of eventually wanting to running this on a Raspberry Pi.
There are two main reference here, without which, I would never have been able to do this.
Thanks to GammaGames for creating and sharing a python script for reading the Kano coding wand.
Thanks to rllynch for creating and sharing a python script for a SmartThings command line interface.
In order to keep this intact, I'm going to copy most of the steps over to this Instructable to capture any tweaks I needed to get it working on my setup.
Here's what you'll need:
- Harry Potter Kano Coding Kit (https://www.amazon.com/Kano-Harry-Potter-Coding-Ki...
- A Linux Based OS (I got one up and running with VirtualBox, then installed on an Raspberry Pi)
- A Bluetooth USB adapter (I used a Kensington Bluetooth 4.0 USB Adapter)
- Internet Connection (For the RPi, I used an Edimax USB WiFi adapter I had from a previous project)
Step 1: Install Linux on Raspberry Pi
Follow the links below to install a Linux OS onto the Raspberry Pi. I used Raspbian Stretch with desktop and recommended software and flashed with Etcher.
Once you've complete the install and have a Linux Command Prompt, it's good practice to run the following two commands to be sure everything is up to date.
sudo apt-get update
sudo apt-get upgrade
Type the following at the command line interface to launch the desktop UI.
Next thing to set up is an internet connection so that you can get to repos and such to install further software. Internet connection will also be required to hit up the SmartThings API. Follow this guide to get connected. Pretty straight forward from the desktop. I used a USB wifi Adapter I had laying around.
Alternatively to the Raspberry Pi, you could use another existing computer to either dual boot a Linux OS along with your existing OS (kind of a pain because you have to reboot to switch between the two) or run an instance of the Linux OS on a VirtualBox. To first get this project initially working, I installed Debian Stretch with Raspberry Pi Desktop in a Virtual Box using this guide:
(note: I had a heck of a time getting Guest Addons installed properly per the instructions above. Never got the cut and paste between host and client to work, which would have been nice, but I did manage to get the resolution updated to utilize my full monitor size. This was a series of google rabit holes which I will not document here.)
Step 2: Install Python 3
Python3 should already be installed with Raspian Stretch.
Step 3: Setup Wand Module
Follow this guide created by GammaGames
I first had to change to a different directory before cloning the kano_wand repo, otherwise my python script couldn't find it. Probably could have updated some path references in some file somewhere, but I didn't dig into that.
git clone <a href="https://github.com/GammaGames/kano_wand.git" rel="nofollow"> https://github.com/GammaGames/kano_wand.git</a>
sudo pip3 install bluepy moosegesture
Had to use sudo for these to get the proper permissions. Also had to use the following commands instead to install numpy, for whatever reason, couldn't get pip to work. Might have been another path issue, but this worked for me so I went with it:
sudo apt-get install python3-numpy
Lastly, in order to get the proper permissions to run bluepy from the python script I found this command.
sudo setcap 'cap_net_raw, cap_net_admin+eip' /usr/local/lib/python3.5/dist-packages/bluepy/bluepy-helper
This is all that's required to get our script up and running. The rest of the GammaGames guide walks through the step by step pieces required in the python script. It's a great breakdown to further understand how the script is structured and what each object is doing. Big thanks to GammaGames for providing this documentation. Also could be useful for troubleshooting a section of the script. For example, the first object shown performs the task of scanning for wands and returning a list of wands discovered. Good verification that your bluetooth setup is firing on all cylinders. To do this, you can copy the code from test1_BLE_wand_detect.py found in the following repo:
Step 4: Setup SmartThings CLI
Below are a copy of the instructions included in the smartthings_cli repo on github (https://github.com/rllynch/smartthings_cli).
I've included here the small tweaks that I had to make on my setup to get everything working. Thanks again to rllynch for providing this.
1) Log into <*see below> and under My SmartApps, create a new SmartApp with the code in groovy/app.groovy.
*Note in the first step there is a reference to a SmartThings website. Pay close attention to where your smartthings account is. This tripped me up for a while as the other site allowed me to login, but couldn't find any of my stuff. I had to use the following link to get to my SmartThings account.
(You're welcome for the 2 hours I spent sorting that out :) this will be important at later step as well.)
2) Click App Settings and under OAuth, click Enable OAuth in Smart App. Note down the OAuth Client ID and OAuth Client Secret. Update the OAuth Client Display to SmartThings CLI Control. Click Update.
3) Go back to My SmartApps then click on SmartThings CLI Control. Click Publish => For Me.
4) Clone the smartthings_cli repository, create a virtualenv if desired (I didn't do this), then run the following commands, replacing CLIENTID and CLIENTSECRET with the ID and secret from step 2.
To clone the smartthings repo you can use the following command. Be sure that the linux command prompt is in your project directory created during the Wand Module Setup.
git clone <a href="https://github.com/rllynch/smartthings_cli.git" rel="nofollow"> <a href="https://github.com/rllynch/smartthings_cli.git</a"> <a href="https://github.com/rllynch/smartthings_cli.git" rel="nofollow"> https://github.com/rllynch/smartthings_cli.git>></a></a>
Then change directory again down to the smartthings_cli directory that was just created.
python setup.py install
smartthings_cli --clientid CLIENTID --clientsecret CLIENTSECRET
5) smartthings_cli will direct you to a URL to authorized access. Copy the URL from the response in the command window AND BE SURE TO UPDATE IT with the proper path as we had to in step 1. Go to that URL in a browser and specify which devices the CLI should be able to access. Click Authorize when finished. You should be redirected to a page reporting smartthings_cli.py received auth code.
Last few things I needed to do in order to get rid of a warning that kept coming up:
sudo apt-get install libssl-dev
pip install service_identity pip install attrs pip install pyopenSSL pip install pyasn1 pip install pyasn1-modules pip install ipaddress
Raspberry Pi should now be set-up to issue smart thing commands from the command line interface, try it out with these examples:
smartthings_cli query switch all
smartthings_cli query switch "Switch Name"
smartthings_cli set switch "Switch Name" on
Step 5: Improve Response
Everything is running at this point but there's a bit of a lag once the wand gesture is captured. In attempt to speed up the response, I've embedded the smarthings logic into the SmartWand python script rather than calling it from a command line as it does in SmartWand.py . This eliminated the need to repeatedly import the modules required for smartthings communication, which is what was slowing everything down. Here's what I had to do to get that working:
python3 -m pip install future
python3 -m pip install twisted
Made update to the python script. See SmartWand2.py stored in the following repo:
Step 6: Make SmartWand Execute on Raspberry Pi Bootup
So you only need to plug in the raspberry pi near your SmartThings things and not require a monitor, and keyboard, I followed these instructions to get the script to run on boot or any time a command line terminal is launched. The script seems pretty robust but does get hung up from time to time, requiring a reboot. Alternatively you could have a keyboard connected and use alt+f4 to kill a running script and ctrl+alt+t to launch a new terminal without needing a monitor to see anything.
Method 2: modify the .bashrc file as described in the link below: