ESP8266 / Sonoff Controlled Ceiling Lights

ok, this is all making sense. I had gotten a bit confused by the Sonoff switches and MQTT thinking I could only really trigger off and on.

What I plan to do is, in the case of the main bathroom lights, instead of the switch controlling the light and then updating HA, I want it to tell HA that the switch has been switched and then HA can make a decision on what to do. In my case, if either of the lights are on, turn them all off, if both are off, turn them all on.

Perfect, just what I want to do. I can trigger it to make HA run an automation instead then all is good as I know how to make that run for each situation (light already on or off to know what to change it to).

I wonā€™t try to add motion to this situation though as our kitchen is centrally located and we end up walking through it far too often when I would not want lights going on per se.

Thanks for your thoughts on this

Exactly what Iā€™m trying to do. Now I just need to figure out how I will power the ESP button. Battery perhaps as if it is only used a couple of times a day it should last quite some time before needing a re-charge

ESPEasy maintains a constant wifi connection I think, so youā€™d definitely need to go with custom firmware option.
Also accessing a battery behind switch plate is awkward.
The biq question is whether you have live and neutral connections at the switch.
If you have L+N at the switch then :tada: you can power your switch from mains - for example I use a sonoff basic as you get esp8266 + PSU and case for ~Ā£5
If no L+N then you need to search out other solutions like @Dullage project in this topic, where heā€™s placing the ESP8266 at the light fitting where there is always neutral (uk wiring). In this case maybe a battery will work, but by not having a persistent connection to wifi your switch will likely have latency as it needs to connect to wifi then publish mqtt topic.

I do this with LIFX bulbs and Flic Bluetooth switches.

The good thing about the Flics is they have a single
click, double click and hold action so you can setup three different light levels or scenes.

I have the babyā€™s room light setup to be 5% on a single click, 20% on a double click and 100% on a hold. Single clicking when the light is on turns it off.

I 3D printed a mock face place that sits over the top of the wall plate so the bulb is always on.

Thanks madpilot.

With the Flic though, you can only setup those 3 options correct? Or do you do that through the Lifx bulb?

The flic offers the three click types - and HA makes the decision on what to do with those click types.

Though: Full disclosure, Iā€™m actually using node-red to do the logic - but you could do the same thing using HAā€™s automations rules.

Another option similar to this would be the Xiaomi Switches. Theyā€™re mega cheap and integrate with HA nicely.

You do need a Xiaomi gateway but they also fairly cheap and getting one means you can use all the Xiaomi stuff.

Hi,

My sonoffs have just arrived. I was planning to update them OTA using this method in the first instance:

Iā€™m very new to Arduino IDE, som startuing from basics. Can I replace the .bin file that updates the firmware with the tasmota .bin file with a bin of this firmware? I very much appreciate Dullage linked out to google for the flashing instructions, but the ability to push a bin file OTA seems easier than soldering/ connecting to the Sonoff pins.

Really appreciate any help!

Thanks

Trux

To update firmware via OTA, you can just follow instructions on README from github. Be sure to check Wiki also for the supported firmware versions before starting the update process.

Hi Dullage,

Fantastic project and I have programmed it to my Wemos D1 on a breadboard!

I have a slightly different spin on the project, I am using DC under lighting in my kitchen and 98% of this is perfect for my use, however I would like to swap the latching switch to a momentary switch, I was hoping it would be an easy swap in the code, could you help me out?

Thanks,
Gavin

1 Like

For anyone looking to use a Sonoff for this, they do fit in a normal receptical box if you remove the plastic casing. I myself am using the Tasmota firmware which gives access to all the GPIOā€™s on the sonoff. I have a push button wired, as well as an LED that illuminates when the switch is OFF to be found in the dark.

Iā€™ve designed and printed decora-style switches that fit nicely behind a switch plate. I just recently re-designed the push button yesterday for better illumination, I will be posting this later on.

Here is my project: https://www.thingiverse.com/thing:2830060

1 Like

Hi Gavster, the part that handles the switch checking is in the file SwitchedRelayClass.cpp. The function is called switchedRelay::switchCheck() and starts on line 56.

You may be able to get away with simply deleting everything after line 56 and replacing it with:

