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

Sonoff Basic R4 Magic switch #20247

Merged
merged 5 commits into from Dec 16, 2023
Merged

Conversation

barbudor
Copy link
Contributor

@barbudor barbudor commented Dec 16, 2023

Description:

Related issue (if applicable): fixes #20067

Implement the Sonoff Basic R4 MagicSwitch feature as we were unsuccessfull.

MagicSwitch description:

The Sonoff Basic R4 include a zero-cross detection circuit connected to GPIO5. In normal operation, a pulse of around 650µs is generated on GPIO5 at a frequency of 100Hz (every 10ms) at each zero-crossing.
While it could be used to trigger power-switching on zero-crossing to extend relay life, it is featured by Sonoff as a MagicSwitch to toggle the relay on power "glitches" using that specific cabling:

image
Note: It can also be used with a single switch circuit by replacing the standard on/off switch by a 2-way switch alone.

When the switch (connected as above) flips, it creates a glitch in the mains powering the Basic R4 and a pulse longer than 1ms is created on GPIO5. If the switch is bouncy, multiple long pulses could be created.

Tasmota feature description:

Set GPIO5 to MagicSwitch with index 1 or use the following template:

{"NAME":"Sonoff Basic R4","GPIO":[0,0,0,0,224,10560,544,0,0,32,0,0,0,0,0,0,0,0,0,0,0,0],"FLAG":0,"BASE":1}

At boot, a virtual Switch is created in Tasmota and its Switchmode is forced to 4.
The information is displayed on the log as:

00:00:00.033 MSW: Switch 1, Switchmode 4

Which means that Switch1 has been assigned to MagicSwitch.

Index 2 disable the internal pull-up, not sure yet it that would be necessary, could be on other hardwares.

MagicSwitch feature will detect a pulse of a minimum of 4000µs to consider the switch is pressed.
A masking window of approximatively 300ms is started to filter bouncing.

Debug and adjustments

By setting the loglevel to 3, a log message will tell the length of the measured pulse and confirm the masking window length (in 50ms units):

17:11:43.993 MSW: length:18464, window:5

You can adjust the minimun length for triggering the MagicSwitch with command MagicSwitchPulse with a value between 1000µs and 500000µs. It is not possible to set below 1000µs without risk to be self-triggered by the zero-cross detection pulse. Above 500000 doesn't make sense as it is likely the BasicR4 will reboot with such glitches.

The value is NOT saved to flash. You can make it permanent either by a boot-rule such as

Rule1 ON power1#boot DO MagicSwitchPulse 5000 ENDON
Rule1 1

Or by self compiling and overriding the default value in your user_config_override.h:

#define MAGICSWITCH_MIN_PULSE 5000

DON'T GO BELOW 1000 as there is not control on compile override !

You can adjust the debouncing window by self compiling and overriding the default value in your user_config_override.h:

#define MAGICSWITCH_MASKING_WINDOW_LEN 6

Comments

The feature will depend on the switch you are using being able to create a large enough (but not too large) glitch during flipping.
IMHO a high quality switch should have the shortest flipping time as possible and wouldn't be suitable with that feature. During my testing I found in my drawers switches that weren't unable to work neither with original Sonoff firmware nor with this Tasmota feature.

Remember that this is triggered by glitches in the mains. If your mains network "naturaly" has glitches, it may be triggered falsely.
Setting MagicSwitchPulse to the highest value that still works with your switch could help filtering smallest power glitches. But if at some point your network has glitches in the same range as your switch flipping time, you are screwed with that feature.

Due to the small size of the feature, it is proposed to include it by default in tasmota32c3 so it is simply available.

Tasmota docs to be updated later.

SECURITY WARNING

Not related to that feature but analysing the BasicR4 hardware shows the lack of a transformer on the power supply circuit. It is confirmed that the ESP GND is at a high potential (measured at 130V on 240V mains network).
Despite not including any power measurment circuit THIS DEVICE IS STRONGLY NOT RECOMMEND FOR HARDWARE HACKING such as attaching external sensors as any wires would create a risk of high voltage shock. This include the 4 pins serial connector that should only be used for flashing while the device is not connected to mains.
Beside Sonoff Basic are known for weak power supplies not suitable for such hacking.

Checklist:

  • The pull request is done against the latest development branch
  • Only relevant files were touched
  • Only one feature/fix was added per PR and the code change compiles without warnings
  • [] The code change is tested and works with Tasmota core ESP8266 V.2.7.4.9
  • The code change is tested and works with Tasmota core ESP32 V.2.0.14
  • I accept the CLA.

NOTE: The code change must pass CI tests. Your PR cannot be merged unless tests pass

@arendst arendst merged commit 7c29038 into arendst:development Dec 16, 2023
64 checks passed
@barbudor barbudor deleted the magic_switch branch December 16, 2023 22:19
hawa-lc4 pushed a commit to hawa-lc4/Tasmota-dev that referenced this pull request Jan 20, 2024
* magic switch take 1

* good to go

* good to go

* final, including in tasmota32c3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants