Created
May 9, 2013 17:16
-
-
Save andypiper/5548978 to your computer and use it in GitHub Desktop.
Read BBC Radio livetext from MQTT and display on LCDsysinfo (http://coldtearselectronics.wikispaces.com/USB+LCD+-+LCD+System+info)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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): | |
| lines = textwrap.wrap(msg.payload, width=20) | |
| for n, s in enumerate(lines): | |
| d.display_text_on_line(int(n)+2, s, 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("test.mosquitto.org", 1883, 60) | |
| mqttc.subscribe("bbc/livetext/radio2", 0) | |
| rc = 0 | |
| while rc == 0: | |
| try: | |
| rc = mqttc.loop() | |
| except KeyboardInterrupt: | |
| mqttc.unsubscribe("bbc/livetext/radio2") | |
| 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
I receive the following error when attempting to run this:
Exception AttributeError: "Mosquitto instance has no attribute '_mosq'" in <bound method Mosquitto.del of <mosquitto.Mosquitto instance at 0x17eedf0>> ignored
Traceback (most recent call last):
File "radio.py", line 23, in
mqttc = mosquitto.Mosquitto()
TypeError: init() takes at least 2 arguments (1 given)
I'm not overly familiar with python to be able to figure out what's wrong. I've looked it up, but was unable to see a solution that worked.