Script to reconnect wifi

  • I occasionally have a wifi dropout and (I guess) because I use a remote SQL database for my library, eventually Kodi totally freezes meaning I have to power cycle to get it working again.

    I know I can automatically start a bash script with Kodi on LibreELEC so have created this one, it seems to start OK but doesn't detect it loses connection (nothing's written to the log). Any ideas please?

    I run LibreELEC on my ODROID-C2, is there a way to temporarily restore the heartbeat on the blue LED to verify it's not a total lockup?
    Thanks for reading

  • At first I would try and find out *why* your wifi is dropping out. If using the internal wifi try removing anything metal that might be in the way of your router. Alternatively buy a USB adapter with a better antenna which can be positioned to get better reception.


    As to the script, I would run it in a terminal and see what the output is.

  • If I start the script manually, line 6 echoes to the console every minute or so as it should. I might disable the tail command and just have it log everything for a while to check it doesn't stop running for no reason.

    I have noticed sometimes after rebooting the device, I usually/always have to manually toggle the connection in LibreELEC settings straight away as it shows "failure" under the network name. I wonder if I've written the 'if' statement incorrectly because it doesn't restart the wifi for this, or log anything to the log file after the first write to say it's active.

    There's no internal wifi in the C2, I'm using a USB adapter, this one: https://www.amazon.co.uk/dp/b06wwhh29y/. I'm not sure why it drops out, the signal is decent enough. It's not a fancy adapter but I didn't have any trouble using it with my (Windows) laptop, and that was through a long USB extension cable in a hotel, so I didn't have to sit by the door to get online as the signal was poor.

  • I don't think your if statement works. Try with

    Code
      ping -c 1 -w 3 172.16.0.1 &>/dev/null
      if [ $? -ne 0 ]; then

    Gone...

  • That modification worked perfectly in a simple test script using that IP (172.16.0.1 is my modem/router) and also as expected with one that's not in use. Hopefully this will act as a 'plaster' until I can sort out a plan B...funds permitting, new carpets could mean running ethernet, but I might just settle for powerline.


    I found how to put the blue LED back on so posted here for reference:

    Code
    echo heartbeat > /sys/class/leds/blue\:heartbeat/trigger

    Doing this means I can see if the unit has locked up completely or if it's just Kodi getting confused.


    Thanks both for your assistance :)

  • EDIT: fixed this, the script wasn't in unix format!

    I'm struggling with this script, probably something simple but this is what I have:


    I get this error when running:

    Code
    autoconnectwifi.sh: line 26: syntax error: unexpected "done" (expecting "then")


    So after figuring my stupid mistake I found this other script and adapted it. This uses the wifi network instead of a ping, maybe better if for some reason the IP will change or unavailable. wifi_servicecode_managed_psk has been changed from what is listed in connmanctl services


    Bash
    #!/bin/bash
    
    while true ; do
        if ifconfig wlan0 | grep -q "inet addr:" ; then
            sleep 60
        else
            connmanctl connect wifi_servicecode_managed_psk &>/dev/null
            sleep 10
        fi
    done

    Edited once, last by Morphy ().