xoseperez / espurna

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

Board 48 Sonoff Dual R2 #341

Closed xoseperez closed 6 years ago

xoseperez commented 6 years ago

Originally reported by: chryss (Bitbucket: chryss, GitHub: chryss)


Hello Xose,

I have a Sonoff Dual R2 and want to command my shutter with my toggle. That what i want : When i push button 0 relay 1 is UP and when i release it, relay 1 is down When i push button 1 relay 2 is UP and when i release it, relay 2 is down. I have change hardware.h with this :

#!arduino

#elif defined(ITEAD_SONOFF_DUAL_R2)

    #define MANUFACTURER        "ITEAD"
    #define DEVICE              "SONOFF_DUAL_R2"

    // Buttons
    #define BUTTON1_PIN         0
    #define BUTTON2_PIN         9
    #define BUTTON3_PIN         10
    #define BUTTON1_RELAY       1
    #define BUTTON2_RELAY       2
    #define BUTTON3_RELAY       1
    #define BUTTON1_MODE        BUTTON_SWITCH | BUTTON_SET_PULLUP
    #define BUTTON2_MODE        BUTTON_SWITCH | BUTTON_SET_PULLUP
    #define BUTTON3_MODE        BUTTON_SWITCH | BUTTON_SET_PULLUP

    // Relays
    #define RELAY1_PIN          12
    #define RELAY2_PIN          5
    #define RELAY1_TYPE         RELAY_TYPE_NORMAL
    #define RELAY2_TYPE         RELAY_TYPE_NORMAL

    // LEDs
    #define LED1_PIN            13
    #define LED1_PIN_INVERSE    1

And is ok for button and relay with option "Zero or one switches active", but on boot relay 1 is up. To correct this, i put "Toggle Before" but relay is up 1 second on boot, do you have a solution for me ?

xoseperez commented 6 years ago

Original comment by chryss (Bitbucket: chryss, GitHub: chryss):


Hi Xose,

I have tried your configuration, but it's KO :-((. The best configuration is

#!arduino

    #define BUTTON1_MODE        BUTTON_SWITCH | BUTTON_SET_PULLUP | BUTTON_DEFAULT_HIGH
    #define BUTTON2_MODE        BUTTON_SWITCH | BUTTON_SET_PULLUP | BUTTON_DEFAULT_HIGH
    #define BUTTON3_MODE        BUTTON_SWITCH | BUTTON_SET_PULLUP | BUTTON_DEFAULT_HIGH

I'm so sorry to repeat but :

if my button 2 (physical) is on (like button ON/OFF) relay 2 is up and is normal.

if I use command Switch 2 (on the web) to OFF it, relay 2 will be down, OK ?

But if i touch my physical button to put if OFF relay 2 change state to up, do you understand ?

That is my problem.

You can transfer this problem to button 3 (or 1) with relay 1.

I think, if a relay is down, the only action to change his state is button ON, not just button change state, do you understand ?

