SOLVED: fix WiFi connection lost on Raspberry Pi

  • I tried this with Raspberry Pi 5 but I think it works also with Pi 4.

    Libreelec 11 lost WiFi connection often. There is also a known bug , I don't know if my problem is related to this bug.

    I have 3 problems:

    - WiFi connection lost

    - router password lost

    - bad routing

    in /storage/.config/ I created a new file wifi_monitor.sh (of course replace MY-SSID-5-GHZ, MY-SSID-2.4-GHZ, MY-WPA-KEY and 192.168.1.1 with your values)

    in /storage/.config/system.d/ I created a new file, wifi_monitor.service

    Code
    [Unit]
    Description=WiFi monitor and reboot if multiple WiFi connects fail
    
    [Service]
    ExecStart=/storage/.config/wifi_monitor.sh
    
    [Install]
    WantedBy=multi-user.target


    then from bash:

    Code
    chmod +x /storage/.config/wifi_monitor.sh
    
    systemctl enable wifi_monitor.service

    how the script works:

    - it does a check every 10 seconds

    - at each check, it fix routing. It remove a bad route with the right one. In my WiFi network the default gateway is 192.168.1.1, so replace this with your (it could be 192.168.0.1, 192.168.2.1 ...)

    - with ip addr show wlan0 | grep "inet " &> /dev/null it checks if you are connected to wlan

    - if you are not connected it force connection to your network using the right key. In my script I have 2 different SSID, one for 5 Ghz and another one for 2.4 Ghz so the script try with one SSID or with the other. Of course you can modify it to match your needs and router settings

    - you can remove the reboot part, I only added it out of an abundance of caution (the script reboot Libreelec after max_attempts consecutive failed connections)

    Note: when you start LibreElec you can still see the annoying popup window for password setting. Looking at Kore you can see you are connect so you can press <- on Kore interface to remove the popup and you are done.