-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtempmon.py
executable file
·156 lines (118 loc) · 4.32 KB
/
tempmon.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
#!/usr/bin/env python
import os
import sys
import glob
import time
from datetime import datetime
import requests
import urllib2
import json
import ConfigParser
import paho.mqtt.client as mqtt
UNITID = "O1"
COMPONENT = "raspi"
CHANNELOUT = "outsidetemp"
GROVESTREAMS_URL = "http://grovestreams.com/api/feed?asPut&api_key=521dfde4-e9e2-36b6-bf96-18242254873f"
# ---- Globals ----
IsConnected=False
cnxnRC=-1
SLEEP_SECONDS = 60*5+5
config = None
# ----------------------------------------------------------------
def getConfigExt( configSysIn, sectionIn, optionIn, defaultIn=None ):
optionOut=defaultIn
if( configSysIn.has_option( sectionIn, optionIn)):
optionOut = configSysIn.get(sectionIn, optionIn)
return optionOut
def getConfigExtBool( configSysIn, sectionIn, optionIn, defaultIn=False ):
optionOut=defaultIn
if( configSysIn.has_option( sectionIn, optionIn)):
optionOut = configSysIn.getboolean(sectionIn, optionIn)
return optionOut
def on_connect(client, userdata, flags, rc):
global IsConnected, cnxnRC
print("CB: Connected;rtn code [%d]"% (rc) )
cnxnRC=rc
if( rc == 0 ):
IsConnected=True
def on_disconnect(client, userdata, rc):
global IsConnected
print("CB: Disconnected with rtn code [%d]"% (rc) )
IsConnected=False
def on_publish(client,userdata,mid):
print("Data Published, Msg ID: [%d]" % mid)
pass
def on_log(client, userdata, level, buf):
print("log: ",buf)
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( client ):
client.on_publish = on_publish
client.on_connect = on_connect
client.on_disconnect = on_disconnect
if( getConfigExtBool(config, "DEFAULT", 'qlog_enable') ):
client.on_log = on_log
if( getConfigExt(config, "DEFAULT", 'user', None) and getConfigExt(config, "DEFAULT", 'pswd', None) ):
print( "Setting User and pswd")
client.username_pw_set( config.get("DEFAULT", 'user'), config.get("DEFAULT", 'pswd') )
client.connect(config.get("DEFAULT", 'broker'), config.get("DEFAULT", 'port'), 60)
client.loop_start()
retry=0
while( (not IsConnected) and cnxnRC==-1 and retry <= 10):
print("Waiting for Connect")
time.sleep(.05)
retry += 1
if( not IsConnected ):
print("No connection could be established: rc[%d]") % cnxnRC
return
while True:
temp_f = 0.0
location = ""
# WUnderground Update
try:
f = urllib2.urlopen( config.get("DEFAULT", 'wunderground_url') )
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)
if ( getConfigExtBool(config, "DEFAULT", 'debug') ) :
print "%s: Current temperature in %s is: %s" % (datetime.now(), location, temp_f)
msgToSend = "%s,%03d,%04d,9990,9990" % (UNITID,0,temp_f*10)
if ( getConfigExtBool(config, "DEFAULT", 'debug') ) :
print "%s: %s" % (datetime.now(), msgToSend)
if( not IsConnected ):
print( "ERROR: NO CONNECTION to queue" )
else:
info = client.publish(config.get("DEFAULT", 'topic'), msgToSend, qos=1)
info.wait_for_publish()
sys.stdout.flush()
time.sleep(SLEEP_SECONDS)
# -------------------------------------
client = mqtt.Client(__file__)
try:
configFile=os.path.splitext(__file__)[0]+".conf"
if( not os.path.isfile( configFile )):
print( "Config file [%s] was not found. Exiting" ) % configFile
exit()
config = ConfigParser.SafeConfigParser()
config.read(configFile)
if( getConfigExtBool(config, "DEFAULT", 'debug') ):
print("Using config file [%s]") % configFile
if ( getConfigExtBool(config, "DEFAULT", 'debug') ) :
print datetime.now()
run( client )
except KeyboardInterrupt:
print "Keyboard Interrupt..."
finally:
print "Exiting."
time.sleep(.25)
client.disconnect()
while( IsConnected ):
print("Waiting for Disconnect")
time.sleep(.05)
client.loop_stop()