ESP8266 / Sonoff Controlled Ceiling Lights

CAUTION: I am not an electrician, the information below should not be considered advise. Please consult a qualified electrician before attempting to replicate any of the below.


I just wanted to share a project I have been working on for the main lighting in our house (i.e. the stuff that’s in the ceiling).

If anything this will be a good record for me if I need to add to it in the future :smile:.

Ambition

I was looking for a (cheap) system that would allow my ceiling lights to be controlled via Home Assistant but also (and crucially) via the normal switch. As I’m sure we’ve all found out, launching an app to turn a light on is not convenient :stuck_out_tongue:.

I also didn’t want to go down the route of “Smart Bulbs” as many of our fittings take pretty specialist bulbs or there are just so many of them it would be too expensive (ceiling spots). Smart Bulbs also tend to require the switch to be in the ‘on’ position to allow for wireless control.

Solution

My solution was to use an Wemos D1 Mini / NodeMCU (both ESP8266 based) micro controller controlled (from Home Assistant) by MQTT.

The basic premise is this. A normal wiring circuit for a ceiling light (in the UK at least) has a live feed running through the switch. With a bit of swapping around we can set it up so that:

  • The micro controller switches the live feed for the light(s) using a relay.
  • The switch(es) in the wall simply connect to the micro controller. The only voltage over the switch cables is 3.3v DC.

It may differ depending on how the house is wired but in our case the installation was very simple, no new cables needed to be run and everything is easily undone if we move. All that was needed was to cut the live feed to and from the switch, put the relay on the live side and plugged the switch wires (now not connected to AC at all) into the mircro controller.

Here’s a simple diagram showing how everything is wired:
Switched Relay Wiring (1)

Note: If wired correctly this even works where there are 2 switches controlling the same light

Micro Controller Software

Using some great libraries I cobbled together some code to run on the micro controllers. You can find a copy on my GitHub page.

Note: By no means am I a developer, I’m very happy with the result but it was very much trial and error and I’m sure there are things that could be improved.

Main Features:

  • Works well with Home Assistant.
  • Basic control using simple MQTT commands (e.g. “1” to turn on, “0” to turn off).
  • Trigger a Home Assistant automation / script with a double toggle of the switch. Some examples of how I use this:
    • Double click the kitchen lights to toggle the under cupboard kitchen lights.
    • Double click the landing switch to toggle all of the downstairs lights (good for when going to bed).
  • Restart the ESP with 6 toggles of the switch. A useful backup but not something I’'ve ever had to use.
  • Plug in as many switches and relays as your micro controller will support.
  • Utilizes a non-blocking reconnect function allowing switch control to continue if MQTT or WiFi connection is lost. Good for girlfriend acceptance.
  • Automatic Recovery and Offline Mode (see below). Again good for girlfriend acceptance.
  • Retained Statuses - The state is published as a retained message allowing your hub (e.g. Home Assistant) to grab the current state if it is restarted. Also used for recovery if the micro controller is restarted.
  • Smart Debounce - Should the ESP detect a switch toggle it will monitor the switch for a further 50ms to ensure it was genuine.

The real winning feature for me is the Automatic Recovery and Offline Mode. I have been running 3 sets of lights with this code for over 6 months now and never had an issue. They have recovered from router restarts, network outages, power cuts etc… When designing this there were a couple of scenarios that needed to be worked around:

Short-Term Issues - The ESP8266 can be tricky when trying to detect WiFi disconnections. In a lot of cases my device believed it was connected to WiFi and so was trying to connect to the MQTT broker, in fact it had lost it’s WiFi conection and so was stuck.

Long-Term Issues - We also want to be able to carry on using our lights without any WiFi / MQTT connection. I’m not sure my girlfriend would be happy if a router / Raspberry Pi issue meant no lights :slight_smile:

Useful for both scenarios:

  • The switch control continues to operate whilst the ESP is trying to reconnect to either the WiFi or MQTT broker.
  • When the ESP is first started it grabs the retained state message for each relay and uses it to recover it’s last state.

Useful for short-term issues:

  • A restart of the ESP is triggered if 2 minutes has elapsed without a connection (see below). This solves most short-term issues.

Useful for long-term issues:

  • The 2 minute restart is not triggered if no connection has been made since the ESP started. In this scenario the ESP sits in an Offline Mode until a connection is made. This avoids the lights turning off every 2 minutes during a long-term issue.

There are further details on how to deploy the code and Home Assistant YAML on my GitHub page. If anyone has any questions I’d be happy to try and answer them.

Hopefully this is useful for someone.

Edit

09/09/2017 - There is now a new fork of the above repo to be used on a Sonoff device. Further details in the repos README.md.

16 Likes

In my ESP8266/MQTT projects, the if (!client.connected()) { reconnect(); } in the loop works just fine and I have no disconnection issues whatsoever. It’s several months that I’m running my units with no problems.

On the other hand, while I’d very much like to build similar, dual-activated units (mqtt + local switch), I can’t think of a way to fit the esp board and relay and the switch in the limited space in the hole-in-the-wall (don’t know the name for that, would it be called a junction box?)

