The light switch, it's been sat on the wall quietly doing its thing for decades with very little change, but our lighting has changed significantly. In the UK we have few choices for home automation light switches and most of these are difficult, unfriendly and just don't do the job all that well, not to mention not being open hardware or software!
It's time for that to change. Lets bring the light switch out from the dark side and what better way to control a photon emitting device than with a Particle Photon!
Here's a sneak peak of the switch in action, watch out for the 'These are not the light bulbs you are looking for' Jedi waves ;-)
Yes, this is an Internet connected light switch...
Note that the landing light isn't usually blue, I set it that way to help show that the switch can control other lights - Honest!
The Problem:So you have modern lights like the Lifx Wifi lights and you want better control of them as well as other lights around your home from a fairly normal light switch on the wall and not a crazy app on your phone.
If like me you live in a rented house you'll find it difficult to replace the light switches without becoming homeless very quickly, perhaps you really don't want to be playing with 240 volt mains electricity, and even then in most UK properties you'll find that the light switch has only a tiny 15mm backing box and "2 wire" connection (live and switched live return + earth). Sadly most home automation switches you can buy need a 3 wire connection (live, neutral and switched return + earth), and a 25mm box.
So, you've replaced the wire to your light switch with a 3 wire system, you've smashed the wall to bits to fit a 25mm backing box, plastered and painted to make it good and your landlord is cool with all that. What next, that new home automation light switch you had in mind is a dimmer type for ye olde incandescent lamps and doesn't work with your new sexy Lifx lights or most LED bulbs come to that. Drat.
Next, ahh yes, you also want the light switch to switch on and off your table lamp, and it would be neat if it could also turn off all the other lights in the house by a special touch when you go to bed at night. Well good luck with that, most of the home automation switches really only want to work with their own lights, or are simple "slave" type controllers for the light at the end of the wire and don't tell anybody if they have been pressed.
You could just leave the existing light switch alone and for the new generation of WiFi light bulbs that sadly is just about the best option - other than fitting a Jedi Light Switch naturally ;-).
To get the best out of WiFi lights you want them powered all the time, even when "off". So your light bulb is off but the light switch is on, all you have to do when you walk into a dark room is:
- Find your phone
- Unlock your phone
- Find the app
- Launch said app
- Wait whilst the app crashes
- Relaunch the app
- Wait whilst it loads the list of your lights and then hides them
- Expand the lights section of the light
- Click on the light you wish to turn on
- Exit the app
- Check your email and txt messages
- Lock your phone and put back into your pocket.
Now that's no fun! Sure it's cool to change your lights from an app but painful day to day. You could alternatively switch the light off and then on again at the light switch, you feel silly but it works for the light on the end of the wire, now explain to your guests that they have to do that, you'll feel doubly silly.
Next up is the issue of switching the light off at the wall switch, if you do that all the lovely automation you have for the light is rendered useless. It won't come on at a pre-set times, it won't come on automatically when you returned home at night, you can't switch it on from your seat when it gets dark and your curtains have closed automatically (I know, seams lazy but hey in my day we use to have to get up and press a button on the telly to change channels - shocking hey!).
You can get extra button things to control the lights, those new little Flic buttons can link to Lifx to control the lights, but it's not so natural to look for that little button to switch the lights off or on as it is to reach for the light switch, you still need your phone in range and switching the light switch off always wins in terms of controlling your lights, and we don't want that.
You could also ask, what else could that light switch be do for me, can it determine the light levels in the room, can it double up as a thermostat to give room to room temperature and humidity sensing, can it glow gently to give a nice funky look or so your guests can find it when they wake up in the the night and the hall way is dark and they can't find the switch, can it flash to draw my attention to the door bell ringing or some other situation. My guess is no.
The Jedi Light Switch features:
- Jedi Wave control to quickly hide your droids in darkness
- Gesture control (on, off, dim, bright) - ok, so this is the Jedi Wave control
- Touch control (single touch on/off or long touch ALL lights off)
- Control one or many [Lifx] lights, not just the one on the end of the wire
- Immediate LED feedback
- Temperature and humidity sensing
- Light level sensing (Ambient and RGB)
- Bright RGB glow functionality (V1.2)
- Dim RGB glow for ease of locating the light switch in the dark (V1.1 and V1.2)
- USB powered
- Optional LiPol battery powered (V1.2)
- Build in LiPol battery charger and management (V1.2)
- No need to replace existing light switch
- No mains wiring alterations or connections needed
- Easy to print customisable case
- Minimal additional height needed over existing switch (approx 3mm)
- Integration with IFTTT and Tinamous through the Particle.publish() system
- Lifx Cloud integration (via Tinamous or IFTTT)
- It doesn't make tea of coffee, although I'm sure it could integrate with a cloud controlled coffee maker
- Internet control of flash and glow functions to aid notifications
- Open hardware and software (On Github). Hack away! :-)
The Jedi Light Switch is made up of four parts.
- A 3D printed backing box that slides between the wall and the current light switch.
- A PCB. No small mechanical switch parts, just a simple "PCB as a front panel" design.
- A cover to hold the PCB in place on the backing box
- Software
The Backing Box:
The box used in the project photo and below is clear, printed on a Form2 SLA printer and really allows the light from the RGB Neopixels to shine though on the V1.2 boards.
However printing on a SLA printer is messy and it's not so easy to find one. I've printed the other cases on a regular 3D printer (an Ultimaker 2).
The backing box is designed such that it has a small flat area to fit just under the existing switch to hold the box in place between switch and wall, so not requiring the existing switch to be removed, just loosened enough to allow a 1mm gap behind it (don't forget to turn the power off when you do that). It also has a 1.6mm sholder to allow the PCB to fit in snug and flush for the front cover.
Both base and cover .stl files are created using JediLightSwitch.scad OpenSCAD file, at the head of the file are options to generate the base, cover or to include models for PCB and switch so you can visualise the design should you want to alter it. Depth variable is also set so you can modify the depth should you wish for one that is not included.
The attached .stl files included allow for different size backing boxes, for newer light switches a 16mm deep box is enough to cover the switch and have clearance between the PCB and the wall switch, however for older switches a deeper box is required, something in the range of 25 to 28mm. You may wish to experiment with this to get the best (smallest) fit.
No supports are needed. Suggest a highish (40+%) fill.
Print time: 3-5 hours.
The Cover:
Like the base the cover is 3D printed from the files attached. I've added a few variations to cover the V1.1 and V1.2 PCBs (see below) as well as different depths so that it correctly fits the base.
If you find the cover is loose over the base (with the PCB fitted), or too tight adjust the coverToBasegap variable in the OpenSCAD file, the cover is only friction fit so this needs to be tight but not too tight.
No supports are needed (you will need to rotate the stl model in your printer software). Suggest a very high (80+%) fill.
Print time: 3-4 hours.
The PCB:
This part is the most complicated and will tax your soldering skills. Two versions of the PCB are available, V1.1 is the most basic and V1.2 builds on this to add some fun features. This is what happens when you get carried away with Eagle open in front of you!
The PCB itself is 100mm square with rounded corners so it covers the wall switch and fits inside the lip of the base box. I've had PCB's made from both OSHPark and DirtyPCBs and both have worked well. You will want to have a stencil made either by OSH Stensils or as part of the DirtyPCBs order.
OSH Park made PCBs come with a nice ENIG coating by default which is good for the surface mount components, they all have a purple solder mask and come in 3's. They also come with the remains of the panel tabs, these need to be filed off to fit properly in the case.
Dirty PCB's has a HASL finish by default so I suggest paying the extra for the ENIG coating which will help with the top layer components. It's a good idea to include a stencil in the order as well as these are nice metal ones. You can select a range of solder mask colours from Dirty PCB's so if you prefer a white, black, red, green, blue or yellow coloured switch they are the best choice. Watch out however as they print the batch number on the board which if you go for a clear or minimal cover the batch label will be visible. These PCB's come in a pack of 10 for $25 (or $40 with the ENIG coating, and +$25 for the stencil).
PCB V1.1:
- Particle Photon WiFi based
- Large touch area
- APDS 9960 Gesture sensor (with light level and RGB level).
- HTU21D temperature and humidity sensor.
- SMD SIL strip contacts for a daughter board expansion
- Less complicated to solder
- Low component count
- Available from Dirty PCBs: http://dirtypcbs.com/view.php?share=15693&accesskey=2acdf5409625ab02788e03de472ba62d
- Available from OSH Park: https://oshpark.com/shared_projects/mrRJjKTD
PCB V1.2:
- All the features of V1.1 but more difficult to solder and lots more components
- LiPol battery charger and monitor
- NeoPixel (WS2812B) for a stronger glow
- Analog light sensor (the APDS-9960 is very directional)
- Front mounting IR LEDs (untested) to use as a remote (internet) remote-control for the telly, HiFi, set-top box etc.
- More test points. Everybody loves test points :-)
- Available from OSH Park: https://oshpark.com/shared_projects/h9wcYkoW
Both versions have the very difficult to solder components (APDS-9960 & HTU21D) on the top layer. These need to be reflowed to solder them properly, the bottom layer of the board also has SMD components but these can be hand soldered. V1.1 is the easiest to solder as it has the least number of components. The top layer has to be done first and prevents us using the reflow technique on the bottom so it's hand soldering all the way on the bottom layer.
Particular care needs to be taken when soldering the large tant capacitor (C1) for the APDS-9960 which may result in significant heat transfer to the top layer and cause the solder to melt and short some pins on the sensor. Check for GND and 3v3 shorts regularly when soldering as this happened to me on 2 boards and they needed a quick reflow on the top layer.
All the resistors and capacitors are 0805 packages, these are small but with practice not overly difficult.
The general principal for soldering all the SMD components on the bottom of the board is:
- Start small (i.e. ICs then resistors/caps) so as to not hinder your working area for the soldering iron
- Heat one pad where the component is to go and apply a small drop of solder
- Use very fine (0.4mm or less) solder for best/easiest results
- Carefully place the component with tweezers ensuring it aligns well with the pads
- Watch for the polarity of the corner LEDs (D1, D2, D3 & D4) and ensure that the lens is poking through the board and not up at you
- Reheat the soldered pad to make a single point of fixing for the component, you may prefer to the drag method for resistors and capacitors
- Solder all the remaining pads when you are happy the component is correctly placed
- Resolder the initial contact to ensure you have a good clean joint
- Use solder wick to remove any excess solder
- Don't solder on the Photon until you've tested the board
Selection of the resistors R5, 6, 8 & 9 and LEDs D1, 2, 3 & 4 are up to you. I've chosen 1k resistors and high intensity blue leds, these shine through grey and white PLA printed covers really nicely (red and black PLA not so well), however if you go for a clear cover increase the resistor and/or decrease the intensity of the LEDs selected, otherwise they will be way to bright. Change the colour if you don't like blue.
Soldering of the top layer is a more involved and should be done before the bottom layer.
Carefully place the stencil over the PCB ensuring the pads of the PCB align with the holes in the stencil. Push solder paste along the component line ensuring all the pads get a covering.
Note that for some reason the OSH Stensils think the large touch pad needs solder paste, hence the big hole in the picture below, remove any solder paste that gets on it.
Carefully remove the stencil (attach the top of the stencil to a frame with electrical tape to allow it to be hinged off and other boards easily placed underneath).
Inspect the PCB to ensure that the pads got a good covering of solder paste, next place the components and reflow the board using either a reflow oven or rework station.
I had best results with a SMD rework station I picked up on eBay (i.e. a fancy hot air gun), by setting this at low flow and a temperature above the melting point of the solder and moving it gently over the components a good reflow was achieved. I found it best to watch the HTU21D (U2) for reflow as the APDS-9960 pads are hidden so it's difficult to tell when the solder has flowed.
Pictured above is the reflow oven I used in my local Makespace, this is a modified (i.e. fixed and made safe) version of the ones sold on eBay, unfortunately it didn't really get up-to the full temperature of the profile and took 2 goes to reflow the solder, even then I ended up having to rework one of the boards with the rework station. More hacking needed!
One nice (unintentional) aspects of the design is that if you don't want a particular part or feature you can leave it off. Should you decide that the top layer soldering is to difficult (actually it's easier than the bottom layer, just more involved) you can leave off the sensors and disable them through software, relying only on the touch part of the switch and modifying the case cover to hide the unused pads.
Likewise with the bottom layer, if you don't want a LiPol battery (currently no power saving is done in the software so a big 2000mAh battery will last a day if you are lucky!) then you can leave the appropriate components off. If you don't want the bright glow of the NeoPixels leave them off, if you don't want touch control, leave the touch IC, resistors and capacitors off.
Before soldering on the Photon (you'll need the headerless version) test your board, it's very difficult to de-solder a photon and whilst the pricing is great you don't want to waste one. Pay attention to any short between GND and 3v3 as well as VIn and check the battery connection and diode polarity as well as LED polarity. I found it useful to solder wires to the test points on the V1.2 board to apply power before the photon was connected.
Only the edge connections of the Photon need to be soldered. The ones in the middle of the board for the RGB LED and USB port are not needed as we have access to those already. I found it easiest to spot solder one corner to hold the Photon in place and to work down the side and then repeat on the other side.
A Note about the eagle files:
I've included eagle schematic and board files for both versions as well as .pdf's. Due to the board size and Eagle's somewhat unfriendly freeware limitations you will need the Make Pro/Personal or full Eagle license to edit the board. Even then the Make versions will be tight due to the 160x100mm max board size. You can however view it without restriction.
PCB assembly time: 1-2 hours.
New skill level: Jedi Soldering Master++
The Software:
Design:
The software is designed to have a hardware abstraction layer to accommodate the changes between the V1.1, 1.2 and future boards, each feature is divided into a base class with the appropriate methods/properties and common functionality and then version specific sub-classes implement any version dependent parts. In the event of a feature not being supported (e.g. battery charging on the V1.1 board) the V1 subclass simply ignores this, similar to how the null object pattern works.
The use of this pattern means that in the main file a simple HARDWARE_REV constant controls which classes are instantiated at power on and are used. No other changes and required between versions even though pin usage may be different and some parts not available. Sadly this does result in the need to use pointers so, erm, sorry about that :-(
Libraries:
I used a slightly modified version of the library from SparkFun for the APDS-9960, I found that the I2C bus would lock-up so I added a 100 microsecond delay in the wireReadDataByte method between writing the I2C register request and reading the result. I'm not sure if this is a requirement of the 9960 or a quirk of I2C on the Photon.
I've also used the HTU21D, Neopixel and Powershield libraries available on the Particle Build platform. The application was developed using the web based Particle build, if you also use that you will need to manually include those libraries as the platform isn't clever enough to bring them in automatically.
How it works:
At power up version specific classes are instantiated each of these implement an init() method which is called to initialise all of the hardware.
Two interrupts are used from the APDS-9960 and touch IC to notify the Photon that an action needs to be taken, these set flags which are inspected during the loop method. If the flag is set then an appropriate handling routine is called.
Sadly the Photon doesn't support wake from two different interrupts so power saving is difficult, and annoyingly one is active high and the other low so a simple diode hardware solution isn't possible either.
When a touch is sensed the pin is checked until the touch is released, this results in a Particle.Publish() message with the name "Status" and data either "Touched" , "Long touch" or "VERY Long touch" depending on how long the pad was touched for. This could be used for example to indicate that all the lights should be turned off, or set a mode to "Away" for automation.
The gesture sensor works in a similar way, publishing "Status" messages with "Swipe Left", "Swipe Right" etc.
The user is given immediate feedback on their actions through the use of the 4 corner LEDs. A swipe left, the left two LEDs are lit. Swipe up, the top two and so on. On touch all four LEDs are light, then on long touch alternating sides, and very long touch alternating top and bottom.
In the background a timer is used to set a flag every 10 seconds, this is used to indicate a set of measurements should be taken which includes temperature, humidity, ambient light level and where appropriate battery charge, voltage and analog light levels. The actual functionality is done as part of loop() rather than on the timer as the measurements are published as senml messages using Particle.Publish() every minute or so.
Publish has a maximum message size of 255 bytes and the senml message is bumping up to that, any changes to the field names or additional fields may result in the need to send a second message.
Once measurements are taken the light level can be inspected and if this is below a threshold can trigger the glow function of the switch.
By publishing the messages as simple 'what happened' messages a 3rd party platform can be used to interpret them and can be easily modified without having to write new firmware to the switch or needing custom versions for each switch.
External Resources:
I had hoped to use the Lifx HTTP API directly from the Phonon code, however sadly https support is not so easy on the photon, and I also did not want to hard code which lights were controlled by the switch and have to have different deployment configurations for different switches. Having a standard output of the switch and that being interpreted by an external service solved both of these problems but does a degree of latency.
Initially I used IFTTT which was nice and easy to connect up a Photon to Lifx, I published "Touched" and "Swipe Left" etc. from the Photon based on the user action and had that trigger a Lifx action. Sadly it turned out that their was a 5-10 second delay between the publishing of the action and the light being controlled, whilst this doesn't sound much it turns out standing in a dark room waiting for the light to come on for 5 seconds really seams like a long time.
Next I moved on to Tinamous.com (full disclosure, I'm the founder of Tinamous, this was one of the reasons for using it :-). Tinamous is an IoT platform that mixes traditional IoT device data with a Twitter like status feed. I had already planned to use Tinamous to log the temperature, humidity and light levels from the switch so extending it to control the lighting was a natural progression.
Connecting the Photons to Tinamous is easy, add a "Particle bot" to your account, this will link up to Particle and add your Core, Photon and Electrons to your Tinamous account. Now anything you publish from your Photon with the name "Status" will appear on the status timeline, likewise publishing senml formatted messages with the name "senml" will add these as measurements for the device.
Tinamous allows for the triggering of real time notifications based on status messages received, so I created a "Lifx Bot" that works much the same as the Particle Bot and links your Tinamous account to Lifx.
Yes you can now chat to your light bulbs:
To control a light you add a notification, select the status message you wish to trigger on e.g. "Touched", filter by which Particle device it has come from (one, many, or all) and then perform a Lifx action such as toggling the power to a light or group, setting the state (power, brightness, colour) or activating a scene.
Having the Photon linked to Tinamous not only means I can monitor the room's environment but also call functions on the Photon/Light switch such as glow or flash, and these can be linked to notifications (e.g. If door bell posts "Ding Dong", flash the light switch LEDs.)
Powering the light switch is the only down side I've found to using the Photon based switch, sure I could have gone for some low power RF device and a small battery but the Photon makes it really easy to get it connected and to be able to send signals to the switch from the internet and to customise the firmware. With all the LEDs in the switch a small battery would not last long, and batteries do eventually need changing even on low power devices.
Version 1.2 of the PCB allows for a LiPol battery, this is not fully tested at this time, please use with caution as LiPol batteries don't always behave well (i.e. they can explode), the designs for the charging and monitoring circuit have been borrowed from SparkFun Photon power shield so they should be good. I've not adapted the case or software at this time to allow for battery usage which are also important design choices.
For a lot of usages it's relatively easy to plug a Micro USB lead into the Photon and a wall adaptor, or some other USB power source, they tend to be readily available now days, I only have a single location that I can't get USB power from (hence why you can see my landing light switch in the background of the video isn't yet a Jedi Light Switch).
However the USB plug going into the Photon does look a bit clumsy, an alternative would be to solder a 2mm SMD JST connector (the same one used for the LiPol battery, it's even the same polarity) onto the VIn + GND connectors on the switch PCB then a small wire could more gratefully exit the case. I have intentionally stayed away from trying to scavenge the power from the actual mains light circuit for obvious reasons.
Other alternatives include replacing the mains cable and wall switch and dropping a USB power lead through that, or perhaps a daughter board on the Jedi Light Switch that connects into a 12V RS485 home network (you'll notice Tx/Rx are even available to a daughter board connector).
Photos and VideosFitting the Jedi Light switch is easy and can be done in under 60 seconds.
The opening at the bottom of the cover not only helps with a little glow in the dark "where's the light switch" issue but is also valuable to watch the Photon's LED when flashing. And yes, blisteringly fast OTA firmware update for the Photon, from compiled code on Particle build to running on the light switch in 8 seconds!
Internet control of the LEDs via Tinamous calling Particle functions in the software.
Special ThanksI have to say a special thanks to the work done by Sparkfun, if it wasn't for their breakout boards, eagle libraries, software and tutorials making this project would have been immensely more difficult, I defiantly owe them a few beerware vouchers!
Inspiration has been taken from:
Sparkfun RGB and Gesture sensor
Sparkfun HTU21D Humidity and Temperature sensor breakout board
Sparkfun Photon Battery Shield
Sparkfun Capacitive Touch Breakout
The gesture and temperature boards feature in the early prototypes:
Comments