2

I am using a Raspberry Pi 2 to show a webpage, using Midori, that shows the information given after invoking a webservice each 12 seconds. For some reason, when Raspbian lose Internet connection the webpage keeps showing the last information given. With Epiphany and Chromiun shows an error page as expected. Anyway, I included a script that checks the Internet connection very 2 minutes and if the connection is down then the webpage is refreshed. So the error page is shown.

How can I detect that the Internet connection is back again and refresh the webpage?

I am using UMTSkeeper to keep internet connection alive.

This is my script to refresh the webpage when connection is down.

ping -c4 192.168.1.1 > /dev/null

if [ $? != 0 ] 
then
  export DISPLAY=:0
  XAUTHORITY=/home/pi/.Xauthority
  xdotool search --onlyvisible --class midori windowsactivate
  xdotool key F5
fi
  • 1
    If I were you, I would concentrate on getting a stable connection rather than fighting the consequences. Anyway, I see you already have a script, doesn't it work? You may want to ping something on the Internet (like 8.8.8.8) rather than a local address, but otherwise I fail to see what problem you're having. – Dmitry Grigoryev Jan 23 '17 at 16:25
  • This script works when Raspbian lost connection so the webpage is refreshed and the error is shown. But after this Raspbian recover the connection but the webpage is not refreshed because this script work just when the connection is lost. I don't want to refresh periodically I want to refresh just after the connection is back. – Rodrick Jan 23 '17 at 17:54
  • Hi Rodirck, did you have the opportunity to test the script I wrote? – Dmitry Grigoryev Jan 27 '17 at 10:43
1

You should keep not only the status ($?) of the current ping command, but also the status of the previous one. Store it in a persistent place (e.g. in a file) or make a script which never exits, so you can use a variable.

Based on two values (let's call these old_ping and new_ping) you'll be able to decide when to refresh:

  • old_ping=0 and new_ping=0: the connection is stable
  • old_ping=0 and new_ping!=0: the connection has just been lost
  • old_ping!=0 and new_ping!=0: the connection has been lost for some time
  • old_ping!=0 and new_ping=0: the connection has just been restored, refresh!

Here's a code sample:

export DISPLAY=:0
export XAUTHORITY=/home/pi/.Xauthority
new_ping=0
while true
do
  old_ping=$new_ping
  ping -c4 192.168.1.1 > /dev/null
  new_ping=$?

  if [ $old_ping != 0 -a $new_ping = 0 ] 
  then
    xdotool search --onlyvisible --class midori windowsactivate
    xdotool key F5
  fi
  sleep 10
done
| improve this answer | |
  • What language is this? Can I do the same for a simple Chromium browser? – Chiwda Apr 20 '17 at 10:40
  • 1
    @Chiwda This is bash, and yes, you can do the same for Chromium (you'll have to find out a way to identify its window in xdotool). – Dmitry Grigoryev Apr 20 '17 at 11:11
  • One last question. Can I send a URL to the browser using this system? The reason, the browser sometimes gets redirected to a Bad Gateway page when the connection is down. – Chiwda Apr 23 '17 at 11:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.