Sonoff-HomeAssistant (Alternative firmware for Sonoff Switches for use with mqtt/HA)

It must be that i did something wrong. Everything is conected like you say. In my code, in loop() I call void checkWallSwitch:

checkWallSwitch() {
wallSwitch = digitalRead(WALLSWITCH);
if (wallSwitch != lastWallSwitch) {
digitalWrite(LED, !digitalRead(LED));
digitalWrite(RELAY, !digitalRead(RELAY));
sendStatus = true;
}
}

But nothing is happened.

This will work

checkWallSwitch() {
wallSwitch = digitalRead(WALLSWITCH);
if (wallSwitch != lastWallSwitch) {
digitalWrite(LED, !digitalRead(LED));
digitalWrite(RELAY, !digitalRead(RELAY));
sendStatus = true;
}
lastWallSwitch=wallSwitch;
}

Try wiring it up like this but connect to +3v not +5v. I am using this on 20 or sonoffs. The 10K resistor is a good value to use

3 Likes

YES! This is it. But important is to set GPIO14 to OUTPUT mode : pinMode(WALLSWITCH, OUTPUT);

It should be input not output.

You should probably have a read of this and understand it. Also if you are going to use my code in the manner that you have you need to read the license information and comply with it. You are better off forking the repo and making your modifications. Thanks

https://www.arduino.cc/en/Tutorial/DigitalPins

Thank you for informations. After reading you link I understand that my solution is not good enough. I’m new in these.

Why is it OTA version the led keep on even the relay is off?OTA FAIL not sure why.please advise,thank you!

Dude you need to supply a little more info. Which model of Sonoff. As for OTA there are a number of reasons it may not work. What have you tried, what is your environment, what is actually happening. That a very simplistic question for a complex answer I’m sorry

Sorry,mine is the normal version I try both your firmware sonoff 1.01p OTA, but once uploaded and connected the led light doesn’t off even the relay is off. And I try to upload by OTA say error while opening python I had install python already.

Mine is the normal version

Have you ever used OTA for an ESP module successfully from the Arduino IDE before?

If this is your first time using OTA, suggest you attempt to prove OTA works before looking at the sonoff for the problem.

Yup is my first time,not really sure how it works for OTA. But now main question other thenOTA is why the led keep light on ?

Hello.
Does anyone here have a sonoffon RF version?
If so how did I get the firmware?

I’m having trouble, I connected the wires all well, but in Arduino this error appears:

Warning: espcomm_sync failed
Error: espcomm_open failed
Error: espcomm_upload_mem failed
Error: espcomm_upload_mem failed

@KmanOz have already made my donation to you as promised. sorry late

Manage to get the OTA works!(very nice!!) but how can the led light work as your 1st version when relay is on led light on when off led also off? After update OTA led light up even relay is off

I’m running into a problem with this. I have the basic Sonoff with your firmware installed. When it’s still powered by the FTDI adapter, it connects to my wifi and mqtt just fine, and I can control it from Home Assistant perfectly. However, as soon as I power it from mains AC, it connects to wifi but not my mosquitto broker. If I try pinging it i get a lot pockets dropped and much higher than normal ping times. The button does work to control the relay on AC power.

Any idea what would cause this?

Check that your mqtt user in the sketch is unique.

You can’t have the same user twice in mosquito.

If you have done a test and then a live version, you may have used the same mosquito user twice.

There is no difference. I even re downloaded a Sonoff I have here with the exact code from Github and it works normally. Sorry not sure what your problem is.

Are you sure you are holding the button down on the Sonoff before you plug in your FTDI? You can release after power is applied.

Is the switch physically located in he same spot on both occasions?

Yes, I’ve determined this is not a problem with your firmware, it seems to be a bad batch of Sonoffs. I’ve tried several other firmwares with the same result. They function perfectly on FTDI power and then function like shit on AC power. Seems like the power supply on mine are bad. Pinging to FTDI power is zero packet loss and about 2 ms. Pinging on AC power is 40-80% packet loss and 60-120 ms. Also constant reboots. Lame.

Can you take a photo of both sides of the main board and post it up here. I wanna compare any obvious component changes to working boards. Seems the power supply isn’t filtering AC properly. It will be a bad capacitor somewhere I recon unless they’ve made changes to the board that is. That’s no good. If we spot something I can let ITEAD know as well. Sorry to hear dude.

Here’s what these problem boards look like: