The Particle Photon has inbuilt capabilities to let you control a LIFX bulb via IFTTT. Perfect for most uses, unless you need the light to come on instantly.
While using IFTTT via Particle Cloud there was a slight lag.
I found turning on the LIFX bulb via the IFTTT service can take between 1-3 seconds to trigger my LIFX bulbs. Not a big issue, unless you are using a motion sensor to active a light as you enter a room - the lag causes people to stop at the door and wait before proceeding into the room.
How do we speed up the response time?
I've found two options:
1. Make a HTTPS call directly from the Particle Device (this was hard to figure out).
2. Use Webhooks via the Particle Cloud to call the LIFX API.
I feel option 1 could be even faster than #2, however at the time of writing - I have not been able to get option 1 working.
Building Option 2: Controlling a LIFX bulb with a Particle Photon using Webhooks.What are we doing?
Here is a quick overview of what the process looks like:
In the diagram below, we have a Particle Photon that will send a message to a 'webhook'. The 'webhook' will then send a message to the LIFX cloud system. The LIFX system will then control the bulb and tell the bulb to turn on.
We'll walk through what we need to do in each box, so you can have a Particle device activate a LIFX bulb.
Step 1. Let's figure out which LIFX Bulb we are going to controlCheck in your LIFX app - look for the name under the bulb you wish to control. In this example we have "Front Door".
The API is something we send a 'message' to that instructs the LIFX system to perform an action on our lightbulb. To control it we need a special key (token) so that only we can change our bulbs.
A. To get your key (token) - go to:
B. Click 'Generate token'.
C. Give it a name - it doesn't matter what the name is.
D. Save your token! You can not go back and get the token again, if you lose it - you can generate a new token in the future.
Now you have your token, save it somewhere safe and proceed to the next step. If you are having trouble - there is a GIF image on this page that shows you which buttons to click.
Step 3. Let's create a Webhook to send a message to the LIFX APIThe Webhook (which is hosted by the Particle team on their website) will let you send a message to the LIFX API. We'll show you how to 'trigger' the Webhook in Step 4.
A. Go to:
B. Click 'New Integration'
C. Click 'Webhook'
D. In the top right - Click 'Custom JSON'
E. Copy in this code: *Remember to put your TOKEN in where it says the word 'TOKEN'. You don't need to put any extra quotations or brackets around it - just make sure there is a space between the word 'Bearer' and your token and there is NO space between the token and the last quotation mark ".
{
"eventName": "LIFX_TURN_ON_FRONT_DOOR",
"url": "https://api.lifx.com/v1/lights/label:Front Door/toggle",
"requestType": "POST",
"headers": {
"Authorization": "Bearer TOKEN-GOES-HERE"
},
"mydevices": true
}
So what does that code mean and what parts do I need to change before I click 'Create Webhook'? This code will 'toggle' the light called 'Front Door' on or off.
F. You need to change some values to make it work for you:
- In the first line - you can swap out the text
LIFX_TURN_ON_FRONT_DOOR
for any descriptive name that helps you identify the action you are performing. Remember this name you will need it later.
- In the second line - you must swap out the text Front Door to be the name of your bulb (You have this name from Step 1). If you also called your bulb 'Front Door' then you can leave it as is.
- In the 5th line - you must replace the text
T
OKEN-GOES-HERE
with the token you now have from step 2D.
G: Once you have modified the code, click 'Create Webhook'.
H: Now go to and make sure it appears in the list.
Step 4. Let's add a line of code into our Particle code to trigger the Webhook we just created in Step 3.*This step assumes you have already connected your Particle device to the cloud and know how to put code onto it. If you need help here - try and refer to another walk through guide e.g.
A. Go to:
B. Create a new app and copy in the below code - this code is for a basic button connected to pin D0 to trigger the Webhook action we created in Step 3.
Code:
//This defines the inputPin that our button is connected to, to be D0
int inputPin = D0;
//This sets the 'LED' to be pin D7
int led = D7;
null
//Now we configure the Pins to be either inputs or outputs
void setup() {
//Set D0 to be the input that we will connect our button to
pinMode(inputPin, INPUT_PULLUP);
//Set the LED pin (D7) to be an output
pinMode(led, OUTPUT);
}
null
//NOTE MAKE SURE YOU PUT IN SUITABLE DELAYS
//This code will keep looping - to avoid having the webhook trigger 20
//times per 1 button press, make sure you have a delay that way it gives
//you enough time to lift your finger off the button before looping again.
//The API has a limit of 1 command every second.
void loop(){
int inputPinState;
inputPinState = digitalRead(inputPin);
//first button checking state
if(inputPinState == LOW){ // Check State of the PIN (i.e. if the button has been pressed)
//This line of code is what triggers the webhook - make sure the webhook
/
ame and the name below match.
Particle.publish("LIFX_TURN_ON_FRONT_DOOR");
digitalWrite(led, HIGH); // Turn ON the LED
delay(1500);
}
else
{
digitalWrite(led, LOW); // Turn the LED off
}
}
*Note: You need to make sure that the line of code:
Particle.publish("LIFX_TURN_ON_FRONT_DOOR");
Has the same 'name' as the Webhook you created in Step 3F. If the names are not the same - the publish command will try to trigger a different Webhook.
Okay - now you are done! Hit the flash button and save that code to your Particle device!
Wiring up the Particle Device to turn the bulb on/off with a button.
Next we are going to wire up the Photon so that by pressing a button - we trigger that special line of code below that will trigger our Webhook.
Particle.publish("LIFX_TURN_ON_FRONT_DOOR");
Step 5. Let's wire up the PhotonA. Connect the Ground terminal (GND) to one side of the button with a cable.
B. Connect the other side of the button to D0 with a cable.
Now you are ready to go!
Power up the device and press the button, the LED on pin D7 will go blue for 1 second when you press the button and the LIFX bulb will turn on or off.
*Be careful about pressing it too many times.
You're done - Congratulations!
Troubleshooting:Q. The LED on D7 is always on - what did I do wrong?
A. Check that the button is around the right way - chances are you've connected it to the same common line.
Q. How do I see if the Webhook worked?
A. When you press the button, view the 'event' list here > If all is right - you should see 'Button pressed 1" and something that looks like this when you 'click' on the "hook-response/LIFX_TURN_ON_FRONT_DOOR/0
" line:
{"data":"{\"results\":[{\"id\":\"dxxxxxxxxx\",\"label\":\"Front Door\",\"status\":\"ok\"}],\"warnings\":[{\"warning\":\"Unknown params\",\"unknown_params\":{\"event\":\"LIFX_TURN_ON_FRONT_DOOR\",\"data\":\"null\",\"published_at\":\"2016-06-11T13:03:10.376Z\",\"coreid\":\"xxxxxxxxxxxxxxxx\"}}]}","ttl":"60","published_at":"2016-06-11T13:03:11.359Z","coreid":"particle-internal","name":"hook-response/LIFX_TURN_ON_FRONT_DOOR/0"}
If you see 'Button pressed 1' that means the code in the Webhook is wrong or the token you entered is incorrect. Leave a comment below and I'll try help you.
Q. It was working - then it stopped
A. Chances are you sent too many commands to the LIFX API too quickly. The limit is 1 action per TOKEN per 1 second interval. Check that the delay is set to more than 1000 i.e Delay(1500);
in the code. If you need to do more than 1 per second, look into getting more tokens and write more Webhooks. But then be careful about how many Publish statements you can send to the Particle API.
See here for API limitations:
Okay - so what if I want to do something other than turn it on/off?There are two options:
1. Check out my GitHub page - I'll add a few more usable examples of code that you copy into the Webhook.
2. Create your own by referring the LIFX API documentation (this is tricky stuff)!
The documentation is located here:
I hope you found this useful and enjoyed it. Please add comments or suggestions below and I'll update the project.
Cheers!
Comments