What is it?
Boris!"A dinosaur that roars each time it is told to in a mention on Twitter". Using the Pi Face, a kids dinosaur toy and Python to create a simple hack where it checks the incoming mentions for your Twitter user name and if it is present, you have been mentioned then it triggers the PiFace output to make the dinosaur roar.
1. Hacking the Dinosaur
The dinosaur or Boris as he is more affectionately know is from Marks and Spencers - a rather loud child's toy that was left on a shelf to gather dust and needed hacking. The hack was very simple - open the Dino up, 6 screws and split the cast in two. The dinosaur was originally activated by a push button which made contact and completed the circuit. Step 1: This was removed via the button hole, I used a small piece of tin foil to permanently enable a connection across the circuit and a voltage when the PiFace port is coded to turn on.
Step 2: remove the two wires, red and black that connect to the battery pack, the combined battery voltage was 4.5V and therefore the PiFace will provide adequate voltage to power the 'Roar'. The wires were then feed back through the 'button' hole and the the whole toy was screwed back together.
2. Wire up the Dino
The wiring up of the Dino to the PiFace is very simple. Firstly the red / live wire is feed into the 5V port and the black wire is fed into any of the other ports, in the example I selected port 7. Ensure the wires are making contact with the port and screw them in tight with the screw heads.
Boris in Action!
3. Setting up Tweepy
Firstly you need to set up the Twitter API account have the authorisation codes and tokens. If you have not yet done this - click here
Next install the python setup tools and pip, use this to install the Tweepy module that will allow the communication between the twitter API and Python.
sudo apt-get install python-setuptools
sudo easy_install tweepy
sudo pip install tweepy
4. Configuring PiFace
Tweepy is combined with the PiFace commands to respond when Boris receives a ROAR tweet. If you have not yet set up your PiFace check out this guide
Open a new Python window (Using Python 2.7)
Basically the code sends the state value of 1 to pin number 7, (it basically turns on pin number 7). This can be combined with the following line of code, pfio.digital _write(7, 0) to turn it off again. You can use this to test that the dino is Roaring. It is advisable to use the time sleep code to add a small delay between the changes of state.
import piface.pfio as pfio
pfio.digital _write(7, 1) #turns on pin 7
pfio.digital _write(7, 0) # turns off pin 7
5. Final Code
To put the code together you will need to ensure that you have signed up for the Twitter API, if not then click here.
The final code makes use of a while statement to check and return the latest Tweet and an IF statement to check if the word ROAR has been mentioned and then Boris should roar! Within the api.mentions_timeline(count=2) the number 2 is the number of Tweets that you want pulled down. Ideally set this to a value of 1 then Boris will only Roar in new Tweets rather than for every Tweet that has a #ROAR instruction. Use the print tweet.user.screen_name to return the screen name of the Twitter user that commanded Boris to roar! The final line is to a delay of 60 seconds, as this is the Twitter API refresh / cache rate and to many requests will max out and the program will halt.
public_tweets = api.mentions_timeline(count=2)
for tweet in public_tweets:
if tweet.text == "@dan_aldred ROAR": #Phrase when the dino respsonds
That's it a simple hack and a Roaring Twitter Dino-bot - Boris!
Download Sample Code
- Raspberry Pi
- 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 >
- PiGlow & Email
- Pibrella Alarm System
- SMS with Python >
- Contact Me