Skip to content

Instantly share code, notes, and snippets.

@andypiper
Created December 2, 2013 21:30
Show Gist options
  • Select an option

  • Save andypiper/7759410 to your computer and use it in GitHub Desktop.

Select an option

Save andypiper/7759410 to your computer and use it in GitHub Desktop.
Show home power / temperature on LCDsysinfo display
#!/usr/bin/python
from pylcdsysinfo import BackgroundColours, TextColours, TextAlignment, TextLines, LCDSysInfo
import mosquitto
import textwrap
import sys
from time import sleep
def on_message(mosq, obj, msg):
if 'CC' in msg.topic:
power = float(msg.payload)*1000
d.display_text_on_line(2, "Power: " + str(power) + " W", False, TextAlignment.CENTRE, TextColours.CYAN)
if 'temp' in msg.topic:
d.display_text_on_line(3, "Temperature: " + msg.payload + " C", False, TextAlignment.CENTRE, TextColours.CYAN)
def on_connect(mosq, obj, msg):
d.display_text_on_line(6, "Connected", False, TextAlignment.CENTRE, TextColours.GREEN)
def on_disconnect():
d.display_text_on_line(6, "Disconnected", False, TextAlignment.CENTRE, TextColours.RED)
d = LCDSysInfo()
d.clear_lines(TextLines.ALL, BackgroundColours.BLACK)
mqttc = mosquitto.Mosquitto()
mqttc.on_message = on_message
mqttc.on_connect = on_connect
mqttc.on_disconnect = on_disconnect
mqttc.connect("m2m.eclipse.org", 1883, 60)
mqttc.subscribe("PowerMeter/CC/andyp", 0)
mqttc.subscribe("PowerMeter/temp/andyp", 0)
rc = 0
while rc == 0:
try:
rc = mqttc.loop()
except KeyboardInterrupt:
mqttc.unsubscribe("PowerMeter/CC/andyp")
mqttc.unsubscribe("PowerMeter/temp/andyp")
mqttc.disconnect()
on_disconnect()
print "Bye!"
sleep(1)
d.clear_lines(TextLines.ALL, BackgroundColours.BLACK)
sys.exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment