Birdbox project (I need some help)

Ahoy

I am trying to set up a twitter birdbox camera. I currently have 3 scripts, everything functions okay apart from if birdhousereply.py is running birdhousehourly.py won’t work, I guess this is a limitation with the camera and python. is there a way i can combine birdhousereply.py and birdhousehourly.py so i can tweet every hour and reply to incoming tweets? i have taken the code from the tweeting babbage tutorial on the raspberry pi site,

auth.py (censored my actual twitter keys)

consumer_key        = '123456789'
consumer_secret     = '123456789'
access_token        = '123456789'
access_token_secret = '123456789'

birdhousereply.py (scans twitter for a mention and sends a reply with a picture)

from twython import Twython
from twython import TwythonStreamer
from picamera import PiCamera
from time import sleep

camera = PiCamera()
camera.resolution = (1920, 1080)
camera.rotation = 180

from auth import (
    consumer_key,
    consumer_secret,
    access_token,
    access_token_secret
)

twitter = Twython(
    consumer_key,
    consumer_secret,
    access_token,
    access_token_secret
)


class MyStreamer(TwythonStreamer):
    def on_success(self, data):
        if 'text' in data:
            username = data['user']['screen_name']
            tweet = data['text']
            twitter_id = data['id_str']
            print("@%s: %s: %s" % (username, tweet, twitter_id))
            sleep(5)
            camera.capture('/home/pi/reply.jpg')
            sleep(1)
            message = "@%s here is your picture" % (username)
            with open('/home/pi/reply.jpg', 'rb') as photo:
                twitter.update_status_with_media(status=message,  in_reply_to_status_id=twitter_id, media=photo)
            print("Tweeted: %s" % message)

twitter = Twython(
    consumer_key,
    consumer_secret,
    access_token,
    access_token_secret
)
stream = MyStreamer(
    consumer_key,
    consumer_secret,
    access_token,
    access_token_secret
)
stream.statuses.filter(track='@pibirdhouse')

birdhousehourly.py (run as an hourly crontab job, takes a picture and tweets it with a random message)

from twython import Twython
from twython import TwythonStreamer
from picamera import PiCamera
from time import sleep
import random

camera = PiCamera()
camera.resolution = (1920, 1080)
camera.rotation = 180

from auth import (
    consumer_key,
    consumer_secret,
    access_token,
    access_token_secret
)

twitter = Twython(
    consumer_key,
    consumer_secret,
    access_token,
    access_token_secret
)

messages = [
    "Hello world",
    "Hi there",
    "My name is Babbage",
    "What's up?",
    "How's it going?",
    "Have you been here before?",
    "Get a hair cut!",
]

message = random.choice(messages)
sleep(3)
camera.capture('/home/pi/reply.jpg')
sleep(1)
with open('/home/pi/reply.jpg', 'rb') as photo:
    twitter.update_status_with_media(status=message, media=photo)
print("Tweeted: %s" % message)

You should use backticks or four spaces to format your code, like so:

```
Code goes here!
```

Using quotes totally stuffs up Python’s indentation, and lacks colour formatting, so it’s virtually impossible to follow.

oops, didn’t know that, I have edited the original post :)

Nice. Honestly, I didn’t know about birdbox tweeting so sorry for asking: Where can I join this and get access ? I am totally unfamiliar with Twitter. Anyhow I am just about to finalize my birdbox which is equipped with RaspBerryPiZeroW and Picamera NoIR. In southern Finland birdnesting is actual april-may so there is still time to establish my gear… Greatful for anykind of help.

Thnx in advance
Carl Blyh
Helsinki Finland

this was really helpful in getting started with Twitter.

https://www.raspberrypi.org/learning/getting-started-with-the-twitter-api/worksheet/

Mine is the same setup, zero W, noir camera and some IR LEDs. I thought it would be nice to have the pictures upload to Twitter but also let people take a photo by sending a message to the account. I can also view the pictures from anywhere in the world.

It all works, just not at the same time… I am going to focus on it tomorrow night.

Hope this helps :)

There are definitely a number of ways to accomplish what you want, but they all have various trade offs. Off the top of my head you could:

  • Combine both scripts into one that runs all the time, tweets hourly and replies

Or:

  • Have one script that accesses the camera and saves out a series of pictures (every minute?), with the other two grabbing them as necessary.

The former approach is possibly simpler overall, but requires understanding and managing Python threading and mutually-exclusive resource access to the camera to get it up and running.

The latter approach requires some careful saving/accessing of images but could in future allow you to do things like generate an animated timelapse gif from the last X snaps and other fun stuff*

*There’s an interesting image processing lib here: http://imageio.github.io/

Thanks Phil! I have been doing a little bit of reading about threading, time is against me at the moment (nature eh?) so for now I am going to settle for tweeting hourly photos, that will enable me to get the box up and running, I can then work on getting the full version up and running with a development pi indoors and just deploy the SD card

I have fitted the box with two sets of IR LEDs and a NoIR camera. if anything happens to the LED I can manually go out and plug in the back up LED without opening the box.

Fingers crossed for some bird action!