Monday, June 24, 2013

Twitter -> LCD via python and arduino

I've got a little LCD screen. I thought it'd be neat if anyone could tweet and it'd show up on that screen. Kind of like that goofy thing at basketball games where you can send a text message somewhere and maybe it'll show up on the huge scoreboard.

Code's on github, if you're into that sort of thing.

Getting the tweets from twitter onto my computer

I created a new account called @dansoffice, and I want to get every tweet that mentions @dansoffice into my twitter program. I want it real-time, and I don't want to get rate limited for polling a lot, so I looked to their streaming APIs.

What I want is the "user stream", so I have to authenticate as @dansoffice and send a GET to: https://userstream.twitter.com/1.1/user.json

Requests is a nice python library to make http easy. Pair it with requests_oauthlib (installed via pip; don't confuse it with requests_oauth), and the authentication is easy too. (make a Twitter app, go to the "OAuth tool", and it will tell you your access token and secret, and your consumer key and secret; use requests_oauthlib per their documentation. "consumer" = "client" in this case.)

And Requests even handles streaming requests via iter_lines... sort of. It's got a "chunk size" - a buffer that must fill up before it does anything. For some reason, this sometimes stops tweets from being read, until the next tweet comes in and clears out the buffer. Known issue.

So in my case, a terrible hack: also request that tweets are delimited by length, so the length comes in, followed by a newline, and clears the buffer; at this point, send another request (this one non-streaming, to mentions_timeline) to get the actual text of the most recent @dansoffice mention. Ugh.

Getting the tweets from my computer to the LCD

Python writes nicely to serial, via pyserial. Found my serial port's name from Arduino (tools->serial port menu). Easy enough to write to it when I get a tweet.

The LCD screen I have is this 20x4 display. I had to solder some header pins to it. Arduino has a nice LCD demo, which was easy and straightforward after I fixed my shoddy solder job. At that point, it became just a matter of chopping a string into 4 lines. (Nothing is easy in C.)

Fin

Now, as long as my little twitter monitor is running, and the Arduino is connected, tweets mentioning @dansoffice show up in real time. Now all I have to do is get a long mini-USB cable to stretch to my window. All the tweet-publicizing excitement of the NBA!

(maybe my next thing should mimic the Skinner-style pizza/t-shirt bribes and exhortations to MAKE SOME NOISE.)

2 comments:

  1. Hey Dan,

    Could this project be updated with using a LED matrix, and arduino Yun for wifi?

    ReplyDelete
  2. Probably. I'm not familiar with Arduino Yun, but that would be super cool - avoid the need to be plugged in. Thanks for the tip!

    ReplyDelete

Note: Only a member of this blog may post a comment.