TeCoEd (Teaching Computing Education)
  • Home
    • Freelance
    • Book
    • Downloading
  • Python
    • Learn Python >
      • Python Modules
    • PyGame Zero
    • Python Programs >
      • Higher or Lower
      • Magic Calculator
      • Password Checker
      • Python Pit
    • What's News App
    • Pixels to Cells
    • Python Mosaics
    • Python OCR
    • L-1-AM
    • Web Scraping >
      • Scraping Trains
    • Weather App
    • Snakes and Windows
    • Python Web Server >
      • Flask
    • Python Picks
  • Ras Pi
    • All About the Pi
    • Getting Started
    • Remote Desktop and VNC
    • Static IP Address
    • Sonic Pi >
      • 3.14
    • Twitter Feed >
      • Tweepy
    • Android & Pi >
      • Advanced Apps
      • Odds
    • A.I on the the Pi
    • CRON
    • Pick Your Own
  • Pi Hardware
    • Pi HATS >
      • Sense Hat Hacks
      • AstroPi HAT
      • Unicorn-HAT >
        • Unicorn Alphabet Disco
        • Uni Codes / Programs
      • Skywriter
      • Piano HAT
    • STS Pi
    • Pi Camera >
      • Pi-Cam, Python & Email >
        • Time Lapse
      • Pi Noir
    • Pipsta >
      • Flask, Input & Printers
    • Raspberry Pi Power >
      • Energenie IR power
    • Pibrella
    • Distance Sensor
    • LCD Screen
    • Pi-Tooth
    • Robot Arm
    • PiGlow
    • PiFM
    • Accelerometer
    • PiFace >
      • Installing PiFace >
        • Python Commands
  • Pi-Hacks
    • Drone Hacks
    • Pi Glue Gun Hack
    • Blinkt!
    • Sonic Pixels
    • R2D2
    • Get to the chopper
    • Astro Bird
    • Twitter Translator
    • Hacking a Robot
    • Nature_Box >
      • Best Nature Photos
    • Wearable Tech >
      • Project New York
      • P.N.Y Part 2 Health
      • P.N.Y Part 3 Games
      • P.N.Y Part 4 Translation
    • Dino-Tweet
    • Other Links
  • Pi-Hacks 2
    • The Joker
    • Hologram Machine
    • Google Vision: Camera Tell
    • Yoda Tweets
    • Pi Phone
    • Darth Beats
    • Twitter Keyword Finder
    • Crimbo Lights Hack
    • Xmas Elf
    • Halloween 2016
    • Halloween Hack 2015
    • Socrative Zombie
    • Voice Translation
    • The Blue-Who Finder
    • GPIO, Twitter
    • Pi Chat Bot >
      • Dictionary Definitions
    • PiGlow & Email
    • Pibrella Alarm System
    • SMS with Python >
      • Spooking a Mobile
  • Pi-Hacks 3
    • LED Dance Suit
    • Ferminal
    • Crypto Tracker
    • David Bowie
    • Lamp Prank >
      • TEST
    • Yoda FM
    • Retro Player
    • LED Pixel Art
    • TARDIS
    • Battleships
    • LED Board
    • Night Vision
    • Enviro+ Weather
  • Minecraft
    • Minecraft API
    • Minecraft Sweeper
    • PiGlove: Minecraft Power Up
    • Minecraft Photo-booth
    • Rendering Pixels
    • Speed Cube
    • Lucky Dip
  • Computing
    • Why Computing?
    • Can You Compute
    • micro:bit
    • Coding Resources
    • Learn to Code >
      • Coding with iPads
      • Apps Creation Tools
      • sKratchInn
      • Sound Editing
    • Cheat Sheets
    • Theory
    • HOUR OF CODING
    • BEBRAS Computing Challange
    • Computer Facts
    • Free Software and Links
  • Contact Me
  • Random Hacks
    • Movile

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 Started


​You will need:
A Raspberry Pi
A singing bird
A speaker
A voice recorde
r

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 Code


To 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 File


The 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 Headphones


Picture

6. Download the Code


PictureClick here for Code
Stay Up to date, follow:
  • @astro_pi
  • @astro_timpeake
  • ​@dan_aldred
  • @raspberry_pi

Powered by Create your own unique website with customizable templates.