Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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:
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 index1
or use the following template:At boot, a virtual Switch is created in Tasmota and its Switchmode is forced to 4.
The information is displayed on the log as:
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):
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
Or by self compiling and overriding the default value in your
user_config_override.h
: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
: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:
NOTE: The code change must pass CI tests. Your PR cannot be merged unless tests pass