Secret Chest

by jeust in Circuits > Raspberry Pi

199 Views, 0 Favorites, 0 Comments

Secret Chest

20210530_154405.jpg

I decided to create a chest, with a secret camera and a Raspberry Pi. This way if anyone wants to break into your chest and steals your item of choice, there will be a picture taken of the intruder. A message can be send to your phone with the use of if this then that.

When you press A, you can put in your password. If the outcome of the password is incorrect, the camera will take a picture, and the Pi will send you a message on your phone.

When you press B, the box will be locked. Then when the Sonar sensor senses that the chest has been opened, It will send messages to your phone, and also take pictures.

The Pi will save the pictures on the sd-card. But using a network storage (I used my phone), you can look at the pictures from your phone/pc.

Supplies

For this build I planned on using the PiCam, but that one broke last minute so I had to change to the webcam.

- Raspberry Pi

- USB webcam

- Sonar sensor

- Wood

- 4x4 Keypad

- Paint (Optional)

Iterations

Screenshot 2021-05-30 160001.png
Screenshot 2021-05-30 160734.png
Screenshot 2021-05-30 161940.png
Screenshot 2021-05-30 162248.png

I began this project, thinking I would use an Arduino. I had bought and camera module and decided to use a Sonar. When the parts were on their way my plan became complicated. The camera had a lot of pins, and the code for making it work was very hard to understand and very long.

I decided to ask a question to Ferdy (My teacher), and he recommended for me to use a raspberry Pi. This way I could use the Picam and make pictures relatively easily.

I got to make it work with the setup of the second picture.

I used these tutorials (3rd and 4th picture) to get the main code down & connect to the Pi. I tweaked the code and made it work.

On the day before the deadline, the Picam broke. This is why I now use an usb webcam, instead of the Picam. If you're gonna do it yourself, I would still recommend using the Picam, because it was less of a hassle to set up.

The next job for me was to build a chest

Building a Chest With a Secret Compartment + Connecting Everything

20210530_155518.jpg
20210530_163848.jpg
20210530_155526.jpg
Screenshot 2021-05-30 164021.png

First you build a chest with a secret compartment. the secret compartment should be at least 4cm high. The length and width depend on the length of the cables you're gonna use. Personally my box is 14x20 cm. Of course this is not a very big chest. I would recommend making your chest at least the size my box is. Otherwise you would have nearly no space inside the chest. Also drill one hole from the secret compartment (For wires) into the chest, and one hole out of the chest for the wires of the Pi.

In the code tab you will see which pins to connect, the only pins that are not in the code is from the sonar sensor. You should connect the ground wire to the ground on the pi, and the vcc to the 5v pin.

Code

