sprintf(g_publish_topic,"/%s/uplink",gateway_id); sprintf(g_subscribe_topic,"/%s/downlink",gateway_id); /*mqtt settings*/ sprintf(g_mqtt_url,"tcp://%s:%s",mqtt_server_IP,UInt32toStr(mqtt_server_port)); memcpy(g_client_device,"ChirpLan_GW1",sizeof("ChirpLan_GW1")); printf("g_mqtt_url is %s | g_client_device = %s | g_mqtt_conn_user = %s | g_mqtt_conn_pwd = %s\r\n",g_mqtt_url,gateway_id,g_mqtt_conn_user,g_mqtt_conn_pwd); if((rc =MQTTClient_create(&g_client, g_mqtt_url,gateway_id,MQTTCLIENT_PERSISTENCE_NONE, NULL))<0) { printf("[%s:%d]MQTTClient_create failure:%s\n",__FUNCTION__,__LINE__,strerror(errno)); return 0; }else{ printf("[%s:%d]MQTTClient_create successfully\n",__FUNCTION__,__LINE__); } conn_opts.keepAliveInterval = 45; conn_opts.cleansession = 1; //conn_opts.username = g_mqtt_conn_user; //conn_opts.password = g_mqtt_conn_pwd; //conn_opts.will = &wopts; MQTTClient_setCallbacks(g_client, NULL, connlost, msgarrvd, delivered);
时间: 2024-03-28 14:39:31 浏览: 56
dh_depr.rar_Linux/Unix编程_Unix_Linux_
这段代码主要是一些变量的初始化和MQTT客户端的创建,包括设置MQTT连接的URL、客户端ID、保持连接的时间、清除会话等参数,以及回调函数的设置。
其中,使用sprintf函数将gateway_id拼接到g_publish_topic和g_subscribe_topic中,用于定义MQTT发布和订阅的主题。使用sprintf函数将mqtt_server_IP和mqtt_server_port拼接到g_mqtt_url中,用于设置MQTT连接的URL。使用memcpy函数将字符串"ChirpLan_GW1"复制到g_client_device中,用于设置MQTT客户端的ID。
然后,使用MQTTClient_create函数创建MQTT客户端,并设置回调函数。最后,设置MQTT连接的参数,包括保持连接的时间和清除会话,以及回调函数的设置。
阅读全文