Eric Traver
Published © GPL3+

Alaska RasPi – TwitterPi

Build a system that will send pics to Twitter via hashtag request as well as motion detection pics taken and sent to Twitter as well.

IntermediateFull instructions provided2 hours734
Alaska RasPi – TwitterPi

Things used in this project

Story

Read more

Code

Code snippet #3

Plain 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 #7

Plain text
def BlinkLed(OnOff, OffOn):
    if BlinkLED == 1:
        GPIO.output(LED, OnOff)
        time.sleep(.5)
        GPIO.output(LED, OffOn)
        time.sleep(.5)

Code snippet #8

Plain 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 #9

Plain 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 #10

Plain text
def SaveNewStringID(id_string):
    PathToFile = WorkDir + "/stringID.txt"
    file = open(PathToFile, "w")
    file.write(id_string)
    file.close()

Code snippet #12

Plain 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 #13

Plain 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 #14

Plain text
def CamFlip():
    if camera.vflip == True:
        camera.vflip = False
    else:
        camera.vflip = True

Code snippet #15

Plain 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 #16

Plain 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 #17

Plain 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 #18

Plain 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 #21

Plain 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 #25

Plain text
try:
    stopThreads = 0
    securityalert = threading.Thread(name='SecurtiyAlert', target=SecurityAlert)
    securityalert.start()
    MonitorTweets()
except KeyboardInterrupt:
    stopThreads = 1
    GPIO.cleanup()
    exit()

Credits

Eric Traver

Eric Traver

1 project • 2 followers
Raspberry Pi tinkerer

Comments