# here you import all needed librariesimport RPi.GPIO as GPIOfrom datetime import datetimeimport timeimport requestsimport osfrom multiprocessing import Process, Manager	GPIO.setwarnings(False)#pin number instead of GPIO numbersGPIO.setmode(GPIO.BOARD)			# the chest starts unlocked
unlocked = True class keypad(): def __init__(self, columnCount = 3): # CONSTANTS if columnCount == 3: self.KEYPAD = [ [1,2,3], [4,5,6], [7,8,9], ["*",0,"#"] ] self.ROW = [26,24,23,22] self.COLUMN = [21,19,10] # mapping for the 4x4 keypad elif columnCount == 4: self.KEYPAD = [ [1,2,3,"A"], [4,5,6,"B"], [7,8,9,"C"], ["*",0,"#","D"] ]# this is where you put your pins if you have a 4x4 keypad self.ROW = [3,5,7,29] self.COLUMN = [31,26,24,21] else: return def getKey(self): # Set all columns as output low for j in range(len(self.COLUMN)): GPIO.setup(self.COLUMN[j], GPIO.OUT) GPIO.output(self.COLUMN[j], GPIO.LOW) # Set all rows as input for i in range(len(self.ROW)): GPIO.setup(self.ROW[i], GPIO.IN, pull_up_down=GPIO.PUD_UP) # Scan rows for pushed key/button # A valid key press should set "rowVal" between 0 and 3. rowVal = -1 for i in range(len(self.ROW)): tmpRead = GPIO.input(self.ROW[i]) if tmpRead == 0: rowVal = i # if rowVal is not 0 thru 3 then no button was pressed and we can exit if rowVal <0 or rowVal >3: self.exit() return # Convert columns to input for j in range(len(self.COLUMN)): GPIO.setup(self.COLUMN[j], GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # Switch the i-th row found from scan to output GPIO.setup(self.ROW[rowVal], GPIO.OUT) GPIO.output(self.ROW[rowVal], GPIO.HIGH) # Scan columns for still-pushed key/button # A valid key press should set "colVal" between 0 and 2. colVal = -1 for j in range(len(self.COLUMN)): tmpRead = GPIO.input(self.COLUMN[j]) if tmpRead == 1: colVal = j # if colVal is not 0 thru 2 then no button was pressed and we can exit if colVal < 0 or colVal > 3: self.exit() return # Return the value of the key pressed self.exit() return self.KEYPAD[rowVal][colVal] def exit(self): # Reinitialize all rows and columns as input at exit for i in range(len(self.ROW)): GPIO.setup(self.ROW[i], GPIO.IN, pull_up_down=GPIO.PUD_UP) for j in range(len(self.COLUMN)): GPIO.setup(self.COLUMN[j], GPIO.IN, pull_up_down=GPIO.PUD_UP) # using ifttt send a http post requestdef wrong_code_ifttt_notification(): requests.post("https://maker.ifttt.com/trigger/wrong_code/with/key/"<Put your key here>"")def box_opened_ifttt_notification(): requests.post("https://maker.ifttt.com/trigger/box_opened/with/key/"<Put your key here>"")def take_a_picture():# it gets a new string for naming the picture files now = datetime.now() current_time = now.strftime("%Y%m%d%H%M%S") filepath = '/home/pi/pictures/Security_' + current_time + '.jpg' # This is a bashcommand that takes the picture, you can set the resolution here, and the filepath. command = 'fswebcam -r 1920x1080 --no-banner ' + filepath# execute the bash command os.system(command) print('Its taking a picture')def verify_password(kp): ###### 4 Digit wait ###### seq = [] while len(seq) != 4 or len(seq) > 4: digit = None while digit == None: digit = kp.getKey()# The keys for the password can only be integers if digit != None and isinstance(digit, int): seq.append(digit) time.sleep(0.5) # every 0.5s the keypad checks if you pressed the buttons
# Check digit code l_unlocked = False if seq == [1, 2, 3, 4]: # set your password here, only numbers work print("Code accepted") l_unlocked = True else: print("Wrong Code") take_a_picture() wrong_code_ifttt_notification() return l_unlockeddef run_box(p_unlocked): kp = keypad(columnCount=4) while True: digit = None # request the box to verify password run_key = "A" #the b buttons locks the chest lock_key = "B" l_unlocked = p_unlocked.value while digit == None: digit = kp.getKey() p_unlocked.value = l_unlocked time.sleep(0.2) if l_unlocked == False and digit == run_key: print('Please put in your password') l_unlocked = verify_password(kp) if digit == lock_key: print('The box is locked!') l_unlocked = False p_unlocked.value = l_unlockeddef get_distance(p_distance): while True: #pin number for the trigger pin TRIG = 23 #pin number for the echo pin ECHO = 19 GPIO.setup(TRIG,GPIO.OUT) GPIO.setup(ECHO,GPIO.IN) GPIO.output(TRIG, False) #asks for distance every 2 seconds time.sleep(2) GPIO.output(TRIG, True) time.sleep(0.00001) 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 l_distance = pulse_duration * 17165 l_distance = round(l_distance, 1) #print('Distance: ', l_distance, 'cm') p_distance.value = l_distance# the whole process when the pi is runningif __name__ == '__main__': #kp = keypad(columnCount=4) running = True manager = Manager() unlocked = manager.Value('unlocked', unlocked) distance = manager.Value('distance', 0) p_run_box = Process(target=run_box, args=(unlocked,)) p_get_distance = Process(target=get_distance, args=(distance,)) p_get_distance.start() p_run_box.start() currently_unlocked = not unlocked.value while running: time.sleep(2) if distance.value > 10 and unlocked.value == False and currently_unlocked == unlocked.value: // you can change this number (10) depending on how high your chest is. box_opened_ifttt_notification() take_a_picture() currently_unlocked != unlocked.value

Downloads

Small Reflection + End Product

raspberry pi chest

I'm very happy with how the project turned out, and with how much I learned doing this project. I had no idea how Python worked, and how to work with and set up a Raspberry Pi. Now I feel confident in being able to make projects work, and I am more confident in writing code. When I was in high school, I got thought how to solder, and I enjoyed doing it again. It went better than expected.