I created a new kind of light switch that will not only operate like a legacy model but also operates smart-bulbs the way they were meant.
A smart bulb, like Lifx and Phillips Hue, are LED light bulbs with built-in Wi-Fi connectivity allowing users to turn them on and off via an application. Today's smart bulbs are being under utilized. Their users are being forced to leave their light switches always on in order to control their smart-bulb remotely. All smart bulb users have faced the everyday frustration of turning on the light switch manually because someone turned it off, which defeats the purpose of a smart bulb. Smart Bulb Light Switch is the solution to this problem!
What is it?Smart Bulb Light Switch is an ordinary looking light switch that allows electricity to continuously flow to your light socket, always keeping your smart bulbs powered on. Smart Bulb Light Switch utilizes the Particle Photons built-in Wi-Fi allowing it to connect to a smart bulb or a group of smart bulbs. This gives it the ability to tell your smart bulb to turn on or off the smart bulb when the switch is toggled, all while never turning off the smart bulb itself. This allows the smart bulb application to work hand and hand with the Smart Bulb Light Switch in the wall.
Who’s it for?- SmartBulb Owners – those who already enjoy the use of a smart bulb but would like the added functionality of using their wall light switch without the frustration of making sure it is on before using the smart bulbs remote controls.
- SmartBulb Manufactures – the companies that already produce smart bulbs will enjoy the added value a Smart Bulb Light Switch can provide to their product.
- EverydayTech Consumers – people that love to find new exciting technology.
- Enjoyment– It’s something I personally enjoy using.
- Demand– There is a large demand for a product like this.
- Innovate– There is an ever growing shift to smart home technology and the need to improve upon it.
- Functionality - toggles on or off the connected smart bulbs via Wi-Fi. When not connected to a smart bulb, it simply toggles the light socket.
- Usability– looks and feel like a legacy light switch making the user unaware that anything has changed.
- Reliability– during connectivity failures Smart Bulb Light Switch behaves like a legacy light switch toggling on and off the electricity to the light socket.
- Performance– turns on the Smart Bulb as quick as a legacy light switch.
- Supportability– it will support Lifx and Phillips Hue with others added in the future.
- Functionality– user can manage connected smart bulbs.
- Usability- application is intuitive and has a detailed tutorial.
- Reliability– 100% reliability is the goal, with fail safes in place to allow for connectivity issues.
- Performance- works as fast as your internet connection will allow.
- Supportability– iPhone app only. For other devices it has a local IP to access its controls.
First thing is first, you will need to create an account with Particle.io in order to access their online Particle IDE and you will also need to purchase a Particle Photon if you don't already have one. For the sake of this tutorial I will assume you have a Photon and have NOT set it up before. If you already have an account and a working Photon skip down to "Step 3 - Start Coding".
- Fill out the form
- Click the "Sign Up" button
The easiest way to claim your Particle Photon with the Particle service is through their mobile app.
- Install the Particle App on your mobile device. It is available for both iPhone & Android (I will be demostrating the iOS app)
- Click the "Get Started" Button at the bottom of the screen and sign in.
- You should see "Setup a new device" menu. From the list, select "Photon":
- Follow the instruction on screen:
- Click "Ready" at the bottom of the screen.
- Again, follow the instruction on screen. If you are unsure of what to do press the "Show me how" button at the bottom of the screen to view a quick video.
- Once you return to the app wait for it to connect to the Photon.
- You will then be prompted to select your wifi network and input your password. Then click connect.
- Your device will then connect to the internet and the Particle Cloud.
- Once setup is complete you will be prompted to name your Photon. Im going to keep the auto generated name for mine, gerbil_doctor, but you can name yours what you'd like. Select Done.
- Now that your Photon is setup, the RGB LED in the center of the board should be breathing cyan and you will see it in your device list.
- If you have any issues with setup you can refer to Particle's troubleshooting page.
- Select your Photon from the the list.
- This is what the Tinker App looks like. You can select any pin to write or read from here. Pin D7 is connected to an on board blue led. If we make that pin HIGH it will turn the pin On and LOW will turn it Off. Let's try it.
- Tap the D7 pin.
- Choose "digitalWrite"
- Now tap the D7 pin and it will alternate between HIGH and LOW, turning the blue LED on and off. Congratulations, you're all setup.
Now that we have successfully created our account and setup our Photon it is time for the fun stuff, writing the code. By default, your Photon should have the Tinker App flashed to it. We will modify the Tinker App's code to publish an event whenever pin D7's state has changed. Which, if you remember, is connected to an on-board blue LED.
- Go to https://build.particle.io to access the online Particle IDE.
- Under "Example Apps" in the "Particle Apps" menu to the left you will see "Tinker" click on it.
- Then click "Use This Example"
- Select line 6 in the coding area. Add this, int previousState = 0; This variable will be used to keep track of the previous state of the pin. By doing this we will be able to ensure that our event code will only run once.
- Copy and Paste the following code to your main loop.
// Read the current state of D7 Pin.
int currentState = digitalRead(D7);
// Check if current state of D7 is different than previous state
if (currentState != previousState){
Particle.publish("Toggle", PRIVATE);
// Update previousState variable
previousState = currentState;
}
- Now you are ready to verify your code by clicking the check mark.
- If all is well you will see "Great Work" at the bottom of the screen.
- You are ready to flash the code to your Photon. Do this by clicking the Lightning bolt in the top left of the screen.
- You will see your device flash magenta as it flashes your code. Just wait until your device starts breathing cyan again and you are done with the code.
- To test that the firmware works correctly, head over to your particle console. Now open your Particle App to the Tinker Screen and tap the D7 pin as you did in step 2, it will alternate between HIGH and LOW, turning the blue LED on and off.
- Look at you console you should see an event named "Toggle" has been published for every time you have tapped the D7 pin.
- Congratulations, you're all setup.
For the purposes of this tutorial I am going to assume you already have a Lifx account and are familiar with how Lifx bulbs work.
Step 1 - Generate An Access TokenAll Lifx API requests require an OAuth 2 access token. You can generate an access token in your account settings. Take care to keep access tokens private as they grant remote access to your lights.
- Log in to Account Settings.
- Click the "Generate New Token" button.
- Name your token something meaningful.
- Copy your token and set aside for now.
Using the Webhook Builder we will create our webhook that will execute an API call every time the event "Toggle" is triggered.
- Go to Particle Integrations page.
- Click "New Integration".
- Select "Webhook" from the list of options.
- Enter the following into the form.
- Event Name:
Toggle
- Request Type:
POST
- Content Type:
JSON
- Device: Select the name of your Device or Any.
- Should look something like this...
- Click "Advanced Settings" to display the advanced settings.
- Scroll down to HTTPHEADERS
- Enter
Authorization
in the next available field.
- In the Empty field to the right then enter
Bearer {{your token}}
replace {{your token}} with the token from the previous section.
- Click Save
- In the top right hand corner click Test
- Every Lifx bulb that you have connected should now toggle its state, congratulations!
- You can now use Tinker like we did before to toggle the state of the Lifx bulb as well. Open the App and tap the D7 pin.
Without showing you how to create an entire app to display and select a Lifx bulb, because that would take FOREVER! I will instead show you how to find a Lifx ID and how to use it with the webhook to control a specific bulb or group.
- Go to Lifx's API documentation for List Lights.
- Scroll to the bottom, Where it says Try It Out.
- Enter All into the selector field.
- Click the Key Icon to the right and enter your token.
- Then click Try It!
- Look through the response and find the name of the device or group that you want to control. In My case it is Kitchen. Then Copy the corresponding ID.
- Navigate back to the Integration that you created in Particle.
- Scroll to the bottom and click Edit.
- In the URL field replace all with
id:{{YourID}}
where {{Your ID}} is the ID of the bulb or group you want to control.
- Scrolldown and click Save.
You are now all done! I hope you found this tutorial to be a good starting point for creating a Smart Bulb Light Switch.
Moving ForwardThis tutorial was meant to give you a good understanding of how we could create a Smart Bulb Light Switch. We left you with the ability to control your Lifx Bulb from a Particle Photon. You could apply the same principle to a Philips Hue. How you proceed from here is up to you. I suggest wiring up any type of switch or sensor to the D7 pin and toggle your smart bulb.
For liability reasons I did not go into how to wire an electrical light switch. But with the right now how and consulting a professional electrician you could connect the photon to a light switch and have it control the bulbs at the flick of a switch. Or go a step further and connect it to a motion sensor and have the bulb turn on at a different brightness depending on time of day.
I hope you found this informative. Happy hacking!
Comments