# 1 Introduction
Protocls 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
![][3]
MQTT.FX Upload Data
![][4]
Check the result on DeviceBit
![][5]
# 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.
![][6]
Then, check the control commad. The result is as follows.
![][7]
> 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.
![][8]
**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.**
![][9]
# 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
[9]: http://doc-resources.lewei50.com/lewei50/img/MQTT-lewei50-20170504-7.jpg
- DeviceBit User Manual
- Quick Start
- 1. Add A Device
- 2. Add a new sensor or controller
- 3. Data Upload Simulation
- 4. WeChat Alert
- 5. Email Alert
- 6. Data Reports
- 7. Account Settings
- 8. WeChat
- 9. Email Auto-sending
- 10. Email Report
- 11. Control Scheme
- 12. Trigger
- 13. MQTT
- 14. Modbus
- 15. Agent
- 16. Credits
- DeviceBit Open Source Tutorials
- 1. DIY WiFi Particles Monitor
- 2. DIY WiFi Particles & HCHO Monitor
- 3. DIY GPRS Particles Monitor
- 4. GPRS Particles Kit V0.1
- 5. WiFi Socket
- Applicaton Doc
- DTU Tutorial 1: Overview
- DTU Tutorial 2: Using DTU to connect JSY-149 single-phase meter to DeviceBit
- DTU Tutorial 3: Using Python+Openwrt DIY DTU & Connect DeviceBit
- DTU Tutorial 4 DIY Low-cost DeviceBit GPRS DTU
- DTU Tutorial 5: DIY Low-cost DeviceBit WIFI DTU
- DeviceBit Other Platform