Howdy,
I have a good example to share for anyone interested in trying out a simple Arduino Python Twitter app.
Good Luck!
Instructable:
http://www.instructables.com/id/simpleTweet01python/Arduino Code
// **************************************************************************************************
// for use with simpleTweet_01_python.py
const int magReed_pin = 10; // pin number
int magReed_val = 0;
int currentDoorState = 1; // begin w/ circuit open
int previousDoorState = 1;
void setup(){
Serial.begin(9600);
pinMode(magReed_pin, INPUT);
}
void loop(){
watchTheDoor();
}
void watchTheDoor(){
magReed_val = digitalRead(magReed_pin);
if (magReed_val == LOW){ // open
currentDoorState = 1;
if (previousDoorState != currentDoorState){
if (Serial.available()<=0) Serial.println("Opened door");
}
}
if (magReed_val == HIGH){ // closed
currentDoorState = 2;
if (previousDoorState != currentDoorState){
if (Serial.available()<=0) Serial.println("Closed door");
}
}
previousDoorState = currentDoorState;
delay(300);
}
// **************************************************************************************************
Python Code
#######################################################################################
# simpleTweet_01_python.py
# visit my instructables for more information
# http://www.instructables.com/member/pdxnat/
print 'running... simpleTweet_01_python'
# import libraries
import twitter
import serial
import time
# connect to arduino via serial port
arduino = serial.Serial('COM4', 9600, timeout=1)
# establish OAuth id with twitter
api = twitter.Api(consumer_key='YOUR_CONSUMER_KEY',
consumer_secret='YOUR_CONSUMER_SECRET',
access_token_key='YOUR_ACCESS_TOKEN_KEY',
access_token_secret='YOUR_ACCESS_TOKEN_SECRET')
# listen to arduino
def listenToArduino():
msg=arduino.readline()
if msg > '':
print 'arduino msg: '+msg.strip()
compareMsg(msg.strip())
# avoid duplicate posts
def compareMsg(newMsg):
# compare the first word from new and old
status = api.GetUserTimeline('yourUsername')
prevMsg = [s.text for s in status]
pM = ""+prevMsg[0]+""
pM = pM.split()
nM = newMsg.split()
print "prevMsg: "+pM[0]
print "newMsg: "+nM[0]
if pM[0] != nM[0]:
print "bam"
postMsg(newMsg)
# post new message to twitter
def postMsg(newMsg):
localtime = time.asctime(time.localtime(time.time()))
tweet = api.PostUpdate(newMsg+", "+localtime)
print "tweeted: "+tweet.text
while 1:
listenToArduino()
#####################################################################################