I echo lexam79’s question - where do you put the esp8266? And, how are you powering it? Your circuit diagram doesn’t show the VCC connection for the esp8266.

@marthocoo +1 for power supply !! I already tried to install Fibaro relay or dimmer that goes behind existing wall switches (it’s more than tricky to fit everything in the box and all the more be able to close it back without any wires going away…

Why not a Sonoff or Broadlink for 5-8 dollars?

Good solution for UK where we don’t have neutral!

How are you powering the Wemos?

By the way, I like the “smart debounce”, might have to steal that for my doorbell that I’ve had to disconnect because for false triggers :slight_smile:

1 Like

Fun and sense of achievement :slight_smile:

1 Like

Hopefully I can answer some of your questions:

First of all it’s worth saying that I have the massive benefit of my partner being an electrician. Basically I built the micro controller part and said “fit this please” :smile:. I’ll try and do my best to explain what she has explained to me in terms of the wiring side of things.

Fitting it all in…

When I first tried to come up with a solution to this my aim was to fit it all behind the light switches. Space was indeed an issue but before I even tried to tackle that I found out that the lights in our house are “3 plated in the ceiling”. This means that essentially only the live part of the AC circuit gets to the switch, there’s no neutral. The result of this is that as well as the space issue I couldn’t easily get AC power to the micro controller behind the switch either.

The plus side of this however is that I can put all of the controller stuff in the ceiling / loft (attic) space. This is put in at the point the live cable gets run down to the switch.

  • This is how our lights were wired even before we made any changes.
  • The round circle is a joint box in the ceiling / loft (attic).
  • The dashed green line is where we essentially cut the wires and sat the controller in-between. Also in the ceiling / loft (attic).

Getting power to the micro controller…

Oh I forgot about this bit! For this we “simply” wired an new plug socket in the loft / ceiling (attic). I say simply, it was simple for me as my partner did it but maybe not so simple for others. I could have used an AC to 5v DC converter but we already had the bits to just put a plug in.

Why not a Sonoff or Broadlink for 5-8 dollars?

This was about the same cost and the custom software gave me extra flexibility such as allowing the switch to be used as normal (I don’t think Sonoff type devices can do this but I might be wrong) and the double click functionality is great to duel purpose light switches.

Note: I know you can probably flash your own software onto Sonoff devices but as I say, this was just as cheap.

3 Likes

Really like this, i to am an uk electrian and have been looking in to similar projects was going to replace sx’s with retractable sx’s for signal inputs. Hadn’t thought about the ability of still being able to control lights when the wifi goes down :slight_smile: . Dont surpose you have found a solution for being able to dim lights (intill someone make a smart g9 lamp) need to find enougher solution. Only idea i’ve had is esp controling latching relay wired as 2way sx to standed wall dimmer. Not perfect by any means.

I did have a look at this but didn’t get far unfortunately. From what I found it adds a whole load of complication and so I left it :smile:.

Do they have Sonoff/Broadlink switches for that cheap that let you use the switch like normal? I would be interested!

@Dullage very interesting, wish I had thought about this before I finished boarding my attic.

You mention adding a new socket to power the ESP8266, was this added to your light ring or on the socket ring/spur.

If the socket is purely used for the ESP8266 the I would presume it would be fine on a light ring, but I’m no electrician which is probably obvious.

Yes, with custom firmware:

2 Likes

In the loft we used the socket ring as it was convenient but in the ceiling we used the light ring. It’s very low current so should be no problem and hey, it’s our house :smile:.

Be safe.

I might give a Sonoff a go for the next one. It’s probably only slightly cheaper but it does has the benefit of having it’s own case. Good shout hijinx.

This said, I would think in cases where you can plug more than one relay into a single micro controller then it’s probably cheaper to buy the separate bits.

1 Like

Completely agree re the flexability of a custom solution - I didn’t want to take away from your nice project in any way!
Buying a sonoff is a kind of short cut for people who lack either the knowledge or time or both to build from scratch.
Building out a microcontroller project is super fun, but its an investment in time and tools etc.
A sonoff allows you to get straight into the software aspect (assuming you want to flash a custom f/w) with only a minor bit of modification (hooking up serial link).
Also that built in PSU is a winner :slight_smile:

2 Likes

Oh I hadn’t considered that it has it’s own PSU, that is a massive bonus and does bring the cost down even more!

I’m going to order one and give it a go.

Sonoff also do multichannel 2 and 4 channels.

Here is their PCB:

BTW I dont have one yet… I haven’t had the time available to dedicate to play it with it.
Now very tempted though :slight_smile:

1 Like

Oh wait, I forgot, in order to continue using the wall switch as a switch my software needs to monitor it on a GPIO pin. :disappointed:

Do you happen to know if the Sonoff exposes any of the ESPs other GPIOs for custom use?

Thats what it mentioned in the youtube I linked:

it is possible to connect a physical switch between GPIO14 and GND on the Sonoff board. This allows the device to connect to UK lightswitches that do not have a neutral connection.