Created
December 19, 2011 22:12
-
-
Save matael/1499147 to your computer and use it in GitHub Desktop.
Article coffeepot
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 python2 | |
#-*- encoding: utf-8 -*- | |
# | |
# bridge.py | |
# | |
# 12/2011 Mathieu Gaborit <[email protected]> | |
# License : WTFPL | |
import sys | |
import os | |
import twitter # le fameux module python-twitter | |
import time | |
import re # les regexs | |
import serial # le module série | |
############################################# | |
################# S E T U P ################# | |
############################################# | |
consumer_key = '' # clé d'API twitter pour le compte lié à la cafetière | |
consumer_secret = '' # Code secret pour le compte de la cafetière | |
master_name = u'' # pseudo twitter du maitre de la cafetière | |
serial_port = "/dev/ttyACM0" # port série vers l'arduino | |
re_start = re.compile('givemecoffee') # motif à matcher pour la mise en route | |
re_stop = re.compile('thanksforcoffee') # motif à matcher pour l'arrêt | |
update_time = 30 # temps séparant deux vérification de tweet (en secondes) | |
############################################# | |
def do_coffee(api, ser): | |
"""Must i send a signal to the coffee pot ?""" | |
mention = api.GetMentions()[:1] # on récupère la dernière mention pour la cafetière | |
if mention[0].user._screen_name == master_name: # si elle vient du maître... | |
# On la traite... | |
if re_start.search(mention[0].text): # si on trouve le motif de lancement | |
print("Hey ! Let's make coffee !") | |
ser.write('1') # on envoie un 1 sur la liaison série | |
return 0 | |
elif re_stop.search(mention[0].text): # si on trouve de motif d'arrêt | |
print("Yeah ! Coffee's ready !") | |
ser.write('0') # on envoie un 0 sur la liaison série | |
return 0 | |
else: # sinon, on attend | |
print("Waiting for a tweet...") | |
return 0 | |
def main(): | |
# créons une instance de la classe d'API | |
try: | |
api = twitter.Api(consumer_key=consumer_key,\ | |
consumer_secret=consumer_secret,\ | |
access_token_key='87711832-0X6wvXnI8mxByu4PrxFO8XVa6uLyBgcLSA6jrXMw',\ | |
access_token_secret='mNcosbbAkHtNubTztJuW9bjBArN60sTgcAUTm6dmX4') | |
print("Connected to the twitter API") | |
except: | |
print("Failed to load twitter API") | |
try: | |
# on tente d'initialiser la liaison série | |
ser = serial.Serial(port=serial_port) | |
print("Serial Connection opened...") | |
except: | |
print("Failed to load serial connection") | |
print("Entering main loop....") | |
while 1: | |
do_coffee(api, ser) | |
time.sleep(update_time) | |
return 0 | |
if __name__ == '__main__': main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment