In termina, install the paho-mqtt module with pip using the following command: Sudo pip3 install paho-mqtt Create a new folder by the name of lab_6 and create a new python file mqtt_script_1.py In the file, TYPE the following code: import paho.mqtt.client as mqtt # The callback for when the client receives a CONNACK response from the server. def on_connect(client, userdata, flags, rc): print("Connected with result code "+str(rc)) # Subscribing in on_connect() means that if we lose the connection and # reconnect then subscriptions will be renewed. client.subscribe("etec224_mqtt_test/") # The callback for when a PUBLISH message is received from the server. def on_message(client, userdata, msg): print(msg.topic+" "+str(msg.payload)) client = mqtt.Client() client.on_connect = on_connect client.on_message = on_message client.connect("broker.emqx.io", 1883, 60) client.loop_forever() Create another file py and add the following code to it. import paho.mqtt.client as mqtt import time # The callback for when the client receives a CONNACK response from the server. def on_connect(client, userdata, flags, rc): print("Connected with result code "+str(rc)) # Subscribing in on_connect() means that if we lose the connection and reconnect then subscriptions will be renewed. client.subscribe("etec224_mqtt_test/") global Connected Connected = True Connected = False client = mqtt.Client() client.on_connect = on_connect client.connect("broker.emqx.io", 1883, 60) client.loop_start() while Connected!= True: #Wait Here time.sleep(0.1) try: while True: value = input('Enter Message : ') client.publish('etec224_mqtt_test/', value) except KeyboardInterrupt: client.disconnect() client.loop_stop() Run the first script by typing in the command python3 mqtt_script_1.py Open up a new terminal and navigate to the folder where the two scripts are created. Run the second script in the new window by typing the command python3 mqtt_script_2.py Any message typed in the second window should appear in the first window. If the same code is experimented with by all the students in the class, then all messages from all the students will appear in all the output windows across the lab.
- In termina, install the paho-mqtt module with pip using the following command:
Sudo pip3 install paho-mqtt
- Create a new folder by the name of lab_6 and create a new python file mqtt_script_1.py
- In the file, TYPE the following code:
import paho.mqtt.client as mqtt
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe("etec224_mqtt_test/")
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
print(msg.topic+" "+str(msg.payload))
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("broker.emqx.io", 1883, 60)
client.loop_forever()
- Create another file py and add the following code to it.
import paho.mqtt.client as mqtt
import time
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
# Subscribing in on_connect() means that if we lose the connection and reconnect then subscriptions will be renewed.
client.subscribe("etec224_mqtt_test/")
global Connected
Connected = True
Connected = False
client = mqtt.Client()
client.on_connect = on_connect
client.connect("broker.emqx.io", 1883, 60)
client.loop_start()
while Connected!= True:
#Wait Here
time.sleep(0.1)
try:
while True:
value = input('Enter Message : ')
client.publish('etec224_mqtt_test/', value)
except KeyboardInterrupt:
client.disconnect()
client.loop_stop()
- Run the first script by typing in the command
python3 mqtt_script_1.py
- Open up a new terminal and navigate to the folder where the two scripts are created.
- Run the second script in the new window by typing the command
python3 mqtt_script_2.py
- Any message typed in the second window should appear in the first window. If the same code is experimented with by all the students in the class, then all messages from all the students will appear in all the output windows across the lab.

Step by step
Solved in 4 steps with 9 images









