-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtempmon_sensoronpi.py
executable file
·183 lines (148 loc) · 4.89 KB
/
tempmon_sensoronpi.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/env python
import os
import sys
import glob
#import xively
import subprocess
import time
from datetime import datetime
import requests
import urllib2
import json
import sqlite3
import RPi.GPIO as GPIO
import paho.mqtt.client as mqtt
LED_PIN = 17
# REMEMBER: OneWire is ALWAYS on Pin #4
# FEED_ID = "1785749146"
# API_KEY = "IjPjyGRBNX4215uvu7sAB86NBjCtklQByFAIb1VoJT2TUeXF"
GROVESTREAMS_URL = "http://grovestreams.com/api/feed?asPut&api_key=521dfde4-e9e2-36b6-bf96-18242254873f"
DEBUG = True
DBLOCATION="/media/USBHDD1/TempMonData/rfmonDB.db"
COMPONENT = "computerroom"
CHANNEL = "computerroomtemp"
CHANNEL_TAGS = "ComputerRoomTemp"
CHANNELOUT = "outsidetemp"
CHANNELOUT_TAGS = "OutsideTemp"
SLEEP_SECONDS = 60*5+5
#api = xively.XivelyAPIClient(API_KEY)
def init_GPIO():
GPIO.setmode(GPIO.BCM)
GPIO.setup( LED_PIN, GPIO.OUT)
def blinkLED( state ):
GPIO.output( LED_PIN, state )
def init_onewire():
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'
return device_file
def get_device_file():
if not hasattr(get_device_file, "static_device_file"):
get_device_file.static_device_file = init_onewire()
return get_device_file.static_device_file
def read_temp_raw():
device_file = get_device_file()
f = open(device_file, 'r')
lines = f.readlines()
f.close()
return lines
def read_temp():
blinkLED( True )
lines = read_temp_raw()
blinkLED( False )
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
lines = read_temp_raw()
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
temp_c = float(temp_string) / 1000.0
temp_f = temp_c * 9.0 / 5.0 + 32.0
temp_c = round(temp_c, 1)
temp_f = round(temp_f, 1)
return temp_c, temp_f
# function to return a datastream object. This either creates a new datastream,
# or returns an existing one
#def xively_getdatastream(ChannelIn, ChannelTagsIn):
# feed = api.feeds.get(FEED_ID)
# try:
# datastream = feed.datastreams.get(ChannelIn)
# if DEBUG:
# print "Existing Stream: Min: ", datastream.min_value, " Max: ", datastream.max_value, " Curr: ", datastream.current_value
# return datastream
#
# except:
# if DEBUG:
# print "Creating new datastream"
# datastream = feed.datastreams.create(ChannelIn, tags=ChannelTagsIn)
#
# datastream.max_value = None
# datastream.min_value = None
#
# return datastream
#
#
#def xively_update( ChannelIn, ChannelTagsIn, currtemp, currdate ):
# datastream = xively_getdatastream(ChannelIn, ChannelTagsIn)
#
# datastream.at = currdate
# datastream.current_value = str(currtemp)
#
# print "Updating Xively ", ChannelIn, ": ", currdate, " -> ",datastream.current_value
#
# # Then send them to the server.
# datastream.update()
#
# return
#
def utc2local (utc):
epoch = time.mktime(utc.timetuple())
offset = datetime.fromtimestamp (epoch) - datetime.utcfromtimestamp (epoch)
return utc + offset
# main program entry point - runs continuously updating our datastream with the
def run():
init_GPIO()
lastUpdateTime = 0
while True:
temp_f = 0.0
location = ""
myDateTime = datetime.utcnow()
localDateTime = utc2local(myDateTime)
# WUnderground Update
try:
f = urllib2.urlopen('http://api.wunderground.com/api/939d46d3584b09b6/geolookup/conditions/q/KVPZ.json')
json_string = f.read()
parsed_json = json.loads(json_string)
location = parsed_json['location']['city']
temp_f = parsed_json['current_observation']['temp_f']
except (requests.exceptions.ConnectionError, requests.HTTPError, urllib2.URLError) as e:
print "Error reading WUnderground info!!({0}): {1}".format(e.errno, e.strerror)
# Insert reading into DB
conn = sqlite3.connect(DBLOCATION)
with conn:
curs = conn.cursor()
curs.execute("INSERT INTO rawdata (nodeid, metricid, metricguid, metricname, metric, metricdt) VALUES (?,?,?,?,?,?) ", \
( "wu", "1", CHANNELOUT, CHANNELOUT_TAGS, temp_f, localDateTime))
curs.close()
if DEBUG:
print "%s: Current temperature in %s is: %s" % (datetime.now(), location, temp_f)
if( (time.time() - lastUpdateTime) >= 0 ):
try:
url = GROVESTREAMS_URL+"&compId="+COMPONENT+"&"+CHANNELOUT+"="+str(temp_f)
urlhandle = urllib2.urlopen(url)
urlhandle.close()
except( requests.exceptions.ConnectionError, requests.HTTPError, urllib2.URLError) as e:
print "Error updating GroveStreams with RF Data!!({0}): {1}".format(e.errno, e.strerror)
lastUpdateTime = time.time()
sys.stdout.flush()
time.sleep(SLEEP_SECONDS)
try:
print datetime.now()
run()
except KeyboardInterrupt:
print "Keyboard Interrupt..."
finally:
print "Exiting."
GPIO.cleanup()