Rick KuhlmanElizabeth Adams
Published © GPL3+

OwnBooth - WeWork Phone Booth Occupancy using KEMET Sensor

Know before you go. Check before you trek. Get calling without stalling. See real-time phone booth occupancy from anywhere in the world.

IntermediateWork in progress12 hours827
OwnBooth - WeWork Phone Booth Occupancy using KEMET Sensor

Things used in this project

Hardware components

Proximity Sensor- Pyroelectric Infrared Sensor Module
KEMET Electronics Corporation Proximity Sensor- Pyroelectric Infrared Sensor Module
×1
Raspberry Pi Zero
Raspberry Pi Zero
×1
DIP Breakout Board 1mm Pitch
×1
2inch Cable - 455-3713-ND
×1
CONN HEADER SMD R/A 5POS 1MM- SM05B-SRSS-TB(LF)(SN)
×1
USB-A to Mini-USB Cable
USB-A to Mini-USB Cable
×1

Software apps and online services

Initial State
Initial State
Raspbian
Raspberry Pi Raspbian

Hand tools and fabrication machines

3D Printer (generic)
3D Printer (generic)
Soldering iron (generic)
Soldering iron (generic)
10 Pc. Jumper Wire Kit, 5 cm Long
10 Pc. Jumper Wire Kit, 5 cm Long
Solder Wire, Lead Free
Solder Wire, Lead Free

Story

Read more

Custom parts and enclosures

Main Housing

Internal Sensor Housing Cover

Main Housing Cover

Pins for Swivel Mount

Swivel Wall Mount

Swivel Component

Schematics

Connection Diagram: Sensor to pi Zero W

There is not a lot to the schematic here. It is essentially off-the-shelf boards and modules with some connectivity between

Code

Main Python Booth Monitoring Process

Python
This is main code for reading the sensor and streaming the readings to Initial State
from ISStreamer.Streamer import Streamer
import RPi.GPIO as GPIO
import time

# ---------Initial State And  User Settings ---------
SENSOR_LOCATION_NAME = "Phone Booth Motion"
BUCKET_NAME = "Booth Test"
BUCKET_KEY = "GQFX67G7L5JR"
ACCESS_KEY = "ist_XXXXXXXOsM77HqeXXXXXLMyXXXXXXXXXEJ4et"
OCCUPANCY_CHECK_INTERVAL = 80
# ---------------------------------

#Initialize Initial State Streamer
streamer = Streamer(bucket_name=BUCKET_NAME, bucket_key=BUCKET_KEY, access_key=ACCESS_KEY)

#Configure I/O on piZeroW
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11, GPIO.IN)

#Initialize some counters
motion_samples = 0
occupancy_check = 0
consecutive_check = 0

try:
        while True:
                #Sample GPIO at Loop Time which is set to 195ms
                if GPIO.input(11):
                        motion_samples = motion_samples + 1
                        consecutive_check = consecutive_check + 1
                        if consecutive_check >= 2:
                                motion_samples = motion_samples + 15
                                print("15")
                        print("+")
                else:
                        consecutive_check = 0
                        print("-")

                #Send Occupancy Samples after some time (set by OCCUPANCY_CHECK_INTERVAL).
                #The Sensitivity of occupied or not is abstracted to an Initial State Expression so it can be changed remotely
                occupancy_check = occupancy_check + 1
                if occupancy_check >= OCCUPANCY_CHECK_INTERVAL:
                        streamer.log(SENSOR_LOCATION_NAME,motion_samples)
                        occupancy_check = 0
                        motion_samples = 0
                        streamer.flush()
                time.sleep(.195)
except KeyboardInterrupt:
        GPIO.cleanup()

Python Process Monitoring

Python
This code can monitor a process in python. For our application this is a separate process that monitor the health of our main Booth monitoring process. Also this code stream the IP Address of the device (especially handy if DHCP dynamically changes it).
import psutil
import time
import sys
from ISStreamer.Streamer import Streamer
from subprocess import PIPE, Popen

# --------- User Settings ---------
# Initial State settings
BUCKET_NAME = "Booth Test"
BUCKET_KEY = "GQFX67G7L5JR"
ACCESS_KEY = "ist_XXXXXXp9k6OsM77HXXXXXXXsALfrXXXXXXXt"
PROCESS_NAME = "Booth Process"
# Set the time between checks
MINUTES_BETWEEN_READS = 1
# ---------------------------------

def main():
        if len(sys.argv) != 2:
                print "Usage: " + sys.argv[0] + " <pid>"
                exit()
        pid = sys.argv[1]

        streamer = Streamer(bucket_name=BUCKET_NAME, bucket_key=BUCKET_KEY, access_key=ACCESS_KEY)
        if psutil.pid_exists(int(pid)) == False:
                print "Error: That process doesn't exist! Exiting ..."
                exit()
        else:
                streamer.log(PROCESS_NAME,"Running")
                streamer.flush()

        while True:
                if psutil.pid_exists(int(pid)) == False:
                        streamer.log(PROCESS_NAME,"Exited")
                        streamer.flush()
                        exit()
                else:
                        streamer.log(PROCESS_NAME,"Running")
                        process = Popen(['hostname', '-I'], stdout=PIPE)
                        output, _error = process.communicate()
                        streamer.log(PROCESS_NAME + " IP Address", output.rstrip())
                        streamer.flush()
                time.sleep(60*MINUTES_BETWEEN_READS)

if __name__ == "__main__":
    main()

Reboot Monitor

Python
This code will stream "exited" after a reboot. You can use Initial State to send you a text message if you care that your device rebooted.
import psutil
import time
import sys
from ISStreamer.Streamer import Streamer

# --------- User Settings ---------
# Initial State settings
BUCKET_NAME = "Booth Test"
BUCKET_KEY = "GQFX67G7L5JR"
ACCESS_KEY = "ist_RGs3sp9k6OsM77HqeYBLMysALfrEJ4et"
PROCESS_NAME = "Booth Process"
# Set the time to wait until you are sure reboot is complete and network connections are restored (i.e. power outage)
MINUTES_DELAY = 5
# ---------------------------------

def main():
        # Wait for ntpd to run for sync of the clock
        found_ntpd = False
        cnt = 0
        while found_ntpd == False:
                for proc in psutil.process_iter():
                        if proc.name() == "ntpd":
                                found_ntpd = True 
                cnt += 1
                if cnt == 60: # assume that ntpd has already run if not found in 60 seconds
                        found_ntpd=True
                time.sleep(1)

        time.sleep(60*MINUTES_DELAY)
        streamer = Streamer(bucket_name=BUCKET_NAME, bucket_key=BUCKET_KEY, access_key=ACCESS_KEY)
        streamer.log(PROCESS_NAME,"Exited")
        streamer.flush()

if __name__ == "__main__":
    main()

BASH Script to Launch all scripts

BatchFile
This file can launch your script and process monitoring script. Use this tutorial to get it automated on boot -https://github.com/initialstate/pi-process-dashboard
#!/bin/bash
# --------- User Settings ---------
PROCESS2RUN="sudo python /home/pi/booth/booth_monitor.py"
MONITOR_SCRIPT="/home/pi/booth/monitor_process.py"
# ---------------------------------
nohup $PROCESS2RUN &
VAR=`pgrep -f "$PROCESS2RUN"`
echo $VAR
nohup python $MONITOR_SCRIPT $VAR &

Credits

Rick Kuhlman

Rick Kuhlman

2 projects • 2 followers
Elizabeth Adams

Elizabeth Adams

16 projects • 81 followers
DIY, Tech, Data

Comments