void switchedRelay::switchCheck() {
	// Lap the timer
	switchIntervalFinish = millis();

	// Calculate the elapsed time, set to 0 if it's the first press
	if (switchCount == 0) {
		switchIntervalElapsed = 0;
	}
	else {
		switchIntervalElapsed = switchIntervalFinish - switchIntervalStart;
	}

	// Check for a change in state, if changed loop for the deboucneDelay to check it's real
	switchState = digitalRead(switchPin);
	if (switchState != prevSwitchState) {
		unsigned long loopStart = millis();
		int supportive = 0;
		int unsupportive = 0;

		// Loop
		while ((millis() - loopStart) < debounceDelay) {
			switchState = digitalRead(switchPin);
			if (switchState != prevSwitchState) {
				supportive++;
			}
			else {
				unsupportive++;
			}
		}

		// Calculate the findings and report
		if (supportive > unsupportive) {
			switchIntervalStart = millis();
			lastStateChange = millis();
			switchCount += 1;
			prevSwitchState = switchState;
		}
	}

	// If the elapsed time is greater that the timeout and the count is higher than 0, do stuff and reset the count
	if (switchIntervalElapsed > switchIntervalTimeout && switchCount > 0) {
		// If the count is greater than 2
		if (switchCount > 2) {
			if (relayState == 0) {
				switchDevice(1);
			}
			else {
				switchDevice(0);
			}

		// Reset the count
		switchCount = 0;
	}
}

I havenā€™t tested this and thereā€™s lots of redundant code left in there but it might get you in the right direction. This also removes any alternative uses for the switch e.g. the double toggle special function or toggle 6 times to reset.

Also ensure you set the switchIntervalTimeout_ in step 1 to 0.

I hope this helps.

1 Like

Thank you Dullage!

I am no expert at writing code and I figured it would be in the SwitchedRelayClass.cpp but I just couldnā€™t work out what changed the states on the switch!

This will be a great help and I will report back if it works, I ended up using the original code and swapping to toggle switches for my current project but it would be neat to use the push switches!

The restart code shouldnā€™t be as necessary as where I am using these they are on DC power supplies with easy access to power down/restart.

I am just waiting on another 5 Wemos to arrive once they do I will report back.

1 Like

A massive thank you! The code works!

OK so I need to tweak it slightly I think as it is a little slow to react, I assume this is a debounce issue so will have another look over the code, the main thing is that is works and works well!

1 Like

@Dullage
Hi Mate,

I now have some Sonoffs and I am struggling a little, When I lose power they donā€™t seem to save the AP details so I lose internet connection, the weird part is that I have 1 unit that has saved the details, I am sure it has something to do with SPIFFS but I donā€™t know enough about them to debug!

I have run the SPIFFS.format(); code included in the sketch but that still doesnā€™t work!

The odd thing is that it does save my MQTT details? Could it be something to do with WiFiManager?

Any help appreciated

Iā€™ve had issues like this before. Itā€™s worth checking your board settings. If youā€™re using the Arduino IDE these are the settings Iā€™ve found work best for me:

image

If you can plug the Sonoff into a Linux machine thereā€™s also a handy tool to help erase the flash:

sudo apt install esptool

Then you can run something like:

sudo python3 /home/pi/.local/lib/python3.5/site-packages/esptool.py -p /dev/ttyS7 erase_flash

Note: You will need to replace ttyS7 with the correct device number! You may also need to change the path to the esptool.py script.

Thanks again @Dullage,

I have played a bit more, I had a eureka moment, originally I had updated Tasmota on to the working Sonoff, I thought it would be worth a go at trying to upload Tasmota to one of the boards that wasnā€™t working.

The first board I tried wouldnā€™t connect on Tasmota or on your firmware, the second and third board however have now worked after using Tasmota first then flashing to Dullage firmware!

No idea if it is a bug?

I have 2 more boards to try, I will try flashing Dullage first and if it doesnā€™t work I will go with the Tasmota then Dullage firmware.

Both using Arduino IDE to upload

Hi Dullage, love the code but this is also wrecking my head. This WiFi problem happens with me also and I have tried these settings you recommend but no luck.

I had a look at the code, now Iā€™m no coder but could it be that the SSID and password isnā€™t saved in the saveConfig class but is stored in ram so canā€™t be recalled after a powercut.

Iā€™ve switched to your switched relay sketch for my sonoffs works perfectly and thanks for sharing your good work.

Hi Conor, Iā€™ve recently had a lot of wifi issues with the SwitchedSonoff firmware and the issues seem to stem from the use of WiFiManager (or at least my implementation of it).

I have since re-worked the code to replace WiFiManager with the standard ESP8266WiFi. This removes the ability to remotely configure / re-configure the devices but has been solidly running my lights for a while now. In fact itā€™s very similar to the SwitchedRelay code only simplified as there can only be one relay with a Sonoff.

Iā€™ll upload it to GitHub and let you know when itā€™s up.

Ok, the simplified code is up now: https://github.com/Dullage/SwitchedSonoffSimple

@Gavster29 - If youā€™re still having issues it might be worth you taking a look at this as well.

I hope this helps.

3 Likes