Home Automation: buttons on the desktop

I want a button on my Mac’s desktop to turn on or turn off the lights I have controlled by the Raspberry Pi. Here’s what I’ve got so far.

First, I wrote a script on the Pi to turn the lights on:

#!/bin/bash
/usr/bin/tdtool --on 1
/usr/bin/tdtool --on 2
/usr/bin/tdtool --on 3

For everything here, I also wrote the equivalent to turn the lights off.

Next, I tested running the script via SSH from my Mac:

ssh -f user@raspberrypi.local /home/user/lights_on.sh &>/dev/null

You can use the Applescript Editor to make pseudo-apps, so I wrote an Applescript to execute the SSH command:

tell application “Terminal”

    do script “ssh -f user@raspberrypi.local /home/user/lights_on.sh &>/dev/null”

    activate

end tell

delay 15

tell application “Terminal”

    quit

end tell

I saved this as file format “Application” in my Applications folder, then dragged it to my desktop (an alias is automatically created, leaving the original in Applications). I can now double-click the app and the script runs, or launch it from Spotlight or the wonderful Alfred:
 
alfred_lights
 
It works but it’s ugly as I have a Terminal window pop up for ~ 15 seconds. There must be a better way.
 
After some searching, I came across Use automator to create X11 shortcuts in OSX. It’s similar to the Applescript trick, but uses Automator instead, so there’s no need for Terminal. I put the SSH command into the “Run shell script” workflow action, and saved it as an application:
 
automator_lights
 
It works! Now I can turn the lights on or off without the Terminal window popping up.
 
There’s a couple of issues:
  • The tdtool command takes a long time to execute on the Pi. It takes about three seconds for all three plugs to switch, whereas the smart plug remote has an “all on / all off” button which is instant. I need to find out why the command is so slow, and/or a way to control all three in one go.
  • I don’t really want “Lights on” and “Lights off” apps, I want a single app that toggles the state. This could be done by making the server-side script smarter, but I’d really like the app icon to reflect the status of the lights too.
Room for improvement, but this is good enough for now.
 
Next up: automating sunsets.
 
References  
This entry was posted in Computing, Home Automation, Personal, Planet, Research and tagged , , , , , , , . Bookmark the permalink.

Comments are closed.