Created
March 18, 2015 12:06
-
-
Save rubdos/d63742b0bb3c613cacc3 to your computer and use it in GitHub Desktop.
Ping pong ball controller Twitter coupling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import tweepy, time, sys, serial, json | |
CONSUMER_KEY = '' | |
CONSUMER_SECRET = '' | |
ACCESS_KEY = '' | |
ACCESS_SECRET = '' | |
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET) | |
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET) | |
api = tweepy.API(auth) | |
ser = serial.Serial("/dev/ttyACM0", 9600) | |
class PingPongListener(tweepy.StreamListener): | |
def on_data(self, data): | |
try: | |
decoded = json.loads(data) | |
text = decoded['text'].encode('ascii', 'ignore') | |
i = text.find("SP") | |
if(i == -1): | |
return True | |
numbers = text[i+2:i+2+3] | |
integer = int(numbers) | |
print "Found new setpoint:", numbers, len(numbers) | |
print decoded | |
ser.write(numbers + "\n") | |
ser.flush() | |
api.create_friendship(screen_name=decoded['user']['screen_name']) | |
api.update_status("@" + decoded['user']['screen_name'] + " I set the setpoint to " + numbers + " for you. #VUB #Arduino", in_reply_to_status_id=decoded['id']) | |
except KeyError: | |
pass | |
except: | |
print("Something strange happened") | |
pass | |
return True | |
l = PingPongListener() | |
stream = tweepy.Stream(auth, l) | |
stream.userstream() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment