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?


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.
Picture
Steps 1 & 2 (No Dinosaurs were hurt in the making..)
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.
Picture
Wiring Guide
Picture

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
or
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 time
import sleep
import piface.pfio as pfio
pfio.init()

pfio.digital _write(7, 1)  #turns on pin 7
sleep(3)
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.

while True:
    public_tweets = api.mentions_timeline(count=2) 
    for tweet in public_tweets:    
        pfio.digital_write(7, 0)
        if tweet.text == "@dan_aldred ROAR": #Phrase when the dino respsonds
            pfio.digital_write(7, 1)

            time.sleep(10)
            print tweet.text
            print tweet.user.screen_name
       else:
            print tweet.text
            print tweet.user.screen_name
            time.sleep(60)


That's it a simple hack and a Roaring Twitter Dino-bot - Boris!

Download Sample Code 


Example Code
File Size: 1 kb
File Type: py
Download File

dino-stream.py
File Size: 1 kb
File Type: py
Download File

Powered by Create your own unique website with customizable templates.