rpiarduinomusings

Raspberry Pi, Arduino, Sensors and the data they produce

A Ping Module

The module presented below is intended to be called by a program that is running as a service on a machine that is operating on a UPS. Its purpose is to detect when utility power has failed so that it can power down the machine. It does this by pinging either a router or another machine on the network either of which are not operating on a UPS.

The overarching goal is to save the file system from being corrupted by a power failure.

When a power failure is detected this module will call os.system(“sudo shutdown -h now”) to stop the operating system.

 
The service program is listed below.

#!/usr/bin/python #********************************************************************** # Program : Ping.py # Date : 20060707 # Description : This module is intended to be used by a program that is # running as a service on a machine that is operating on # a UPS. Its purpose is to detect when utility power has # failed so that it can power down the machine. # # The overarching goal is to save the file system from # being corrupted by a power failure. # # It does this by pinging either a router or another # machine on the network either of which are not # operating on a UPS. # # Using this program as a module within a service program # allows the pinging process and potential shutdown to # take place during non-critical operations such as when # writting data to a file. If running as a stand-alone # service, the potential exists for a shutdown to happen # when files have been opened by other service programs. # # To use: import Ping # Example Call: Ping.pingIt("192.168.128.4") # Outcome: If the ping determines that there is power then control # returns to the caller, otherwise the system will be # brought down and the caller will not be in a critical # phase of processing when that occurs. # #********************************************************************** import os import platform import sys import time import datetime from datetime import datetime #********************************************************************** #***** VARIABLES ***** #********************************************************************** progName = os.path.basename(__file__) # Full path of this python script mypath=os.path.abspath(__file__) # Path location only (excluding script name) baseDir=mypath[0:mypath.rfind("/")+1] baseFileName=mypath[mypath.rfind("/")+1:mypath.rfind(".")] #********************************************************************** #***** FUNCTION DECLARATIONS ***** #********************************************************************** #---------------------------------------------------------------------- def myFunction(): print "Entered myFunction()" return; #---------------------------------------------------------------------- # This function accepts a value to be compared with the low and high # values that are also provided. These values are expressed as integers # and if the value (the first parameter) falls between the low and high # values (inclusive), True will be returned, otherwise False. #---------------------------------------------------------------------- def inRange(value, low, high): intVal = int(value) intLow = int(low) intHigh = int(high) if intLow <= intHigh: return intLow <= intVal <= intHigh else: return intLow <= intVal or intVal <= intHigh #---------------------------------------------------------------------- def execute(): hostname = "10.0.0.8" #Sacrificial Pi pingIt(hostname) return #---------------------------------------------------------------------- def pingIt(hostname): print (" ") print ("In pingIt using " + hostname ) # Construct ping parameters as a function of OS ping_str = "-n 1" if platform.system().lower()=="windows" else "-c 1" response = os.system("ping " + ping_str + " " + hostname) if response == 0: print hostname, 'is up!' else: print hostname, 'is down!' #If the router is on a UPS, you can send an Email to notify #you of the fact that utility power has failed. Also sleep #for a specified time before trying again. After the number #of attempts have occurred, power down this Pi. #Uncomment this before going live!!!!! #os.system("sudo shutdown -h now") #********************************************************************** #***** M A I N L I N E C O N T R O L L E R ***** #********************************************************************** if __name__ == '__main__': print("+++++++++++++++++++++++++++++++++++") print("%s - Entering Program" % progName) print "Path to program = " + mypath print sys.version_info print "The O/S = " + platform.system() try: execute() finally: print("%s - Exiting Program" % progName) print("+++++++++++++++++++++++++++++++++++") print("") #********************************************************************** #********************************************************************** #***** E N D O F S O U R C E ***** #********************************************************************** #**********************************************************************

 
The ATXRaspi Power Board ($15) could be incorporated into this scheme. While the Ping.py program will safely bring down the OS, saving the SD card(s) which is the overarching goal, it doesn’t actually shutdown the Pi. This board could do that without actually having to push a physical button. See this video.

The board can be ordered from here.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: