Created
February 15, 2014 22:16
-
-
Save marlhammer/9025916 to your computer and use it in GitHub Desktop.
monitor.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import subprocess | |
import re | |
import sys | |
import time | |
import datetime | |
import gspread | |
# Google Account | |
email = '<YOUR GMAIL ADDRESS>' | |
password = '<YOUR GMAIL PASSWORD>' | |
spreadsheet = '<YOUR SPREADSHEET NAME>' | |
# Login | |
try: | |
gc = gspread.login(email, password) | |
except: | |
print "Unable to log in. Check your email address/password" | |
sys.exit() | |
# Open worksheet | |
try: | |
worksheet = gc.open(spreadsheet).sheet1 | |
# worksheet = gc.open_by_key('<YOUR SPREADSHEET KEY>') | |
except: | |
print "Unable to open the spreadsheet. Check your filename: %s" % spreadsheet | |
sys.exit() | |
# Append data | |
while(True): | |
output = subprocess.check_output(["./Adafruit_DHT", "22", "4"]); | |
print output | |
matches = re.search("Temp =\s+([0-9.]+)", output) | |
if (not matches): | |
time.sleep(3) | |
continue | |
tempC = float(matches.group(1)) | |
tempF = tempC * 9.0; | |
tempF = tempF / 5.0; | |
tempF = tempF + 32.0; | |
matches = re.search("Hum =\s+([0-9.]+)", output) | |
if (not matches): | |
time.sleep(3) | |
continue | |
humidity = float(matches.group(1)) | |
print "Temperature: %.1f C" % tempC | |
print "Temperature: %.1f F" % tempF | |
print "Humidity: %.1f %%" % humidity | |
try: | |
values = [datetime.datetime.now(), tempC, tempF, humidity] | |
worksheet.append_row(values) | |
except: | |
print "Unable to append data. Check your connection?" | |
sys.exit() | |
# Pause for next reading | |
time.sleep(120) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment