Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
138 lines (114 sloc) 4.6 KB
# Karl Palsson, 2011
# A karlnet consumer, that also pulls from a pachube dashboard to take actions.
# An alternative to having to set up triggers and expose a url to let them call
# I'm not real happy with how this keeps the api key in the source. But I've never really dealt with api keys before, so I'm not sure of a better
# way... at least, not just yet.
# this might be better than pygame?
# but is very linux specific:
import os
import time
import socket
import ConfigParser
import json
import jsonpickle
import httplib
import pygame
import mosquitto
configFile = ConfigParser.ConfigParser()['config.default.ini', 'config.ini'])
# use int(xxx, 0) because configParser.getint() doesn't magically determine hex/dec
config = {
'node' : int(configFile.get('karlnet', 'node'), 0),
'probe' : int(configFile.get('karlnet', 'probe'), 0),
'mqtthost' : configFile.get('karlnet', 'mqtthost'),
'apikey' : configFile.get('pachube', 'apikey'),
'dashboardFeed' : int(configFile.get('pachube', 'dashboardFeed'),0)
class Container():
unblob = {}
import logging
logging.basicConfig(level=logging.DEBUG, format="%(asctime)s %(levelname)s %(name)s - %(message)s",
log = logging.getLogger("main")
sound = pygame.mixer.Sound("phone_2.wav")
qq = Container()
def fetch_pachube(feedId):
conn = httplib.HTTPConnection('')
headers = {"X-APIKey" : config["apikey"]}
conn.request("GET", "/v1/feeds/%d.json" % feedId, headers=headers)
blob = conn.getresponse()
if blob.status != httplib.OK:
raise httplib.HTTPException("Couldn't read from pachube dashboard, did you set the API key?: %d %s" % (blob.status, blob.reason))
return json.load(blob)
def get_knob(blob, tag):
for node in blob['datastreams']:
#log.debug("checking datastream: %s", node['tags'])
if node['tags'][0] == tag:
return node['values'][0]['value']
def handle_mq_packet(obj, msg):
kp = jsonpickle.decode(msg.payload)
if (kp['node'] == config['node']):
realTemp = kp['sensors'][config['probe']]['value']"Current temp on probe is %d", realTemp)
threshold = get_knob(obj.unblob, "set temp mash")
if threshold is not None:
setTemp = int(threshold)
cooling = setTemp < 50
heating = not cooling
log.debug("threshold is %d, mode: %s", setTemp, "cooling" if cooling else "heating")
# Assume cooling if the setTemp is under 50, assume heating if the set temp is over 50....
if (cooling and realTemp < setTemp) or (heating and realTemp > setTemp):
log.warn("ok, it's ready!")
alarm = get_knob(obj.unblob, "mash temp alarm active")
log.debug("alarm = %s", alarm)
if int(alarm) > 0 and not obj.playing :"Music is on!!!!!")
obj.playing = True
elif int(alarm) == 0 and obj.playing:"alarm off, and we're already playing")
obj.playing = False
elif obj.playing:
log.debug("No need to do anything, alarm is on, and we're already playing")
if (heating):"Turning music off.... we're below the threshold")
else:"Turning music off.... we're above the threshold")
obj.playing = False
def runMain(blob):
clientid = "karlnet_pachube@%s/%d" % (socket.gethostname(), os.getpid())
mqttc = mosquitto.Mosquitto(clientid, obj=blob)
mqttc.on_message = handle_mq_packet
last = 0
threshold = None
blob.playing = False
while True:
if (time.time() - last > 60):"Fetching current dashboard values from pachube, incase they've changed")
blob.unblob = fetch_pachube(feedId=config['dashboardFeed'])
last = time.time()
if __name__ == "__main__":
except KeyboardInterrupt:
print "got a keyboard interrupt""QUIT - quitting due to keyboard interrupt")
raise SystemExit
log.exception("Something really bad!")