If i use only my physical button to command my shutter, it's OK, but if i want to mix Physical with virtual (like mqtt) command, it's a problem :((.

xoseperez commented 6 years ago

Original comment by chryss (Bitbucket: chryss, GitHub: chryss):


i don't modify configuration, i just download 1.12 and upgrade Sonoff Dual.

xoseperez commented 6 years ago

What is your current configuration for the buttons? BUTTON2 is a SWITCH_BUTTON?

xoseperez commented 6 years ago

Original comment by chryss (Bitbucket: chryss, GitHub: chryss):


ok Xose, i try 1.12 and now it's OK, relays are down on boot :-).

With Switch Sync Mode "Zero or ..." it's ok to but, if my button 2 is on (like button ON/OFF) relay 2 is up and if I use command to OFF it, relay is down ...you follow me ? But if I release button 2, relay 2 switch up....and that's not what I want, because after, button 2 is invert, if ON Relay 2 is down and if OFF Relay 2 is up.

Normal fonction is Button 2 ON--> Relay 2 Up and Button 2 OFF --> Relay 2 DOWN. Do you understand ?

xoseperez commented 6 years ago

Original comment by chryss (Bitbucket: chryss, GitHub: chryss):


Hi Xose, where i can put your configuration ?

#!arduino
#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
    #define BUTTON1_RELAY           1
    #define BUTTON2_MODE            BUTTON_PUSHBUTTON| BUTTON_DEFAULT_HIGH | BUTTON_SET_PULLUP
    #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           2

What do you want I test ?

xoseperez commented 6 years ago

Original comment by chryss (Bitbucket: chryss, GitHub: chryss):


no, i haven't try, perhaps this evening.

xoseperez commented 6 years ago

Removing milestone: 1.12.0 (automated comment)

xoseperez commented 6 years ago

Have you solved/tested the last configuration I posted using BUTTON3?

xoseperez commented 6 years ago

Original comment by chryss (Bitbucket: chryss, GitHub: chryss):


Yes it's switch for the roller shutter ;-)

xoseperez commented 6 years ago

The use any other button. But the sonoff will only enter flash mode it it reboot while you are holding the button down... If it's a pushbutton no porblem, if it's a switch then yes, use another button.

xoseperez commented 6 years ago

Original comment by chryss (Bitbucket: chryss, GitHub: chryss):


Be carrefull because Button1 is on GPI0, no ? If there is a power failure, Sonoff enters programming mode when the power returns if button1 is on or pressed . Prefer use Button2 and Button3.

xoseperez commented 6 years ago

How about this configuration:

    #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
    #define BUTTON1_RELAY           1
    #define BUTTON2_MODE            BUTTON_PUSHBUTTON| BUTTON_DEFAULT_HIGH | BUTTON_SET_PULLUP
    #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           2

And then enable sync mode "Zero or one switches active".

xoseperez commented 6 years ago

Original comment by chryss (Bitbucket: chryss, GitHub: chryss):


Just a reminder of the need

When button 1 is pressed, relay 1 is closed When you release button 1, relay 1 is open

When button 2 is pressed, relay 2 is closed When you release button 2, relay 2 is open

Relay 1 is never closed at the same time as Relay 2.

When button 1 is pressed and relay 1 is closed, if button 2 is pressed (by command not button), then relay 1 opens and relay 2 closes, that's correct and is OK.

Normaly when you release the button 1 (which remained pressed), nothing should happen and it is normal. Today when button 1 is released, relay 1 closes.

I think it's a problem today.

Is it clear to you?

xoseperez commented 6 years ago

Original comment by chryss (Bitbucket: chryss, GitHub: chryss):


That's ok,

The Start log is

#!arduino

[WEBSOCKET] Requested action: reboot
[WEBSOCKET] #2 disconnected
[WIFI] Connecting to chryss House

 ets Jan  8 2013,rst cause:1, boot mode:(3,7)

load 0x4010f000, len 1384, room 16 
tail 8
chksum 0x2d
csum 0x2d
v09826c6d
~ld
[SETTINGS] EEPROM size: 4096 bytes
[SETTINGS] Settings size: 1073 bytes

[INIT] ESPURNA 1.11.2
[INIT] xose.perez@gmail.com
[INIT] http://tinkerman.cat

[INIT] CPU chip ID: 0x98CF70
[INIT] CPU frequency: 80 MHz
[INIT] SDK version: 1.5.3(aec24ac9)
[INIT] Core version: 2_3_0
[INIT] Core revision: 

[INIT] Flash chip ID: 0x144051
[INIT] Flash speed: 40000000 Hz
[INIT] Flash mode: DOUT

[INIT] Flash sector size:     4096 bytes
[INIT] Flash size (CHIP):  1048576 bytes
[INIT] Flash size (SDK):   1048576 bytes /  256 sectors
[INIT] Firmware size:       450048 bytes /  110 sectors
[INIT] OTA size:            577536 bytes /  141 sectors
[INIT] SPIFFS size:              0 bytes /    0 sectors
[INIT] EEPROM size:           4096 bytes /    1 sectors
[INIT] Empty space:          16384 bytes /    4 sectors

[INIT] MANUFACTURER: ITEAD
[INIT] DEVICE: SONOFF_DUAL_R2
[INIT] SUPPORT: ALEXA DEBUG_SERIAL DEBUG_TELNET DOMOTICZ HOMEASSISTANT MDNS NTP TELNET TERMINAL WEB

[INIT] Last reset reason: Reboot from web interface
[INIT] Free heap: 32680 bytes
[INIT] Power: 4094 mV
[INIT] Power saving delay value: 10 ms

[TELNET] Listening on port 23
[WEBSERVER] Webserver running on port 80
[RELAY] Retrieving mask: 3
[RELAY] Relay #0 boot mode 0
[RELAY] Relay #1 boot mode 0
[RELAY] #0 set to OFF
[RELAY] #1 set to OFF
[RELAY] Number of relays: 2
[BUTTON] Number of buttons: 3
[LED] Number of leds: 1
[MQTT] MQTT_USE_ASYNC = 1
[MQTT] MQTT_AUTOCONNECT = 1
[MAIN] Uptime: 0 seconds
[MAIN] Free heap: 27048 bytes
[MAIN] Power: 4094 mV
[MAIN] Time: Not set
[BUTTON] Pressed #0, event: 2
[RELAY] #0 scheduled ON in 0 ms
[BUTTON] Pressed #1, event: 2
[RELAY] #1 scheduled ON in 0 ms
[WIFI] Connecting to chryss House
[RELAY] #0 set to ON
[RELAY] #1 set to ON
[RELAY] Saving mask: 3
[MDNS] OK
[NTP] Error: NTP server not reachable
[WIFI] MODE STA -------------------------------------
[WIFI] SSID House
[WIFI] IP   192.168.44.75
[WIFI] MAC  60:01:94:98:CF:70
[WIFI] GW   192.168.44.254
[WIFI] DNS  192.168.44.254
[WIFI] MASK 255.255.255.0
[WIFI] HOST Sonoff_D_0
[WIFI] ----------------------------------------------
[WEBSERVER] Request: GET /index.html
[WEBSOCKET] #1 connected, ip: 192.168.44.3, url: /ws
[MQTT] MQTT brokers found: 0
[NTP] Time: 2018-01-02 21:46:40
xoseperez commented 6 years ago

If you use telnet and reboot you will lose connection... best way is to connect it to your computer via UART, just like you flashed it. You can use the serial monitor in the Arduino IDE, just select the right port and 115200 as baudrate. Once connected you will start to see debug messages. If you want to reboot just write "reset" and ENTER. It will then reboot and all the booting messages will show up in the terminal window.

xoseperez commented 6 years ago

Original comment by chryss (Bitbucket: chryss, GitHub: chryss):


Hi Xose,

Happy new year :-) I found my problem, wrong wifi password :-(, it's Ok now. What do you need, i put 1.11.2 and try telnet but can't understand how to give you boot or debug log.

Can you give me command ?

xoseperez commented 6 years ago

Original comment by Guido Mersmann (Bitbucket: Geit_de, GitHub: Unknown):


Update works like before. Dual R2 power on state for both relays is inverted.

xoseperez commented 6 years ago

Original comment by chryss (Bitbucket: chryss, GitHub: chryss):


I try 1.11.2 and are many dysfunction like can't reconnect after add wifi :-(.

I think my dual is perhaps dead.

After password change, i take this screenshot with no change like default configuration.

Capture d’écran 2017-12-30 à 13.19.02.png Capture d’écran 2017-12-30 à 13.19.21.png

xoseperez commented 6 years ago

Original comment by Guido Mersmann (Bitbucket: Geit_de, GitHub: Unknown):


Using gpio0 as a push button on the wall would be a problem, when someone is holding it for a few seconds. That would cause a firmware reset.

So you say there is no way to configure the button to trigger the relay on a gpio state change without adapting the source and recompile?

I mean most people (at least in germany) have on/off switches in their walls instead of push buttons. Push buttons are mostly used for triggering pulse burst relays and save wires for installing a proper X-ciruit (no clue what the english name is) without an relays.

On of the switches I control with the dual now needs to be turned on and off with a proper speed to swap the light state. With current software I would have to swap it by a push button or compile a special version for it, which is not very handy.

I also was thinking about if these switches could exposed to the outside, so switching/pushing would send information out instead of being directly bonded to the relays. So a dual would appear as two relays and two switches and e.g. some server could parse information from the switches and e.g. do not turn on the light on day light.

xoseperez commented 6 years ago

Button 0 can still be used like a pushbutton with a pullup. Actually that's how most Sonoff or ESP8266 boards with buttons are wired. This way you have an easy way to enter flash mode by holding the button down (to ground) while booting.

You can configure any button (except button 0) like a "BUTTON_SWITCH". It will then toggle the linked relay with every transition (from LOW to HIGH and HIGH to LOW). It is advised to have a pullup to avoid floating. That would be "BUTTON_SWITCH | BUTTON_SET_PULLUP" like in your code above.

Can you copy paste here the debug log for the boot sequence? Just after the INIT section comes TELNET, WEBSERVER and the the RELAY init section.

xoseperez commented 6 years ago

Original comment by Guido Mersmann (Bitbucket: Geit_de, GitHub: Unknown):


Button 0 is a no go on the R2, as it is gpio0, so it must be unused. (Stupid R2 board design)

I soldered wires to the onboard switches contacts. Button 1 is unused as the relays switches a power socket.

But the switches are is not the problem as both relays are on after power off, even as the second one (with the power socket) has not even a switch connected.

Stupid question button related: Is it possible to have the button connectors act as On/Off (open/close) instead of needing actual pushbuttons? I have on/off switches here I would like to use even with the sonoff connected. Or is this some kind of interrupt thing. e.g. line goes down triggers interrupt?

xoseperez commented 6 years ago

Could it be a problem with the buttons? How do you have button0 and button1 connected?

xoseperez commented 6 years ago

Original comment by Guido Mersmann (Bitbucket: Geit_de, GitHub: Unknown):


I can confirm that 1.11.1 makes no difference on power up state fail. Even tried to force preferences change. Changed to power on, Saved, changed back to power off, saved again and rebooted.

Both switches turned on again.

Both switches configured to always off, don´t pules, 0 pulse time, mqtt empty. No relay sync obviously.

xoseperez commented 6 years ago

How are the switches configured on the switches tab?

xoseperez commented 6 years ago

Original comment by chryss (Bitbucket: chryss, GitHub: chryss):


I have just test the 1.11.1 and it's the same, on the default configuration, relay1 and relay2 are up at boot :-( Capture d’écran 2017-12-29 à 13.52.11.png

xoseperez commented 6 years ago

Original comment by chryss (Bitbucket: chryss, GitHub: chryss):


No, 1.11.0 only

xoseperez commented 6 years ago

Have you tested 1.11.1?

xoseperez commented 6 years ago

Original comment by chryss (Bitbucket: chryss, GitHub: chryss):


and on boot relay1 and relay2 is up (not only relay1) sorry.

xoseperez commented 6 years ago

Original comment by chryss (Bitbucket: chryss, GitHub: chryss):


no physical resistor on button ;-)

xoseperez commented 6 years ago

The reboot status issue should be fixed with 1.11.1 released this morning.

You might need to add BUTTON_SET_PULLUP in the BUTTON1_MODE if there is no physicar resistor:

 #define BUTTON1_MODE        BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH | BUTTON_SET_PULLUP
xoseperez commented 6 years ago

Original comment by chryss (Bitbucket: chryss, GitHub: chryss):


I confirm what Guido Mersmann announces, when option "Boot mode" is "Always ON", relay1 and relay2 is down on boot :-).

