User question
how do I write an autoexec.bat to toggle the relay from either input
Artificial Intelligence Response
Direct answer to the question
- If you’re on an OpenBeken/BK72xx device: put two event handlers in autoexec.bat so the relay toggles when either button/input fires.
- Minimal working script (example: buttons on channels 1 and 2, relay on channel 3):
- setChannel 3 0
- addEventHandler OnClick 1 toggleChannel 3
- addEventHandler OnClick 2 toggleChannel 3
Key points
- Map your GPIOs to channels first (two inputs as Button roles, one output as Relay).
- Use OnClick to get one toggle per press (debounce handled by firmware).
- Reboot to apply; verify with eventDump in the console.
Detailed problem analysis
- Goal: emulate a two‑way/three‑way light switch—either input causes a single output (the relay) to toggle state.
- Event-driven vs. polling: OpenBeken provides an event engine; you register handlers that react to input transitions. This is superior to legacy DOS batch polling because it is low-latency, debounced, and does not waste CPU.
- Channel roles:
- Inputs: set each input GPIO to a Button role (Button, Button_n depending on the electrical polarity).
- Output: set the relay GPIO to Relay (or Relay_n) role.
- Why OnClick: It triggers once per short press (debounced). Using OnChannelChange would also work, but you must guard against both edges if you only want a single toggle on a press.
- Power‑on state: explicitly force a known relay state at boot (setChannel 3 0 or 1) so subsequent toggles are deterministic.
Current information and trends
- Modern Wi‑Fi smart switches that run OpenBeken expose a startup script called autoexec.bat and an event system (OnClick/OnDblClick/OnHold, etc.). The approach above aligns with current community best practices for multi-input-to-one-relay behavior on BK7231-class devices.
- For very old PCs (pure DOS), autoexec.bat cannot directly read hardware pins; you must call a helper utility or TSR. That path is increasingly rare compared to embedded firmware solutions.
Supporting explanations and details
- Example autoexec.bat (OpenBeken)
- setChannel 3 0
- addEventHandler OnClick 1 toggleChannel 3
- addEventHandler OnClick 2 toggleChannel 3
- Optional alternatives:
- If you prefer to toggle on any state change (older firmware or no OnClick):
- addEventHandler OnChannelChange 1==1 toggleChannel 3
- addEventHandler OnChannelChange 2==1 toggleChannel 3
- If one input is active-low, set that channel’s role to Button_n (or invert with setPinRole or invertChannel) so OnClick semantics stay consistent.
- Validation steps
- In Web UI: configure Module → map pins to channels (1,2 = Button/Button_n; 3 = Relay/Relay_n).
- Console: eventDump to confirm handlers; press each input and watch log messages; getChannel 3 to see relay state; set logLevel 4–6 for more detail.
Ethical and legal aspects
- If the relay switches mains voltage, you must ensure:
- Adequate creepage/clearance, proper enclosure, and strain relief.
- Use components with appropriate agency ratings (UL/ETL) and follow local electrical code (e.g., NEC in the United States).
- Provide fail-safe defaults (e.g., relay off on boot for safety-critical loads).
Practical guidelines
- Implementation checklist (OpenBeken)
- Identify channels: which GPIOs are your two inputs and the relay?
- Assign roles: two Buttons + one Relay; adjust polarity (…_n roles) if needed.
- Create autoexec.bat with the three lines shown.
- Reboot; test each input; confirm single toggle per click.
- Robustness tips
- Force known state at boot (setChannel 3 0).
- If accidental double toggles are seen with OnChannelChange, switch to OnClick or add a short guard (e.g., a small delay command after toggle if your firmware supports it).
- Legacy DOS/PC alternative (only if you truly mean MS-DOS autoexec.bat)
- You need a helper tool to read inputs and drive an output (e.g., RELAY.EXE, READPORT.EXE).
- autoexec.bat then “glues” logic by calling those tools; batch alone cannot access hardware ports.
- Example pattern:
- @ECHO OFF
- C:\UTIL\READIN1.EXE > C:\TMP\A.TXT
- C:\UTIL\READIN2.EXE > C:\TMP\B.TXT
- IF contents of A or B changed since last boot THEN C:\UTIL\RELAY.EXE /TOGGLE
- In practice, write a small TSR/service in C and invoke it from autoexec.bat.
Possible disclaimers or additional notes
- Firmware command names can vary slightly by version; if OnClick is unavailable, use OnChannelChange with conditions as shown.
- If your device uses Tasmota or ESPHome, the syntax and file names differ (Rules/Automation or YAML); the OpenBeken script won’t apply verbatim.
Suggestions for further research
- Review your device’s channel map and roles in its Module configuration.
- Check the firmware’s event reference for all available triggers (OnClick, OnDblClick, OnHold, OnRelease) to tailor behavior.
- If using a legacy DOS PC, identify or build a small command-line tool that can read inputs and toggle the output reliably (with debounce and edge detection), then wrap it from autoexec.bat.
Brief summary
- For OpenBeken devices: put three lines in autoexec.bat to toggle the relay when either input is pressed; use OnClick on each input channel and toggleChannel on the relay channel. Ensure channel roles and polarities are correct and set a known relay state at boot.
- If you actually mean MS-DOS: autoexec.bat can only orchestrate external hardware-access utilities; you’ll need a helper program to read inputs and toggle the relay.
If you can confirm your platform (OpenBeken/BK7231 vs. a DOS PC vs. something else) and which channels/pins map to your inputs/relay, I can tailor the exact script—including polarity, initial state, and any special behaviors (double-click, long-press, lockouts).
Disclaimer: The responses provided by artificial intelligence (language model) may be inaccurate and misleading. Elektroda is not responsible for the accuracy, reliability, or completeness of the presented information. All responses should be verified by the user.
User question
how do I write an autoexec.bat on a 2 switch device to toggle the relay when the either switch is opened and toggle again when either switch is closed
Artificial Intelligence Response
Direct answer to the question
autoexec.batin the device file-system and put only four lines (adjust channel numbers to your wiring):Key points
•
OnChannelChangefires on any edge (0→1 or 1→0).•
toggleChannel 3inverts the present relay state, so every change of either switch flips the relay.• Channel 1/2 = the two switches Channel 3 = the relay (change as required).
Detailed problem analysis
Functional requirement
Two maintained (rocker) switches shall emulate the classic “staircase / 2-way lighting” circuit: every time either switch moves, the load must change state.
Why
OnChannelChange(or its new aliasaddChangeHandler)–
OnClickis for momentary buttons and triggers only when pressed.–
OnChannelChange <ch>triggers on both rising and falling edges of a maintained input, exactly what is needed here.– Since firmware 1.17 (Dec-2023)
addChangeHandler ChannelX …became a shorter synonym; both syntaxes co-exist.Channel & GPIO mapping example
Role
Button_nenables internal pull-up and built-in debouncing (≈50 ms), so no extra delay is necessary.Relay initialisation
setChannel 3 0establishes a predictable start-up state. Change to1or remove if you want power-on memory instead.Debounce / chatter
Mechanical contact bounce is already absorbed by the
Button_ndriver. If you used the more genericInput_n, addaddEventHandler OnChannelChange 1 STABLE toggleChannel 3to filter out transitions shorter than the firmware’s “stable” window.
Current information and trends
• Firmware builds after 2023-12-15 expose
addChangeHandleras a shorthand and allow conditional edge filters, e.g.addChangeHandler Channel1==1 toggleChannel 3(react only on opening).• Newer OpenBeken templates auto-populate channel numbers, so many commercial 2-gang modules already come with Ch1/Ch2 for keys and Ch3 for relay, requiring no manual GPIO setup.
• Upcoming releases (road-map Q3-2024) plan “multi-input grouping” to attach one handler to several channels at once:
addChangeHandler {1,2} toggleChannel 3.Supporting explanations and details
Analogy: Think of each switch as a software interrupt source. Whenever an interrupt occurs, the ISR (
toggleChannel 3) flips a state variable (relay). The ISR does not care whether the edge was rising or falling; hence the system naturally mimics the hardware 2-way circuit.Example test sequence (Console):
Ethical and legal aspects
Practical guidelines
Implementation checklist
autoexec.bat, press “Save”, then “Restart”.logLevel 3) while operating the switches; you should see alternating “CH3: 0” / “CH3: 1”.Common pitfalls & cures
• Relay toggles twice on one flip → you accidentally assigned both physical switch poles to separate GPIOs; wire only one pole and use internal pull-up.
• Logic inverted → use
Button_n_Invfor the switch orRelay_n_Invfor the relay, whichever matches your hardware.Possible disclaimers or additional notes
CfgLEDor RAM-only options if that concerns you.Suggestions for further research
if $CH1==$CH2 then …constructs to implement AND/synchronised behaviour.addEventHandler OnChannelChange 1 backlog toggleChannel 3; publish stat/switch1 $CH1.trigger:integration to off-load logic to HA for more complex rules.Brief summary
Use the
addEventHandler OnChannelChange(or its modern aliasaddChangeHandler) on each switch channel and point it totoggleChannelof the relay. Four concise lines inautoexec.batreproduce the classical 2-way staircase circuit completely in firmware, with built-in debouncing and without complex logic.