Example: Display Internet connectivity status

Arvydas Juskevicius edited this page Apr 7, 2016 · 1 revision

This example shows how to use BlinkStick as an Internet connectivity status indicator.

Script works by checking connectivity to Google DNS server. When connection can be established, BlinkStick will light up green. When connection is not available, BlinkStick will start pulsing red and will turn green back again when connection is reestablished.

import socket
import time
from blinkstick import blinkstick

This functions checks connection to Google DNS server
If DNS server is reachable on port 53, then it means that
the internet is up and running
def internet_connected(host="", port=53):
    Host: (google-public-dns-a.google.com)
    OpenPort: 53/tcp
    Service: domain (DNS/TCP)
        socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect((host, port))
        return True
    except Exception as ex:

    return False

# Find first BlinkStick
led = blinkstick.find_first()

# Can't do anything if BlinkStick is not connected
if led is None:
    print "BlinkStick not found...\r\nExiting..."
        # Store value of last state in this variable
        connected = False

        while (True):
            if internet_connected():
                # If previously internet was disconnected, then print message
                # and set BlinkStick to green
                if not connected:
                    print "Internet is up!"
                    connected = True

                # Wait for 1 second before checking for internet connectivity
                # If previously internet connected, then print message
                if connected:
                    print "Internet is down..."
                    connected = False

                # BlinkStick pulse API call lasts for 1 second so this acts
                # as delay before next check for internet is performed
    except KeyboardInterrupt:
        print "Exiting... Bye!"