Little precision anyway ... Even with the option "Boot mode" at "Always ON", at boot, relay1 is up for less than a second. Peharps is important to you Xose.

Just one question

I prefer use Button3 and Button1 to control my shutter, because if i use Button1 connected to my switch, if it lock ON and if there is a power failure, Sonoff enters programming mode when the power returns.

I just need to change from this :

#!arduino

    #define BUTTON1_MODE        BUTTON_SWITCH | BUTTON_SET_PULLUP
    #define BUTTON2_MODE        BUTTON_SWITCH | BUTTON_SET_PULLUP
    #define BUTTON3_MODE        BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH

to this

#!arduino

    #define BUTTON1_MODE        BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH
    #define BUTTON2_MODE        BUTTON_SWITCH | BUTTON_SET_PULLUP
    #define BUTTON3_MODE        BUTTON_SWITCH | BUTTON_SET_PULLUP

That's right ?

xoseperez commented 6 years ago

I found the problem with the relay statuses on reboot. Will release it asap.

xoseperez commented 6 years ago

Original comment by Guido Mersmann (Bitbucket: Geit_de, GitHub: Unknown):


Not sure if that is related, but with the r2 the power on state is inverted.

When I reboot it with power on state set to off, all lights and power plugs are on and vice versa.

This only happens with the dual r2. The dual r1 works fine.

