DIY WeMo Light Clap-Clap Switch running on C.H.I.P.

in #diy8 years ago

If you haven't heard before, C.H.I.P. is a $9 computer that comes with:

It is a perfect device for IoT and home automation so when I discovered C.H.I.P I instantly fundraised their Kickstarter campaign

C.H.I.P.

My reward came this past week and I immediately started thinking about what could I build with my 3 spare tiny computers hanging around. And the first idea came out: build a clap-clap switch for the WeMo Light Switch so I no longer have to use their buggy and slow android app to turn off the lights every time I go to sleep.

These are the instructions if followed:

I connected C.H.I.P. to my Mac Mini via USB. I used C.H.I.P. Flasher to flash their Headless 4.4 Debian image. You will need a cable to set your C.H.I.P in FEL mode. After flashing I was able connect to C.H.I.P. via SSH over the serial port (screen /dev/tty.xxx). Then I connected to my Wi-Fi and updated apt packages.

nmcli device wifi list
nmcli device wifi connect MYSSID password MYPASSWORD ifname wlan0
nmcli device status
nmcli connection show --active

apt-get update
apt-get -y upgrade

I installed some useful packages like avahi so I can access my C.H.I.P. from any computer on my network using ssh [email protected]

apt-get install -y htop psmisc git-core curl build-essential apt-transport-https software-properties-common ca-certificates unattended-upgrades locales usbmount avahi-daemon unzip wget

I added my public key as an authorized key, so I no longer have to write any password to login my C.H.I.P.:

ssh-keygen -t rsa -N '' -f ~/.ssh/id_rsa
curl -s https://github.com/knoopx.keys >> ~/.ssh/authorized_keys

Then I set my local timezone and fixed the locales:

echo "Europe/Madrid" > /etc/timezone
dpkg-reconfigure -f noninteractive tzdata
dpkg-reconfigure locales

After trying different clap detection libraries I finally decided to make my own so I could fine-tune the detection algorithm. Its source code is available here https://github.com/knoopx/weclap. To install is just run:

curl -sL https://deb.nodesource.com/setup_6.x | bash -
apt-get install -y sox nodejs
npm install -g https://github.com/knoopx/weclap
weclap setup
weclap start

To run it as a daemon at system startup just copy the skeleton systemd configurion to its proper place:

curl -L https://raw.githubusercontent.com/knoopx/weclap/master/share/systemd.service > /etc/systemd/system/weclap.service
systemctl enable weclap
systemctl start weclap

And finally got it running, here's a video of the clap-clap switch in action:

One fun thing to note is that when I play music the hit-hats are sometimes detected as claps and the lights go crazy. I have to figure out a way to stop detection when music is playing. :D