logo elektroda
logo elektroda
X
logo elektroda
Dostępna jest polska wersja

Czy wolisz polską wersję strony elektroda?

Nie, dziękuję Przekieruj mnie tam

OpenBeken autoexec.bat script to toggle relay on BK7231 from two button inputs

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).

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

  • Create /edit autoexec.bat in the device file-system and put only four lines (adjust channel numbers to your wiring):
// ---------- 2-way switch logic ----------
setChannel 3 0 // start with relay OFF
addEventHandler OnChannelChange 1 toggleChannel 3 // react to switch-1
addEventHandler OnChannelChange 2 toggleChannel 3 // react to switch-2
// ---------------------------------------

Key points
OnChannelChange fires on any edge (0→1 or 1→0).
toggleChannel 3 inverts 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

  1. 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.

  2. Why OnChannelChange (or its new alias addChangeHandler)
    OnClick is 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.

  3. Channel & GPIO mapping example

    setPinRole 14 Button_n // switch-1 on GPIO14
    setPinChannel 14 1
    setPinRole 12 Button_n // switch-2 on GPIO12
    setPinChannel 12 2
    setPinRole 13 Relay_n // relay on GPIO13
    setPinChannel 13 3

    Role Button_n enables internal pull-up and built-in debouncing (≈50 ms), so no extra delay is necessary.

  4. Relay initialisation
    setChannel 3 0 establishes a predictable start-up state. Change to 1 or remove if you want power-on memory instead.

  5. Debounce / chatter
    Mechanical contact bounce is already absorbed by the Button_n driver. If you used the more generic Input_n, add
    addEventHandler OnChannelChange 1 STABLE toggleChannel 3
    to filter out transitions shorter than the firmware’s “stable” window.


Current information and trends

• Firmware builds after 2023-12-15 expose addChangeHandler as 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):

setChannel 1 1 // open switch-1 → relay toggles ON
setChannel 2 1 // open switch-2 → relay toggles OFF
setChannel 1 0 // close switch-1 → relay toggles ON

Ethical and legal aspects

  • Mains relays must be rated for the load; incorrect ratings may cause fire.
  • Follow local electrical codes (e.g. NEC, IEC 60364). Where required, installations must be done by a licensed electrician.
  • When the module is used in life-safety applications (staircase lighting, evacuation paths) implement a watchdog or fallback to prevent darkness on firmware crash.

Practical guidelines

Implementation checklist

  1. Flash or OTA-upgrade to the latest OpenBeken (≥1.17) for debounced inputs.
  2. Verify GPIO roles under “Configure → Pins”.
  3. Upload the four-line autoexec.bat, press “Save”, then “Restart”.
  4. Observe log (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_Inv for the switch or Relay_n_Inv for the relay, whichever matches your hardware.


Possible disclaimers or additional notes

  • Some low-cost modules save channel states to flash every time they change; with very frequent toggling this might shorten flash life-span. Consider CfgLED or RAM-only options if that concerns you.
  • If the device is battery-powered, every relay toggle consumes surge current; evaluate power budget accordingly.

Suggestions for further research

  • Explore if $CH1==$CH2 then … constructs to implement AND/synchronised behaviour.
  • Combine with MQTT: addEventHandler OnChannelChange 1 backlog toggleChannel 3; publish stat/switch1 $CH1.
  • Study Home-Assistant’s trigger: integration to off-load logic to HA for more complex rules.

Brief summary

Use the addEventHandler OnChannelChange (or its modern alias addChangeHandler) on each switch channel and point it to toggleChannel of the relay. Four concise lines in autoexec.bat reproduce the classical 2-way staircase circuit completely in firmware, with built-in debouncing and without complex logic.

Ask additional question

Wait...(2min)
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.