ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
#1 Introduction Protocol supported by DeviceBit MQTT: MQTT 3.1 and 3.1.1 The device can upload data, receive control commands, and return the results, fully compatible with the mode of DeviceBit TCP service. Recommend MQTT client: MQTT.fx # 2 Service Desription 1. Server: mqtt://mqtt.lewei50.com:1883 2. Connect Client Id: using SN or Userkey_ device ID 3. When connecting, the server uses Client Id for verification, and only connections are allowed to be established by validation. 4. Limits Up to 5 connections at most for the same IP, The IP will be banned for 10 minutes if the pub times are more than 20 times or the connection times are more than 10 times for the same IP in 30 seconds. 5. Only subjects that publish or subscribe in the mode of /lw/*/Client Id are allowed Only subjects in the mode of /lw/*/Client Id are allowed to be published or subscribed /lw/u/clientid Upload DAta /lw/c/clientid Control Command /lw/r/clientid Return the result # 3 Cases ## 3.1 Upload Data After Connection, publish the following subject on time:/lw/u/clientid For content format, please refer to:[HTTP upload API][2] ``` javascript [ { "Name":"T1", "Value":"1" }, { "Name":"01H1", "Value":"96.2" } ] ``` MQTT.FX Settings ![][http://doc-resources.lewei50.com/lewei50/img/MQTT-lewei50-20170504-1.jpg] MQTT.FX Upload Data ![][http://doc-resources.lewei50.com/lewei50/img/MQTT-lewei50-20170504-2.jpg] Check the result on DeviceBit ![](https://upload-images.jianshu.io/upload_images/5875248-214c51d83265cfae.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) # 3.2 Receive Control Command Detail Steps: For MQTT.FX and DeviceBit settings, the control service of DeviceBit is needed. The MQTT connection should be set in Device Settings in User Center of DeviceBit, as the following picture shows. ![13-3.png](https://upload-images.jianshu.io/upload_images/5875248-528caf6c5c8a0d67.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) Then, check the control commad. The result is as follows. ![13-12.jpg](https://upload-images.jianshu.io/upload_images/5875248-e3d7bb8e6280b8f9.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) > The result is show: client has subscribed the messages published by DeviceBit {"f":"hello","p1":"1","p2":"2","p3":"3","p4":"4"} > But after a while, there is an error notice. The reason for this is that after the client receive the messages published by the platform, it doesn't subscribe correct reply (This reply is necessary required by DeviceBit). If device is not the control type, the reply is not necessary. ![13-10.png](https://upload-images.jianshu.io/upload_images/5875248-f495cce7ff3145b4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) **The correct operation is after clicking on test on the platform, publish a reply at once, as the following picture shows. Then, the process will be all right.** ![13-13.jpg](https://upload-images.jianshu.io/upload_images/5875248-d4f049723497f609.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) # 4 Control processing logic on device side 1 After connection, suscribe the subject:/lw/c/clientid to receive the control command. The message format received is json: ``` {f:"方法名",p1:"xxx",p2:"xxx",p3:"xxx",p4:"xxx",p5:"xxx"} ``` Except f, the other items are optional and which are needed depends on the actual requirements. 2 Operate after receiving command, and after operation, the result should be returned, and publish the subject: /lw/r/clientid. The message format is ``` javascript { "successful": true, "message": "xxxx", "data":[ { "id": "C1", "value": "1" }, { "id": "C2", "value": "2" } ] } ``` Data should be returned according to actual operation. API Online test for Control Device: http://www.lewei50.com/dev/apitest/11 API Online test for SN Device: http://www.lewei50.com/api/V1/gateway/excuteCommandBySN/设备SN,userkey为空即可 # 5 Client code example(JS) **JS code only for description. For real client, lua on nodemcu is recomended.** ``` javascript var mqtt = require('mqtt'); //var clientId = 'xxxxC3F2'; //SN方式 var clientId = 'xxxxxxb372e484d9be1632b99227899_01'; //userkey_gatewayno方式 var settings = { keepalive: 600, clientId: clientId } var client = mqtt.connect('mqtt://mqtt.lewei50.com:1883', settings); client.on('connect', function () { //订阅控制主题 client.subscribe("/lw/c/" + clientId); //定时上传数据 setInterval(uploadData, 5000) }) //上传数据 var uploadData = function () { client.publish('/lw/u/' + clientId, '[{"Name":"T1","Value":"12"}]'); }; //收到控制消息后,返回结果 client.on('message', function (topic, message) { if (topic == "/lw/c/" + clientId) { var obj = JSON.parse(message); console.log(obj.f); //输出调用的方法 client.publish('/lw/r/' + clientId, JSON.stringify({ successful: true, message: null, data: 'testok' })); } }) ``` [1]: https://github.com/lewei50/lua [2]: http://www.lewei50.com/dev/apiinfo/3 "api" [3]: http://doc-resources.lewei50.com/lewei50/img/MQTT-lewei50-20170504-1.jpg [4]: http://doc-resources.lewei50.com/lewei50/img/MQTT-lewei50-20170504-2.jpg [5]: http://doc-resources.lewei50.com/lewei50/img/MQTT-lewei50-20170504-3.jpg [6]: http://doc-resources.lewei50.com/lewei50/img/MQTT-lewei50-20170504-4.jpg [7]: http://doc-resources.lewei50.com/lewei50/img/MQTT-lewei50-20170504-5.jpg [8]: http://doc-resources.lewei50.com/lewei50/img/MQTT-lewei50-20170504-6.jpg