Improving the “Flash LED on Twitter Search” program

Twitter Fight in LED form

Pi

Back in January, I built a Raspberry Pi powered device which tracked Twitter for a search term, flashing an LED each time there was a match, and logging the lot to a file.

The other day, as you can see from the comments on that post, I was contacted by someone called Robert who needed help modifying the code to change how it worked.

Instead of one search term, he wanted it to check for four. Each term had its own LED to flash, and each match for each term would be counted for a minute, after which the LED that corresponded to the most popular term would stay lit for a few seconds. After some to-ing and fro-ing, I finally came up with this, not entirely elegant but working, solution.

What you’ll need:

  • A Raspberry Pi
  • Four LEDs and some way of connecting them to the Pi
  • Python (along with the tweepy and GPIO libraries)
  • Your Twitter API tokens (see here, and choose Add New App)

Now, assuming you’ve wired the LEDs up to pins 22, 23, 24 and 25 (oh, and I’m using the gpio.BOARD method here – we found it differs from gpio.BCM – either works, but they’re not the same numbers so be aware), use this code:

(You can download a zip of this here)

As you can see, much of the code is the same as before. There’s an extra timeout check (for the resetting every minute), and it now prints current term match counts on the screen as they’re hit.

If you want three, five, or a different number of terms, it should be straightforward to modify. I haven’t coded it to count the number of terms and adjust accordingly!

To run it, assuming you’ve made the script executable (chmod +x multiledtweet.py should do it), use this command:

./multiledtweet.py searchterma searchtermb searchtermc searchtermd

Where searchterma, searchtermb etc. are your terms.

./multiledtweet.py murder death kill mushroom

for example. Mushroom was not very popular in comparison, I found.

mushroom-mdk

One thought on “Improving the “Flash LED on Twitter Search” program”

Leave a Reply