logo elektroda
logo elektroda
X
logo elektroda

[Solved] Need Script for WifiLed at startup, when connection is established change LED function to On/Off

arjen2 816 6
ADVERTISEMENT
  • #1 20799255
    arjen2
    Level 3  
    Dear Forum,

    I got a smart plug with an energy meter. I flashed it with OpenBeken. Inside, there is 1 LED positioned behind the (only) button.
    I can assign the pin of the LED to WifiLED. It then flashes at startup, but after the connection is established, it goes off and stays off.
    I can assign it to LED with the same channel as the button and relay. It then reacts to the state of the relay (on/off).

    Now, I want both functionalities for the LED.
    So, at startup, it flashes indicating that it is trying to establish a WiFi connection, and after that has settled, the LED will follow the relay state. If the relay is on, then the LED will be on, and vice versa.
    It would also be nice if the LED flashes in other cases, e.g., when an OTA is in progress or when the WiFi connection is down. Maybe even with different flash frequencies to distinguish between states.

    I tried the following script, but nothing changed. The pin is assigned to LED, and it will follow the relay state.

    /LedScript.bat
    // 3 November 2023
    //
    // Sets pin 10 to WifiLED_n
    // after WIFI connection
    // change it to LED_n
    // and connect to channel 1
    // (Relay and Button)
    
    setPinRole 10 WifiLED_n
    waitFor WiFiState 4
    setPinRole 10 LED_n
    setPinChannel 10 1


    Is there a simple way to achieve this dual functionality? Please help.
  • ADVERTISEMENT
  • #2 20803891
    p.kaczmarek2
    Moderator Smart Home
    Well... I copied your script into my LittleFS on BK7231N device, as you can see below:
    LittleFS interface on BK7231N device with autoexec.bat file editing.
    And it just works.

    At the startup, the LED blinks, indicating the wifi state. Once the device connects to network, it starts to follow the relay state, as you requested.

    So the basic idea is working correctly. Is it also working for you or is there an issue?
    We can later try to extend it a bit, just as you said.
    Helpful post? Buy me a coffee.
  • ADVERTISEMENT
  • Helpful post
    #3 20803922
    DeDaMrAz
    Level 20  
    I also tested this script on a smart plug device and it is working as described.

    Open editor with script code and a list of bat files.

    What is going on after you manually reset the device?

    EDIT: It will even work with remember last state set.
  • Helpful post
    #4 20804036
    p.kaczmarek2
    Moderator Smart Home
    Here is my version of the script. It will work even if WiFi state changes back to disconnected during usage.

    
    alias mode_wifi setPinRole 10 WifiLED_n
    alias mode_relay setPinRole 10 LED_n
    
    // at reboot, set WiFiLEd
    mode_wifi
    // then, setup handlers
    addChangeHandler WiFiState == 4 mode_relay 
    addChangeHandler WiFiState != 4 mode_wifi 
    
    Helpful post? Buy me a coffee.
  • ADVERTISEMENT
  • #5 20804063
    arjen2
    Level 3  
    Thanks all for your reply.

    My initial autoexec.bat was:

    startDriver ntp
    // ntp_setServer 217.147.223.78
    ntp_timeZoneOfs 1:00 
    
    PowerSave
    
    exec ledscript.bat


    With ledscript.bat as in the original post. It looks like the order of commands in the script is important, so I changed my autoexec.bat to:

    // LedScript.bat
    // 3 November 2023
    //
    // Sets pin 10 to WifiLED_n
    // after WIFI connection
    // change it to LED_n
    // and connect to channel 1
    // (Relay and Button)
    
    setPinRole 10 WifiLED_n
    //setPinChannel 10 5
    waitFor WiFiState 4
    setPinRole 10 LED_n
    setPinChannel 10 1
    
    startDriver ntp
    // ntp_setServer 217.147.223.78
    ntp_timeZoneOfs 1:00 
    
    PowerSave
    
    // exec ledscript.bat


    And that worked.

    Added after 4 [minutes]:

    @p.kaczmarek2 Your code might be even better.


    alias mode_wifi setPinRole 10 WifiLED_n
    alias mode_relay setPinRole 10 LED_n
    
    // at reboot, set WiFiLEd
    mode_wifi
    // then, setup handlers
    addChangeHandler WiFiState == 4 mode_relay 
    addChangeHandler WiFiState != 4 mode_wifi 


    Would that also blink when OTA is involved? I'll check.

    UPDATE. Led is not blinking when OTA file is flashed. It will flash at reboot though.
  • #6 20804222
    p.kaczmarek2
    Moderator Smart Home
    Following code was never designed to blink during OTA. It was made to also blink when device that has been already connected to WiFi loses its connection. That's when a change of WiFi state from 4 to non-4 value triggers an event.
    Helpful post? Buy me a coffee.
  • ADVERTISEMENT
  • #7 21396991
    arjen2
    Level 3  
    My autoexec.bat:


    
    alias mode_wifi setPinRole 10 WifiLED_n
    alias mode_relay backlog setPinRole 10 LED_n; setPinChannel 10 1
    
    // at reboot, set WiFiLEd
    mode_wifi
    // then, setup handlers
    addChangeHandler WiFiState == 4 mode_relay 
    addChangeHandler WiFiState != 4 mode_wifi 
    
    startDriver ntp
    // ntp_setServer 217.147.223.78
    ntp_timeZoneOfs 1:00 
    
    PowerSave
    
    exec timer1.bat
    


    And to be complete, the timer1.bat file (used it for setting on/off timers) :

    // Set timer
    //
    // format: addClockEvent HH:MM[:SS] {weekdayflags BYTE} {UserID} {Command..}
    //
    // WeekDayFlag is a bitflag on which day to run, 0xff = every day, 0x01 = Sunday, 0x02 = Monday, 0x03 = Sunday AND Monday 
    // UserID is a unique id so event can be removed later.
    
    // the following does not work...
    //alias SWITCHON 20:40
    //alias SWITCHOFF 20:40:30
    
    addClockEvent 07:00 0xff 1 POWER ON
    addClockEvent 23:00 0xff 1 POWER OFF

Topic summary

The discussion addresses scripting a single LED on a smart plug flashed with OpenBeken to indicate WiFi connection status and relay state. The LED is assigned to a pin configured as WifiLED during startup, causing it to blink while connecting to WiFi. Once connected (WiFiState == 4), the pin role switches to LED linked to the relay channel, enabling the LED to reflect the relay's on/off state. Scripts shared include aliases for toggling pin roles between WifiLED_n and LED_n, with change handlers monitoring WiFiState to dynamically switch LED behavior. The order of commands in autoexec.bat is critical: setting the pin role to WifiLED_n, waiting for WiFi connection, then switching to LED_n with the relay channel. The solution also supports reverting to WiFi LED mode if the connection drops. Although blinking during OTA updates was requested, the provided scripts do not implement distinct LED patterns for OTA or different WiFi states beyond connected/disconnected. Additional timer scripts for scheduled relay control were shared but are separate from the LED control logic. The device platform mentioned is BK7231N, and the LED pin used in examples is pin 10.
Summary generated by the language model.
ADVERTISEMENT