-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpitemp.py
executable file
·85 lines (71 loc) · 2.37 KB
/
pitemp.py
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#! /usr/bin/env python
import subprocess
import re
import sys
import time
import datetime
import gspread
import requests
import xml.etree.ElementTree as ET
def get_temp():
"""Get the temperature and humidity readings from the DHT22"""
count = 0
while True:
# Temp
output = subprocess.check_output(
["/home/andy/python/bitbucket/pitemp/Adafruit_DHT", "2302", "4"])
count += 1
print ("Attempt %s: %s") % (count, output)
temp_match = re.search("Temp =\s+([0-9.]+)", output)
humid_match = re.search("Hum =\s+([0-9.]+)", output)
# if the beginning of output contains temp and numbers,
# we can assume we are getting valid data
if temp_match:
temp = float(temp_match.group(1))
humidity = float(humid_match.group(1))
break
return (temp, humidity)
def get_external_temp():
"""using openweathermap, get the tempertare at time of call"""
baseurl = "http://api.openweathermap.org/data/2.5/weather"
query = "?q=salhouse&mode=xml"
url = baseurl + query
r = requests.get(url)
root = ET.fromstring(r.text)
kelvin = float(root[1].attrib.get('value'))
celcius = kelvin - 272.15
return celcius
def write_to_gdocs(temp, ext_temp, humidity):
"""write our data to a google spreadsheet"""
# Account details for google docs
email = '[email protected]'
password = 'oknifzuxspiwyobt'
spreadsheet = 'pitemp'
# Login with your Google account
try:
gc = gspread.login(email, password)
except:
print "Unable to log in. Check your email address/password"
sys.exit()
# Open a worksheet from your spreadsheet using the filename
try:
worksheet = gc.open(spreadsheet).sheet1
except:
print "Unable to open the spreadsheet. \
Check your filename: %s" % spreadsheet
sys.exit()
try:
values = [datetime.datetime.now(),
temp,
ext_temp,
humidity]
worksheet.append_row(values)
#success!
print "Wrote a row to %s at %s" % (spreadsheet,
datetime.datetime.now())
except:
print "Unable to append data. Check your connection?"
write_to_gdocs(get_temp()[0],
get_external_temp(),
get_temp()[1]
)