#!/usr/bin/env python
# from datetime import datetime
import RPi.GPIO as GPIO
import time
import mysql.connector as mysql
from smbus2 import SMBus
from mlx90614 import MLX90614
from gpiozero import Buzzer, LED
from picamera import PiCamera
from evdev import InputDevice
from select import select
from luma.led_matrix.device import max7219
from luma.core.interface.serial import spi, noop
from luma.core.render import canvas
from luma.core.virtual import viewport
from luma.core.legacy import text, show_message, textsize
from luma.core.legacy.font import proportional, CP437_FONT, TINY_FONT, SINCLAIR_FONT, LCD_FONT
bus = SMBus(1)
sensor = MLX90614(bus, address=0x5A)
serial = spi(port=0, device=0, gpio=noop())
device = max7219(serial, cascaded=4, block_orientation=-90)
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
TRIG = 18
ECHO = 23
# buzzer = 25
# led_blue = 17
# led_yellow = 27
# led_red = 22
led_blue = LED(17)
led_yellow = LED(27)
led_red = LED(22)
buzzer = Buzzer(25)
pir_sensor = 4
relay_alarm = 9
current_state = 0
GPIO.setup(TRIG, GPIO.OUT)
GPIO.setup(ECHO, GPIO.IN)
GPIO.setup(pir_sensor, GPIO.IN)
GPIO.setup(relay_alarm, GPIO.OUT)
print("Initialzing PIR Sensor......")
time.sleep(1)
print("PIR Ready...")
print(" ")
class Security:
global dbHost
global dbUser
global dbPass
global dbNameEmp
global dbNameSecure
dbHost = 'localhost'
dbUser = 'root'
dbPass = 'password'
dbNameEmp = 'empinfo'
dbNameSecure = 'secureinfo'
def __init__(self, devIn, devOut):
self.devIn = devIn
self.devOut = devOut
def returnPirSensor(self):
GPIO.input(pir_sensor, False)
# ฟังชั่นสำหรับบันทึกการ-เข้า
def EntranceIn(self):
keys = "X^1234567890XXXXqwertzuiopXXXXasdfghjklXXXXXyxcvbnmXXXXXXXXXXXXXXXXXXXXXXX"
dev = InputDevice('/dev/input/event0')
rfid_presented = ""
while True:
r, w, x = select([dev], [], [])
for event in dev.read():
if event.type == 1 and event.value == 1:
if event.code == 28:
dbConnEmp = mysql.connect(
host=dbHost, user=dbUser, passwd=dbPass, db=dbNameEmp)
curEmp = dbConnEmp.cursor()
rfid_presented = rfid_presented.replace("X", "")
print(rfid_presented)
curEmp.execute(
"Select RFidEmp FROM RFidMaster WHERE (RFidStatus = 1) AND RFidNumber = '%s'" % (rfid_presented))
resultRFid = curEmp.fetchone()
if curEmp.rowcount != 1:
print(" ")
print("ACCESS DENIED")
with canvas(device) as draw:
text(draw, (0, 0), "None", fill="white",
font=proportional(CP437_FONT))
time.sleep(1)
device.clear()
else:
dbConnSecure = mysql.connect(
host=dbHost, user=dbUser, passwd=dbPass, db=dbNameSecure)
curSecure = dbConnSecure.cursor()
curSecure.execute(
"SELECT EntraId,DATE_FORMAT(CreateDate, '%Y%m%d') AS CreateDate FROM entranlog ORDER BY EntraId DESC, CreateDate DESC LIMIT 1")
resultKey = curSecure.fetchone()
KeyDate = time.strftime("%Y%m%d")
if curSecure.rowcount < 0:
KeyId = KeyDate+"0001"
else:
if resultKey[1] == KeyDate:
iSum = int(resultKey[0])
def add(x, y):
return x + y
KeyId = ('%d' % add(iSum, 1))
else:
KeyId = KeyDate+"0001"
create_date = time.strftime(
'%Y-%m-%d %H:%M:%S')
insertSecure = "INSERT INTO entranlog (EntraId,EntraAction,EntraStatus,CreateBy,CreateDate) VALUES (%s,%s,%s,%s,%s)"
valSecure = (
KeyId, 1, 1, resultRFid[0], create_date)
curSecure.execute(insertSecure, valSecure)
dbConnSecure.commit()
print(" ")
print("Welcome " + resultRFid[0])
show_message(device, resultRFid[0], fill="white", font=proportional(
CP437_FONT), scroll_delay=0.04)
print("Temperature: %.1fc" %
(sensor.get_object_1()))
rfid_presented = ""
else:
rfid_presented += keys[event.code]
# ฟังชั่นสำหรับบันทึกการ-ออก
def EntranceOut(self):
keys = "X^1234567890XXXXqwertzuiopXXXXasdfghjklXXXXXyxcvbnmXXXXXXXXXXXXXXXXXXXXXXX"
dev = InputDevice('/dev/input/event1')
rfid_presented = ""
while True:
r, w, x = select([dev], [], [])
for event in dev.read():
if event.type == 1 and event.value == 1:
if event.code == 28:
dbConnEmp = mysql.connect(
host=dbHost, user=dbUser, passwd=dbPass, db=dbNameEmp)
curEmp = dbConnEmp.cursor()
rfid_presented = rfid_presented.replace("X", "")
print(rfid_presented)
curEmp.execute(
"Select RFidEmp FROM RFidMaster WHERE (RFidStatus = 1) AND RFidNumber = '%s'" % (rfid_presented))
resultRFid = curEmp.fetchone()
if curEmp.rowcount != 1:
print(" ")
print("ACCESS DENIED")
with canvas(device) as draw:
text(draw, (0, 0), "None", fill="white",
font=proportional(CP437_FONT))
time.sleep(1)
device.clear()
else:
dbConnSecure = mysql.connect(
host=dbHost, user=dbUser, passwd=dbPass, db=dbNameSecure)
curSecure = dbConnSecure.cursor()
curSecure.execute(
"SELECT EntraId,DATE_FORMAT(CreateDate, '%Y%m%d') AS CreateDate FROM entranlog ORDER BY EntraId DESC, CreateDate DESC LIMIT 1")
resultKey = curSecure.fetchone()
KeyDate = time.strftime("%Y%m%d")
if curSecure.rowcount < 0:
KeyId = KeyDate+"0001"
else:
if resultKey[1] == KeyDate:
iSum = int(resultKey[0])
def add(x, y):
return x + y
KeyId = ('%d' % add(iSum, 1))
else:
KeyId = KeyDate+"0001"
create_date = time.strftime(
'%Y-%m-%d %H:%M:%S')
insertSecure = "INSERT INTO entranlog (EntraId,EntraAction,EntraStatus,CreateBy,CreateDate) VALUES (%s,%s,%s,%s,%s)"
valSecure = (
KeyId, 2, 1, resultRFid[0], create_date)
curSecure.execute(insertSecure, valSecure)
dbConnSecure.commit()
print(" ")
print("Welcome " + resultRFid[0])
show_message(device, resultRFid[0], fill="white", font=proportional(
CP437_FONT), scroll_delay=0.04)
print("Temperature: %.1fc" %
(sensor.get_object_1()))
rfid_presented = ""
else:
rfid_presented += keys[event.code]
# ฟังชั่นสำหรับวัดอุณหภูมิ
def PIRSensor(self):
while True:
GPIO.output(TRIG, False)
time.sleep(0.5)
GPIO.output(TRIG, True)
time.sleep(0.01)
GPIO.output(TRIG, False)
while GPIO.input(ECHO) == 0:
pulse_start = time.time()
while GPIO.input(ECHO) == 1:
pulse_end = time.time()
pulse_duration = pulse_end - pulse_start
distance = pulse_duration * 11150
distance = round(distance, 2)
if distance > 0 and distance < 4:
print("Distance: %scm" % (distance))
# print "Ambient Temperature :", sensor.get_ambient()
print("Temperature: %.1fc" % (sensor.get_object_1()))
print(" ")
msgTemp = ("%.1f" % (sensor.get_object_1()))
w, h = textsize(msgTemp, font=proportional(CP437_FONT))
if w <= device.width:
x = round((device.width - w) / 2)
with canvas(device) as draw:
text(draw, (x, 0), msgTemp, fill="white",
font=proportional(CP437_FONT))
else:
show_message(device, msgTemp, fill="white", font=proportional(
CP437_FONT), scroll_delay=0.04)
GPIO.output(buzzer, GPIO.HIGH)
time.sleep(0.3)
GPIO.output(buzzer, GPIO.LOW)
GPIO.output(led_yellow, GPIO.LOW)
else:
GPIO.output(led_yellow, GPIO.HIGH)
time.sleep(0.5)
# ฟังชั่นสำหรับตรวจจับการเคลื่อนไหว
def PIRSensor(self):
while True:
led_blue.on()
time.sleep(0.1)
current_state = GPIO.input(pir_sensor)
if current_state == 1:
GPIO.output(relay_alarm, True)
led_blue.off()
led_red.blink(0.1, 0.2)
buzzer.beep(0.5, 0.25, 2)
time.sleep(5)
GPIO.output(relay_alarm, False)
led_red.off()
led_yellow.blink(0.5, 0.5)
time.sleep(5)
led_yellow.off()