Tested with 1.11.0.

xoseperez commented 6 years ago

Original comment by chryss (Bitbucket: chryss, GitHub: chryss):


I have tried and nothing have change with

#!arduino
#define BUTTON3_MODE        BUTTON_SWITCH | BUTTON_DEFAULT_HIGH
xoseperez commented 6 years ago

Original comment by chryss (Bitbucket: chryss, GitHub: chryss):


I'll try tomorrow and I'll tell you

xoseperez commented 6 years ago

Yes, I think it's clearer now. I don't have a DUAL R2 but I think the physical button is normally high via a pull up so it should be:

 #define BUTTON3_MODE        BUTTON_SWITCH | BUTTON_DEFAULT_HIGH

Actually they should all add the BUTTON_DEFAULT_HIGH option. Not sure this could be causing the issue. I don't know how it could be closed with the "Always OFF" option, unless it's logic is inversed.

xoseperez commented 6 years ago

Original comment by chryss (Bitbucket: chryss, GitHub: chryss):


Sorry about my description of requirement.

The requirement is :

When Button0 or Button3 is push, relay1 is up and when i release Button0 or Button3, relay1 is down.

When Button1 is push, relay2 is up and when i release Button1, relay2 is down.

With this configuration, all are OK, but relay1 is UP on the boot, and if i put "Alway OFF" nothing change :-(.

if i put "Toggle Before", the relay1 is up just 1 second on boot and down after 1 second.

Is-it clear, do you understand, sorry about my english ?

Thanks for your help

xoseperez commented 6 years ago

Sorry, I don't understand the requirement. I can see both relays are working fine but on boot relay 1 is on and you don't want it to be ON, id that right? You can change the boot mode to "Always OFF" to ensure the relays are OFF on boot.