Creating a Motion Sensing Light with a Spark Core - Revisited with DO IFTTT integration
New in this Artice: Code Review and Adding in IFTTT Do functions.
Guide
New Code
After posting about my experience on the spark.io forums, now particle.io. I received some great feedback and made some changes to my code to make it run smoother and decided to add the ability to toggle the lights on and off for a set amount of time. One of the big changes here are moving some functionality to seperate functions. Including functions to set the lights off (set_light_off
) for a set amount of time and then a function to overwrite that setting and turn the lights on (set_light_on
). Another change was to remove the delay from the loop, this removes the noticable lag between triggering the motion sensor and the light turning on. Also as recommend in the thread, there is now a flag can_turn_light_on
to avoid banging away on the digital pin to write it high every time it loops. The final code that I am running at home is shown below. You can view the original article to see how the circuit is laid out as well as the supplies used.
IFTTT Do Functions
I am using the Do app for iOS which provides access to spark functions. One of the keys to having access to your functions is getting them to compile correctly. To do this I had to make sure that my functions returned a value and that they took some string arguments. I am not really sure why this is required and would love some feedback on that, but I digress. below is my code striped to a single function and only the important parts. First, we have are declarations which must have a return type and take arguments. Next, in setup we must declare our function for use by the Spark API using Spark.function(call_name, function);
. In this case function is whatever the name of the function you want is call is, and call_name is what you will show external systems. Sample code is shown below
To test your function from the command line you can install the spark-cli. And use the following call assuming your spark core is called sample_core
spark call sample_core LightsOff
the key here is to use the function call name you defined as the first argument for Spark.function
in setup
.
Now to make things even more interesting you can use the Do app from IFTTT. The setup here is insanely easy and very useful.
- Inside the App, Configure IFTTT to use the spark channel
- Select Spark under Channels
- Select Create a New Recipe
- Select Call a Function
- Select your core and the proper function in this case
LightsOff
on “sample_core” - provide an unused input.
now you should be able to turn the light off from your phone or anywhere you can trigger the ifttt event!
Conclusion
Nice little update with some cleaner, more readable code, and added functionaility. Really nice to have the ability to turn my lights out from my phone before starting a movie and have them stay off.
###Congratulations you can now set your light on or off from your phone. 🍻