xoseperez / espurna

Home automation firmware for ESP8266-based devices
http://tinkerman.cat
GNU General Public License v3.0
3k stars 638 forks source link

Sonoff Dual Button0/1 behavior #57

Closed xoseperez closed 6 years ago

xoseperez commented 7 years ago

Originally reported by: Gabriel Ricardo Hahmann (Bitbucket: grhahmann, GitHub: Unknown)


I have been using sonoff devices to automate my home, one of the main benefits I saw was to be able to keep using the standard wall switches so I just put the sonoffs in the background.

With the standard sonoff and Sonoff-Tasmota firmware, I was able to do that, his Dual firmware is not able to use the button header so I switched to yours but I just realized the behavior is not what I expected.

I'm using standard wall switches, not push button so when I change the wall switch to the "on" position it will take button 0 to GND, what I see with espurna is that it will only change the relay if I switch on/off the switch like if it was a punch button.

Is it possible to change that behavior, so that the switch will always toggle the curb switch state? I was not able to find any configuration for that.

By the way, thanks for the amazing project, I really liked the way you have implemented it in a modular way, so you can choose which firmware you want to deploy and get some additional room for more code to that specific device.

Regards, Gabriel.

xoseperez commented 7 years ago

Original comment by Fabricio N. Costa (Bitbucket: fabricioncosta, GitHub: fabricioncosta):


How about try to reprogramming the C8051F330 ? Do you think that this is possible? I think the pins VCC C2K C2D GNC is aim to this.

xoseperez commented 7 years ago

Original comment by Fabricio N. Costa (Bitbucket: fabricioncosta, GitHub: fabricioncosta):


I mounted the suggested circuit. It works when I move the switch gently. In normal use, it don't change the relay state.

xoseperez commented 7 years ago

Original comment by Fabricio N. Costa (Bitbucket: fabricioncosta, GitHub: fabricioncosta):


Yes. I do not have the transistor on hand, so I was wondering if there was a solution that used only a resistor. I'll buy the transistors :). Thank you!

xoseperez commented 7 years ago

We are still talking about the dual here, right? The dual has two more buttons in a header in the board so yes, using the same circuit I posted above you can drive the two relays from that header like wall switches.

xoseperez commented 7 years ago

Original comment by Fabricio N. Costa (Bitbucket: fabricioncosta, GitHub: fabricioncosta):


Do you think it is possible to solder a wire in an unused input of the controller and put a pull up resistor to use it as an external wall switch?

xoseperez commented 7 years ago

This looks like a debouncing circuit with a Schmitt Trigger to clean the signal (and negate it). It will certainly toggle the switch when closed but will do nothing when you open it again. Another option (not ideal) is to click-and-unclick the switches every time. A bit weird but it will work using my circuit above. Actually when testing with the switch you can see in the pictures I noticed it's very sensitive: I did not even had to fully change the switch position, a slight pressure was enough to toggle the relay.

xoseperez commented 7 years ago

Original comment by Gabriel Ricardo Hahmann (Bitbucket: grhahmann, GitHub: Unknown):


@xoseperez That`s great! Thank you for taking your time to do it! Unfortunately all my house switches are SPST, I have a few SPDT where I need multiple switches to control the same light bulb, but that is not the case. The alternative I see is to replace those 2 SPST switches to SPDT, not a big deal, just wondering if the following circuit will work as it uses a SPST or the case just applies to PB:

SwitchDebounce2a.jpg

Do you think I can replace the 74HC14 by 1K resistor and the NPN? Also checked and I have 2N2222 and BC547, checked online and understand they can replace the 2N3904 for this scenario, right?

If you dont see any other alternatives, Ill stick to your advice and replace the SPST by a SPDT and you can close this ticket! Thank you so much!

xoseperez commented 7 years ago

Oops, sorry, my bad. I did not realise you were talking about the Dual (even thou it's in the title itself...).

The Dual has an additional microcontroller that handles the buttons and the relays. The ESP8266 gets notifications from that microcontroller via serial. Problem is that it only notifies of button release events (rising edges). That means you can't have long click events for instance (since you don't know when the user pressed the button, only when she released it). But it also means it doesn't work for toggle switches (wall switches).

Well, it does not work easy, but you can make it work. The trick is to add a little additional circuitry to force the wall switch behave like a pushbutton. Wall switches are single-pole-double-throw (SPDT) devices with 2 possible states, right? Almost, there is a third state when the pole is moving from one output to the other. Its floating. If we connect the pole to ground and the two outputs together to a NPN with a pull-up the wall switch will normally tie the NPN gate to ground and prevent current from flowing across it, except when the pole is moving, during that few milliseconds the output is pulled up by the resistor and the NPN allows flow. You have a push button!

Here is a schematic of the solution. I don't know if you can do it. I just did here and it works a treat with a Dual. See pics.

WallSwitch2Pulse.png

IMG_20170206_214354x.jpg

IMG_20170206_214431x.jpg

xoseperez commented 7 years ago

Original comment by Gabriel Ricardo Hahmann (Bitbucket: grhahmann, GitHub: Unknown):


I've changed the hardware.h as you said, after updating to 1.6.0, mine looks like this know:

#!arduino

#elif defined(SONOFF_DUAL)

    #define MANUFACTURER        "ITEAD"
    #define DEVICE              "SONOFF_DUAL"
    #define BUTTON1_PIN         0
    #define BUTTON1_MODE        BUTTON_SWITCH
    #define BUTTON2_MODE        BUTTON_SWITCH
    #define LED1_PIN            13
    #define LED1_PIN_INVERSE    1
    #undef SERIAL_BAUDRATE
    #define SERIAL_BAUDRATE     19230

Compiled it and updated OTA but it didn't worked. Any other change I have to do? Just to make sure we are on the same page, I'm trying to use the GND/BUTTON0/BUTTON1/GND header.

By the way the OTA update is amazing! Thanks!

xoseperez commented 7 years ago

Hi

Great timing!

Just released version 1.6.0 that supports toggle switches like the one you describe. You just have to define the button mode in the hardware.h file like this:

    #define BUTTON1_MODE        BUTTON_SWITCH