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