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.)

Wednesday, June 19, 2013

Embedding a browser in a python application on a mac

I heard about PyQtWebkit (e.g. here). Sounds great.

First I had to get PyQt. Or maybe PyQt and Webkit, or Py and QtWebkit, or who knows. I wanted all those parts.

Tried to install it from source (e.g. from Riverbank) but that was a hopeless mess. Version 4 or 5? I don't know. I had to build SIP first, which eventually went fine. I tried to build PyQt, but had trouble because it couldn't find qmake, so I went looking for that. But then (as I often do when building things from source) I wondered if I was doing this whole thing wrong.

Tried to install PyQt using MacPorts, but either didn't find the right port or something didn't work. So I tried Homebrew (which is like MacPorts but newer; says you shouldn't run both but it's been okay for me so far):
brew install pyqt

Turns out, I guess, PyQtWebkit is included in PyQt; I just ran the code at the top of the above link, and bam! Google.pl in a python Qt window.
I wonder if I could have used pip to get PyQt; looks doubtful though.

Looks like instead maybe I could have downloaded PyQtX. But I haven't tried that.

Related: Selenium, but that drives an external browser, doesn't let you put one in your own app. Followed the instructions here.