Hive MQ Cloud
For trying to connect remote students and messaging together, I tested hive MQ cloud.
Sign Up and Login
Just follow instruction at “Sign up now” button in HiveMQ web page.
2. Setup
2.1. Setup credentail (user name and password)
- name: kannai2021
- password: kannai**2021
2.2. “Getting started with Paho Python”
If you do not have python3 virtual environment, run below command at terminal(Mac/Linux) or gitbash(Windows):
| $ python3 -m venv ~/env1
$ source ~/env1/bin/activate
|
Install Paho python
| (env1) $ pip install paho-mqtt
|
3. Run sample code
| (env1) $ mkdir ~/repo/python-paho-hiveMQ-cloud
(env1) $ cd ~/repo/python-paho-hiveMQ-cloud
|
Then create mqtt-client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 | 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):
if rc == 0:
print("Connected successfully")
else:
print("Connect returned result code: " + str(rc))
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
print("Received message: " + msg.topic + " -> " + msg.payload.decode("utf-8"))
# create the client
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
# enable TLS
client.tls_set(tls_version=mqtt.ssl.PROTOCOL_TLS)
# set username and password
client.username_pw_set("kannai2021", "kannai******2021")
# connect to HiveMQ Cloud on port 8883
client.connect("135cfd6aa0594471b9fece89248de675.s1.eu.hivemq.cloud", 8883)
# subscribe to the topic "my/test/topic"
client.subscribe("my/test/topic")
# publish "Hello" to the topic "my/test/topic"
client.publish("my/test/topic", "Hello")
# Blocking call that processes network traffic, dispatches callbacks and handles reconnecting.
client.loop_forever()
|
| (env1) $ python mqtt_client.py
Connected successfully
Received message: my/test/topic -> Hello
Connected successfull
|
Last update: April 30, 2021