Pedro Henrique Fonseca Bertoleti
Published © CC BY

Network Downtime Monitor

IoT is here to stay! But without a reliable Internet connection, everything is ruined, isn't it? Use this project to check it out!

BeginnerFull instructions provided2 hours905
Network Downtime Monitor

Things used in this project

Hardware components

USB-A to Micro-USB Cable
USB-A to Micro-USB Cable
×1
SD Card (8GB)
×1

Software apps and online services

PuTTy

Story

Read more

Code

Source-code

Python
Here's project's source code. Pay attention to the comments and have fun!
import httplib
import urllib
import os
import time

#global variables
TwitterAccountOfYourInternetProvider = "@NULLNULLNULL" #put here your Internet provider Twitter's account
NameOfYourCity = "Sao Bernardo do Campo - SP"
APIKeyThingSpeak = 'KKKKKKKKKKKKKKKK'   #replace KKKKKKKKKKKKKKKK by your api_key of ThingTweet
WriteAPIKey = 'WWWWWWWWWWWWWWWW'   #replace WWWWWWWWWWWWWWWW by your channel's write api key

#Downtime monitor variables
InitTimestamp_DownTime = 0
IsItOffline = 0
TotalDownTime = 0
DownTimeReportFile = "DownTimeReport.txt"
TotalDowntimesDetected = 0
NumberOfAttempts = 1

def VerifyAndRegisterDownTime(res):
	global IsItOffline
	global InitTimestamp_DownTime
	global TotalDownTime
	global DownTimeReportFile
	global TotalDowntimesDetected
	global NumberOfAttempts

	if (res == 0):
		print " "
		print "Internet is on!"
	else:
		print " "
		print "Internet is off..."

        #Check #1: Internet was on and now it's off
	if ((res!=0) and (IsItOffline == 0)):
		InitTimestamp_DownTime = time.time()
		IsItOffline = 1
	        return

        #Check #1: Internet was off and now it's on
	if ((res == 0) and (IsItOffline == 1)):
		TotalDownTime = time.time() - InitTimestamp_DownTime
		StringDT =  "DownTime detected! "+str(TotalDownTime)+" seconds offline\n"
		print StringDT
		TxtFile = open(DownTimeReportFile,"a")
		TxtFile.write(StringDT)
		TxtFile.close()
        TotalDowntimesDetected = TotalDowntimesDetected + 1
		IsItOffline = 0
	    SendTweet(TotalDownTime)
		SendDownTimeDataToThingSpeak(TotalDownTime)
		return

def SendTweet(DuracaoDT):
	global TwitterAccountOfYourInternetProvider
	global NameOfYourCity
	global APIKeyThingSpeak
        
        
    StringtoTweet = TwitterAccountOfYourInternetProvider+", a "+str(DuracaoDT)+" seconds downtime was detected. I'm on "+NameOfYourCity+". #DownTimeDetected"
	params = urllib.urlencode({'api_key': APIKeyThingSpeak, 'status': StringtoTweet})  
	conn = httplib.HTTPConnection("api.thingspeak.com:80")
	conn.request("POST","/apps/thingtweet/1/statuses/update",params)
	resp = conn.getresponse()
	conn.close()

def SendTweetSystemInit():
	global APIKeyThingSpeak

    StringtoTweet = "Downtime Monitor Started!"
	params = urllib.urlencode({'api_key': APIKeyThingSpeak, 'status': StringtoTweet})  
	conn = httplib.HTTPConnection("api.thingspeak.com:80")
	conn.request("POST","/apps/thingtweet/1/statuses/update",params)
	resp = conn.getresponse()
	conn.close()
	
def SendDownTimeDataToThingSpeak(DuracaoDT):	  	
	global WriteAPIKey

	params = urllib.urlencode({'field1': str(DuracaoDT),'key':WriteAPIKey})
   	headers = {"Content-typZZe": "application/x-www-form-urlencoded","Accept": "text/plain"}
	conn = httplib.HTTPConnection("api.thingspeak.com:80")
   	conn.request("POST", "/update", params, headers)
	resp = conn.getresponse()


#---------------
#  MAIN PROGRAM
#---------------

SendTweetSystemInit()

while True:
	try:
		os.system("clear")
       	print "---------------------------"
	    print "     DownTime Monitor      "
      	print "---------------------------"
	    print " "
		print "Attempt #"+str(NumberOfAttempts)+" - "+str(TotalDowntimesDetected)+" detected downtime(s)"
	    print " "

		PingResult = os.system("ping -c 1 8.8.8.8")
	    VerifyAndRegisterDownTime(PingResult)
		time.sleep(20) 
      	NumberOfAttempts = NumberOfAttempts + 1
	except (KeyboardInterrupt):
		print "This application is closed. Good bye."
		exit(1)

Credits

Pedro Henrique Fonseca Bertoleti

Pedro Henrique Fonseca Bertoleti

8 projects • 50 followers
Hi there! My name is Pedro Bertoleti. I am aboslutely crazy about: - Electronics - Embedded software design - Technology

Comments