MQTT Access
Summary
This paper describes how to use the Paho MQTT Eclipse client library to achieve BeOP Data Access Platform for data access
Data format (Web Interface)
JSON
{
"pointName":"_pointName_",
"pointValue":"_pointValue_"
}
Data Description
pointName:the point name in your project pointValue:the point value in your project
Data transmission process
1、Taking RabbitMQ as a MQTT broker, receive messages in JSON format
2、Call real time storage method to store information in real time database
Python Client
The Paho Python Client provides a client class with support for both MQTT v3.1 and v3.1.1 on Python 2.7 or 3.x. It also provides some helper functions to make publishing one off messages to an MQTT server very straightforward.
Download
The Python client can be downloaded and installed from PyPI using the pip tool:
pip install paho-mqtt
Documentation
Full client documentation is available here
Getting Started
Here is a very simple example that subscribes to the broker "YOUR TOPIC NAME" topic tree and send message:
import paho.mqtt.client as mqtt
import json
def on_publish(mqttc, obj, mid):
print("OnPublish, mid: "+str(mid))
if __name__ == '__main__':
mqttc = mqtt.Client(client_id="clientId", clean_session=True, userdata=None)
mqttc.username_pw_set("YOUR NAME", "YOUR PWD")
mqttc.on_publish = on_publish
mqttc.connect(host, 1883, 60)
mqttc.subscribe("YOUR TOPIC NAME", 0)
jsonMessage={"pointName":"YOUR POINTNAME","pointValue":"YOUR POINTVALUE"}
strMessage=json.dumps(jsonMessage)
mqttc.publish("YOUR TOPIC",strMessage, 0, False)
mqttc.disconnect()
JAVA Client
Download
You need to download org.eclipse.paho.client.mqttv3-1.0.1.jar for development
Documentation
Full client documentation is available here
Here is a very simple example that subscribes to the broker "YOUR TOPIC NAME" topic tree and send message:
Implementation of MqttCallback interface
public class MqttHandler implements MqttCallback
Instantiate the MqttClient
MqttClient client = new MqttClient("YOUR URL", clientId);
client.setCallback(this);
//YOUR RUL can be :tcp://serverHost:Port
set connect attribute
MqttConnectOptions options = new MqttConnectOptions();
options.setCleanSession(true);
options.setUserName(userName);
options.setPassword(pwd.toCharArray());
client.connect(options);
client.subscribe("YOUR TOPIC", 0);
Data format: JSON
JSONObject jsonObj = new JSONObject();
jsonObj.put("pointName", tfPointName.getText());
jsonObj.put("pointValue",tfPointValue.getText());
set message and send
MqttMessage mqttMsg = new MqttMessage(jsonObj.toString().getBytes());
mqttMsg.setRetained(false);
mqttMsg.setQos(0);
client.publish("YOUR TOPIC", mqttMsg);
finally ,don't forget to disconnect
client.disconnect();