package com.baidu.iot;
import java.io.InputStream;
import java.security.KeyStore;
import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttMessage;
public class Thermometer {
public static void main(String[] args) throws Exception {
String endpoint = “hostname”; //输入创建实例endpoint返回的hostname
String username = “yourendpoint/yourthing”; //输入创建thing返回的username
String password = “Dm3yyvOHb7zt/uRWsPgsfsgbnj7CxuVMc+uDbf4j960=”; //输入创建principal返回的password
String topic = “building-b/floor-5/temperature-1”; //订阅的消息主题,本例是指订阅b号楼第五层的温度
TrustManagerFactory tmf = TrustManagerFactory.getInstance(“X509”);
tmf.init((KeyStore)null);
TrustManager[] trustManagers = tmf.getTrustManagers();
SSLContext ctx = SSLContext.getInstance(“TLS”);
ctx.init(null, trustManagers, null);
MqttConnectOptions options = new MqttConnectOptions();
options.setCleanSession(true);
options.setUserName(username);
options.setPassword(password.toCharArray());
options.setSocketFactory(ctx.getSocketFactory());
MqttClient client = new MqttClient(endpoint, “java-client”);
client.connect(options);
MqttMessage message = new MqttMessage();
message.setPayload(“15”.getBytes());
client.publish(topic, message);
client.disconnect();
}
}
本文章来源于互联网,如有侵权,请联系删除!原文地址:python iot hub_国内物联网平台(1) ——百度物接入IoT Hub
相关推荐: MQTT与物联网平台(一):基础知识和设备接入物联网平台的实现(以华为云为例)
文章目录 前言 一、MQTT是什么? 二、接入物联网平台(以华为云为例) 1.创建产品和新增设备 2.建立TCP连接 3.建立MQTT连接 三、学习交流群 前言 本文简单介绍了MQTT的基础知识和使用方法,以蓝牙WIFI双模芯片W801作为硬件设备,接入到华为…