java 上报 thingsboard 遥测属性 · 物联网平台-威尼斯人最新

download · 2020年03月25日 · 最后由 回复于 2022年08月24日 · 599 次阅读

import org.apache.commons.lang.stringutils;
import org.apache.log4j.logger;
import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.persist.memorypersistence;
import javax.servlet.servletexception;
import javax.servlet.http.httpservlet;
import java.io.datainputstream;
import java.io.ioexception;
import java.io.outputstream;
import java.math.biginteger;
import java.net.serversocket;
import java.net.socket;
import java.net.unknownhostexception;
import java.text.decimalformat;
import java.util.arraylist;
import java.util.hashmap;
import java.util.list;
import java.util.map;
public class clientdemo {
    /**
     * @param args
     * @throws ioexception
     * @throws unknownhostexception
     */
    private static logger logger=logger.getlogger(clientdemo.class);
    public void totb(string content){
        logger.info("开始执行dht方法*************************");
        string host = "tcp://47.99.141.22:1883"; //tbip地址和mqtt端口
        string topic = "v1/devices/me/telemetry";//tb遥测数据api接口
        string clientid = "client2";
        mqttclient client=null;
        mqttconnectoptions options;
        mqtttopic topic;
        string username="iqhq2135klo1ptvb6hcy";//设备秘钥
        try {
            // host为主机名,clientid即连接mqtt的客户端id,一般以唯一标识符表示,memorypersistence设置clientid的保存形式,默认为以内存保存
            client = new mqttclient(host, clientid, new memorypersistence());
            // mqtt的连接设置
            options = new mqttconnectoptions();
            // 设置是否清空session,这里如果设置为false表示服务器会保留客户端的连接记录,这里设置为true表示每次连接到服务器都以新的身份连接
            options.setcleansession(true);
            // 设置连接的用户名
            options.setusername(username);//设备秘钥
            // 设置连接的密码
            //options.setpassword(password.tochararray());
            // 设置超时时间 单位为秒
            options.setconnectiontimeout(10);
            // 设置会话心跳时间 单位为秒 服务器会每隔1.5*20秒的时间向客户端发送个消息判断客户端是否在线,但这个方法并没有重连的机制
            options.setkeepaliveinterval(20);
            // 设置回
            client.setcallback(new mqttcallbackextended() {
                @override
                public void messagearrived(string topic, mqttmessage message) throws exception {
                    string context = new string(message.getpayload());
                }
                @override
                public void deliverycomplete(imqttdeliverytoken token) {
                    // todo auto-generated method stub
                }
                @override
                public void connectionlost(throwable cause) {
                    // todo auto-generated method stub
                }
                @override
                public void connectcomplete(boolean reconnect, string serveruri) {
                    // todo auto-generated method stub
                }
            });
            topic = client.gettopic(topic);
            //setwill方法,如果项目中需要知道客户端是否掉线可以调用该方法。设置最终端口的通知消息
            options.setwill(topic, "close".getbytes(), 2, true);
            client.connect(options);
//            sendcon = "{\"temperature\":\"25\",\"humidity\":\"12\",\"light\":\"255\"}";//json格式
            client.publish(topic, content.getbytes(), 0, false);//推送数据到tb上
        } catch (mqttexception e) {
            e.printstacktrace();
        } catch (illegalargumentexception e) {
            e.printstacktrace();
        } finally {
        }
    }
}

thingsboard 话题讨论区:https://www.iotschool.com/topics/node8

欢迎大家加入 thingsboard 二次开发讨论群:121202538

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册
网站地图