#!/usr/bin/python # # Axe-Twitter script # v0.1, by Stelios M. (steliosm@steliosm.net) # http://steliosm.net # # Read values from a PicAxe through the serial port and post them to Twitter! # PicAxe must also send the Username/Password for the Twitter account. # # TwitterAPI provided by: # http://avinashv.net/2008/04/python-and-twitter/ # import base64, httplib, urllib, serial, string class TwitterAPI: def __init__(self, username, password): # generate authentication header string self.authentication = { "Authorization": "Basic %s" % base64.encodestring("%s:%s" % (username, password)).strip() } # create connection self.connection = httplib.HTTPConnection("twitter.com", 80) def update_status(self, status): # send post response with authenticated status self.connection.request("POST", "/statuses/update.xml", urllib.urlencode({ "status": status }), self.authentication) response = self.connection.getresponse() return response.status def main(): # Create an serial port object # Set the serial port the PicAxe is connected to ser = serial.Serial ('/dev/ttyUSB0', 4800) # Read the data from the serial port in a loop while (1): # Read data from the serial port myData = ser.readline() # Strip off the last 2 characters (cr & lf) myData = myData[:-2] # Split the data into 3 parts seperated by ':' myData = myData.split(':') # Create a new TwitterAPI object myTwitter = TwitterAPI(myData[0], myData[1]) # Update the status myTwitter.update_status(myData[2]) # Start the program! if __name__ == "__main__": main()