Tuesday, November 21, 2017

MySensors Livolo switches project - Part 1

Where should I start...hmm...let's start with the ending - does this seem ok? Let's see...

Livolo switches and MySensors and BLE and ...

  At the beginning there was darkness lots of darkness until Livolo light switches appeared and suddenly things  started to change and shed some light. Ok enough storytelling ...

  A friend of mine asked me if I could "automate" his home lights so that he could switch the lights on and off at its will and in each room independently. I replied - why not? I like to automate stuff more than anything so this was an opportunity  for me to put some things into practice. More than that I thought - pff, light bulbs ON, OFF, ON... that should be a piece of cake, right?  Well...the reality turned to be a little bit different.

  • First and most important one - we should integrate with the existing infrastructure
  • Touch sensors
  • Radio control and feedback

Let's take each of the above and discuss in detail:

1. Infrastructure:
  Here in the EU we have classic mechanical switches and a simple wiring in the walls as we don't need more than that. So starting from the main panel where all the "electricity" is distributed in the house we have this (a simple schematic and just a small part of it denoting a light switch circuit only):

  So how can one replace the above mechanical switch with an electronic one (with touch sensors, radio, bla, bla ...) and keep the existing wiring/infrastructure? Well some of you might say it's impossible - how to power up an electronic device using only the live wire and close the circuit via the light bulb back to neutral.
  A team of smart guys from China (but not the only ones around here) created an electronic light switch named Livolo which can just do that - act like a replacement of a mechanical switch and that remains powered in both states (no matter if the light is OFF or ON).

  How does it work?

After some while of searching and doing research I found out that the magic works based on some preconditions:
  1. Having two power supplies - yes two are needed: one for the OFF state (stand-by) and one for the ON state (active)
  2. The electronic switch must draw very little current in the OFF state and this is achievable based on the fact that every light bulb has some leakage current due to the electronics inside it (CFL and or LED bulbs) and for the classic ones (incandescent) - the electrical resistance of the filament. This precondition is required so that the light bulb still stays off and for the electronic switch to steal some of that "current" (more appropriate is to steal some energy).
  3. In the ON state - this is the most happiest case because when the series circuit is closed there's enough current to make all work but there must be a low and sufficient voltage drop across the electronic switch so that it has again enough power to work
So to summarize the above there are two different conditions in which the internal power supplies from the electronic switch must work:
  1. High voltage drop and low  current in the OFF (stand-by) state
  2. Low voltage drop and high current in the ON state
  First condition is achieved via an internal switching power supply called a ringing choke converter or self oscillating flyback converter - this is so popular due to the reduced component count which is a must in this case (well the efficiency is not so great but it's enough for this project to work). Oh and please watch out - this kind of power supplies (as all dc-dc or ac-dc converters which work in "switching" mode) are noisy in general so extra care should be taken to not mess up other circuits around ...

  Now the second power supply that works when the switch is in the ON state - well this one is still a bit of mystery to me but the general approach that I see here is to have some kind of an element wired in series with the relay contacts which drops some voltage over it (so basically it steals some voltage from the light bulb but not too much so that it doesn't malfunction). Next we can pick that voltage and feed it via a dc-dc converter or a voltage stabilizer circuit in order to have a predictable DC voltage output.

  Luckily for us the Chinese engineers took care for us about all the intricate details of this special supply and provided a 3V output which we can use to power up our custom BLE enabled board.

  Why BLE? The main reason is that we need a board which draws very little current - as low as a few milliamps and even lower if possible. This is because of the switch powering limitations that I talked above. If we can achieve this state then we're on the safe side and the switch will work stable for a long period of time and without surprises.

  Let's talk about the Livolo switch construction details a little bit. It has two boards inside it:
  1. One for the power supplies and relays
  2. One for the touch sensing part and LED signaling
 There's a variant which has a radio receiver in it also - which is more expensive btw but it doesn't provide feedback because it only has a radio receiver and not a transceiver  so not good for us.

  What we can do about it? Well we replicate the front board with touch sensing a custom radio solution - BLE in our case and make it work and behave according to our needs. This is all this project is about after all right?

Now we have so many options out there in choosing a radio solution and a MCU (microcontroller) but not all of them are good for our project here. Why? Because:
  1. We need a MCU combined with a radio transceiver that draws very little current (ideally a few milliamps on average and even lower)
  2. We need good software support and open source is a must
  3. We need a low component count in order to reduce complexity, cost, maintenance, space constraints ...
  What I tried and experimented so far:
  1. A custom solution made by me using Arduino boards and custom software/radio protocol over radio transceivers low level libraries(I tried RFM12, NRF24L01, RFM69, and many others) - I gave up because it needed too much work for ONE man and it wasn't very reliable(the software part and my custom protocol - which worked to some degree so hey it wasn't too bad in  the end because I learned some more stuff too)
  2. Next there was ( and still is actually) the great MySensors project - this one worked very great for me to some degree but when it was "applied" against Livolo switches and other actuators where high reliability is desired - well not doing so well in my case and as the name says MySensors this is very good for sensors and battery powered devices - I still have MySensors nodes but mainly battery powered ones. So don't get me wrong here it's a great project and it will be much more great as development advances but for now and for this situations wasn't very fit - at least for me.
  3. And the final step and last thing that I tried - BLE. In the end it turned out to be the most reliable way of making this switches work as expected and not crash so yeeyy! For this one I used the almighty NRF51822 RF SOC chip from Nordic Semiconductor - thank you Nordic Semiconductor for creating this - it's really a great chip. More than that it's supported by ARM GCC toolchain and by the Arduino community wich is a big plus - oh my - dreams start to come true. There are other BLE solutions out there in the wild of course but not that popular and not so open source and not very great support for hobbyists like me.
  In the next post I will start to explain and provide step/pictures of the custom boards and the rest of the parts needed for this project to work. So stay tuned ...

Tuesday, May 17, 2016

The story of a home automation system

This is a series of posts regarding my progress and "story" of a home automation system - pitfalls, what I learned, what I created as seen in the next to come pages.