What is it?
|
Astro Bird in Action
|
If you haven’t heard already Major Tim Peake has been on-board the International Space Station, the ISS, which is orbiting above our heads at around 17,150 miles per hour (that's about 5 miles per second!) Tim likes to tweet a lot and frequently sends updates on his mission and exciting views of planet earth. His twitter ID is @astro_timpeake and you can follow his updates. Twitter uses a bird and makes reference to birds talking or ‘tweeting’, so I envisaged a hack which makes a bird sing or ‘tweet’ every time @astro_timpeake posts. Even better, the bird sings every time another user sends a tweet which contains the ID @asrtro_timpeake or retweets a tweet containing the ID tag.
1. Getting StartedYou will need:
A Raspberry Pi A singing bird A speaker A voice recorder Connect up you Raspberry Pi and boot it up, ensure that you are connected to the Internet. This can be achieved via WiFi or Ethernet cable. Once it is connected then load the LX Terminal and update and install the software below, In the LX Terninal type: sudo apt-get update sudo apt-get install mpg321 sudo apt-get install python-setuptools sudo easy_install pip sudo pip install tweepy 2. Set Up Twitter CodeTo interact with Twitter you are required to register and set up Keys with the API site. If you have these then you can continue with this step. If not then head over to this short guide which will walk you through the steps. Open a new Python code window and add the code below to import the required modules and connect to Twitter.
import sys, subprocess, urllib, time, tweepy, os # == OAuth Authentication == # # This mode of authentication is the new preferred way # of authenticating with Twitter. # The consumer keys can be found on your application's Details # page located at https://dev.twitter.com/apps (under "OAuth settings") consumer_key= 'xxxxxx' consumer_secret= 'xxxxxxxxxxxxxxxxxxxx' # The access tokens can be found on your applications's Details # page located at https://dev.twitter.com/apps (located # under "Your access token") access_token= 'xxxxxxxxxxxxxxxxxxxxxx' access_token_secret= 'xxxxxxxxxxxxxxxxxxxxx' auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) api = tweepy.API(auth) Replace the xxxx with your Twitter API keys. This code authorises you to stream your timeline so that you can check for messages from the @astro_timpeake twitter account. You may be required to register your phone number if you want to develop your program further and access Direct Messages or post photos and videos to your timeline. |
3.Scan for Messages
Now to stream the tweets and search for the keyword, @astro_timpeake. To retrieve the tweets use tweet.text and assign it to a variable, you can print this out on screen to read each tweet.
tweet_to_check = tweet.text print (tweet_to_check) Now check if the tweet contains the keyword using find("@astro_timpeake") >= 0. This code looks at each tweet and returns the position that the keyword is located at, for example, 7 would mean that the keyword begins at the 7th letter in the tweet. If the tweet does not contain the keyword then it will return a value of -1 which is less than zero and therefore the code does not trigger a response. If the number is greater than or equal to zero then the keyword is located at some position within the tweet. The rest of the code displays the tweet and a short notification. if tweet_to_check.find("@astro_timpeake") >= 0: print tweet_to_check print ("found a Tim Peake Tweet") #play mp3 sound 4. Trigger the MP3 FileThe real trick with this hack is that the Tweet triggers a short beep at a particular pitch to be played through a speaker. The bird is activated by this beep and then it begins to chirp. I experimented with various pitches and volumes and found that if the bird is near the speaker then the volume can be set to fairly low. You may be able to hear the beep if you listen carefully. The beep was created using PySound on the laptop but Sonic Pi would be an excellent choice and you can export the file as an MP3. To play the beep use the line:
os.system('mpg321 bird.mp3 &') 5. Force the Audio Through the Headphones6. Download the CodeStay Up to date, follow:
|