Things used in this project

Hardware components:
102110017%206
MediaTek Labs LinkIt Smart 7688 Duo
×1
Adafruit industries ada592 image 75px
USB-A to Micro-USB Cable
×1
SD Card (8GB)
×1
Software apps and online services:
PuTTy

Code

Source-codePython
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

Pedrobertoleti
Pedro Henrique Fonseca Bertoleti
6 projects • 12 followers
Hi there! My name is Pedro Bertoleti. I am aboslutely crazy about: - Electronics - Embedded software design - Technology
Contact

Replications

Did you replicate this project? Share it!

I made one

Love this project? Think it could be improved? Tell us what you think!

Give feedback

Comments

Add projectSign up / Login