Timothy Bargo
Created March 14, 2018

LANETECH HS - PCL - Remote and Motion Controlled Computer

This project was part of the Lane Tech HS Physical Computing Lab coure.

37
LANETECH HS - PCL - Remote and Motion Controlled Computer

Things used in this project

Hardware components

Raspberry Pi 1 Model B+
Raspberry Pi 1 Model B+
×1
PIR Motion Sensor (generic)
PIR Motion Sensor (generic)
×1
General Purpose Transistor NPN
General Purpose Transistor NPN
×1
Relay
×1

Story

Read more

Code

AutoCompSleep.py

Python
The main script to that manages the sensor, and determines whether to wake or sleep
!/usr/bin/env python

import RPi.GPIO as GPIO
import time
import subprocess

GPIO.setmode(GPIO.BCM)

GPIO.setup(23, GPIO.IN)
GPIO.setup(24, GPIO.OUT)

global relayState
relayState = 24    #The relay is controlled with pin 24 of the Raspberry Pi
GPIO.output(relayState, False)

global PIRState
PIRState = 23    #The motion sensor state is received with pin 23 of the Raspberry Pi


global timeUntilSleep
timeUntilSleep = 2 #this value * 5 is the amount of seconds until the system will sleep, 180 is 30 minutes

global motionSensed
motionSensed = False


def sleepMode():
        print("sleep")
        GPIO.output(24, True)
        subprocess.Popen(["bash", "computerSleep.sh"])
def wakeMode():
        print("wake")
        GPIO.output(relayState, False)
        subprocess.Popen(["bash", "computerWake.sh"])

try:
        time.sleep(2) # to stabilize sensor
        while True:
                if timeUntilSleep < 0 and GPIO.input(PIRState):
                        timeUntilSleep = 180
                        wakeMode()
                        print("reset from sleep")
                elif GPIO.input(PIRState) and not motionSensed:
                        motionSensed = True
                        timeUntilSleep = 180
                        print("motion sensed")
                elif timeUntilSleep == 0:
                        sleepMode()
                        print("countdown equals 0, sleep")
                        timeUntilSleep = timeUntilSleep - 1
               else:
                        time.sleep(5)
                        timeUntilSleep = timeUntilSleep - 1
                        print timeUntilSleep*5,"seconds until sleep"
                        motionSensed = False

        print "loop exited"


except:
        GPIO.cleanup()
        print("error")

computerSleep.sh

Plain text
A Bash script.
Sleeps the computer upon run.
#!/bin/bash

#ssh into user at computer, and suspend the system

ssh -t tuser@192.168.1.219 "sudo systemctl suspend"

computerWake.sh

Plain text
A Bash script.
Wakes the computer upon run.
#!/bin/bash

MAC=94:de:80:67:fc:77    #MAC address of computer to wake

sudo etherwake -i eth0 $MAC

start.sh

Plain text
A Bash script.
Run by cron at system boot to start the whole program.
#!/bin/bash
python /home/pi/MotionControlledComputer/AutoCompSleep.py

Credits

Timothy Bargo
1 project • 0 followers

Comments