目录
Moswuitto库
Mosquitto基本API
常用函数介绍
C编程实现代码
Head File
C Source File
Make File
测试案例
连接前
阿里云设备状态
连接后
运行程序
阿里云设备运行状态
发送下行消息
运行结果
树莓派下配置mosquitto详解,有关于如何在树莓派下配置mosquitto大家可以参考看一下这篇博客。
树莓派下mosquitto的shell命令的使用,有关如mosquitto的shell命令的使用。大家可以 参考一下这篇博客。
-
Moswuitto库
Mosquitto官方库
-
Mosquitto基本API
-
常用函数介绍
1int mosquitto_lib_init(void);
作用:初始化mosquitto lib,必须在任何其他mosquitto函数之前调用。
返回值:MOSQ_ERR_SUCCESS
1struct mosquitto *mosquitto_new(const char *id,bool clean_session,void *obj)
作用:创建新的mosquitto实例
参数:
第一个参数id,用作mosquitto实例id的字符串。如果为空,将生成随机id。如果id为空,则clean_session必须为ture。
第二个参数clean_session,设置为true可指示代理清除断开连接时的所有消息和订阅,设置为false可指示代理保留这些消息和订阅。
第三个参数obj指针,作为参数传递给指定回调。
返回值:失败返回空指针,错误信息在errno,成功返回mosquitto实例。
1void mosquitto_destroy(struct mosquitto *mosq);
作用:用来释放mosquitto实例关联的内存;
参数:struct mosquitto *mosq是mosquitto_new返回的实例。
1int mosquitto_lib_cleanup(void);
作用:用来释放库相关的资源
1int mosquitto_connect(struct mosquitto *mosq,const char *host,int port,int keepalive);
作用:连接mqtt服务器
参数:
第一个参数*mosq是mosquitto_new返回的实例
第二个参数是mqtt服务器的ip地址。
第三个参数是mqtt服务器的端口
第四个参数是超时时间。
返回值:
成功:MOSQ_ERR_SUCCESS
1int mosquitto_publish(struct mosquitto *mosq,int *mid,const char *topic,int payloadlen,const void *payload,int qos,bool retain);
作用:消息发布
参数:
第一个参数*mosq是mosquitto_new返回的实例;
第二个参数*mid是int类型的指针。如果不为NULL,函数会将其设置为此特定消息的消息ID。然后,可以将其与发布回调一起使用,以确定何时发送消息;
第三个参数是*topic发布的主题;
第四个参数payloadlen是载荷长度;
第五个参数*payload是载荷;
第六个参数qos是服务质量;
第七个参数retain设置为true可保留消息。
返回值:
成功:MOSQ_ERR_SUCCESS
1
2int mosquitto_subscribe(struct mosquitto *mosq,int *mid,const char *sub,int qos)
作用:消息订阅
参数:
第一个参数*mosq是mosquitto_new返回的实例;
第二个参数*mid是int类型的指针。如果不为NULL,函数会将其设置为此特定消息的消息ID。然后,可以将其与发布回调一起使用,以确定何时发送消息;
第三个参数是*topic订阅的主题;
第四个参数qos是服务质量。
返回值:
成功:MOSQ_ERR_SUCCESS
-
C编程实现代码
源码Git仓库
-
Head File
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51/******************************************************************************** * Copyright: (C) 2020 ysn * All rights reserved. * * Filename: mosquitto_sub.h * Description: This head file is mosquitto_sub. * * Version: 1.0.0(28/07/20) * Author: tianjincheng <473892093@qq.com> * ChangeLog: 1, Release initial version on "28/07/20 05:27:49" * ********************************************************************************/ #ifndef _MOSQUITTO_SUB_H_ #define _MOSQUITTO_SUB_H_ #include <stdio.h> #include <string.h> #include <stdlib.h> #include <getopt.h> #include <libgen.h> #include <unistd.h> #include <errno.h> #include "mosquitto.h" #define BUF_SIZE 128 /* * if define MAIN: will use this file's main func to test. * if not define MAIN: will use the mian file's mian func to test. * */ #define MAIN typedef struct mosquitto_s { char *host; int port; char *topic; char *client_id; char *user; char *password; int keepalive; char *recv_message; } mosquitto_t; /* --- end of struct mosquitto_s ---*/ extern int init_mosquitto_argc (int argc, char **argv, mosquitto_t *mosquitto); extern int mosquitto_sub_init (struct mosquitto *mosqut, mosquitto_t *mosquitto); #endif /* ----- #ifndef _MOSQUITTO_SUB_H_ ----- */
-
C Source File
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295/********************************************************************************* * Copyright: (C) 2020 ysn * All rights reserved. * * Filename: moquito.c * Description: This file is mosquit API. * * Version: 1.0.0(27/07/20) * Author: tianjincheng <473892093@qq.com> * ChangeLog: 1, Release initial version on "27/07/20 08:06:42" * ********************************************************************************/ #include "mosquitto_sub.h" #include "mosquitto.h" static int g_stop = 0; static void print_usage (char *progname); static void connect_callback (struct mosquitto *mosqut, void *obj, int rc); static void disconnect_callback (struct mosquitto *mosqut, void *obj, int rc); static void message_callback (struct mosquitto *mosqut,void *obj, const struct mosquitto_message *msg); static void subscribe_callback (struct mosquitto *mosqut, void *obj, int mid, int qos_count, const int *granted_qos); static void mosquitto_sub_clean (struct mosquitto *mosqut, void (disconnect_callback)(struct mosquitto *mosqut, void *, int)); /************************************************************************************** * Description: Start to mosquitto subscribe func. * Input Args: * mosqut: The mosquitto lib provide the struct of mosquitto. * mosquitto :The uesr provide arguments. * Output Args: No. * Return Value: 0:SUCCESS; other: ERROR. *************************************************************************************/ int mosquitto_sub_start (struct mosquitto *mosqut, mosquitto_t *mosquitto) { int rv = 1; rv = mosquitto_lib_init(); if (rv) { printf("Init mosquitto lib error:%sn", strerror(errno)); return rv; } mosqut = mosquitto_new(mosquitto->client_id, true, (void *)mosquitto); if (!mosqut) { printf("Mosquitto new error:%sn", strerror(errno)); return rv; } else printf("Mosquitto new success.n "); mosquitto_connect_callback_set(mosqut, connect_callback); mosquitto_message_callback_set(mosqut, message_callback); mosquitto_username_pw_set(mosqut, mosquitto->user, mosquitto->password); mosquitto_subscribe_callback_set(mosqut, subscribe_callback); while (!g_stop) { rv = mosquitto_connect(mosqut, mosquitto->host, mosquitto->port, mosquitto->keepalive); if (rv) { printf("Connect server error:%sn", strerror(errno)); sleep(1); continue; } else printf("mosquitto connect success.n"); mosquitto_loop_forever(mosqut, -1, 1); if( rv != MOSQ_ERR_SUCCESS ) { printf("%sn", strerror(errno)); mosquitto_sub_clean(mosqut, disconnect_callback); } } return 0; } /* ----- End of mosquitto_sub_start() ----- */ /************************************************************************************** * Description: To clean after mosquitto subscribe get disconnected. * Input Args: * mosqut: The mosquitto lib provide the struct of mosquitto. * disconnect_callback: The func pointer which is point disconnect_callback func. * Output Args: No. * Return Value: No. *************************************************************************************/ static void mosquitto_sub_clean (struct mosquitto *mosqut, void (disconnect_callback)(struct mosquitto *mosqut, void *, int)) { mosquitto_disconnect_callback_set(mosqut, disconnect_callback); mosquitto_destroy(mosqut); mosquitto_lib_cleanup(); } /* ----- End of mosquitto_sub_clean() ----- */ #ifdef MAIN int main (int argc, char **argv) { int rv = -1; mosquitto_t mosquitto; struct mosquitto *mosqut; memset(&mosquitto, 0, sizeof(mosquitto_t)); rv = init_mosquitto_argc(argc, argv, &mosquitto); if (rv) { printf("Init mosquitto arguments error:%sn", strerror(errno)); } rv = mosquitto_sub_start(mosqut, &mosquitto); if (rv) { printf("mosquitto subscribe start error:%sn", strerror(errno)); } mosquitto_sub_clean(mosqut, disconnect_callback); return 0; } /* ----- End of main() ----- */ #endif /************************************************************************************** * Description: Init mosquitto argcs. * Input Args: agrc & argv : form main func, mosquitto: the struct of mosquitto need init argc. * Output Args: NO. * Return Value: -1: ERROR, 0: SUCCESS. *************************************************************************************/ int init_mosquitto_argc (int argc, char **argv, mosquitto_t *mosquitto) { if (argc < 0 ) return -1; int ch = -1; struct option opts[] = { {"--host", required_argument, NULL, 'H'}, {"--port", required_argument, NULL, 'p'}, {"--topic", required_argument, NULL, 't'}, {"--client_id", required_argument, NULL, 'i'}, {"--user", required_argument, NULL, 'u'}, {"--password", required_argument, NULL, 'P'}, {"--keepalive", required_argument, NULL, 'l'}, {"help", no_argument, NULL, 'h'}, {NULL, 0, NULL, 0 } }; while((ch=getopt_long(argc, argv, "H:p:t:i:u:P:l:h", opts, NULL)) != -1 ) { switch(ch) { case 'H': mosquitto->host = optarg; break; case 'p': mosquitto->port = atoi(optarg); break; case 't': mosquitto->topic = optarg; break; case 'i': mosquitto->client_id = optarg; break; case 'u': mosquitto->user = optarg; break; case 'P': mosquitto->password = optarg; break; case 'l': mosquitto->keepalive = atoi(optarg); break; case 'h': print_usage(argv[0]); return -1; } } mosquitto->recv_message = NULL; if (NULL == mosquitto->host || !mosquitto->keepalive ||!mosquitto->port || NULL == mosquitto->client_id || NULL == mosquitto->password || NULL == mosquitto->user || NULL == mosquitto->topic) { print_usage(argv[0]); return -1; } return 0; } /* ----- End of inin_mosquitto_argc() ----- */ /************************************************************************************** * Description: Print usage help information. * Input Args: The program name. * Output Args: NO. * Return Value: NULL. *************************************************************************************/ static void print_usage (char *progname) { printf("%s usage : n", progname); printf("-H(--host):Input the Aliyun equioment host name.n"); printf("-p(--port):Input the Aliyun equioment host port.n"); printf("-i(--client_id):Input the Aliyun equioment client id.n"); printf("-t(--topic):Input the Aliyun equioment subscribe topic.n"); printf("-u(--user):Input the Aliyun equioment user name.n"); printf("-P(--password):Input the Aliyun equioment user's password name.n"); printf("-l(--keepalive):Input the keepalive.n"); printf("-h(--help): print this help information.n"); return ; } /* ----- End of print_usage() ----- */ /************************************************************************************** * Description: mosquitto subscribe connect call back func. * Input Args: * mosquitto: the struct of mosquitto argc; * obj: mosquitto_new provide argument; * rc: 0: connect success, else connect error. * Output Args: NO. * Return Value: -1: ERROR, 1: SUCCESS. *************************************************************************************/ static void connect_callback (struct mosquitto *mosqut, void *obj, int rc) { if (NULL == obj) return ; mosquitto_t *mosquitto; int mid; mosquitto = (mosquitto_t *)obj; if (!rc) { if (mosquitto_subscribe(mosqut, &mid, mosquitto->topic, 0) != MOSQ_ERR_SUCCESS) { printf("Set the topic error:%sn", strerror(errno)); return ; } else printf("subscribe topic:%s success.n", mosquitto->topic); } else printf("connect_callback errorn"); } /* ----- End of connect_callback() ----- */ /************************************************************************************** * Description: mosquitto subscribe disconnect call back func. * Input Args: * mosqut: the struct of mosquitto argc; * obj: mosquitto_new provide argument; * rc: 0: connect success, else connect error. * Output Args: NO. * Return Value: -1: ERROR, 0: SUCCESS. *************************************************************************************/ static void disconnect_callback (struct mosquitto *mosqut, void *obj, int rc) { g_stop = 1; } /* ----- End of disconnect_callback() ----- */ /************************************************************************************** * Description: The subscribe callback func. * Input Args: The system provide agrcs. * Output Args: No; * Return Value: No. *************************************************************************************/ static void subscribe_callback (struct mosquitto *mosqut, void *obj, int mid, int qos_count, const int *granted_qos) { printf("Call the function: on_subscriben"); } /* ----- End of subscribe_callback() ----- */ /************************************************************************************** * Description: The message callback func. * Input Args: * mosqut: the struct of mosquitto; * obj: the subscribe new provide user argument; * msg: the recv broker informations. * Output Args: No. * Return Value: No. *************************************************************************************/ static void message_callback (struct mosquitto *mosqut,void *obj, const struct mosquitto_message *msg) { mosquitto_t *mosquitto; mosquitto = (mosquitto_t *)obj; memcpy(&mosquitto-> recv_message, (char *)&msg->payload, strlen((char *)msg->payload)); printf("recv_message: %sn", mosquitto->recv_message); if (0 == strcmp(msg->payload, "quit")) mosquitto_disconnect(mosqut); } /* ----- End of message_callback() ----- */
-
Make File
1
2
3
4
5
6
7
8
9
10
11C = gcc OBJ= mosquitto_sub.o OUTPUT = mosquitto_sub ALL: ${OBJ} ${CC} ${CFLAGS}${OBJ} -o ${OUTPUT} -lmosquitto ${LDFLAGS} .PHONY: clean clean: -rm -rf *.o ${OUTPUT}
-
测试案例
-
连接前
-
阿里云设备状态
-
连接后
-
运行程序
-
阿里云设备运行状态
-
发送下行消息
-
运行结果
最后
以上就是单纯抽屉最近收集整理的关于树莓派环境下利用mosquitto库使用C编程完成与阿里云的上行通信(Subscribe))的全部内容,更多相关树莓派环境下利用mosquitto库使用C编程完成与阿里云内容请搜索靠谱客的其他文章。
发表评论 取消回复