#!/usr/bin/env python3
"""A PyQt5 GUI utility to monitor and send MQTT server messages."""
################################################################
# Written in 2018-2020 by Garth Zeglin This Python application is a tool intended for debugging programs which pass short data messages back and forth across the network via a MQTT server. It supports opening an authenticated connection to the server, subscribing to a class of messages in order to receive them, viewing message traffic, and publishing new messages on a specified message topic. The first set of controls configures server parameters before attempting a connection. Changes will not take effect until the next connection attempt. Your username and password is specific to the MQTT server and will be provided by your instructor. This may be individual or may be a shared login for all students in the course. Please note, the password will not be your Andrew password. MQTT works on a publish/subscribe model in which messages are published on topics identified by a topic name. The name is structured like a path string separated by / characters to organize messages into a hierarchy of topics and subtopics.
Our course policy will be to prefix all topics with a student andrew ID, e.g. if your user name is xyzzy, we ask that you publish on the 'xyzzy' topic and sub-topics, as per the following examples.
IDeATe MQTT Monitor
Connecting
Listening
xyzzy top-level topic on which user 'xyzzy' should publish xyzzy/status a sub-topic on which user 'xyzzy' could publish xyzzy/sensor another sub-topic on which user 'xyzzy' could publish xyzzy/sensor/1 a possible sub-sub-topic
The message subscription field specifies topics to receive. The subscription may include a # character as a wildcard, as per the following examples.
# | subscribe to all messages |
xyzzy | subscribe to the top-level published messages for user xyzzy |
xyzzy/# | subscribe to all published messages for user xyzzy, including subtopics |
Changing the subscription field immediately changes what is received; the monitor unsubscribes from the previous pattern and subscribes to the new one. Entering an empty field defaults to the global pattern '#'.
The large text field is the console area which shows both debugging and status log messages as well as received messages.
At the bottom are a topic field and data field for publishing plain text messages. Pressing enter in the data field will transmit the data string on the specified topic. The text is not cleared after entry, so pressing enter multiple times will send the same text multiple times.
The MQTT protocol supports binary messages (i.e. any sequence of bytes), but this tool currently only supports sending messages with plain text.
The IDeATE server has more detailed information on the server help page at https://mqtt.ideate.cmu.edu
""") text.scrollToAnchor("top") text.setReadOnly(True) panel.show() def mqtt_server_name_entered(self): name = self.mqtt_server_name.text() self.write("Server name changed: %s" % name) self.main.set_server_name(name) def decode_port_selection(self): title = self.port_selector.currentText() if title == "": return None else: return int(title.split()[0]) # convert the first token to a number def mqtt_port_selected(self, title): portnum = self.decode_port_selection() self.write("Port selection changed: %s" % title) self.main.set_server_port(portnum) def mqtt_username_entered(self): name = self.mqtt_username.text() self.write("User name changed: %s" % name) self.main.set_username(name) def mqtt_password_entered(self): name = self.mqtt_password.text() self.write("Password changed: %s" % name) self.main.set_password(name) def connection_requested(self): # When the connect button is pressed, make sure all fields are up to # date. It is otherwise possible to leave a text field selected with # unreceived changes while pressing Connect. hostname = self.mqtt_server_name.text() portnum = self.decode_port_selection() username = self.mqtt_username.text() password = self.mqtt_password.text() self.main.set_server_name(hostname) self.main.set_server_port(portnum) self.main.set_username(username) self.main.set_password(password) self.main.connect_to_mqtt_server() def mqtt_sub_entered(self): sub = self.mqtt_sub.text() if sub == '': self.mqtt_sub.setText("#") sub = "#" self.write("Subscription changed to: %s" % sub) self.main.set_subscription(sub) def mqtt_topic_entered(self): topic = self.mqtt_topic.text() self.write("Topic changed to: %s" % topic) self.main.set_topic(topic) def mqtt_payload_entered(self): topic = self.mqtt_topic.text() payload = self.mqtt_payload.text() self.main.send_message(topic, payload) ################################################################ class MainApp(object): """Main application object holding any non-GUI related state.""" def __init__(self): # Attach a handler to the keyboard interrupt (control-C). signal.signal(signal.SIGINT, self._sigint_handler) # load any available persistent application settings QtCore.QCoreApplication.setOrganizationName("IDeATe") QtCore.QCoreApplication.setOrganizationDomain("ideate.cmu.edu") QtCore.QCoreApplication.setApplicationName('mqtt_monitor') self.settings = QtCore.QSettings() # uncomment to restore 'factory defaults' # self.settings.clear() # MQTT server settings self.hostname = self.settings.value('mqtt_host', 'mqtt.ideate.cmu.edu') self.portnum = self.settings.value('mqtt_port', None) self.username = self.settings.value('mqtt_user', 'students') self.password = self.settings.value('mqtt_password', '(not yet entered)') # Create a default subscription based on the username. The hash mark is a wildcard. username = getpass.getuser() self.subscription = self.settings.value('mqtt_subscription', username + '/#') # default message to send self.topic = self.settings.value('mqtt_topic', username) self.payload = self.settings.value('mqtt_payload', 'hello') # create the interface window self.window = MainGUI(self) # Initialize the MQTT client system self.client = mqtt.Client() self.client.enable_logger(mqtt_log) self.client.on_log = self.on_log self.client.on_connect = self.on_connect self.client.on_disconnect = self.on_disconnect self.client.on_message = self.on_message self.client.tls_set() self.window.show_status("Please set the MQTT server address and select Connect.") return ################################################################ def app_is_exiting(self): if self.client.is_connected(): self.client.disconnect() self.client.loop_stop() def _sigint_handler(self, signal, frame): print("Keyboard interrupt caught, running close handlers...") self.app_is_exiting() sys.exit(0) ################################################################ def set_server_name(self, name): self.hostname = name self.settings.setValue('mqtt_host', name) def set_server_port(self, value): self.portnum = value self.settings.setValue('mqtt_port', self.portnum) def set_username(self, name): self.username = name self.settings.setValue('mqtt_user', name) def set_password(self, name): self.password = name self.settings.setValue('mqtt_password', name) def connect_to_mqtt_server(self): if self.client.is_connected(): self.window.write("Already connected.") else: if self.portnum is None: log.warning("Please specify the server port before attempting connection.") else: log.debug("Initiating MQTT connection to %s:%d" % (self.hostname, self.portnum)) self.window.write("Attempting connection.") self.client.username_pw_set(self.username, self.password) self.client.connect_async(self.hostname, self.portnum) self.client.loop_start() def disconnect_from_mqtt_server(self): if self.client.is_connected(): self.client.disconnect() else: self.window.write("Not connected.") self.client.loop_stop() ################################################################ # The callback for when the broker responds to our connection request. def on_connect(self, client, userdata, flags, rc): self.window.write("Connected to server with with flags: %s, result code: %s" % (flags, rc)) if rc == 0: log.info("Connection succeeded.") elif rc > 0: if rc < len(mqtt_rc_codes): log.warning("Connection failed with error: %s", mqtt_rc_codes[rc]) else: log.warning("Connection failed with unknown error %d", rc) # Subscribing in on_connect() means that if we lose the connection and reconnect then subscriptions will be renewed. client.subscribe(self.subscription) self.window.show_status("Connected.") self.window.set_connected_state(True) return # The callback for when the broker responds with error messages. def on_log(client, userdata, level, buf): log.debug("on_log level %s: %s", level, userdata) return def on_disconnect(self, client, userdata, rc): log.debug("disconnected") self.window.write("Disconnected from server.") self.window.show_status("Disconnected.") self.window.set_connected_state(False) # The callback for when a message has been received on a topic to which this # client is subscribed. The message variable is a MQTTMessage that describes # all of the message parameters. # Some useful MQTTMessage fields: topic, payload, qos, retain, mid, properties. # The payload is a binary string (bytes). # qos is an integer quality of service indicator (0,1, or 2) # mid is an integer message ID. def on_message(self, client, userdata, msg): self.window.write("{%s} %s" % (msg.topic, msg.payload)) return ################################################################ def set_subscription(self, sub): if self.client.is_connected(): self.client.unsubscribe(self.subscription) try: self.client.subscribe(sub) self.subscription = sub self.settings.setValue('mqtt_subscription', sub) except ValueError: self.window.write("Invalid subscription string, not changed.") self.client.subscribe(self.subscription) else: self.subscription = sub self.settings.setValue('mqtt_subscription', sub) def set_topic(self, sub): self.topic = sub self.settings.setValue('mqtt_topic', sub) def send_message(self, topic, payload): if self.client.is_connected(): self.client.publish(topic, payload) else: self.window.write("Not connected.") self.payload = payload self.settings.setValue('mqtt_payload', payload) ################################################################ def main(): # Optionally add an additional root log handler to stream messages to the terminal console. if False: console_handler = logging.StreamHandler() console_handler.setLevel(logging.DEBUG) console_handler.setFormatter(logging.Formatter('%(levelname)s:%(name)s: %(message)s')) logging.getLogger().addHandler(console_handler) # initialize the Qt system itself app = QtWidgets.QApplication(sys.argv) # create the main application controller main = MainApp() # run the event loop until the user is done log.info("Starting event loop.") sys.exit(app.exec_()) ################################################################ # Main script follows. This sequence is executed when the script is initiated from the command line. if __name__ == "__main__": main()