smart-green-house-server-an.../how_to_use_mqtt/sub1_text.c

25 lines
1.3 KiB
C
Raw Normal View History

2023-10-24 17:19:13 +08:00
1. mosquitto的库环境
// int mosquitto_lib_init(void);
2. mosquitto的客户端
// client = mosquitto_new("166", true, userdata);
3.
// mosquitto_connect_callback_set(
// struct mosquitto* mosq, // 客户端
// void (*on_connect)( // 回调函数
// struct mosquitto* mosq, // 客户端数据
// void* obj, // 创建客户端时的回调参数
// int rc // 0表示连接成功其它表示失败
// ))
// mosquitto_connect_callback_set(client, connect_callback); 连接确认回调
// mosquitto_subscribe_callback_set(client, on_subscribe);设置订阅回调函数
// mosquitto_message_callback_set(client,on_message);置订阅消息到来的回调函数【重要】
4.
// int flag = mosquitto_connect(client, "10.12.156.19", 1883, 60);
5.
// flag = mosquitto_subscribe(client, &msgId, "dht11", 0);
6.
// mosquitto_loop_start(client); // 接收网络数据
7.mosquitto的客户端
// mosquitto_destroy(client);
8.
// mosquitto_lib_cleanup();