Develop the first python client application by Paho
I. Purpose:
1. Try to send messages between 2 clients via MQTT protocol. Then, we will create 2 clients:
- One for publishing (or send) the message. Other for subscribing (or receive message).
II. Pre-condition:
- You already installed Mosquitto broker, Python.
III. Step by step
1. Download example from Github
https://github.com/eclipse/paho.mqtt.python
2. Modify subscriber(client) source
In folder example/client_sub.py. Modify some pieces of source code.
before
mqttc.connect("mqtt.eclipse.org", 1883, 60)
mqttc.subscribe("$SYS/#", 0)
after
mqttc.connect("192.168.203.76", 1883, 60)
mqttc.subscribe("test/topic1", 0)
note:
"192.168.203.76": IP address of machine which Mosquito broker installed on.
1883: mqtt port
3. Modify publisher(client) source
In folder example/publish_single.py.
before:
publish.single("paho/test/single", "boo", hostname="mqtt.eclipse.org")
after:
publish.single("test/topic1", "Let's make your passion great again", hostname="192.168.203.76")
4. Start broker
Open a command prompt and type the following text:
mosquitto -p 1883
5. Start subscriber
Open the second command prompt and go to example directory by "cd" command:
cd D:\paho.mqtt.python-master\paho.mqtt.python-master\examples
python client_sub.py
6. Start publisher to send the very first message
Open the third command prompt and type:
python publish_single.py
7. Look at the second command prompt to check the received message.
Comments
Post a Comment