#!/usr/bin/env python2.7 # Tweet the temperature, light, and sound levels with our Raspberry Pi # http://dexterindustries.com/GrovePi/projects-for-the-raspberry-pi/sensor-twitter-feed/ # tweet.py by Alex Eames http://raspi.tv/?p=5908 import tweepy # GrovePi + Sound Sensor + Light Sensor + Temperature Sensor + LED # http://www.seeedstudio.com/wiki/Grove_-_Sound_Sensor # http://www.seeedstudio.com/wiki/Grove_-_Light_Sensor # http://www.seeedstudio.com/wiki/Grove_-_Temperature_and_Humidity_Sensor_Pro # http://www.seeedstudio.com/wiki/Grove_-_LED_Socket_Kit import grovepi import sys import time import math # Connections sound_sensor = 0 # port A0 light_sensor = 1 # port A1 temperature_sensor = 4 # port D4 led = 3 # port D3 # Consumer keys and access tokens, used for OAuth consumer_key = 'YzffjNm6xxxxxxxxxxxx5fI0KZ0c' consumer_secret = 'C1Y0WCKppB99ZweQFs9nlP0D87LIxxxxxxxKA749LY5' access_token = '1185864264-HpYphEjnfr5ZwgtT0igPoxxxxxxxxxxVULG6gZ98' access_token_secret = 'F3RPwD842Kylrk8JgCKtztcySoxxxxxxUvcwj6zxHp' # 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) grovepi.pinMode(led,"OUTPUT") last_sound = 0 while True: # Error handling in case of problems communicating with the GrovePi try: # Get value from temperature sensor [temp,humidity] = grovepi.dht(temperature_sensor,1) t=temp h=humidity # Get value from light sensor light_intensity = grovepi.analogRead(light_sensor) # Give PWM output to LED grovepi.analogWrite(led,light_intensity/4) # Get sound level sound_level = grovepi.analogRead(sound_sensor) if sound_level > 0: last_sound = sound_level # Post a tweet hora = time.strftime("%H:%M:%S") print ("(%s) Home's Temp %.1f, Hum: %d, Light: %d, Sound: %d" %(hora,t,h,light_intensity/10,last_sound)) tweet_text="(%s) Home's Temp %.1f, Hum: %d, Light: %d, Sound: %d" %(hora,t,h,light_intensity/10,last_sound) if len(tweet_text) <= 140: api.update_status(tweet_text) else: print "tweet not sent. Too long. 140 chars Max." #time.sleep(30) except IOError: print "Error" except: print "Duplicate Tweet" break