Sunday 20 November 2016

Checking your internet connection: a simple Gambas project

A few weeks ago I noticed my internet radio was dropping out for a few moments at a time.


Further investigation showed I was sometimes losing the internet altogether.


So I decided to create a simple program to monitor internet availability.

When an internet problem arises, I can never be sure whether its my router, the cabling or my ISP that is at fault. But once I had noticed that I was losing the internet on more than one device, I decided to use my always-on Pi server to create a log of internet up/down time. This server is actually connected to the router via an ethernet cable.



This is a relatively easy programming task, especially in a RAD (rapid application development) environment like Gambas.

The program details


The basic program tasks are:-
  1. ping a target url every 20 seconds
  2. if no reply, ping a second target
  3. if no reply, ping a third target
  4. if no reply, time-stamp a log with the comment "down"
  5. if a ping attempt is successful, time-stamp a log with the comment "up"
I have used three reliable urls in the hope that if one fails, one of the others will be OK.

In Gambas, just add a timer to a small blank Gambas form. Also include in the project the simple files class described in a earlier post: Gambas with Class

The main form declarations look like this:-

'Gambas Main form class file

Public thisFile As New ClsFiles

Const TARGET_1 As String = "www.google.com"
Const TARGET_2 As String = "www.bbc.co.uk"
Const TARGET_3 As String = "www.thinkbroadband.com"
Const PING_INTERVAL As Integer = 20000    '20s
Const LOG_FILE As String = "/home/pi/Ping_log"

Public blnConnected As Boolean


I'm using the Form_Open event to initialise the files object and the 20s ping timer.

Public Sub Form_Open()

  thisFile.PathFile = LOG_FILE
  If Not thisFile.FileExists Then
    thisFile.FileText = ""
  Endif
  tmrPing.Delay = PING_INTERVAL
  tmrPing.Start()

End


Then the code in the timer event is used to perform the ping checks:-

Public Sub tmrPing_Timer()
Dim blnSuccess As Boolean

  If PingTarget(TARGET_1) = True Then
    blnSuccess = True
    Me.text = "Good ping: " & TARGET_1
  Else If PingTarget(TARGET_2) = True Then
    blnSuccess = True
    Me.text = "Good ping: " & TARGET_1
  Else If PingTarget(TARGET_3) = True Then
    blnSuccess = True
    Me.text = "Good ping: " & TARGET_1
  Else
    Me.text = "Bad ping"
  Endif

 
...and also to compare the current status with the previous status, and stamp the log if the status has changed...

  If blnSuccess = True And blnConnected = False Then
    StampLog(blnSuccess)
  Endif
  If blnConnected = True And blnSuccess = False Then
    StampLog(blnSuccess)
  Endif
  blnConnected = blnSuccess


...and also give a simple indication that the program is still running (a heart-beat) by simply toggling the form between blue and green...

  If Me.Background <> Color.Blue Then
    Me.Background = Color.Blue
  Else
    Me.Background = Color.Green
  Endif
End


The other routines are PingTarget which executes a command-line ping...

Public Function PingTarget(strURL As String) As Boolean
Dim strResult As String

  Exec ["ping", "-c 1" "-w 1", strURL] To strResult
  If InStr(strResult, "64 bytes from") > 0 Then 'successful ping
    Return True
  Else
    Return False
  Endif
 
End


...and StampLog, which writes text to a file including time-stamp, and either "up", "down" or program "started"...

Public Function StampLog(bConnected As Boolean) As Boolean
Dim strFileContents As String
 
  strFileContents = thisFile.FileText
  If InStr(Me.Tag, "running") > 0 Then
    If bConnected = True Then
      thisFile.FileText = Format(Now(), "yyyy-mm-dd,hh:nn:ss,") & "up" & gb.Lf & strFileContents
    Else
      thisFile.FileText = Format(Now(), "yyyy-mm-dd,hh:nn:ss,") & "down" & gb.Lf & strFileContents
    Endif
  Else
    thisFile.FileText = Format(Now(), "yyyy-mm-dd,hh:nn:ss,") & "*****Started" & gb.Lf & strFileContents
    Me.Tag = "running"
  Endif
 
End



So for now, I can open the log file to view a record of "up" and "down" times. In the longer term I need to write to an HTML file on the Pi server which can be opened and viewed remotely, on my laptop.

2 comments:

  1. I thought this was a good idea so, based on your code I wrote a revised version which if you have no objection I would like to publish on the Gambas Farm. You can download my code with: -

    wget www.cogier.com/programs/Pinger.tar

    Check out www.gambas.one


    ReplyDelete
  2. Hi Cogier, yes no problem, please go ahead.

    The new Gambas website is looking really good!

    ReplyDelete