|
| 1 | +#!/usr/bin/python |
| 2 | + |
| 3 | +import subprocess |
| 4 | +import re |
| 5 | +import sys |
| 6 | +import time |
| 7 | +import datetime |
| 8 | +import gspread |
| 9 | + |
| 10 | +# =========================================================================== |
| 11 | +# Google Account Details |
| 12 | +# =========================================================================== |
| 13 | + |
| 14 | +# Account details for google docs |
| 15 | + |
| 16 | +password = '$hhh!' |
| 17 | +spreadsheet = 'SpreadsheetName' |
| 18 | + |
| 19 | +# =========================================================================== |
| 20 | +# Example Code |
| 21 | +# =========================================================================== |
| 22 | + |
| 23 | + |
| 24 | +# Login with your Google account |
| 25 | +try: |
| 26 | + gc = gspread.login(email, password) |
| 27 | +except: |
| 28 | + print "Unable to log in. Check your email address/password" |
| 29 | + sys.exit() |
| 30 | + |
| 31 | +# Open a worksheet from your spreadsheet using the filename |
| 32 | +try: |
| 33 | + worksheet = gc.open(spreadsheet).sheet1 |
| 34 | + # Alternatively, open a spreadsheet using the spreadsheet's key |
| 35 | + # worksheet = gc.open_by_key('0BmgG6nO_6dprdS1MN3d3MkdPa142WFRrdnRRUWl1UFE') |
| 36 | +except: |
| 37 | + print "Unable to open the spreadsheet. Check your filename: %s" % spreadsheet |
| 38 | + sys.exit() |
| 39 | + |
| 40 | +# Continuously append data |
| 41 | +while(True): |
| 42 | + # Run the DHT program to get the humidity and temperature readings! |
| 43 | + |
| 44 | + output = subprocess.check_output(["./Adafruit_DHT", "2302", "4"]); |
| 45 | + print output |
| 46 | + matches = re.search("Temp =\s+([0-9.]+)", output) |
| 47 | + if (not matches): |
| 48 | + time.sleep(3) |
| 49 | + continue |
| 50 | + temp = float(matches.group(1)) |
| 51 | + |
| 52 | + # search for humidity printout |
| 53 | + matches = re.search("Hum =\s+([0-9.]+)", output) |
| 54 | + if (not matches): |
| 55 | + time.sleep(3) |
| 56 | + continue |
| 57 | + humidity = float(matches.group(1)) |
| 58 | + |
| 59 | + print "Temperature: %.1f C" % temp |
| 60 | + print "Humidity: %.1f %%" % humidity |
| 61 | + |
| 62 | + # Append the data in the spreadsheet, including a timestamp |
| 63 | + try: |
| 64 | + values = [datetime.datetime.now(), temp, humidity] |
| 65 | + worksheet.append_row(values) |
| 66 | + except: |
| 67 | + print "Unable to append data. Check your connection?" |
| 68 | + sys.exit() |
| 69 | + |
| 70 | + # Wait 30 seconds before continuing |
| 71 | + print "Wrote a row to %s" % spreadsheet |
| 72 | + time.sleep(30) |
0 commit comments