Things used in this project

Parts & Materials:
Pi 3 02
Raspberry Pi 3 Model B
×1
13285 01
PIR Motion Sensor (generic)
×1
11026 02
Jumper wires (generic)
×5
11868 00a
Raspberry Pi Camera module
×1
Software apps and online services:
Eipgsd2k
Twitter

Code

Code snippet #3Plain text
LED = 26
PIR = 4
GPIO.setmode(GPIO.BCM)
GPIO.setup(PIR, GPIO.IN)
GPIO.setup(LED, GPIO.OUT)
GPIO.output(LED, GPIO.LOW)
Code snippet #7Plain text
def BlinkLed(OnOff, OffOn):
    if BlinkLED == 1:
        GPIO.output(LED, OnOff)
        time.sleep(.5)
        GPIO.output(LED, OffOn)
        time.sleep(.5)
Code snippet #8Plain text
def TakePicture():
    PathToFile = WorkDir + "/mypic.JPG"
    for i in range(0,4):
        BlinkLed(0, 1) # Blink off then on
    camera.capture(PathToFile)
    GPIO.output(LED, 0) # led off
Code snippet #9Plain text
def GetStringID():
    PathToFile = WorkDir + "/stringID.txt"
    try:
        file = open(PathToFile, "r")
        NUMBER = int(file.read())
        file.close()
    except IOError:
        NUMBER = 1
    return NUMBER
Code snippet #10Plain text
def SaveNewStringID(id_string):
    PathToFile = WorkDir + "/stringID.txt"
    file = open(PathToFile, "w")
    file.write(id_string)
    file.close()
Code snippet #12Plain text
def SendStatus(user_name):
    if BlinkLED == 1:
        BLINK = "True"
    else:
        BLINK = "False"
    if motionDetection == 1:
        MD = "ON"
    else:
        MD = "OFF"
    tweet = "@" + user_name + " " + str(time.strftime("%c")) \
    + "\nFlip = " + str(camera.vflip) \
    + "\nBlink LED = " + BLINK \
    + "\nThread Running = " + ThreadRunning \
    + "\nMotion Detection = " + MD
    status = api.update_status(status=tweet)
Code snippet #13Plain text
def UpdateUsers(newUser):
    PathToFile = WorkDir + "/Authorized.txt"
    Authorized.append(newUser)
    file = open(PathToFile, "w")
    file.write("")
    file.close
    file = open(PathToFile, "a")
    for u in Authorized:
        file.write(u + "\n")
    file.close()
Code snippet #14Plain text
def CamFlip():
    if camera.vflip == True:
        camera.vflip = False
    else:
        camera.vflip = True
Code snippet #15Plain text
def CheckForCommands(commandString, user_name):
    global BlinkLED
    global motionDetection
    global stopThreads
    tmpList = commandString.split(" ")
    cmdString = tmpList[0]
    cmdList = cmdString.split(":")
    if "cmd" in cmdList:
        if "adduser" in cmdList:
            UpdateUsers(cmdList[2])
            return 0
        elif "flip" in cmdList:
            CamFlip()
            return 1
        elif "blinkoff" in cmdList:
            BlinkLED = 0
            return 0
        elif "blinkon" in cmdList:
             BlinkLED = 1
            return 0
        elif "alerton" in cmdList:
            BlinkLED = 0
            motionDetection = 1
            SendStatus(user_name)
            return 0
        elif "alertoff" in cmdList:
            BlinkLED = 1
          motionDetection = 0
            SendStatus(user_name)
            return 0
        elif "status" in cmdList:
            SendStatus(user_name)
            return 0
        elif "reboot" in cmdList:
            GPIO.cleanup()
           os.system("reboot")
        elif "shutdown" in cmdList:
            GPIO.cleanup()
            os.system("shutdown now")
        elif "stop" in cmdList:
            stopThreads = 1
            GPIO.cleanup()
            exit()
        else:
            return 0
    else:
        return 1
Code snippet #16Plain text
def MonitorTweets():
    global id_string
    while True:
        search_text = "#YOURHASHTAG"
        search_result = api.search(search_text, rpp=1, since_id=id_string)
        for i in search_result:
            id_string = i.id_str
            SaveNewStringID(id_string)
            tweet = api.get_status(id_string)
           user_name = tweet.user.screen_name
          if user_name in Authorized:
                if user_name in SuperUser:
                    check = (CheckForCommands(i.text,user_name))
                else:
                    check = 1
                if check == 1:
                    TakePicture()
                    SendReply(user_name)
        BlinkLed(1,0) # Blink on then off
        time.sleep(30)
        BlinkLed(1, 0)
        time.sleep(28)
Code snippet #17Plain text
def SecurityAlert():
    global ThreadRunning
    while True:
        try:
            if stopThreads == 1:
                exit()
            if motionDetection == 1:
                if GPIO.input(PIR):
                    time.sleep(1)
                    TakePicture()
                    SendReply(SuperUser[0])
                    time.sleep(60)
        except:
            ThreadRunning = 'NO'
            SendReply(SuperUser[0])
            exit()
Code snippet #18Plain text
CONSUMER_KEY = 'YOUR CONSUMER KEY GOES HERE'
CONSUMER_SECRET = 'YOUR CONSUMER SECRET GOES HERE'
ACCESS_KEY = 'YOUR ACCESS KEY GOES HERE'
ACCESS_SECRET = 'YOUR ACCESS SECRET GOES HERE'

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)
Code snippet #21Plain text
try:
    PathToFile = WorkDir + "/Authorized.txt"
    Authorized = []
    with open(PathToFile) as file:
        for line in file:
            line = line.strip()
            Authorized.append(line)
        file.close()
 except IOError:
     Authorized = [SuperUser]
Code snippet #25Plain text
try:
    stopThreads = 0
    securityalert = threading.Thread(name='SecurtiyAlert', target=SecurityAlert)
    securityalert.start()
    MonitorTweets()
except KeyboardInterrupt:
    stopThreads = 1
    GPIO.cleanup()
    exit()

Credits

Img 2559 eddbntlfja
Eric Traver

Raspberry Pi tinkerer

Replications

Did you replicate this project? Share it!

I made one

Love this project? Think it could be improved? Tell us what you think!

Give feedback

Comments

Add projectSign up / Login
Community Projects Magazine Maker Faire Shop Share
Respect project