拓扑结构

需求

  1. 已知服务器(IP:127.0.0.1,Port:1883)运行的是常规的MQTT_Broker(cloud:0),并且接受任何clientid、用户、密码的连接

  2. zhbox使用以下用户连上MQTT_Broker,并创建2个任务

    • 每间隔30秒,给主题(topic):hello发布(publish)一条消息(payload):{”hello":30}
    • 每间隔60秒,给主题(topic):world发布(publish)一条消息(payload):{”wrold":60}
    1
    2
    3
    clientid = "test1clientid";
    usr = "test1usr";
    pwd = "test1pwd";
  3. PC端打开一个常规MQTT客户端(如:mqtt.fx)使用以下用户连上MQTT_Broker后,订阅主题(topic):hello,应该就能收到zhbox发出并经过MQTT_Broker转发过来的消息:{”hello":"World"}

    1
    2
    3
    clientid = "mqttfx";
    usr = "mqttfxusr";
    pwd = "mqttfxpwd";

配置zhbox

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//filename: test1.cfg
enable = true; //使能
loglevel = 5; //0 Emergency,1 Alert,2 Critical,3 Error,4 Warning,5 Notice,6 Informational,7 Debug

northls = ( //多个连接,数组
{ //第1个连接
cloud = 0; //云平台,0表示常规MQTT_Broker
name = "localgeneral"; //给该连接起个名字

addr = "127.0.0.1"; //Broker端的地址
port = 1883; //Broker端的端口
clean_session = true;
clientid = "test1clientid"; //客户端ID,连接的唯一标识
usr = "test1usr"; //用户名字
pwd = "test1pwd"; //用户密码
keepalive = 60;
qos = 2;

subtopics = []; //订阅的主题
tasksls = ( //任务列表
{ //第1个任务
interval = 30; //间隔30秒
pubtopic = "hello"; //publish to topic 发送到该主题
payloadfmt = "{\"hello\":30}"; //payload format
propertyfmt = "";
properties = ();
},
{ //第2个任务
interval = 60; //间隔60秒
pubtopic = "world"; //publish to topic 发送到该主题
payloadfmt = "{\"world\":60}"; //payload format
propertyfmt = "";
properties = ();
}
);
}
)

运行zhbox

1
zhbox -sc ./test/test1config/test1.cfg -f

运行mqtt.fx

订阅主题(topic):hello

接收消息{"hello":30}