Two Way communication Using MQTT and Python

MQTT is a publish and subscribe protocol with no direct connection between clients.

However many applications require a client to client type connection. Examples are:

  • Chat
  • Sensor or device control

This can be achieved in all versions of MQTT but it has been made easier in MQTTv5 with the introduction of request response in the publish payload.

In this tutorial we look at achieving the same in MQTTv3.1.1.





If you look at the simple diagram below which show a typical communication between MQTT clients.

mqtt_two_way_communication
Because there is no direct connection between client A and client B when any of the clients receives data it doesn’t know who sent it.

There are however several ways of overcoming this the main ones are:

  • Include the client name in the payload data
  • Use a topic that identifies the sending client

The most common implementation is using topic names.A typical design using two clients is.

Client A publishes to topic client_a using the topic topic_base/send/client_a.

Client B subscribes to topic_base/send/client_a and any messages it receives on this topic comes from client A.

Client B publishes to topic client_a using the topic topic_base/send/client_b.

Client A subscribes to topic_base/send/client_b and any messages it receives on this topic comes from client B.

mqtt_two_way_communication-topics

Example Python Scripts

You can download example scripts for client A and Client B. When you run them you should see messages on Client A from Client B and vice versa.

Although the scripts use Python the technique is applicable to all client types.

Related tutorials

 

 

Please rate? And use Comments to let me know more
[Total: 1   Average: 5/5]

4 comments

  1. Hi Steve,
    I am trying to run my first mqtt client program using python 3.8 on windows 10 & it was installed using the steps mentioned on your website. But running the program i get following error:

    Traceback (most recent call last):
    File “mqttclient.py”, line 23, in
    client.on_connect = on_connect
    NameError: name ‘client’ is not defined

    When i checked paho-mqtt for installation, i got this below:
    c:\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\paho\mqtt

    This is my code:

    import paho.mqtt.client as mqtt mqtt_broker_ip=”mqtt.eclipse.org” mqtt_broker_port=1883 mqtt_subscription_topic_name=”Test”

    def on_connect(client, userdata, flags, rc): print(“Connected with result code “+str(rc))

    def on_message(client, userdata, msg): print(msg.topic+” “+str(msg.payload))

    mqtt_client=mqtt.Client(“P1”) client.on_connect = on_connect client.on_message = on_message

    print(“connecting to broker”) client.connect(broker_address,mqtt_broker_port,keepalive=60)

    client.loop_start()

    print(“Subscribing to topic”) client.subscribe(mqtt_subscription_topic_name) print(“Publishing message to topic”)

    client.publish(mqtt_subscription_topic_name,”OFF”)
    client.loop_forever()

    Can you tell me where i am making a mistake(using explicit path of the mqtt also getting the same error)?

    1. Hi
      You shouldn’t use loop_forever and looP-start in same script. Replace the lopp_forever with a while loop to hold the script.
      while True:
      pass

Leave a Reply

Your email address will not be published. Required fields are marked *