liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include "mbtk_mqtt.h" |
| 3 | #include "pthread.h" |
| 4 | #include "string.h" |
| 5 | #include "unistd.h" |
| 6 | #include "sys/stat.h" |
| 7 | #include "sys/types.h" |
| 8 | #include "sys/socket.h" |
| 9 | #include "netinet/in.h" |
| 10 | #include "arpa/inet.h" |
| 11 | #include "fcntl.h" |
| 12 | #include "mbtk_sock2.h" |
| 13 | |
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 14 | void iot_yield(Cloud_MQTT_t *piot_mqtt,iot_device_info_t *gateway); |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 15 | |
| 16 | struct opts_struct opts = { |
| 17 | (char *)"iot-dev", 0, (char *)"\n", QOS0, "admin", "password", (char *)"localhost", 1883, 0 |
| 18 | };//初始化结构体 |
| 19 | |
| 20 | iot_device_info_t gateway = { |
| 21 | .iotstatus = IOT_STATUS_LOGIN, |
| 22 | .model = {"2022"}, |
| 23 | .company = {"/my"} |
| 24 | };//初始化主题 |
| 25 | |
| 26 | static Cloud_MQTT_t *miot_mqtt; |
| 27 | |
| 28 | void set_mqtt_t(Cloud_MQTT_t *piot_mqtt) |
| 29 | { |
| 30 | miot_mqtt = piot_mqtt; |
| 31 | } |
| 32 | Cloud_MQTT_t * get_mqtt_t() |
| 33 | { |
| 34 | return miot_mqtt; |
| 35 | } |
| 36 | |
| 37 | void mqtt_data_rx_cb(void *pbuf, int len) |
| 38 | { |
| 39 | |
| 40 | printf("data = %s\n", (unsigned char *)pbuf); //打印接收到的数据 |
| 41 | } |
| 42 | |
| 43 | |
| 44 | void MQTTMessageArrived_Cb(MessageData* md) |
| 45 | { |
| 46 | MQTTMessage *message = md->message; |
| 47 | |
| 48 | Cloud_MQTT_t *piot_mqtt = get_mqtt_t(); |
| 49 | |
| 50 | if (NULL != piot_mqtt->DataArrived_Cb) { |
| 51 | piot_mqtt->DataArrived_Cb((void *)message->payload, message->payloadlen);//异步消息体 |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | void *mqtt_thread(void *arg) |
| 56 | { |
| 57 | while (gateway.iotstatus != IOT_STATUS_DROP){ |
| 58 | printf("gateway.iotstatus is %d\n",gateway.iotstatus); |
| 59 | iot_yield(arg,&gateway); //维持服务器稳定,断开重连 |
| 60 | } |
| 61 | printf("gateway.iotstatus is IOT_STATUS_DROP"); |
| 62 | // mqtt_device_disconnect(arg); |
| 63 | pthread_exit(NULL); |
| 64 | return NULL; |
| 65 | } |
| 66 | |
| 67 | int main(int argc, char *argv[]) |
| 68 | { |
| 69 | if(argc >6 || argc < 4) |
| 70 | { |
| 71 | printf("input error.\n example:./mqtt_test 192.168.1.1 8080 clientid username password\n"); |
| 72 | return 0; |
| 73 | } |
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 74 | int ret ; |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 75 | char will_msg[256] = {"hello world"}; //初始化遗嘱数据 |
| 76 | static int retry_count = 0; |
| 77 | pthread_t thread_ID; //定义线程id |
| 78 | static int i = 0; |
| 79 | |
| 80 | Cloud_MQTT_t *Iot_mqtt; |
| 81 | Iot_mqtt = (Cloud_MQTT_t *)malloc(sizeof(Cloud_MQTT_t)); |
| 82 | memset(Iot_mqtt,0x0,sizeof(Cloud_MQTT_t)); |
| 83 | |
| 84 | iot_mqtt_init(Iot_mqtt,argv[1],atoi(argv[2]),argv[3],argv[4],argv[5],30,3,"aaa","aaa",mqtt_data_rx_cb); //初始化 |
| 85 | mqtt_will_msg_set(Iot_mqtt, will_msg, strlen(will_msg)); //设置遗嘱 |
| 86 | set_mqtt_t(Iot_mqtt); |
| 87 | ret = mqtt_device_connect(Iot_mqtt); //初始化并连接mqtt服务器 |
| 88 | |
| 89 | |
| 90 | while (ret < 0 && retry_count <= 5) { |
| 91 | printf("ret = %d\r\n", ret); |
| 92 | retry_count ++ ; |
| 93 | printf("retry_count = %d\n",retry_count); |
| 94 | sleep(3); |
| 95 | ret = mqtt_device_connect(Iot_mqtt); |
| 96 | } |
| 97 | if(ret < 0 && retry_count > 5) |
| 98 | { |
| 99 | printf("reconnect times more than 5 exit\n"); |
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 100 | return -1; |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 101 | } |
| 102 | retry_count = 0; |
| 103 | |
| 104 | printf("connect success\n"); |
| 105 | int rc = mbtk_MQTTSubscribe(&Iot_mqtt->Client, Iot_mqtt->sub_topic, opts.qos, MQTTMessageArrived_Cb); |
| 106 | if (rc) { |
| 107 | printf("mqtt subscribe fail \n"); |
| 108 | ret = -105; |
| 109 | return 0; |
| 110 | } |
| 111 | gateway.iotstatus = IOT_STATUS_CONNECT; |
| 112 | |
| 113 | pthread_create(&thread_ID, NULL, &mqtt_thread, (void *)Iot_mqtt); //创建一个线程执行mqtt客户端 |
| 114 | pthread_detach(thread_ID); //设置线程结束收尸 |
| 115 | |
| 116 | while (1) |
| 117 | { |
| 118 | mbtk_MQTTPublish("my yes", 6, 0,&Iot_mqtt->Client,Iot_mqtt->pub_topic,2,0);//循环发布"my yes" |
| 119 | sleep(3); //睡眠3s |
| 120 | i++; |
| 121 | if (i > 5) |
| 122 | { |
| 123 | mbtk_MQTTUnsubscribe(&Iot_mqtt->Client,Iot_mqtt->sub_topic); |
| 124 | break; |
| 125 | } |
| 126 | } |
| 127 | return 0; |
| 128 | } |