Sunday, May 1, 2011

Serial Coms with Twitter and Python

For my project, I wanted to make a universal switch that would be operated using twitter. The interaction would be that from anywhere, you can make a tweet and turn on and off any electronic appliance. This could either be done by having a switch tail or by having a servo motor to manually flip the switch. I just got the communication between twitter and the arduino working so I'll post the code.

A few notes: Make sure you install the following packages: pySerial, simpleJson, simpleGeo-oauth2, httpLib2, and python-twitter. Also, make sure you are using a 32 bit version of python because the 64 bit has trouble dealing with serial communications. Another important bit of info is that you cannot have the arduino's serial monitor open during communication because python will not recognize that com port as being open.

Also, you will need a twitter account and a registered app to get the 'key' and 'secret' values. To register and app, go to dev.twitter.com

Anyway, on to code. Here's what you have to be running on the connected PC:
#******************************************#
# Based off of code by by Gregg Horton 2011 #
# Modified by Eric Kenvin #
# *****************************************#

##Import Libraries

import twitter
import serial
import time

##authenticate yourself with twitter
api = twitter.Api(consumer_key='key', consumer_secret='secret', access_token_key='key', access_token_secret='secret')

##set to your serial port
print ('before opening')
ser = serial.Serial(2, 19200)

## check serial port
def checkokay():
line=ser.readline()
print line
## Welcome message
print ('Welcome To Would you kindly!')
print ('Awaiting Orders')
def action():
status = [ ]
x = 0

status = api.GetUserTimeline('UserName') ##grab latest statuses

checkIt = [s.text for s in status] ##put status in an array

## check for match and write to serial if match
if checkIt[0] == 'would you kindly turn on?':
print 'Tweet Recieved, turning on'
ser.write('1')
elif checkIt[0] == 'would you kindly turn off?':
ser.write('0')
print 'stopped, awaiting instructions.'
else:
ser.write('0')
print 'Awaiting Tweet'


while 1:
checkokay()
action()
time.sleep(30) ## twitter apps only allow for 350 requests per hour, so you cannot be updating constantly

###################################################################

Here's the arduino side code

int relayPin = 13; // LED connected to digital pin 13
int incomingByte = 0; //declare incoming byte
// The setup() method runs once, when the sketch starts

void setup()
{
// initialize the digital pin as an output:
pinMode(relayPin, OUTPUT);
Serial.begin(19200); // set up Serial library at 19200 bps

Serial.println("Arduino is ready!");
}

void loop()
{
if (Serial.available() > 0)
{
// read the incoming byte:
incomingByte = Serial.read();
Serial.println(incomingByte);
if (incomingByte == 49)
{
digitalWrite(relayPin, HIGH);
}
else
{
digitalWrite(relayPin, LOW);
}

// say what you got:
Serial.print("I received: ");
Serial.println(incomingByte, DEC);
}
}


I based this project off of the instructable found here http://www.instructables.com/id/Tweet-a-Pot-Twitter-Enabled-Coffee-Pot/

No comments:

Post a Comment