Nataliya Lisjo, student ECUtbildning Software developer in Industrial IoT, december 2020 <h2>============================</h2> # Mosquitto MQTT protocol -- computer is your MQTT Broker ![](https://i.imgur.com/se4obih.png) -download "mosquitto-1.6.10a-install-windows-x64" -go to "cmd" in your PC -> runs as administrator -write "net start mosquitto" --> now, your comp is BROKER MQTT--> -to stop "net stop mosquitto" ![](https://i.imgur.com/8bTtymm.png) <h2>VS code & paho-mqtt 1.5.1</h2> go to https://pypi.org/project/paho-mqtt/ copy string "pip install paho-mqtt" and paste in VSCode -> new terminal . This file will be installed by itself in your VScode editor Type in terminal : "pip list" --> you can see the list of installed libraries to controll <h2> Create MQTT folder </h2> with 2 files which you can find bellow "**CODE**": pub.py ---- publish sub.py --------subscribe <h2> Start run MQTT protocol </h2> Open 2 windows in Windows PowerShell in right folder MQQT ![](https://i.imgur.com/NIiodmA.png) Type "ls" (l~~i~~s~~t~~),then you can see ,if everythings see each other ![](https://i.imgur.com/vpPq68b.png) in 1 windows type "python .\pub.py" --- Enter in 2 windows type "python .\sub.py" --- Enter now it should send message and ??? ____________ got mistake somewhere ![](https://i.imgur.com/cjTAo5u.png) now everythings work it was mistake in topic name in file pub.py . different topics name in files sub.py and pub.py in code. ![](https://i.imgur.com/KTViA7M.png) ![](https://i.imgur.com/7e4aYZR.png) good luck... # Code Save those 2 file in MQQT folder separate pub.py ---- publish ``` import random import time from paho.mqtt import client as mqtt_client broker = 'localhost' port = 1883 topic = "/python/mqtt" client_id = f'python-mqtt-{random.randint(0,1000)}' def connect_mqtt(): def on_connect(client, userdata, flags ,rc): if rc == 0: print("Connected to MQTT Broker!") else: print("Failed to connect") client = mqtt_client.Client(client_id) client.on_connect = on_connect client.connect(broker,port) return client def publish(client): msg_count = 0 while True: time.sleep(1) msg = f"message: {msg_count}" result = client.publish(topic, msg) status = result[0] if status == 0: print(f" Send '{msg}' to topic '{topic}'") else: print(f" Failed to send message to topic: '{topic}'") msg_count += 1 def run(): client = connect_mqtt() client.loop_start() publish(client) if __name__ == '__main__': run() ``` sub.py --------subscribe ``` import random from paho.mqtt import client as mqtt_client broker = "localhost" port = 1883 topic = "/python/mqtt" client_id = f'python-mqtt-{random.randint(0,1000)}' def connect_mqtt() -> mqtt_client: def on_connect(client, userdata, flags, rc): if rc == 0: print("Connected to MQTT Broker") else: print("Failed to connect!") client = mqtt_client.Client(client_id) client.on_connect = on_connect client.connect(broker,port) return client def subscribe(client:mqtt_client): def on_message(client, userdata, msg): print(f"Received '{msg.payload.decode()}' from '{msg.topic}' topic") client.subscribe(topic) client.on_message = on_message def run(): client = connect_mqtt() subscribe(client) client.loop_forever() if __name__ == '__main__': run() ``` ====================================== <!-- hitwebcounter Code START --> <a href="https://www.hitwebcounter.com" target="_blank"> <img src="https://hitwebcounter.com/counter/counter.php?page=7729729&style=0001&nbdigits=5&type=page&initCount=0" title="Total Website Hits" Alt="Web Hits" border="0" /></a>