Skip to content

Instantly share code, notes, and snippets.

@marlhammer
Created February 15, 2014 22:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marlhammer/9025916 to your computer and use it in GitHub Desktop.
Save marlhammer/9025916 to your computer and use it in GitHub Desktop.
monitor.py
#!/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