Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
Tasos
Created February 22, 2020 © GPL3+

Guard tour monitoring system

Main component of the system is the Kemet Pyroelectric sensor with WiFi connectivity to a Gateway, thus forming a network of checkpoints.

IntermediateFull instructions provided24 hours38
Guard tour monitoring system

Things used in this project

Story

Read more

Schematics

Gerber Files for Guard Monitoring PCB

Gerber Files for PCB production

Guard Monitor PCB BOM

Bill of Materials

Guard Monitor PCB Schematic

PCB Schematic

Code

tw1.py

Python
Use a Kemet Pyroelectric sensor to Detect, and then characterize, and upload a Proximity event to a Cloud service.
import utime, ubinascii, urequests, esp32
from machine import I2C, ADC, Pin
from ntptime import settime
from umqtt.simple import MQTTClient

#Time management functions ---------------------------------

def show_time(tm):
	#now = utime.localtime()
	hour=tm[3]
	minu=tm[4]
	sec=tm[5]
	print("Current time is:   ",hour+2,":",minu,":",sec)

def new_shift(tm):
	hour=tm[3]+2
	minu=tm[4]
	secs=tm[5]
	count=1
	print("Creating Log File")
	f = open ("Shiftlog.txt", "w")
	f.write("%d\n" % hour)
	f.write("%d\n" % minu)
	f.write("%d\n" % secs)
	f.close()
	f=open("Counter.txt", "w")
	f.write("%d\n" % count)
	f.write("%d\n" % hour)
	f.write("%d\n" % minu)
	f.write("%d\n" % secs)
	f.close()

def record_time(tm, ctr):
	#tm = utime.localtime()
	hour=tm[3]+2
	minu=tm[4]
	secs=tm[5]
	f=open("Counter.txt", "w")
	f.write("%d\n" % ctr)
	f.write("%d\n" % hour)
	f.write("%d\n" % minu)
	f.write("%d\n" % secs)
	f.close()	
	
try:
	settime()
	utime.sleep(0.015)
	now = utime.localtime()
	show_time(now)
	utime.sleep(0.015)
	
	
except:
	print("No Internet, Time not Set")
	

interupt1 = Pin(25, mode = Pin.IN)
esp32.wake_on_ext0(pin = interupt1, level = esp32.WAKEUP_ANY_HIGH)	

tour_int=900	# 15 minutes
margin=int(tour_int/5)		# 20%
first_m=int(tour_int/6)
second_m=int(tour_int/2)
tour_max=tour_int+margin
tour_min=tour_int-margin

# MQTT Parameters -----------------------------------------------

SERVER = "mqtt.thingspeak.com"
client = MQTTClient("umqtt_client", SERVER)
topic = "channels/" + "952328" + "/publish/" + "AJPBJH3R0Q5XU2A9"

msg="None"

# Wait for Event -----------------------------------------------

try:
	import machine
	dat=machine.reset_cause()
	if dat==1:
		print("*******************")
		print("New Shift")
		now = utime.localtime()
		new_shift(now)
	else:	
		print("Reset Cause: ", dat)
		print("Board must be Reset")
		machine.reset()
except:
		print("Serious error occured")
	
while True:
	
			try:
					import machine, network
					nic = network.WLAN(network.STA_IF)
					nic.disconnect()
					utime.sleep(1.0)
					print("Sleeping....")
					utime.sleep(0.15)
					machine.lightsleep()
					print("Up Again....")
					event=0
					f = open ("Counter.txt")
					ct=f.readline().rstrip("\n")
					hr=f.readline().rstrip("\n")
					mn=f.readline().rstrip("\n")
					sc=f.readline().rstrip("\n")
					f.close()
					print("Calculating Wakeup Time...")
					nw = utime.localtime()
					hour=nw[3]+2
					minu=nw[4]
					sec=nw[5]
					print("Read Previous...")
					thr1=hour-int(hr)
					thr2=minu-int(mn)
					thr3=sec-int(sc)
					print("Subtracted...")
					diff=thr1*3600+thr2*60+thr3	#In seconds
					print("Difference is: ", diff)
					ctr=int(ct)
					# record_time(nw, ctr)
					
# Characterize Event  -----------------------------------------------
					if diff>tour_min and diff<tour_max:
						#report Guard Event 
						ctr=int(ct)+1
						record_time(nw, ctr)
						msg="Guard Event"
						print(msg)
						event=1
					if diff>tour_max:
						if diff>tour_max and diff<tour_max+first_m:
							#report Late Guard
							ctr=int(ct)+1
							record_time(nw, ctr)
							msg="Late Guard"
							print(msg)
							event=2
						if diff>tour_max+first_m or diff==tour_max+first_m:
							#report Guard Missed Checkin
							ctr=int(ct)+1
							record_time(nw, ctr)
							msg="Guard Missed Checkin"
							print(msg)
							event=3
					if diff<tour_min:
						if diff>tour_min-first_m:
							#report Early Guard
							ctr=int(ct)+1
							record_time(nw, ctr)
							msg="Early Guard"
							print(msg)
							event=4
						if diff<tour_min-first_m or diff==tour_min-first_m:
							#Do nothing
							msg="Ignoring Event xxxxxxxxxxxxxxxxxxxxxxxxxx"
							print(msg)
							msg="Spurious"
							
					print("Event is:  ", event)
					if not nic.isconnected():
						print("Connecting to network...")
						nic.active(True)
						nic.connect("WIND_2.4G_5AC9B3", "15F32F093C") # Connect to an AP
						while not nic.isconnected():
							pass
							
# Transmit Event accordingl  ----------------------------------------------
					print("Connecting to network SUCCESS")
					utime.sleep(1)
					l=True
					count=0
					payload = "field1="+str(ctr)+"&field2="+str(event)+"&field3="+str(event)+"&field4="+str(diff)
					
					#print("Payload Ready")
					while(l):
						try:
							print('Data Transmit try:', count)
							client.connect()
							client.publish(topic, payload)
							client.disconnect() 
							l=False
						except:
							count=count+1
							if count>5:
								l=False
							print('Data was not transmitted...')
							utime.sleep(5)
			except:
					print("Light Sleep Failed")	
				
						

Credits

Tasos

Tasos

11 projects • 2 followers

Comments