Hi spark community! I have been having trouble with my twitter bot that tweets my pi’s cpu temp. I want my pi to tweet it’s cpu temp and it does but I also want it to send a new updated tweet every 300 seconds. I’ve tried to place the while True in different places but it all does the same thing. It tweets every 300 seconds but it tweets the same tweet and the twitter api doesn’t allow it. I want it to send a updated cpu temp every 300 seconds and it isn’t doing that. I have another project for my spark core which displays my tweets but I’m done with that project. Feel free to edit the code. This has been very frustrating to try and edit but it doesn’t do what I want. This does is going to be where my core is displaying the pi’s cpu temp so it is important to my spark project. The spark project was much easier.
#!/usr/bin/env python2.7
# tweet2.py by Alex Eames http://raspi.tv/?p=5941
import tweepy
import sys
import os
from datetime import datetime
from time import sleep
while True:
i = datetime.now()
degree = unichr(176) # code for degree symbol
# Consumer keys and access tokens, used for OAuth
consumer_key = 'type in your consumer key here'
consumer_secret = 'type in your consumer secret here'
access_token = 'type in your access token here'
access_token_secret = 'type in your access token secret here'
# 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: # use entered text as tweet
tweet_text = sys.argv[1]
while True:
else: # if no entered text, tweet the temp
now = i.strftime('%Y/%m/%d %H:%M:%S')
cmd = '/opt/vc/bin/vcgencmd measure_temp'
line = os.popen(cmd).readline().strip()
temp = line.split('=')[1].split("'")[0]
print now + ' Pi Processor Temperature is '+ temp + ' ' + degree +'C'
tweet_text = now + ' Pi Processor Temperature is '+ temp + ' ' + degree +'C'
if len(tweet_text) <= 140:
api.update_status(status=tweet_text)
else:
print "tweet not sent. Too long. 140 chars Max."
sleep(60)
I’ve edited your post to properly format the code. Please check out this post, so you know how to do this yourself in the future. Thanks in advance! ~Jordy