首页 > 编程语言 >【Java代码调用华为云IoT MQTT】

【Java代码调用华为云IoT MQTT】

时间:2024-05-31 14:57:33浏览次数:26  
标签:mqttClient Java String IoT eclipse MQTT new org paho

目录

在这里插入图片描述
欢迎关注微信公众号:数据科学与艺术 作者WX:superhe199

下面是使用Java代码调用华为云IoT MQTT:

import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;

public class HuaweiCloudMqttExample {

    public static void main(String[] args) {
        String brokerAddress = "c065e19a05.iot-mqtts.cn-north-4.myhuaweicloud.com";
        int brokerPort = 1883;
        String clientId = "62e0ee8a4c7c4e3646bcaf4d_0001_0_0_2022072707";
        String username = "62e0ee8a4c7c4e3646bcaf4d_0001";
        String password = "13b9e75dddad59a7fd4cebc00a2c14a355820ec04b0af83d40f3c6ca24a42fd4";
        String topic = "$oc/devices/62e0ee8a4c7c4e3646bcaf4d_0001/sys/properties/report";

        try {
            MqttClient mqttClient = new MqttClient(
                    String.format("tcp://%s:%d", brokerAddress, brokerPort), clientId, new MemoryPersistence());
            MqttConnectOptions connectOptions = new MqttConnectOptions();
            connectOptions.setUserName(username);
            connectOptions.setPassword(password.toCharArray());

            mqttClient.connect(connectOptions);

            MqttMessage message = new MqttMessage("Hello, Huawei IoT".getBytes());
            message.setQos(0);

            mqttClient.publish(topic, message);

            mqttClient.disconnect();
            mqttClient.close();
        } catch (MqttException e) {
            e.printStackTrace();
        }
    }

}

请注意,这里使用的是Paho MQTT客户端库来进行MQTT通信,你需要将以下依赖项添加到你的项目中:

<dependency>
    <groupId>org.eclipse.paho</groupId>
    <artifactId>org.eclipse.paho.client.mqttv3</artifactId>
    <version>1.2.4</version>
</dependency>

在代码中,将brokerAddress替换为你的Broker地址,将brokerPort替换为你的Broker端口,将clientIdusernamepassword替换为你的设备相关信息,将topic替换为你要发布消息的主题。然后运行代码即可发送消息到华为云IoT平台。

下面是使用Java代码调用和订阅华为的MQTT的示例代码:

import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;

public class MqttClientExample {

    public static void main(String[] args) {
        String broker = "tcp://c065e19a05.iot-mqtts.cn-north-4.myhuaweicloud.com:1883";
        String clientId = "62e0ee8a4c7c4e3646bcaf4d_0001_0_0_2022072707";
        String username = "62e0ee8a4c7c4e3646bcaf4d_0001";
        String password = "13b9e75dddad59a7fd4cebc00a2c14a355820ec04b0af83d40f3c6ca24a42fd4";
        String topic = "$oc/devices/62e0ee8a4c7c4e3646bcaf4d_0001/sys/properties/report";

        MemoryPersistence persistence = new MemoryPersistence();
        try {
            MqttClient mqttClient = new MqttClient(broker, clientId, persistence);

            MqttConnectOptions connOpts = new MqttConnectOptions();
            connOpts.setCleanSession(true);
            connOpts.setUserName(username);
            connOpts.setPassword(password.toCharArray());

            mqttClient.setCallback(new MqttCallback() {
                @Override
                public void connectionLost(Throwable throwable) {
                    System.out.println("Connection lost!");
                }

                @Override
                public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception {
                    System.out.println("Received message: " + new String(mqttMessage.getPayload()));
                }

                @Override
                public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
                    // Not used in this example
                }
            });

            mqttClient.connect(connOpts);

            mqttClient.subscribe(topic);

            MqttMessage message = new MqttMessage();
            message.setPayload("Hello from Java!".getBytes());
            mqttClient.publish(topic, message);

            mqttClient.disconnect();
        } catch (MqttException me) {
            me.printStackTrace();
        }
    }
}

请注意,这是使用Eclipse Paho MQTT客户端库来实现的。您需要将以下Maven依赖项添加到您的项目中:

<dependency>
    <groupId>org.eclipse.paho</groupId>
    <artifactId>org.eclipse.paho.client.mqttv3</artifactId>
    <version>1.2.5</version>
</dependency>
<dependency>
    <groupId>org.eclipse.paho</groupId>
    <artifactId>org.eclipse.paho.android.service</artifactId>
    <version>1.1.1</version>
</dependency>

请确保您已经正确地替换了brokerclientIdusernamepasswordtopic变量的值。此示例中的代码将连接到指定的华为MQTT代理,订阅指定的主题,并向该主题发布一条消息。

希望这可以帮助您开始使用华为的MQTT服务!

标签:mqttClient,Java,String,IoT,eclipse,MQTT,new,org,paho
From: https://blog.csdn.net/qq_31532979/article/details/139069679

相关文章