Controlling an air conditioner with a Spark Core

Spark Core and IR LED taped in front of my AC unit.

The iPhone app is fairly... minimalist.

Step 1: Use the arduino code from this Instructable and read in the 'power' button signal from your existing remote. You'll need an IR Receiver for this, I used this one from RadioShack.

Step 2: Use the output of step 1 to create the proper sequence of signals/delays. The arduino code can be directly used in the Spark, you just need to remove the lines referring to 'cli()' and 'sei()'. You'll need an infra-red LED to output the signals, I used this one from RadioShack (PS I know it is stupid expensive, but I live next door to one!)

Step 3: Register the function in the Spark cloud so you can call it via HTTP request. For my code that looked like this:

void setup()   {                
  pinMode(IRledPin, OUTPUT);  
  Spark.function("turnOnAC", turnOnAC);
}

Step 4: Use cURL to test make sure it works. First grab your access_token by authenticating:

curl https://api.spark.io/oauth/token -u spark:spark \          
    -d grant_type=password \
    -d username=<YOUR_EMAIL> \
    -d password=<YOUR_PASSWORD>

That will return an access_token. Then you can use that to call the named function, in my case "turnOnAC". You'll need the access token from the above as well as your deviceID, which can be found in Spark Build.

curl https://api.spark.io/v1/devices/<DEVICE_ID>/<FUNCTION_NAME> \
    -d access_token=<ACCESS_TOKEN> 

Step 5: Make an iPhone app to make these exact cURL requests. I'll put the code online at some point but it is very simple. Wire up this function to a switch in the UI:

For a complete wrap up of the code, see the gist.