Internet Connection Test

  • 170 Views
  • Last Post 26 July 2016
  • Topic Is Solved
Norry posted this 25 July 2016

What I'm wanting to do:

Run a script every X minutes that will test to see if the computer has internet access.

If true (has internet), do nothing.

If false (no internet), run scene.

Sometimes loss of connection is due to modem or router, and a power cycle is the fix. This could be used to automatically power cycle those (controlled outlet) devices, even when I'm away.

I am grateful for any assistance getting this working.

There seems to be several ways to check for internet and various opinions on what is best. I can't seem to make use of any of them so far, with my very limited knowledge.

One discussion

Another

Yet another

 

  • Liked by
  • rscott
Order By: Standard | Newest | Votes
rscott posted this 25 July 2016

This should work, though I haven't personally tested it yet. Create a script file and add this to it, then save the file. After that, setup your scene with this script in the post spot and set it on a 10 minute repeatable timer. 

NOTE: On line 19, there's a spot to enter the name of the script you want to run. Be sure to change that to the scene that will be cycling the power on your router.

 

using System;

using System.Collections.Generic;

using System.Text;

using MLS.ZWave.Service.Rules;

using MLS.ZWave.BusinessObjects;

using System.Net.NetworkInformation;

 

/// <summary>

/// This script checks the internet and if appears down, will execute a scene

///

/// ALWAYS MAKE COPIES OF SCRIPTS YOU INTEND TO CUSTOMIZE OR YOUR CHANGES 

/// COULD BE LOST.

/// </summary>

public class CheckInternetRunScene : ScriptBase, ScriptInterface {

    public void runScript() {

        try {

 

            var sceneName = "myScene";

 

            // Ping Google.com; if no response, execute the scene

            if (!pingHost("www.google.com")) {

                activateScene(sceneName);

            }

            

        } catch (Exception ex) {

            // Log the exception here

            var message = ex.Message;

        }

    }

 

    public static bool pingHost(string nameOrAddress) {

        bool pingable = false;

        Ping pinger = new Ping();

        try {

            PingReply reply = pinger.Send(nameOrAddress);

            pingable = reply.Status == IPStatus.Success;

        } catch (PingException) {

            // Discard PingExceptions and return false;

        }

        return pingable;

    }

 

}

 

 

  • Liked by
  • Norry
Norry posted this 26 July 2016

I see several things I was doing wrong in my attempts from this. Thank you, it works great with one change, I had to add "s to the scene name. Otherwise, works perfect. Thank you once again.

activateScene(sceneName);

activateScene("sceneName");

Close