OpenHarmony开发16 —— 改变环境,重构代码
OpenHarmony开发16 —— 改变环境,重构代码
-
由于我从家里到了学校,所以原本的IP配置等都需要修改
-
检查代码里的IP,端口等
-
因为hispark都是用Dvceco Tools 控制的,所以剩下的部分基本不需要更改了
-
检查EMQX等工具的配置,建立新的连接
school
-
测试通过
-
-
为系统添加订阅等功能,因为本来的publish是需要用消息队列控制的,所以我们不能直接修改,小灯的控制进程申请应该在一切之前,于是我们写到
rtosv2_msgq_main
里 -
受不了了,全重构了...... 现在新的代码如下:
//hello_world.c #include <stdio.h> #include <string.h> #include <unistd.h> #include <stdlib.h> #include "ohos_init.h" #include "cmsis_os2.h" #include "iot_errno.h" #include "iot_gpio.h" #include "iot_uart.h" #include "wifi_connect.h" #include "MQTTClient.h" #include "MQTTLiteOS.h" #define DEBUG_MODE 0 #define MAX_CHAR_LEN 20 #define MAIN_STACK_SIZE 1024*30 #define WIFI_IOT_UART_IDX_1 1 #define OS_DELAY_RUN 50 #define OS_DELAY_ERR 200 #define OS_DELAT_FST 10 #define UART_TASK_STACK_SIZE 1024 * 8 #define UART_TASK_PRIO 25 #define BUFF_SIZE 1000 #define QUEUE_SIZE 3 #define MQTT_TASK_STACK_SIZE 1024 * 10 #define MQTT_SUBSCRIBE_TIME 5 char CONNECT_SSID[MAX_CHAR_LEN]; char CONNECT_PASSWORD[MAX_CHAR_LEN]; char CONNECT_IP[MAX_CHAR_LEN]; uint32_t CONNECT_PORT; // 用于MQTT发送和接受的数组大小 static unsigned char sendBuf[BUFF_SIZE]; static unsigned char readBuf[BUFF_SIZE]; Network network; MQTTClient client; MQTTString clientId; MQTTPacket_connectData clientData; osMessageQueueId_t qid; osThreadId_t mainid; // 用于记录主进程id,统计内存等 typedef struct { osThreadId_t tid; int count; } message_entry; // 用于管理不同环境下配置 void Config(void){ uint32_t MODE = DEBUG_MODE; CONNECT_PORT = 1883; if(MODE == 0){ strcpy(CONNECT_SSID,""); strcpy(CONNECT_PASSWORD,""); strcpy(CONNECT_IP,""); }else if(MODE == 1){ strcpy(CONNECT_SSID,""); strcpy(CONNECT_PASSWORD,""); strcpy(CONNECT_IP,""); }else if(MODE == 2){ strcpy(CONNECT_SSID,""); strcpy(CONNECT_PASSWORD,""); strcpy(CONNECT_IP,""); }else{ printf("[Config Warning] Error! Config has not declared!\n"); return; } printf("[Config] IP: %s \n",CONNECT_IP); } // 这里是怕只初始化一次会出现错误,所以暂时这么写 void MQTT_Client_Init(void){ printf("[MQTT] MQTTClient Init...\n"); MQTTString tmpId = MQTTString_initializer; clientId = tmpId; MQTTPacket_connectData tmpData = MQTTPacket_connectData_initializer; clientData = tmpData; MQTTClientInit(&client, &network, 2000, sendBuf, sizeof(sendBuf), readBuf, sizeof(readBuf)); clientId.cstring = "Hispark"; clientData.clientID = clientId; clientData.willFlag = 0; clientData.MQTTVersion = 3; clientData.keepAliveInterval = 0; clientData.cleansession = 1; printf("[MQTT] MQTTClient Init Successfully!\n"); } bool All_Init(void){ printf("[Network] Network Connect...\n"); WifiConnect(CONNECT_SSID,CONNECT_PASSWORD); NetworkInit(&network); printf("[Network] Network Init Successfully!\n"); NetworkConnect(&network, CONNECT_IP, CONNECT_PORT); printf("[Network] Network Connect Successfully!\n"); MQTT_Client_Init(); printf("[MQTT] MQTT Connect Start...\n"); int rc = MQTTConnect(&client, &clientData); if (rc != 0) { printf("[MQTT Warning] MQTTConnect : %d\n", rc); return false; } printf("[MQTT] MQTT Successfullt Connect!\n"); printf("[UART] UART Init Start...\n"); IotUartAttribute uart_attr = { .baudRate = 9600, .dataBits = 8, .stopBits = 1, .parity = 0, }; uint32_t ret = IoTUartInit(WIFI_IOT_UART_IDX_1, &uart_attr); if (ret != IOT_SUCCESS){ printf("[UART Warning] Failed to init uart! Err code = %d\n", ret); return false; } printf("[UART] UART Init Successfully!\n"); return true; } void messageArrived(MessageData* data){ printf("[MQTT] Message arrived on topic %.*s: %.*s\n", data->topicName->lenstring.len, data->topicName->lenstring.data,data->message->payloadlen, data->message->payload); } void MQTT_Subscribe_Task(void){ printf("[MQTT] Subscribe Task Start\n"); int rc =-1; for(int i = 0; i < MQTT_SUBSCRIBE_TIME; ++i){ printf("[MQTT] MQTTSubscribe %d...\n",i); rc = MQTTSubscribe(&client, "substopic", 2, messageArrived); if(rc != 0){ printf("[MQTT Warning] Subscribe Failed.\n"); } osDelay(OS_DELAY_RUN); } } void receiver_thread(void){ printf("[MQTT] Receiver Thread Start!\n"); message_entry rentry; int rc = -1; while(1){ osMessageQueueGet(qid, (int *)&rentry, NULL, osWaitForever); printf("[MQTT] %s get %d from %s by message queue.\r\n",osThreadGetName(osThreadGetId()), rentry.count, osThreadGetName(rentry.tid)); osDelay(OS_DELAY_RUN); if(rentry.count > 0){ printf("[MQTT] Publish Task Start\n"); while(1){ MQTTMessage message; char payload[30]; message.qos = 2; message.retained = 0; message.payload = payload; sprintf(payload, "CO2浓度: %d",rentry.count); message.payloadlen = strlen(payload); if ((rc = MQTTPublish(&client, "pubtopic", &message)) != 0){ printf("[MQTT Warning] Return code from MQTT publish is %d\n", rc); return; }else{ printf("[MQTT] Send Finish!\n"); } osDelay(OS_DELAT_FST); } } } } void sender_thread(void){ printf("[UART] Sender Thread Start!\n"); uint8_t uart_buff[BUFF_SIZE] = {0}; uint8_t *uart_buff_ptr = uart_buff; message_entry sentry; while(1){ uint32_t length = IoTUartRead(WIFI_IOT_UART_IDX_1, uart_buff_ptr, BUFF_SIZE); if(length > 0){ printf("[UART] Uart read data:\n"); for(int i=0;i<length;i++){ printf("%.2X ",uart_buff_ptr[i]); } printf("\n"); if(length==16){ if(uart_buff_ptr[0]!=0x42 || uart_buff_ptr[1]!=0x4D) printf("数据头错误!\n"); else { uint8_t checknum = 0; for(int i=0;i<15;i++) checknum += uart_buff_ptr[i]; printf("%d %d\n",checknum,uart_buff_ptr[15]); if(checknum != uart_buff_ptr[15]) printf("校验数据错误!\n"); else { uint32_t res = 0; res = uart_buff_ptr[6]*256 + uart_buff_ptr[7]; printf("CO2浓度为: %d\n",res); //while(1) //{ sentry.tid = osThreadGetId(); sentry.count = res; printf("[UART] %s send %d to message queue.\r\n", osThreadGetName(osThreadGetId()), res); osMessageQueuePut(qid, (const int *)&sentry, 0, osWaitForever); osDelay(10); //} } } } } osDelay(OS_DELAY_RUN); } } osThreadId_t MQTT_Publish_Thread(){ osThreadAttr_t attr; attr.name = "MQTT_Thread"; attr.attr_bits = 0U; attr.cb_mem = NULL; attr.cb_size = 0U; attr.stack_mem = NULL; attr.stack_size = MQTT_TASK_STACK_SIZE; attr.priority = osPriorityNormal; printf("MQTT Thread Create!\n"); osThreadId_t tid = osThreadNew((osThreadFunc_t)receiver_thread, NULL, &attr); if (tid == NULL) { printf("[MQTT] osThreadNew(MQTT) failed.\r\n"); } else { printf("[MQTT] osThreadNew(MQTT) success, thread id: %d.\r\n",tid); } return tid; } osThreadId_t UART_Thread() { osThreadAttr_t attr; attr.name = "UART_Thread"; attr.attr_bits = 0U; attr.cb_mem = NULL; attr.cb_size = 0U; attr.stack_mem = NULL; attr.stack_size = UART_TASK_STACK_SIZE; attr.priority = UART_TASK_PRIO; printf("[UART] UART Thread Create!\n"); osThreadId_t tid = osThreadNew((osThreadFunc_t)sender_thread, NULL, &attr); if (tid == NULL) { printf("[UART] osThreadNew(UART) failed.\r\n"); } else { printf("[UART] osThreadNew(UART) success, thread id: %d.\r\n",tid); } return tid; } void mainThreadTask(void){ if(All_Init() == false) return; qid = osMessageQueueNew(QUEUE_SIZE, sizeof(message_entry), NULL); while(1){ MQTT_Subscribe_Task(); osThreadId_t ctid = MQTT_Publish_Thread(); osThreadId_t ptid = UART_Thread(); osDelay(OS_DELAY_RUN); osThreadTerminate(ctid); osThreadTerminate(ptid); uint32_t stackSpace = osThreadGetStackSpace(mainid); printf("[MainProgram] Remain Stack Space: %d\n",stackSpace); osDelay(OS_DELAY_RUN); } } void HelloWorld(void){ Config(); osThreadAttr_t attr; attr.name = "main_program"; attr.attr_bits = 0U; attr.cb_mem = NULL; attr.cb_size = 0U; attr.stack_mem = NULL; attr.stack_size = MAIN_STACK_SIZE; attr.priority = osPriorityNormal; mainid = osThreadNew((osThreadFunc_t)mainThreadTask, NULL, &attr); if (mainid == NULL) { printf("[MainProgram] Failed to create mainThreadTask!\n"); } } SYS_RUN(HelloWorld);