Pythonscript zendt tweets naar Twitter
Volgens de instructie op * SparkFun Getting Started with Raspberry Pi part 2: Wifi and Python and Twitter oh my!
Maar in plaats van dat ik twython
installeer, installeer ik Tweepy
.
Tweepy installatie
Eerst PIP installeren
sudo apt-get update sudo apt-get install python-pip
daarna Tweepy
sudo pip install tweepy
hello_tweet.py
Het volgende script tweet een tekst gegeven op de commandoregel. Het is geschreven naar het voorbeeld op Tweeting with Python tweepy on the Raspberry Pi – part 2 pi twitter app series.
#!/usr/bin/env python2.7 # tweet.py by Alex Eames http://raspi.tv/?p=5908 import tweepy import sys # Twitter API Consumer keys and access tokens, consumer_key = '{secret}' consumer_secret = '{secret}' access_token = '{secret}' access_token_secret = '{secret}' # OAuth process, using the keys and tokens auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) # Creation of the actual interface, using authentication api = tweepy.API(auth) if len(sys.argv) >= 2: tweet_text = sys.argv[1] else: tweet_text = "Still messing about with tweepy and twitter API. :)" if len(tweet_text) <= 140: api.update_status(status=tweet_text) else: print "tweet not sent. Too long. 140 chars Max."
Temperature-tweet.py
Dit script uitgevoerd met sudo python temperature-tweet.py
leest via I2C de temperatuur en luchtvochtigheid uit de HIH6130-sensor en tweet deze naar mijn kanaal #sikkepitje . Het script is gemaakt naar het voorbeeld van de Youtube-instructiefilm op Youtube SparkFun Getting Started with Raspberry Pi Part 3: Tweeting Weather Station, maar is herschreven zodat het de twitterbibliotheek Tweepy gebruikt i.p.v. Twython. Je moet wel eerst de I2C-bus op de Raspberry inschakelen.
#!/usr/bin/env python2.7 # tweet.py by Alex Eames http://raspi.tv/?p=5908 import tweepy import sys from smbus import SMBus import time # Twitter API Consumer keys and access tokens, # benodigd om te tweeten naar https://twitter.com/sikkepitje_ consumer_key = '{secret}' consumer_secret = '{secret}' access_token = '{secret}-{secret}' access_token_secret = '{secret}' # OAuth process, using the keys and tokens auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) # Creation of the actual interface, using authentication twitter = tweepy.API(auth) # I2C globals ADDR = 0x27 bus = SMBus(1) # is I2C-bus nummer 1 # Special characters deg = u'\N{DEGREE SIGN}' # Main loop while True: # get current system date and time datetime = time.strftime("%Y-%m-%d %H:%M:%S") # Read data from sensor bus.write_byte(ADDR, 0x00) ans = bus.read_i2c_block_data(ADDR, 0x00, 4) # Convert to human readable humidity humd = ((ans[0] & 0x3f) << 8) + ans[1] humd = humd * float('6.10e-3') humd = '{:.0f}'.format(humd) # Convert temperature to Celsius temp = (ans[2] << 8) + ans[3] temp = temp >> 2 temp = (temp * float('1.007e-2')) - 40. temp = '{:.1f}'.format(temp) # Print date, time, temperature and umidity print datetime print 'Temperatuur: ' + str(temp) + deg + 'C' print 'Luchtvochtigheid: ' + str(humd) + '%' print '' # Post to Twitter! msg = 'Hier is de weerrobot! Het is ' + datetime + \ '. De temperatuur is ' + str(temp) + \ deg + 'C, en de luchtvochtigheid is ' + \ str(humd) + '%.' twitter.update_status(status=msg) # Delay (in seconds) before next time.sleep (60)