Jan 13, 2011

Python+Arduino = Simple Twitter Search Client

There are lots of twitter/arduino mashup examples out on the web. Here's one that's pretty easy to put together.




Requirements:
  • computer running python + Twython, a python add-in for twitter
  • 20x4 serial-enabled LCD
  • an arduino or arduino clone
This was developed in Ubuntu 10.04 (Linux). I leave it as an exercise to the interested student to make this work on Windows or Mac.

Steps:
  1. Install python. You can do this from the Ubuntu Software Center.
  2. Install twython. Here's the source on github.
  3. Hook up your LCD to the arduino. Serial LCDs only need +5V, GND and digital signal to TX (outbound to LCD from arduino).
Notes:
  • I used the Modern Device LCD117 serial kit hooked up to a 20x4 LCD from an ebay seller. Another option is this one from sparkfun.com.
  • I used a seeedstudio seeeduino, an arduino clone. Serial TX to the arduino(the RX pin on the arduino) will cause a real arduino to auto-reset. The seeeduino has a switch for manual reset, which avoids the problem. Here's the comments from the arduino.cc site:

    This setup has other implications. When the Duemilanove is connected to either a computer running Mac OS X or Linux, it resets each time a connection is made to it from software (via USB). For the following half-second or so, the bootloader is running on the Duemilanove. While it is programmed to ignore malformed data (i.e. anything besides an upload of new code), it will intercept the first few bytes of data sent to the board after a connection is opened. If a sketch running on the board receives one-time configuration or other data when it first starts, make sure that the software with which it communicates waits a second after opening the connection and before sending this data.

    The Duemilanove contains a trace that can be cut to disable the auto-reset. The pads on either side of the trace can be soldered together to re-enable it. It's labeled "RESET-EN". You may also be able to disable the auto-reset by connecting a 110 ohm resistor from 5V to the reset line; see this forum thread for details.

Python code:
#!/usr/bin/env python
import feedparser
import time
import serial
from sys import stdout
ser = serial.Serial('/dev/ttyUSB0', 9600)

from twython import Twython
twitter = Twython()
while(1):
  time.sleep(5)
  search_results = twitter.searchTwitter(q="%23arduino",lang="en",rpp="1")
    # use '%23' to prefix hash tag
  for tweet in search_results["results"]:
      #print tweet["created_at"]
      #print tweet["from_user"]
      #print tweet["text"]
      to_arduino = tweet["from_user"]+": "+tweet["text"]
      #print to_arduino
      ser.write(to_arduino[0:80].encode('utf-8'))
      time.sleep(5)
      ser.write(to_arduino[80:159].encode('utf-8'))
      time.sleep(5)
#end of script

Arduino code:
/***************************************************************************
* Twitter search client. Can be modified to display other
* RSS feeds by changing the source python script.
* Expects data input to serial (RX on arduino) to be formatted
* for your display. This example used 20x4 LCD (20 chars x 4 lines).
*
* Tested to work with Modern Device LCD117 Serial Kit (phanderson product)
*
* This is really just a general serial receive and print routine
*
* thisoldgeek@gmail.com
* 01/13/2011
****************************************************************************/



byte incomingFeeds;
int i = 0;
char feed[180]; // current feed from RSS

void setup()
{Serial.begin(9600);
delay(100);
Serial.print("?c0"); // set no cursor
Serial.print("?f"); // clear screen
}

void loop()
{
i = 0;

while(Serial.available()) // are there any bytes on the serial port ???
{
incomingFeeds = Serial.read(); // one instance of feed
feed[i]=(byte(incomingFeeds)); // store the feed chars in an array
i++;
}
feed[i]='\0'; // need to end the string

if (strlen(feed) >= 1)
{ Serial.print("?f"); // clear screen
Serial.print(feed); // print all 4 lines.
//May need a different print rtn for other serial LCD's
}
delay(1000); // needs a delay or drops most of the characters from the sender
}

1 comment:

  1. Wow simple but so greats.... Thank you very much. This is my best tutorial source for playing twitter and arduino via serial.... Thanks again.

    ReplyDelete