Here’s a video of the system working:
I wanted to create a way to push data from my Raspberry Pi monitoring plant growth to myself. Instead of creating an email server and sending emails, or setting up an sms client, I decided to install tweepy and use twitter and python to send me the data.
First thing’s first, I had to create a dummy account (@eso_rpi) and sign up for the Twitter Dev Program, which is a free way to access the API. You will need to generate a set of consumer keys and access tokens for your app. The process is pretty simple, and the tweepy example is pretty straight forward. If you run into trouble you could easily google it as the process is pretty well documented.
Here’s my code, you will need to download and install tweepy and apscheduler for this to work:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
#tweepy setup, you must use the keys given to you when you create your app import tweepy consumer_key="" consumer_secret="" access_token="" access_token_secret="" #APscheduler setup from datetime import datetime from apscheduler.scheduler import Scheduler import time import sys import logging #if you start getting logging errors, uncomment these two lines logging.basicConfig() #the adc's SPI setup import spidev spi = spidev.SpiDev() spi.open(0, 0) #fuction that can read the adc def readadc(adcnum): # read SPI data from MCP3008 chip, 8 possible adc's (0 thru 7) if adcnum > 7 or adcnum < 0: return -1 r = spi.xfer2([1, 8 + adcnum << 4, 0]) adcout = ((r[1] & 3) << 8) + r[2] return adcout #"logs in" to twitter, auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) api = tweepy.API(auth) def readandtweet(): millivolts = (readadc(0)*(3300.0/1024.0)) temp_c = (((millivolts - 100.0)/10)-40.0) tmp1 = str(format(((temp_c * 9.0 / 5.0) + 32),'.2f')) #converts the value from the tmp sensor into a useable fahrenheit ldr1 = str(format(((100-(float(readadc(1))/1024)*100)),'.2f')) #makes the value produced by the LDR into a percentage send = 'Brightness: ' + ldr1 + '% / ' + 'Temperature: ' + tmp1 + ' Deg F' #builds the text of the tweet print "Tweeting:" , send #for debug purposes api.update_status(send) #tweets the tweet readandtweet() #runs the program once on start #this executes the function every 30 mins scheduler = Scheduler(standalone=True) scheduler.add_interval_job(readandtweet, minutes=30) scheduler.start() |
The wiring diagram is the same as it is here, except there is an ldr connected to port 1:
There you go!