Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch logic gets inverted #552

Closed
Tardymo opened this issue Feb 11, 2018 · 7 comments
Closed

Switch logic gets inverted #552

Tardymo opened this issue Feb 11, 2018 · 7 comments
Labels
button enhancement New feature or request question

Comments

@Tardymo
Copy link
Contributor

Tardymo commented Feb 11, 2018

If there is a switch tied to relay and relay toggling occurs (button press, MQTT, web e.t.c) while switch is "on", that switch gets inverted so that turning it "off" actually switched relay "on". Is there an easy way to fix this?
There are two different ways it could work and I'll give an example using SONOFF_RF
Turn relay on using swith connected to GPIO14 and then toggle relay using push button or RF remote. At this point relay is off and turning swith off will actually turn relay on. But instead more logical way would be to leave relay off when switch is turned back off. Or second option would be to not allow turning relay off while switch is turned "on".
In other words - I want switches to switch relay on or off not toggle it on each switching edge.

@xoseperez
Copy link
Owner

You can configure your switch as a pushbutton with explicit action for press and release. You will also want to disable any secondary options on this button to avoid unwanted side-effects (ap mode, factory reset). This could be a starting point:

    #define BUTTON1_MODE            BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH | BUTTON_SET_PULLUP
    #define BUTTON1_PRESS           BUTTON_MODE_ON
    #define BUTTON1_CLICK           BUTTON_MODE_OFF
    #define BUTTON1_DBLCLICK        BUTTON_MODE_NONE
    #define BUTTON1_LNGCLICK        BUTTON_MODE_NONE
    #define BUTTON1_LNGLNGCLICK     BUTTON_MODE_NONE

Change BUTTON_MODE flags to match your hardware (maybe it's default low or it already has a pullup).

@Tardymo
Copy link
Contributor Author

Tardymo commented Feb 17, 2018

Actually I need both buttons to act independently of one another and swith the same single relay. So one button should be a toggle button and the other should be an on/off switch. If at least one of the buttons in in "on" position, relay should be switched on. For example if first button is clicked, relay switches on and no matter what I do with the second button, relay stays on until button is clicked again. And vice versa - if second button (switch) is pressed - relay stays on until button is released. Toggling the first button should not swith the relay off. However ralay should stay on even if second button is released, if first button was clicked while the second button was pressed.
Think of both buttons as being connected to OR gate output of which controls the relay.

As far as I know, current firmware will not allow such configuration. But I know there were plans to make buttons and relays independently configurable, so if two buttons are controlling one relay, nice feature to have would be to define logical function between buttons (AND, OR, XOR).

@xoseperez xoseperez added the enhancement New feature or request label Feb 18, 2018
@Tardymo
Copy link
Contributor Author

Tardymo commented Mar 4, 2018

I patched the code to make it work the way I need until such functionality is available in stock espurna firmware, but there is one issue. Here's what I did:

// Buttons
#define BUTTON1_PIN             0
#define BUTTON1_MODE            BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH
#define BUTTON1_RELAY           2
#define BUTTON2_PIN             14
#define BUTTON2_MODE            BUTTON_PUSHBUTTON | BUTTON_SET_PULLUP | BUTTON_DEFAULT_HIGH
#define BUTTON2_PRESS           BUTTON_MODE_ON
#define BUTTON2_CLICK           BUTTON_MODE_OFF
#define BUTTON2_DBLCLICK        BUTTON_MODE_NONE
#define BUTTON2_LNGCLICK        BUTTON_MODE_NONE
#define BUTTON2_LNGLNGCLICK     BUTTON_MODE_NONE
#define BUTTON2_RELAY           3

// Relays
#define RELAY1_PIN          12
#define RELAY1_TYPE         RELAY_TYPE_NORMAL
#define RELAY2_PIN          100
#define RELAY2_TYPE         RELAY_TYPE_NORMAL
#define RELAY3_PIN          100
#define RELAY3_TYPE         RELAY_TYPE_NORMAL

And then in relay.ino at the end of relayLoop function I added this line:

_relays[0].target_status = _relays[1].current_status || _relays[2].current_status;

As you can see, I created two dummy relays which are assigned to each of the buttons and then actual relay is controlled by OR-ing two dummy relays.
The problem is I need the second dummy relay to switch off when button 2 is released, but that does not happen. In debug output I see this:

[020199] [BUTTON] Button #1 event 1
[020201] [RELAY] #2 scheduled ON in 0 ms
[020214] [RELAY] #2 set to ON
[020226] [RELAY] #0 set to ON
[021227] [RELAY] Saving mask: 5
[021408] [BUTTON] Button #1 event 4

BUTTON_MODE_OFF does not work for some reason after click event
I can make it work by defining button 2 as BUTTON_SWITCH, but if for some reason dummy relay 2 gets toggled by MQTT or REST, button functionality will be inverted and I must prevent that from happening.

@xoseperez
Copy link
Owner

I think part of the problem is the naming. BUTTON_PUSHBUTTON is a momentary push button: you press it, you might hold it down and you released and it goes back to the original state. A BUTTON_SWITCH is a wall switch, ever time you click it (or slide it, or toggle it) it toggles between teo stable states.

Now, there is a tricky thing in ESPurna with indexes. All lists (arrays) are 0-based, so the "Button #1 event 1" log line above is actually BUTTON2_PRESS and the "Button #1 event 4" is BUTTON2_LNGCLICK, that you have defined as "do nothing".

@Tardymo
Copy link
Contributor Author

Tardymo commented Mar 5, 2018

Yes, PRESS event is fine, but I am not getting CLICK event after button is released. Instead there is "event 4". I am releasimg the button quickly so LNGCLICK should not have been triggered. Are LNGCLICK and LNGLNGCLICK triggered on button release? In that case these events must be set to BUTTON_MODE_OFF in my case, right?

@xoseperez
Copy link
Owner

LNGCLICK is triggered when the button has been held down for at least 2 seconds, LNGLNGCLICK is 10 seconds. Maybe the button logic is inverted?

@xoseperez
Copy link
Owner

Closing since there has been no feedback for several months. Feel free to reopen if necessary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
button enhancement New feature or request question
Projects
None yet
Development

No branches or pull requests

2 participants