做这项工作之前,我已经做了一些准备工作了,我之前写的 “基于mosquitto库搭建mqtt客户端发布实现与阿里云的通信”以及“基于树莓派3B SHT20温湿度采样”都是为此项目做好准备的,大家如果想要完成树莓派与阿里云的通信,请务必先了解一下,我之前的博客,链接:基于树莓派3B SHT20温湿度采样
基于mosquitto库搭建mqtt客户端发布实现与阿里云的通信
再有了这些准备工作之后,我们就可以完成此项任务了,首先我们要了解基本的流程:如下图
我把sht20封装成一个函数写在了回调函数里面,然后采样温湿度,打包成josn格式,发送给阿里云。
在这里我们先来看一下我代码中怎么把温度和湿度,打包成josn格式的,首先看josn文件。
关于此篇博客,跟我之前那个“基于mosquitto库搭建mqtt客户端发布实现与阿里云的通信”的博客很像,只不过,我只是在原来的代码的基础上加了sht20的温湿度采样的函数,所以在这里,我只附上主函数的源码吧,关于更多源码,我会在下面给出gitee的链接地址。
主函数源码:
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/********************************************************************************* * Copyright: (C) 2020 makun<1394987689@qq.com> * All rights reserved. * * Filename: mosquitto_pub.c * Description: This file * * Version: 1.0.0(2020年07月08日) * Author: makun <1394987689@qq.com> * ChangeLog: 1, Release initial version on "2020年07月08日 22时30分04秒" * ********************************************************************************/ #include <stdio.h> #include <errno.h> #include <unistd.h> #include <libgen.h> #include <getopt.h> #include <string.h> #include <mosquitto.h> #include <stdlib.h> #include "conf.h" #include "cJSON.h" #include "sht20.h" #define PROG_VERSION "1.0.0" #define PATH_INT "./mqtt_aly_conf.ini" int get_temp_humidity(int fd, float *temp, float *humidity); void pub_conn_callback(struct mosquitto *mosq, void *obj, int rc); static void print_usage( char *progname) { printf("Usage %s [option]...n", progname); printf("%s is makun studi MQTT daemon program running on RaspberryPin", progname); printf("-p (--port): the port of the server you want to connectn"); printf("-h (--host): the hostname of the server you want to connectn"); printf("-u (--user): the username of the clientn"); printf("-P (--passwd): the passwd of the client youn"); printf("-i (--clientid): the clientid of the usern"); printf("-t (--topic): the topic of the client you want to pubn"); printf("-H (--help): Display this help informationn"); printf("-v (--version): Display the program versionn"); printf("%s Version %sn", progname, PROG_VERSION); return ; } int main (int argc, char **argv) { char *host = NULL; int port ; char *clientid = NULL; char *user = NULL; char *passwd = NULL; char *topic = NULL; int rv = -1; int opt= -1; char *progname = NULL; bool session = true; mqtt_ctx_t mqtt; struct mosquitto *mosq = NULL; struct option long_options[]= { {"host", required_argument, NULL, 'h'}, {"port", required_argument, NULL, 'p'}, {"user", required_argument, NULL, 'u'}, {"passwd",required_argument, NULL,'P'}, {"topic", required_argument, NULL, 't'}, {"clientid", required_argument, NULL, 'i'}, {"help", no_argument, NULL, 'H'}, {"version", no_argument, NULL, 'v'}, {0, 0, 0, 0} }; progname = (char *)basename(argv[0]); while( (opt = getopt_long(argc, argv,"h:p:u:P:i:tHv", long_options,NULL)) != -1) { switch (opt) { case 'h': host = optarg; break; case 'p': port = atoi(optarg); break; case 'u': user = optarg; break; case 'P': passwd = optarg; break; case 'i': clientid = optarg; case 't': topic = optarg; break; case 'v': printf("%s Version %sn",progname, PROG_VERSION); return 0; case 'H': print_usage(progname); return 0; default: break; } } rv=set_mqtt_conf(PATH_INT, host, port, clientid, user,passwd, topic); if(rv < 0) { printf("set mqtt conf is failure %dn", rv); return -1; } memset(&mqtt, 0, sizeof(mqtt)); rv = gain_mqtt_conf(PATH_INT, &mqtt); if(rv < 0) { printf("gain mqtt conf failure %dn", rv); return -2; } /*必须在任何其他mosquitto功能之前调用*/ mosquitto_lib_init(); /*创建一个新的mosquitto客户端实例,第二个参数为true,代理清除断开连接时的所有消息和订阅*/ mosq = mosquitto_new(mqtt.clientid,session, (void *)&mqtt ); if(!mosq) { printf("mosquitto new failure: %sn", strerror(errno)); goto cleanup; } printf("Create mosquitto successfulyn"); /*设置连接回调,当代理发送CONNACK消息以响应连接时,将调用此方法*/ mosquitto_connect_callback_set(mosq, pub_conn_callback); /*配置mosquitto实例的用户名和密码*/ if( mosquitto_username_pw_set(mosq, mqtt.username,mqtt.passwd) !=MOSQ_ERR_SUCCESS) { printf("mosquitto username and passwd failure:%sn",strerror(errno)); goto cleanup; } while(1) { /*连接MQTT代理*/ if(mosquitto_connect(mosq, mqtt.hostname, mqtt.port, mqtt.keepalive) != MOSQ_ERR_SUCCESS ) { printf("mosquitto connect server failure:%sn",strerror(errno)); continue; sleep(1); } /*无线阻塞循环调用loop*/ mosquitto_loop_forever(mosq, -1, 1 ); sleep(10); } cleanup: printf("program will exitn"); mosquitto_destroy(mosq); mosquitto_lib_cleanup(); return 0; } void pub_conn_callback(struct mosquitto *mosq, void *obj, int rc) { mqtt_ctx_t *mqtt; int mid; char *msg; cJSON *root = cJSON_CreateObject(); cJSON *item = cJSON_CreateObject(); int retain = 0; int fd; float temp; float humidity; int rv = -1; float payload1; float payload2; if(!mosq ||!obj) { printf("invalid input argumentn"); return ; } rv = get_temp_humidity(fd, &temp, &humidity); if(rv == 0) { payload1 = temp; payload2 = humidity; } mqtt = (mqtt_ctx_t *)obj; cJSON_AddItemToObject(root, "method", cJSON_CreateString(mqtt->method));//根节点下添加 cJSON_AddItemToObject(root, "id", cJSON_CreateString(mqtt->id));//根节点下添加 cJSON_AddItemToObject(root, "params",item); cJSON_AddItemToObject(item, "CurrentHumidity", cJSON_CreateNumber(payload1)); cJSON_AddItemToObject(item, "CurrentTemperature", cJSON_CreateNumber(payload2)); cJSON_AddItemToObject(root, "version", cJSON_CreateString(mqtt->version)); msg = cJSON_Print(root); printf("%sn", msg); if(!rc) { if( mosquitto_publish(mosq,&mid,mqtt->pubTopic,strlen(msg)+1, msg, mqtt->pubQos, retain) != MOSQ_ERR_SUCCESS ) { printf("Mosq_Publish() error: %sn", strerror(errno)); return ; } printf("pubilush topic:%sn",mqtt->pubTopic) ; } mosquitto_disconnect(mosq) ; } int get_temp_humidity(int fd, float *temp, float *humidity) { uint8_t serialnumber[8]; fd = sht2x_init(); if(fd < 0) { printf("SHT2x initialize failuren"); return 1; } if( sht2x_softreset(fd) < 0 ) { printf("SHT2x softreset failuren"); return 2; } if( sht2x_get_serialnumber(fd, serialnumber, 8) < 0) { printf("SHT2x get serial number failuren"); return 3; } if( sht2x_get_temp_humidity(fd, &temp, &humidity) < 0 ) { printf("SHT2x get get temperature and relative humidity failuren"); return 3; } printf("Temperature=%lf ℃ relative humidity=%lf%n", temp, humidity); return 0; close(fd); }
当运行之后我们来看一下运行结果,我们先看在树莓派上的运行结果:
我们在树莓派上看到我们的温湿度分别为26.069939 和57.549042,然后我们登录阿里云的后台,看温湿度上报的情况。
与我们上述上报的情况是一样的,这样我们就是实现了树莓派sht20采样温湿度上报阿里云的一个项目,下面我附上源码地址,感兴趣的可以看一下
源码链接 https://gitee.com/ma_kung/mqtt
最后
以上就是帅气书本最近收集整理的关于树莓派使用mosquitto库搭建客户端将SHT20采样的温湿度上报阿里云的全部内容,更多相关树莓派使用mosquitto库搭建客户端将SHT20采样内容请搜索靠谱客的其他文章。
发表评论 取消回复