def cloud_data_read(log_msg): """ Function name : cloud_data_read Parameter : log_msg Functionality : Writes the data into cloud server Function Invoked : utils.print_msg(), cloud_read() Return Value : Returns True on Success and False on failure """ try: cloud_read_status, data = \ cloud_read(config.DATA_CHANNEL_ID, config.DATA_CHANNEL_READ_API_KEY) if cloud_read_status is True: utils.print_msg( config.STR_SYMBOL * config.NUMERIC_CONSTANT_FIFTEEN + ' ' + log_msg + " Intruder Breach Alert Information " + config.STR_SYMBOL * config.NUMERIC_CONSTANT_FIFTEEN) intruder_status_msg = data['field1'] utils.print_msg("Intruder status: %s" % intruder_status_msg) # Either safe or breach time_stamp = data['field2'] utils.print_msg("Time Intruder detected: %s" % time_stamp) distance_in_cm = data['field3'] utils.print_msg("Distance Intruder Identified: %s" % distance_in_cm) return True else: return False except Exception as e_obj: utils.print_msg('EXCEPTION: In cloud_data_read(), due to %s' % e_obj) return False
def system_reset(): try: cloud.system_reset_status_cloud_data_write(1) return redirect(url_for('index')) except Exception as e_obj: utils.print_msg('EXCEPTION: In system_reset(), due to %s' % e_obj) return redirect(url_for('index'))
def cloud_read(channel_id, read_api_key): """ Function name : cloud_read Parameter : channel_id, read_api_key Functionality : Reads the data from the cloud server Function Invoked : utils.print_msg() Return Value : Returns True and data on Success, False and None on failure """ conn = None try: conn = urllib.urlopen("http://api.thingspeak.com/channels/%s/feeds/" "last.json?api_key=%s" % (channel_id, read_api_key)) response = conn.read() data = json.loads(response) return True, data except Exception as e_obj: utils.print_msg('EXCEPTION: In cloud_read(), due to %s' % e_obj) return False, None finally: conn.close()
def system_reset_status_cloud_data_write(system_reset_status): """ Function name : system_reset_status_cloud_data_write Parameter : system_reset_status Functionality : Writes the system reset status cloud data server Function Invoked : utils.print_msg(), cloud_write() Return Value : Returns True on success, False on failure """ try: cloud_write_status, timestamp = cloud_write( system_reset_status, config.STR_NONE, config.SYSTEM_CHANNEL_WRITE_API_KEY, config.STR_SYSTEM) if cloud_write_status is True: return True else: utils.print_msg('Invalid data provided:' '\nsystem_reset_status - %s' '\nsystem_reset_time - %s' % (system_reset_status, timestamp)) return False except Exception as e_obj: utils.print_msg('EXCEPTION: In system_reset_status_cloud_data_write()' ', due to %s' % e_obj) return False
def cloud_data_write(msg=config.STR_NONE, distance_in_cm=config.STR_NONE): """ Function name : cloud_data_write Parameter : msg, distance_in_cm Functionality : Writes the data into cloud server Function Invoked : cloud_write(), utils.print_msg() Return Value : Returns True on success, False on failure """ try: cloud_write_status, timestamp = cloud_write( msg, distance_in_cm, config.DATA_CHANNEL_WRITE_API_KEY, config.STR_DATA) if cloud_write_status is True: return True else: utils.print_msg('Invalid data provided:' '\nmsg - %s' '\ntimestamp - %s' '\ndistance in cm - %s' % (msg, timestamp, distance_in_cm)) return False except Exception as e_obj: utils.print_msg('EXCEPTION: In cloud_data_write(), due to %s' % e_obj) return False
def range_finder(range_find): """ Function name : range_finder Functionality : Finds the intruder distance Function Invoked : print_msg() Return value : Returns True and distance in cm, otherwise False, None """ try: pulse_start = 0 pulse_end = 0 if range_find is True: utils.print_msg("Measuring the Intruder distance") gpio.output(config.ULTRASONIC_SENSOR_TRIGGER_PIN, False) # Set port/pin value to 0/GPIO.LOW/False time.sleep(config.NUMERIC_CONSTANT_TWO ) # Waiting time For Sensor To Settle gpio.output(config.ULTRASONIC_SENSOR_TRIGGER_PIN, True) # Set port/pin value to 1/GPIO.HIGH/True time.sleep(config.NUMERIC_CONSTANT_ONE / 10000) gpio.output(config.ULTRASONIC_SENSOR_TRIGGER_PIN, False) while gpio.input(config.ULTRASONIC_SENSOR_ECHO_PIN) == \ config.NUMERIC_CONSTANT_ZERO: pulse_start = time.time() while gpio.input(config.ULTRASONIC_SENSOR_ECHO_PIN) == \ config.NUMERIC_CONSTANT_ONE: pulse_end = time.time() pulse_duration = pulse_end - pulse_start distance_in_cm = pulse_duration * 17150 # The speed of sound at sea level is 34300 cm/s # Hence the formula is 34300 * (time/2) = intruder_distance distance_in_cm = round(distance_in_cm, 2) utils.print_msg("Distance: %s cm" % distance_in_cm) return True, distance_in_cm else: utils.print_msg('Invalid range find parameter value. ' 'Given range_find is %s' % range_find) return False, None except Exception as e_obj: utils.print_msg('EXCEPTION: In range_finder(), due to %s' % e_obj) return False, None
def cloud_write(msg_or_system_reset_status, distance_in_cm, write_api_key, signal): """ Function name : cloud_write Parameter : msg_or_system_reset_status, distance_in_cm, write_api_key, signal Functionality : Writes the data into cloud server Function Invoked : utils.print_msg() Return Value : Returns True and timestamp on success, False and None on failure """ conn = None try: timestamp = datetime.datetime.now() if str(signal).upper() == config.STR_DATA: params = urllib.urlencode({ 'field1': str(msg_or_system_reset_status), 'field2': str(timestamp), 'field3': str(distance_in_cm), 'key': write_api_key }) else: params = urllib.urlencode({ 'field1': str(msg_or_system_reset_status), 'field2': str(timestamp), 'key': write_api_key }) headers = { "Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain" } conn = httplib.HTTPConnection("api.thingspeak.com:80") conn.request("POST", "/update", params, headers) utils.print_msg('Writing the data into cloud....') time.sleep(30) utils.print_msg('Data written to the cloud successful') return True, timestamp except Exception as e_obj: utils.print_msg('EXCEPTION: In cloud_write(), due to %s' % e_obj) return False, None finally: conn.close()
def index(): try: temp_last_distance_data = 0 while True: cloud_read_status, data = \ cloud.cloud_read(config.DATA_CHANNEL_ID, config.DATA_CHANNEL_READ_API_KEY) if cloud_read_status is True: utils.print_msg(data) system_cloud_read_status, system_data = \ cloud.cloud_read(config.SYSTEM_CHANNEL_ID, config.SYSTEM_CHANNEL_READ_API_KEY) if system_cloud_read_status is True: utils.print_msg(data) if temp_last_distance_data != data['field3']: return render_template('home.html', data_value=data, system_value=system_data) else: return redirect(url_for('index')) except Exception as e_obj: utils.print_msg('EXCEPTION: In index(), due to %s' % e_obj) return redirect(url_for('index'))
def system_reset_status_cloud_data_read(): """ Function name : system_reset_status_cloud_data_read Parameter : None Functionality : Reads the system reset status cloud data server Return Value : Returns system_reset_status on success, False on failure """ try: cloud_read_status, data = \ cloud_read(config.SYSTEM_CHANNEL_ID, config.SYSTEM_CHANNEL_READ_API_KEY) if cloud_read_status is True: utils.print_msg( config.STR_SYMBOL * config.NUMERIC_CONSTANT_FIFTEEN + " Intruder Alert System Reset Status " + config.STR_SYMBOL * config.NUMERIC_CONSTANT_FIFTEEN) system_reset_status = data['field1'] if config.NUMERIC_CONSTANT_ONE == int(system_reset_status): system_reset_string = config.STR_ON else: system_reset_string = config.STR_OFF utils.print_msg("System reset status: %s" % system_reset_string) # If value is 1, do reset last_system_reset_time = data['field2'] utils.print_msg("Last System reset time: %s" % last_system_reset_time) if config.STR_ON.lower() == str(system_reset_string).lower(): utils.print_msg("Updating the reset system information into" " cloud server") time.sleep(15) system_reset_status_check = \ system_reset_status_cloud_data_write(0) if system_reset_status_check is False: return False utils.print_msg(config.STR_SYMBOL_STAR * config.NUMERIC_CONSTANT_EIGHTY) return int(system_reset_status) else: return False except Exception as e_obj: utils.print_msg('EXCEPTION: In system_reset_status_cloud_data_read(), ' 'due to %s' % e_obj) return False
def main_execution(): """ Function name : main_execution Functionality : Checks the Intruder alert and finds its distance in cm Function Invoked : print_msg(), range_finder() Return value : None """ utils.print_msg('*' * config.NUMERIC_CONSTANT_SIXTY) utils.print_msg('\t\t\t\tIntruder Alert System') utils.print_msg('*' * config.NUMERIC_CONSTANT_SIXTY) try: area_safe_count = config.NUMERIC_CONSTANT_ZERO while True: pir_sensor_digital_data = gpio.\ input(config.PIR_SENSOR_DIGITAL_OUTPUT_PIN) if config.NUMERIC_CONSTANT_ONE == int( pir_sensor_digital_data ): # Compare dynamic value with constants utils.print_msg(config.STR_SYMBOL * config.NUMERIC_CONSTANT_THIRTY) utils.print_msg("\tBreach Alert by Intruder") utils.print_msg(config.STR_SYMBOL * config.NUMERIC_CONSTANT_THIRTY) gpio.output(config.LED_PIN, gpio.HIGH) utils.print_msg('LED Status: ON') utils.print_msg('Current Intruder status: %s' % config.STR_BREACH) distance_in_cm, range_find_status = \ range_finder(BOOLEAN_RANGE_FINDER) cloud_write_status = cloud.cloud_data_write( config.STR_BREACH, str(distance_in_cm)) cloud_read_status = cloud.cloud_data_read(config.STR_RECENT) area_safe_count = config.NUMERIC_CONSTANT_ZERO elif (config.NUMERIC_CONSTANT_ZERO == int(pir_sensor_digital_data))\ and area_safe_count == 0: area_safe_count = config.NUMERIC_CONSTANT_ONE utils.print_msg(config.STR_SYMBOL * config.NUMERIC_CONSTANT_THIRTY) utils.print_msg("\tArea Under Safe") utils.print_msg(config.STR_SYMBOL * config.NUMERIC_CONSTANT_THIRTY) utils.print_msg('LED Status: OFF') utils.print_msg('Current Intruder status: %s' % config.STR_SAFE) gpio.output(config.LED_PIN, gpio.LOW) cloud_write_status = cloud.\ cloud_data_write(config.STR_SAFE, str(config.NUMERIC_CONSTANT_ZERO)) cloud_read_status = cloud.cloud_data_read(config.STR_LAST) system_rest_status_check = \ cloud.system_reset_status_cloud_data_read() if config.NUMERIC_CONSTANT_ONE == system_rest_status_check: utils.print_msg(config.STR_SYMBOL * config.NUMERIC_CONSTANT_THIRTY) utils.print_msg("Intruder Alert System Force Restart") utils.print_msg(config.STR_SYMBOL * config.NUMERIC_CONSTANT_THIRTY) utils.print_msg( "Updating the reset data information into cloud" " server") cloud.cloud_data_write(config.STR_SAFE, config.NUMERIC_CONSTANT_ZERO) gpio.output(config.LED_PIN, gpio.LOW) utils.print_msg(config.STR_SYMBOL * config.NUMERIC_CONSTANT_THIRTY) utils.print_msg("System force reset") utils.print_msg(config.STR_SYMBOL * config.NUMERIC_CONSTANT_THIRTY) utils.print_msg('LED Status: OFF') utils.print_msg('Current Intruder status: %s' % config.STR_SAFE) utils.print_msg(config.STR_SYMBOL_STAR * config.NUMERIC_CONSTANT_EIGHTY) time.sleep(config.NUMERIC_CONSTANT_TEN ) # Loop iterates once in every 10 seconds except Exception as e_obj: utils.print_msg('EXCEPTION: In main_execution(), due to %s' % e_obj) finally: gpio.cleanup()
"Updating the reset data information into cloud" " server") cloud.cloud_data_write(config.STR_SAFE, config.NUMERIC_CONSTANT_ZERO) gpio.output(config.LED_PIN, gpio.LOW) utils.print_msg(config.STR_SYMBOL * config.NUMERIC_CONSTANT_THIRTY) utils.print_msg("System force reset") utils.print_msg(config.STR_SYMBOL * config.NUMERIC_CONSTANT_THIRTY) utils.print_msg('LED Status: OFF') utils.print_msg('Current Intruder status: %s' % config.STR_SAFE) utils.print_msg(config.STR_SYMBOL_STAR * config.NUMERIC_CONSTANT_EIGHTY) time.sleep(config.NUMERIC_CONSTANT_TEN ) # Loop iterates once in every 10 seconds except Exception as e_obj: utils.print_msg('EXCEPTION: In main_execution(), due to %s' % e_obj) finally: gpio.cleanup() # Execution begins here if __name__ == '__main__': try: main_execution() except Exception as e_obj: utils.print_msg('EXCEPTION: Fail to start the intruder alert ' 'system. Due to %s' % e_obj)