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.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

 

  1. In termina, install the paho-mqtt module with pip using the following command:

 

Sudo pip3 install paho-mqtt

 

  1. Create a new folder by the name of lab_6 and create a new python file mqtt_script_1.py

 

  1. 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()

 

 

  1. 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()

 

 

 

  1. Run the first script by typing in the command

 

python3 mqtt_script_1.py

 

  1. Open up a new terminal and navigate to the folder where the two scripts are created.

 

  1. Run the second script in the new window by typing the command

 

python3 mqtt_script_2.py

 

  1. 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.
Expert Solution
steps

Step by step

Solved in 4 steps with 9 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY