logo elektroda
logo elektroda
X
logo elektroda

Implementing a Variable Relay Loop in OpenBeken Configuration

angzero 1290 18
ADVERTISEMENT
  • #1 21102869
    angzero
    Level 4  

    Relay toggle
    Good morning everyone,

    I have an issue writing a script that I can't figure out on my own, I have a feeling that the answer is stupid but I can't figure out what is going wrong.

    I have a BK7231N running the latest 1.17.601 OpenBeken. the device is a simple relay switch with a button.

    I am trying to create a script that runs on startup, turns on the relay for 1 minute and then turns it off for 30 minutes, and then loops again, and I tried running the scripts from github and they run perfectly for running and turning off the script using event handlers, but whenever I try to modify those scripts to get it to what I need it to, the device crashes and restarts.

    Here's the original script that runs fine unmodified:

    // advanced delay script
    // See: https://www.elektroda.com/rtvforum/topic4032982.html
    // When user toggles relay on, relay will turn off after delay
    // The current delay value is displayed on www gui
    // The constant delay value is displayed on www gui and fully adjustable and remembered between reboots 
    // Used channels:
    // Channel 1 - relay
    // Channel 2 - current countdown value
    // Channel 3 - constant time for countdown
    // this will make channel 3 save in memory
    setStartValue 3 -1
    // if channel 3 is not set (default first case), set it to 15 seconds11
    if $CH3==0 then setChannel 3 15
    // display text field for channel 3 on www page
    setChannelType 3 TextField
    // set label
    setChannelLabel 3 TurnOffTime
    // display timer seconds (read only) for channel 2 on www page
    setChannelType 2 TimerSeconds
    // shorthand for setting channel 2 to value of channel 3
    alias turn_off_after_time backlog setChannel 2 $CH3
    // shorthand for clearing current timer value
    alias on_turned_off setChannel 2 0
    // shorthand to set relay off
    alias turn_off_relay setChannel 1 0
    // check used to turn off relay when countdown value reaches 0
    alias do_check if $CH2==0 then turn_off_relay 
    // an update command; it decreases current countdown by 1 and checks for 0
    alias do_tick backlog addChannel 2 -1; do_check
    // event triggers for channel 1 changing to 0 and 1
    addChangeHandler Channel1 == 1 turn_off_after_time 
    addChangeHandler Channel1 == 0 on_turned_off
    // main loop - infinite
    again:
    if $CH2!=0 then do_tick
    delay_s 1
    goto again


    I can't figure out what is going wrong or debug it properly, for example, the script crashes when I run something like:
    // check used to turn off relay when countdown value reaches 0
    alias do_check if $CH2==0 then "backlog turn_off_relay; echo "we are done""


    I appreciate the help of anyone who can test this and see if it's a common theme or if it's something wrong I'm doing
  • ADVERTISEMENT
  • #2 21102923
    p.kaczmarek2
    Moderator Smart Home
    It looks like you are trying to use nested quotes. I don't think it's supported, not just in OBK, but anywhere else....

    I will check this crash in Simulator, but I'd suggest you changing:
    
    alias do_check if $CH2==0 then "backlog turn_off_relay; echo "we are done""
    

    to
    
    alias do_turn backlog turn_off_relay; echo "we are done"
    alias do_check if $CH2==0 then do_turn 
    


    Added after 3 [minutes]:

    Btw : a script that runs on startup, turns on the relay for 1 minute and then turns it off for 30 minutes, and then loops again

    
    again:
    setChannel 1 1
    delay_s 60
    setChannel 1 0
    delay_s 1800
    goto again
    
    Helpful post? Buy me a coffee.
  • ADVERTISEMENT
  • #3 21103328
    angzero
    Level 4  

    >>21102923

    Thanks a lot for the quick response and I appreciate your help on this, honestly when you posted that simple loop script, I couldn't help but laugh on how did I miss that and why was I complicating things, but unfortunately, it didn't work and the device still crashes (restart) right after "setChannel 1 0" (I think)

    What is interesting though, is that I replaced the setChannel commands with "POWER TOGGLE", and the script ran without crashing, so now I'm confused as to what is happening and why is it crashing

    Were you able to replicate this behavior?, should I wipe the device and test again?, I don't have another one to test so I would need to wipe to see if it's just a specific behavior to my device
  • #4 21103342
    p.kaczmarek2
    Moderator Smart Home
    This script seems to work for me:
    LittleFS filesystem user interface with editing of autoexec.bat file. Screenshot of the OBK Delay Script Sample interface showing voltage, current, and other energy parameters with buttons for configuration.
    Code: JSON
    Log in, to see the code

    what is your device exactly, what kind of template do you have?
    Helpful post? Buy me a coffee.
  • #5 21103498
    angzero
    Level 4  
    >>21103342>>21103342

    The only difference is that i'm not running it as autoexec.bat only to avoid it constantly running at startup and crashing, instead i named mine "simple.bat" which shouldn't make a difference "i think"

    Here are the details of my device:
    Code: JSON
    Log in, to see the code


    and here are some images of the device itself:
    Packaging of a WiFi smart outlet with remote control and app functionality. Close-up of an electronic module labeled CB2S, with a visible QR code and serial numbers.
  • ADVERTISEMENT
  • #6 21103545
    p.kaczmarek2
    Moderator Smart Home
    Still working, sorry:
    Screenshot of LittleFS file management user interface.

    So you're saying that:
    
    POWER ON
    

    etc, does not crash?
    Helpful post? Buy me a coffee.
  • #7 21103560
    angzero
    Level 4  

    >>21103545
    Oh no i believe you, i'm not sure what's happening and i think i might need to reset the device fully and test again, but yes, the POWER TOGGLE option does not cause a crash for me:

    Screenshot of a text editor with an open script.bat file.
  • #8 21103562
    p.kaczmarek2
    Moderator Smart Home
    Yea but what about POWER ON or POWER OFF?
    Helpful post? Buy me a coffee.
  • #9 21103594
    angzero
    Level 4  
    >>21103562
    using POWER ON and POWER OFF works too it seems
  • #11 21103605
    angzero
    Level 4  

    >>21103602
    I tested a couple on the past couple of days all with the same result, right now I'm on 1.17.601
  • ADVERTISEMENT
  • #12 21103608
    p.kaczmarek2
    Moderator Smart Home
    Ok what happens if you open this page:

    BK7231T_WB3S_WhiteStripWindows tool interface on a webpage.
    And enter your setChannel line there?
    Helpful post? Buy me a coffee.
  • #13 21103646
    angzero
    Level 4  

    p.kaczmarek2 wrote:
    Ok what happens if you open this page:

    BK7231T_WB3S_WhiteStripWindows tool interface on a webpage.
    And enter your setChannel line there?


    No issue there, i can execute commands here and in the Log section in the webapp, its honestly inconsistent with its breaking so i'm not sure what is the trigger here and whether it is the setChannel command, as that works with no issue using the scripts posted on the OpenBeken github, then suddenly on a simple loop script like the one provided earlier, it crashes, but not when using the POWER TOGGLE or POWER ONOFF variants, and i'm not sure if anyone else has faced this or just me
  • #15 21103733
    angzero
    Level 4  
    >>21103673

    yes paired with home assistant
  • #16 21103740
    DeDaMrAz
    Level 19  
    @angzero

    Do you mind sharing your full script with us so we can try to replicate that?

    I have several devices running with both seChannel and POWER ON/OFF scripted for days now without any issues.
  • #17 21103780
    angzero
    Level 4  

    DeDaMrAz wrote:
    @angzero

    Do you mind sharing your full script with us so we can try to replicate that?

    I have several devices running with both setChannel and POWER ON/OFF scripted for days now without any issues.


    At this point, I'm convinced that there is something wrong with my device honestly, because earlier when I tried the following script:
    
    again:
    setChannel 1 1
    delay_s 60
    setChannel 1 0
    delay_s 1800
    goto again


    The device would crash until I replaced the setChannel with POWER TOGGLE or ONOFF, but now it's working with no issues

    How do you guys debug and test scripts efficiently to determine what crashes the system, is there a specific log level or settings that you use for script development and analysis ?
  • #18 21103831
    p.kaczmarek2
    Moderator Smart Home
    Are you sure that you did a full reboot between scripts?

    I am finding most of the issues in Windows build (Simulator):
    OpenBeken IoT device simulator - first early alpha version release for testing
    but it's a bit different than running on MCU, obviously, so then I am also using log levels and some manual testing to narrow down what's wrong.

    Your situation seems very strange and not easily replicable. Maybe the stack size should be increased, but I still can't see how you're filling your stack more than us.
    Helpful post? Buy me a coffee.
  • #19 21105148
    DeDaMrAz
    Level 19  
    angzero wrote:
    How do you guys debug and test scripts efficiently to determine what crashes the system, is there a specific log level or settings that you use for script development and analysis ?


    On my end I would use a test device on a board that has logport exposed and log it via PuTTY with extra debug options selected from a web app then analyze the log for anomalies.

    Here is a question for you when your device crashed there is a feature in OBK on the index page that is giving a reboot reason, did you manage to maybe catch that info??

    Screenshot showing the device's reboot reason.

Topic summary

The discussion revolves around a user attempting to implement a variable relay loop script on a BK7231N device running OpenBeken version 1.17.601. The user aims to create a script that activates a relay for 1 minute and then deactivates it for 30 minutes in a continuous loop. Initial attempts using the "setChannel" command resulted in device crashes, while using "POWER TOGGLE" and "POWER ON/OFF" commands did not cause issues. Various suggestions were made, including checking for nested quotes in the script, ensuring a full reboot between script changes, and analyzing logs for anomalies. The user expressed uncertainty about whether the problem was specific to their device, as others reported successful execution of similar scripts. Debugging strategies were discussed, including using log ports and monitoring reboot reasons.
Summary generated by the language model.
ADVERTISEMENT