0
1、rt-thread setting下面添加pahomqq组件
2、msh下面显示rw007正常
3、执行wifi scan
4、wifi join SSID 密码 , 命令连接指定SSID
5、ping www.baidu.com
6、创建mqtt_test.c脚本
- #include
- #include
- #include
- #include
- #include "netdb.h"
- #include
- #include
- #include
- #include "paho_mqtt.h"
- #define MQTT_Uri "tcp://192.168.31.42:1883" // MQTT服务器的地址和端口号
- #define ClientId "rw007NO01" // ClientId需要唯一
- #define UserName "MyRw007" // 用户名
- #define PassWord "123456" // 用户名对应的密码
- /* 定义一个MQTT线程句柄结构体指针 */
- static rt_thread_t app_mqtt_thread = RT_NULL;
- /* 定义一个MQTT客户端结构体 */
- static MQTTClient client;
- /* 收到订阅的"Bear_Pi"主题的消息时的回调函数*/
- static void mqtt_sub_callback(MQTTClient *c, MessageData *msg_data)
- {
- *((char *)msg_data->message->payload + msg_data->message->payloadlen) = '\0';
- rt_kprintf("Receive topic: %.*s, message data:rn", msg_data->topicName->lenstring.len, msg_data->topicName->lenstring.data);
- rt_kprintf("%.*srn", msg_data->message->payloadlen, (char *)msg_data->message->payload);
- }
- /* 默认的订阅回调函数,如果有订阅的 Topic 没有设置回调函数,则使用该默认回调函数 */
- static void mqtt_sub_default_callback(MQTTClient *c, MessageData *msg_data)
- {
- *((char *)msg_data->message->payload + msg_data->message->payloadlen) = '\0';
- rt_kprintf("Receive topic: %.*s, message data:rn", msg_data->topicName->lenstring.len, msg_data->topicName->lenstring.data);
- rt_kprintf("%.*srn", msg_data->message->payloadlen, (char *)msg_data->message->payload);
- }
- /* 连接成功回调函数 */
- static void mqtt_connect_callback(MQTTClient *c)
- {
- rt_kprintf("mqtt connect success! rn");
- }
- /* 上线回调函数 */
- static void mqtt_online_callback(MQTTClient *c)
- {
- rt_kprintf("mqtt online rn");
- paho_mqtt_publish(&client, QOS1, "RW007_Pub", "Hello, I am RW007 with RT_Thread.");
- }
- /* 下线回调函数 */
- static void mqtt_offline_callback(MQTTClient *c)
- {
- rt_kprintf("mqtt offline rn");
- }
- static void app_mqtt_thread_entry(void *parameter)
- {
- int count = 1;
- char msg_buf[128];
- /* 对MQTT客户端结构体变量进行配置 */
- client.isconnected = 0;
- client.uri = MQTT_Uri;
- /* 配置MQTT的连接参数 */
- MQTTPacket_connectData condata = MQTTPacket_connectData_initializer;
- memcpy(&client.condata, &condata, sizeof(condata));
- client.condata.clientID.cstring = ClientId;
- client.condata.keepAliveInterval = 30;
- client.condata.cleansession = 1;
- client.condata.username.cstring = UserName;
- client.condata.password.cstring = PassWord;
- /* 为mqtt申请内存 */
- client.buf_size = client.readbuf_size = 1024;
- client.buf = rt_calloc(1, client.buf_size);
- client.readbuf = rt_calloc(1, client.readbuf_size);
- if (!(client.buf && client.readbuf))
- {
- rt_kprintf("no memory for MQTT client buffer!rn");
- return ;
- }
- /* 设置回调函数 */
- client.connect_callback = mqtt_connect_callback;
- client.online_callback = mqtt_online_callback;
- client.offline_callback = mqtt_offline_callback;
- /* 订阅一个主题,并设置其回调函数 */
- client.messageHandlers[0].topicFilter = rt_strdup("RW007_Sub");
- client.messageHandlers[0].callback = mqtt_sub_callback;
- client.messageHandlers[0].qos = QOS1;
- /* 设置默认的回调函数 */
- client.defaultMessageHandler = mqtt_sub_default_callback;
- /* 启动 mqtt client */
- paho_mqtt_start(&client);
- while(1)
- {
- rt_sprintf(msg_buf, "publish %d times.", count++);
- paho_mqtt_publish(&client, QOS1, "RW007_Pub", msg_buf);
- rt_thread_mdelay(1000);
- }
- }
- static int app_mqtt_init(void)
- {
- rt_err_t rt_err;
- /* 创建MQTT线程*/
- app_mqtt_thread = rt_thread_create("app_mqtt thread",
- app_mqtt_thread_entry, RT_NULL, 2048, 6, 10);
- /* 如果获得线程控制块,启动这个线程 */
- if (app_mqtt_thread != RT_NULL)
- rt_err = rt_thread_startup(app_mqtt_thread);
- else
- rt_kprintf("app_mqtt_thread create failure !!! n");
- /* 判断线程是否启动成功 */
- if( rt_err == RT_EOK)
- rt_kprintf("app_mqtt_thread startup ok. n");
- else
- rt_kprintf("app_mqtt_thread startup err. n");
- return rt_err;
- }
- MSH_CMD_EXPORT(app_mqtt_init, app_mqtt_init sample);
复制代码
7、运行 app_mqtt_init
8、测试通过
|
|