To whom it may concern: I tried a lot and it seems, that the Kobo has problems with its "sleep" function (does not wake up until manually hitting the home button), so I created a delay_function simply by counting. Just to be sure, I encapsulate the other functions by a timeout.
Wifi is lost 10-30 times a day, but the current script detects a missing Wifi connection and tries to reconnect.
So far, it has been running for the last 3 days without major issues (so still not perfectly tested, but looks quite promising so far)
Code:
# network details
LOG_FILE="/wifijob.log"
log_message() {
local MESSAGE="$@"
echo "$(date +"%Y-%m-%d %H:%M:%S") : $MESSAGE" >> "$LOG_FILE"
}
INTERFACE="wlan0"
WIFI_SSID="MYWIFI"
WIFI_PASSWORD="MYWIFIPASSWORD"
# The IP address to ping
HOST="192.168.0.1"
delay_function() {
local count=0
local delay=$1 # Number of seconds to wait
local start_time=$(date +%s)
while [ $(( $(date +%s) - start_time )) -lt $delay ]; do
count=$((count + 1))
done
echo "Waited for $delay seconds by counting to $count."
}
# Function to reconnect to Wi-Fi
reconnect_wifi() {
log_message " ################# Host is not reachable. Reconnecting to WiFi"
timeout 2 echo " ################# Host $HOST is not reachable. Reconnecting to Wi-Fi..."
timeout 60 ifconfig $INTERFACE down
log_message "Ifconfig down. Now sleep for 10s"
timeout 2 echo "Ifconfig down. Now sleep for 10s"
delay_function 10;
timeout 60 ifconfig $INTERFACE up
log_message "Ifconfig up. Now sleep for 10s"
timeout 2 echo "Ifconfig up. Now sleep for 10s"
delay_function 10;
timeout 60 wpa_cli reassociate
log_message "Wifi hopefully reassociated. Sleep for 30s, check ping and re-enter loop."
timeout 2 echo "Wifi hopefully reassociated. Sleep for 30s, check ping and re-enter loop"
status=$(wpa_cli status)
timeout 2 echo "Current Wi-Fi status: $status"
log_message "Current Wi-Fi status: $status"
delay_function 30;
timeout 60 wpa_cli reconfigure
log_message "Wifi hopefully reconfigured. Sleep for 30s, check ping and re-enter loop."
timeout 2 echo "Wifi hopefully reconfigured. Sleep for 30s, check ping and re-enter loop"
status=$(wpa_cli status)
timeout 2 echo "Current Wi-Fi status: $status"
log_message "Current Wi-Fi status: $status"
delay_function 30;
timeout 10 ping -c 1 $HOST > /dev/null 2>&1
# Check and log the ping result
if [ $? -ne 0 ]; then
log_message "Ping is still negative :( Now trying to reconnect."
timeout 2 echo "Ping is still negative :( Now trying to reconnect."
timeout 60 wpa_cli disconnect # Remove all known networks
delay_function 5;
##timeout 60 wpa_cli remove_network all
##timeout 60 wpa_cli save_config
networks=$(wpa_cli list_networks | grep -v "network id" | wc -l)
if [ "$networks" -eq 0 ]; then
timeout 2 echo "No known networks found. Adding new network..."
log_message "No known networks found. Adding new network..."
NETWORK_ID=$(wpa_cli add_network)
timeout 60 wpa_cli set_network $NETWORK_ID ssid "\"$WIFI_SSID\""
timeout 60 wpa_cli set_network $NETWORK_ID psk "\"$WIFI_PASSWORD\""
timeout 60 wpa_cli enable_network $NETWORK_ID
timeout 60 wpa_cli save_config
timeout 2 echo "Network added with SSID: $WIFI_SSID"
log_message "Network added with SSID: $WIFI_SSID"
else
timeout 2 echo "Known networks already configured. No action needed."
log_message "Known networks already configured. No action needed."
fi
# Reconnect to the new network
timeout 60 wpa_cli reconnect
log_message "Wifi hopefully reconnected. Sleep for 30s, check ping and re-enter loop."
timeout 2 echo "Wifi hopefully reconnected. Sleep for 30s, check ping and re-enter loop"
delay_function 30;
status=$(wpa_cli status)
timeout 2 echo "Current Wi-Fi status: $status"
log_message "Current Wi-Fi status: $status"
fi
}
while true; do
# Ping the host
timeout 10 ping -c 1 $HOST > /dev/null 2>&1
# Check the ping result
if [ $? -ne 0 ]; then
reconnect_wifi
else
timeout 2 echo "Host $HOST is reachable."
#log_message "Host $HOST is reachable."
fi
delay_function 10
done