Skip to content

Instantly share code, notes, and snippets.

@koma5
Created December 13, 2016 18:32
Show Gist options
  • Save koma5/b8f45463ced3070ed0b22f06299332d4 to your computer and use it in GitHub Desktop.
Save koma5/b8f45463ced3070ed0b22f06299332d4 to your computer and use it in GitHub Desktop.
Usage : ./mqttThermostat.py minTempC maxTempC stopTime
#!/usr/bin/python
import mosquitto, sys, datetime
min = float(sys.argv[1])
max = float(sys.argv[2])
stopTime = datetime.datetime.strptime(sys.argv[3], "%H:%M").time()
print min, max
client = mosquitto.Mosquitto("vwThermostat.py on mqtt");
client.connect("127.0.0.1");
lastminute = 0
def on_message(self, obj, msg):
global heaterState, min, max, lastminute
#print msg.topic + " " + msg.payload
#sys.stdout.write(msg.payload)
minute = datetime.datetime.now().minute
if minute % 10 == 0 and minute != lastminute:
print datetime.datetime.now().time().strftime("%H:%M"),
lastminute = minute
print msg.payload,
sys.stdout.flush()
if msg.topic == "vw/temp/1":
if heaterState != "on" and float(msg.payload) < min:
print "somebody clapped twice"
client.publish("vw/heater", "on", 0)
if heaterState != "off" and float(msg.payload) >= max:
print "turning it off again..."
client.publish("vw/heater", "off", 0)
if msg.topic == "vw/heater/state":
heaterState = msg.payload
client.on_message = on_message
client.subscribe("vw/temp/1", 0)
client.subscribe("vw/heater/state", 0)
client.publish("vw/heater", "off", 0)
heaterState = "off"
while datetime.datetime.now().time() < stopTime:
client.loop()
print "time to turn thermostat and heater off"
client.unsubscribe("vw/temp/1")
client.publish("vw/heater", "off", 0)
client.disconnect()
exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment