OpenHarmony开发15 —— 消息队列
OpenHarmony开发15 —— 消息队列
说点别的,这几天没更新真的是被这个消息队列折磨完了,谁知道鬼鸿蒙它不进行任何提示!为什么stack overflow会不提示啊!!!太折磨了太折磨了
-
这是消息队列的文档:OpenHarmony/vendor_hisilicon - Gitee.com
-
修改
BUILD.gn
static_library("myapp") { sources = [ "hello_world.c", "src/wifi_connect.c", ] include_dirs = [ "//utils/native/lite/include", "//base/iot_hardware/peripheral/interfaces/kits", "//third_party/cmsis/CMSIS/RTOS2/Include", "//utils/native/lite/include", "//commonlibrary/utils_lite/include", "//base/iot_hardware/interfaces/kits/wifiiot_lite", "//foundation/communication/wifi_lite/interfaces/wifiservice", "//vendor/hisi/hi3861/hi3861/third_party/lwip_sack/include/", "//third_party/cJSON", "//third_party/paho_mqtt/MQTTPacket/src", "//third_party/paho_mqtt/MQTTClient-C/src", "//third_party/paho_mqtt/MQTTClient-C/src/liteOS", "//third_party/cmsis/CMSIS/RTOS2/Include", "include", ] deps = [ "//third_party/paho_mqtt:pahomqtt_static", ] }
-
目前融合了MQTT协议,UART,WIFI连接,消息队列的混合代码:
#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 CONNECT_SSID ""
#define CONNECT_PASSWORD ""
#define CONNECT_IP ""
#define CONNECT_PORT 1883
#define UART_TASK_STACK_SIZE 1024 * 8
#define UART_TASK_PRIO 25
#define UART_BUFF_SIZE 1000
#define WIFI_IOT_UART_IDX_1 1
#define NUM 1
#define OS_DELAY 5
#define OS_DELAY_F 3
#define OS_DELAY_S 20
#define OS_DELAY_T 80
#define ATTR_STACK_SIZE 1024
#define QUEUE_SIZE 3
static const char *data = "Hello, Hispark!\r\n";
static unsigned char sendBuf[1000];
static unsigned char readBuf[1000];
Network network;
typedef struct {
osThreadId_t tid;
int count;
} message_entry;
osMessageQueueId_t qid;
void sender_thread(void/*int *arg*/)
{
static int count = 0;
message_entry sentry;
//(int)arg;
while (NUM) {
sentry.tid = osThreadGetId();
sentry.count = count;
printf("[Message Test] %s send %d to message queue.\r\n", osThreadGetName(osThreadGetId()), count);
osMessageQueuePut(qid, (const int *)&sentry, 0, osWaitForever);
count++;
osDelay(OS_DELAY);
}
}
void receiver_thread(void/*int *arg*/)
{
//(int)arg;
message_entry rentry;
while (NUM) {
osMessageQueueGet(qid, (int *)&rentry, NULL, osWaitForever);
printf("[Message Test] %s get %d from %s by message queue.\r\n",
osThreadGetName(osThreadGetId()), rentry.count, osThreadGetName(rentry.tid));
osDelay(OS_DELAY_F);
}
}
osThreadId_t newThread(char *name, osThreadFunc_t func, int *arg)
{
osThreadAttr_t attr = {
name, 0, NULL, 0, NULL, 1024*2, osPriorityNormal, 0, 0
};
osThreadId_t tid = osThreadNew(func, arg, &attr);
if (tid == NULL) {
printf("[Message Test] osThreadNew(%s) failed.\r\n", name);
} else {
printf("[Message Test] osThreadNew(%s) success, thread id: %d.\r\n", name, tid);
}
return tid;
}
void rtosv2_msgq_main(int *arg)
{
//(int)arg;
qid = osMessageQueueNew(QUEUE_SIZE, sizeof(message_entry), NULL);
osThreadId_t ctid1 = newThread("receiver1", receiver_thread, NULL);
osThreadId_t ctid2 = newThread("receiver2", receiver_thread, NULL);
osThreadId_t ptid1 = newThread("sender1", sender_thread, NULL);
osThreadId_t ptid2 = newThread("sender2", sender_thread, NULL);
osThreadId_t ptid3 = newThread("sender3", sender_thread, NULL);
osDelay(OS_DELAY_S);
uint32_t cap = osMessageQueueGetCapacity(qid);
printf("[Message Test] osMessageQueueGetCapacity, capacity: %u.\r\n", cap);
uint32_t msg_size = osMessageQueueGetMsgSize(qid);
printf("[Message Test] osMessageQueueGetMsgSize, size: %u.\r\n", msg_size);
uint32_t count = osMessageQueueGetCount(qid);
printf("[Message Test] osMessageQueueGetCount, count: %u.\r\n", count);
uint32_t space = osMessageQueueGetSpace(qid);
printf("[Message Test] osMessageQueueGetSpace, space: %u.\r\n", space);
osDelay(OS_DELAY_T);
osThreadTerminate(ctid1);
osThreadTerminate(ctid2);
osThreadTerminate(ptid1);
osThreadTerminate(ptid2);
osThreadTerminate(ptid3);
osMessageQueueDelete(qid);
}
static void MessageTestTask(void)
{
osThreadAttr_t attr;
attr.name = "rtosv2_msgq_main";
attr.attr_bits = 0U;
attr.cb_mem = NULL;
attr.cb_size = 0U;
attr.stack_mem = NULL;
attr.stack_size = ATTR_STACK_SIZE;
attr.priority = osPriorityNormal;
if (osThreadNew((osThreadFunc_t)rtosv2_msgq_main, NULL, &attr) == NULL) {
printf("[MessageTestTask] Failed to create rtosv2_msgq_main!\n");
}
}
void messageArrived(MessageData* data)
{
printf("Message arrived on topic %.*s: %.*s\n", data->topicName->lenstring.len, data->topicName->lenstring.data,
data->message->payloadlen, data->message->payload);
}
static void MQTT_DemoTask(void)
{
WifiConnect(CONNECT_SSID,CONNECT_PASSWORD);
printf("Starting ...\n");
int rc, count = 0;
MQTTClient client;
NetworkInit(&network);
printf("NetworkConnect ...\n");
begin:
NetworkConnect(&network, CONNECT_IP, CONNECT_PORT);
printf("MQTTClientInit ...\n");
MQTTClientInit(&client, &network, 2000, sendBuf, sizeof(sendBuf), readBuf, sizeof(readBuf));
MQTTString clientId = MQTTString_initializer;
clientId.cstring = "hispark";
MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
data.clientID = clientId;
data.willFlag = 0;
data.MQTTVersion = 3;
data.keepAliveInterval = 0;
data.cleansession = 1;
printf("MQTTConnect ...\n");
rc = MQTTConnect(&client, &data);
if (rc != 0) {
printf("MQTTConnect: %d\n", rc);
NetworkDisconnect(&network);
MQTTDisconnect(&client);
osDelay(200);
goto begin;
}
printf("MQTTSubscribe ...\n");
rc = MQTTSubscribe(&client, "substopic", 2, messageArrived);
if (rc != 0) {
printf("MQTTSubscribe: %d\n", rc);
osDelay(200);
goto begin;
}
while (++count)
{
MQTTMessage message;
char payload[30];
message.qos = 2;
message.retained = 0;
message.payload = payload;
sprintf(payload, "message number %d", count);
message.payloadlen = strlen(payload);
if ((rc = MQTTPublish(&client, "pubtopic", &message)) != 0){
printf("Return code from MQTT publish is %d\n", rc);
NetworkDisconnect(&network);
MQTTDisconnect(&client);
goto begin;
}
osDelay(50);
}
}
static void UART_Task(void)
{
uint8_t uart_buff[UART_BUFF_SIZE] = {0};
uint8_t *uart_buff_ptr = uart_buff;
uint32_t ret;
//WifiIotUartAttribute uart_attr = {
IotUartAttribute uart_attr = {
//baud_rate: 9600
.baudRate = 9600,
//data_bits: 8bits
.dataBits = 8,
.stopBits = 1,
.parity = 0,
};
MQTTINIT();
ret = IoTUartInit(WIFI_IOT_UART_IDX_1, &uart_attr);
if (ret != IOT_SUCCESS)
{
printf("Failed to init uart! Err code = %d\n", ret);
return;
}
printf("UART START\n");
while (1)
{
printf("=======================================\r\n");
printf("*************UART_example**************\r\n");
printf("=======================================\r\n");
//通过串口1发送数据
IoTUartWrite(WIFI_IOT_UART_IDX_1, (unsigned char *)data, strlen(data));
//通过串口1接收数据
IoTUartRead(WIFI_IOT_UART_IDX_1, uart_buff_ptr, UART_BUFF_SIZE);
uint8_t length = strlen(uart_buff_ptr);
printf("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];
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);
}
}
}
usleep(500000);
}
}
void MQTTINIT(void)
{
osThreadAttr_t attr;
attr.name = "MQTT_DemoTask";
attr.attr_bits = 0U;
attr.cb_mem = NULL;
attr.cb_size = 0U;
attr.stack_mem = NULL;
attr.stack_size = 10240;
attr.priority = osPriorityNormal;
if (osThreadNew((osThreadFunc_t)MQTT_DemoTask, NULL, &attr) == NULL) {
printf("[MQTT_Demo] Falied to create MQTT_DemoTask!\n");
}
}
void UARTINIT(void)
{
osThreadAttr_t attr;
attr.name = "UART_Task";
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;
if (osThreadNew((osThreadFunc_t)UART_Task, NULL, &attr) == NULL)
{
printf("[ADCExample] Falied to create UART_Task!\n");
}
}
void HelloWorld(void)
{
//UARTINIT();
//MQTTINIT();
MessageTestTask();
}
//SYS_RUN(HelloWorld);
APP_FEATURE_INIT(HelloWorld);
- 更新!2023.02.07经过修改,目前已经支持成功接收到消息
#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 CONNECT_SSID ""
#define CONNECT_PASSWORD ""
#define CONNECT_IP ""
#define CONNECT_PORT 1883
#define UART_TASK_STACK_SIZE 1024 * 8
#define UART_TASK_PRIO 25
#define UART_BUFF_SIZE 1000
#define WIFI_IOT_UART_IDX_1 1
#define NUM 1
#define OS_DELAY 5
#define OS_DELAY_F 3
#define OS_DELAY_S 20
#define OS_DELAY_T 80
#define ATTR_STACK_SIZE 1024
#define QUEUE_SIZE 3
static const char *data = "Hello, Hispark!\r\n";
static unsigned char sendBuf[1000];
static unsigned char readBuf[1000];
Network network;
typedef struct {
osThreadId_t tid;
int count;
} message_entry;
osMessageQueueId_t qid;
MQTTClient client;
/*
void sender_thread(void)
{
static int count = 0;
message_entry sentry;
//(int)arg;
while (NUM) {
sentry.tid = osThreadGetId();
printf("[Message Test] %s send %d to message queue.\r\n", osThreadGetName(osThreadGetId()), count);
osMessageQueuePut(qid, (const int *)&sentry, 0, osWaitForever);
count++;
osDelay(OS_DELAY);
}
}
*/
void receiver_thread(void)
{
//(int)arg;
message_entry rentry;
while (NUM) {
osMessageQueueGet(qid, (int *)&rentry, NULL, osWaitForever);
printf("[Message Test] %s get %d from %s by message queue.\r\n",
osThreadGetName(osThreadGetId()), rentry.count, osThreadGetName(rentry.tid));
osDelay(OS_DELAY_F);
}
}
/*
osThreadId_t newThread(char *name, osThreadFunc_t func, int *arg)
{
osThreadAttr_t attr = {
name, 0, NULL, 0, NULL, 1024*2, osPriorityNormal, 0, 0
};
osThreadId_t tid = osThreadNew(func, arg, &attr);
if (tid == NULL) {
printf("[Message Test] osThreadNew(%s) failed.\r\n", name);
} else {
printf("[Message Test] osThreadNew(%s) success, thread id: %d.\r\n", name, tid);
}
return tid;
}*/
void messageArrived(MessageData* data)
{
printf("Message arrived on topic %.*s: %.*s\n", data->topicName->lenstring.len, data->topicName->lenstring.data,
data->message->payloadlen, data->message->payload);
}
static void MQTT_DemoTask(void)
{
WifiConnect(CONNECT_SSID,CONNECT_PASSWORD);
printf("Starting ...\n");
int rc;
NetworkInit(&network);
printf("NetworkConnect ...\n");
begin:
NetworkConnect(&network, CONNECT_IP, CONNECT_PORT);
printf("MQTTClientInit ...\n");
MQTTClientInit(&client, &network, 2000, sendBuf, sizeof(sendBuf), readBuf, sizeof(readBuf));
MQTTString clientId = MQTTString_initializer;
clientId.cstring = "hispark";
MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
data.clientID = clientId;
data.willFlag = 0;
data.MQTTVersion = 3;
data.keepAliveInterval = 0;
data.cleansession = 1;
printf("MQTTConnect ...\n");
rc = MQTTConnect(&client, &data);
if (rc != 0) {
printf("MQTTConnect: %d\n", rc);
NetworkDisconnect(&network);
MQTTDisconnect(&client);
osDelay(200);
goto begin;
}
message_entry rentry;
while(1){
osMessageQueueGet(qid, (int *)&rentry, NULL, osWaitForever);
printf("[Message Test] %s get %d from %s by message queue.\r\n",
osThreadGetName(osThreadGetId()), rentry.count, osThreadGetName(rentry.tid));
//osDelay(OS_DELAY_F);
MQTTMessage message;
char payload[30];
message.qos = 2;
message.retained = 0;
message.payload = payload;
sprintf(payload, "message number %d", 1314);
message.payloadlen = strlen(payload);
/*
printf("MQTTSubscribe ...\n");
rc = MQTTSubscribe(&client, "substopic", 2, messageArrived);
if (rc != 0) {
printf("MQTTSubscribe: %d\n", rc);
osDelay(200);
goto begin;
}*/
if ((rc = MQTTPublish(&client, "pubtopic", &message)) != 0){
printf("Return code from MQTT publish is %d\n", rc);
NetworkDisconnect(&network);
MQTTDisconnect(&client);
goto begin;
}
osDelay(50);
}
}
static void UART_Task(void)
{
uint8_t uart_buff[UART_BUFF_SIZE] = {0};
uint8_t *uart_buff_ptr = uart_buff;
uint32_t ret;
message_entry sentry;
//WifiIotUartAttribute uart_attr = {
IotUartAttribute uart_attr = {
.baudRate = 9600,
.dataBits = 8,
.stopBits = 1,
.parity = 0,
};
ret = IoTUartInit(WIFI_IOT_UART_IDX_1, &uart_attr);
if (ret != IOT_SUCCESS)
{
printf("Failed to init uart! Err code = %d\n", ret);
return;
}
printf("UART START\n");
while (1)
{
//IoTUartWrite(WIFI_IOT_UART_IDX_1, (unsigned char *)data, strlen(data));
//通过串口1接收数据
int length = IoTUartRead(WIFI_IOT_UART_IDX_1, uart_buff_ptr, UART_BUFF_SIZE);
if(length > 0)
{
printf("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("[Message Test] %s send %d to message queue.\r\n", osThreadGetName(osThreadGetId()), res);
osMessageQueuePut(qid, (const int *)&sentry, 0, osWaitForever);
osDelay(OS_DELAY);
}
}
}
}
}
usleep(500000);
}
}
osThreadId_t newMQTTThread()
{
osThreadAttr_t attr;
attr.name = "MQTT_DemoTask";
attr.attr_bits = 0U;
attr.cb_mem = NULL;
attr.cb_size = 0U;
attr.stack_mem = NULL;
attr.stack_size = 10240;
attr.priority = osPriorityNormal;
printf("MQTT Thread Create!\n");
osThreadId_t tid = osThreadNew((osThreadFunc_t)receiver_thread, NULL, &attr);
if (tid == NULL) {
printf("[Message Test] osThreadNew(MQTT) failed.\r\n");
} else {
printf("[Message Test] osThreadNew(MQTT) success, thread id: %d.\r\n",tid);
}
return tid;
}
osThreadId_t newUARTThread()
{
osThreadAttr_t attr;
attr.name = "UART_Task";
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 Thread Create!\n");
osThreadId_t tid = osThreadNew((osThreadFunc_t)UART_Task, NULL, &attr);
if (tid == NULL) {
printf("[Message Test] osThreadNew(UART) failed.\r\n");
} else {
printf("[Message Test] osThreadNew(UART) success, thread id: %d.\r\n",tid);
}
return tid;
}
void rtosv2_msgq_main(int *arg)
{
//(int)arg;
qid = osMessageQueueNew(QUEUE_SIZE, sizeof(message_entry), NULL);
osThreadId_t ctid = newMQTTThread();
osThreadId_t ptid = newUARTThread();
osDelay(OS_DELAY_S);
uint32_t cap = osMessageQueueGetCapacity(qid);
printf("[Message Test] osMessageQueueGetCapacity, capacity: %u.\r\n", cap);
uint32_t msg_size = osMessageQueueGetMsgSize(qid);
printf("[Message Test] osMessageQueueGetMsgSize, size: %u.\r\n", msg_size);
uint32_t count = osMessageQueueGetCount(qid);
printf("[Message Test] osMessageQueueGetCount, count: %u.\r\n", count);
uint32_t space = osMessageQueueGetSpace(qid);
printf("[Message Test] osMessageQueueGetSpace, space: %u.\r\n", space);
osDelay(OS_DELAY_T);
osThreadTerminate(ctid);
osThreadTerminate(ptid);
osMessageQueueDelete(qid);
}
static void MessageTestTask(void)
{
osThreadAttr_t attr;
attr.name = "rtosv2_msgq_main";
attr.attr_bits = 0U;
attr.cb_mem = NULL;
attr.cb_size = 0U;
attr.stack_mem = NULL;
attr.stack_size = ATTR_STACK_SIZE;
attr.priority = osPriorityNormal;
if (osThreadNew((osThreadFunc_t)rtosv2_msgq_main, NULL, &attr) == NULL) {
printf("[MessageTestTask] Failed to create rtosv2_msgq_main!\n");
}
}
/*
void MQTTINIT(void)
{
osThreadAttr_t attr;
attr.name = "MQTT_DemoTask";
attr.attr_bits = 0U;
attr.cb_mem = NULL;
attr.cb_size = 0U;
attr.stack_mem = NULL;
attr.stack_size = 10240;
attr.priority = osPriorityNormal;
if (osThreadNew((osThreadFunc_t)MQTT_DemoTask, NULL, &attr) == NULL) {
printf("[MQTT_Demo] Falied to create MQTT_DemoTask!\n");
}
}*/
/*
void UARTINIT(void)
{
osThreadAttr_t attr;
attr.name = "UART_Task";
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;
if (osThreadNew((osThreadFunc_t)UART_Task, NULL, &attr) == NULL)
{
printf("[ADCExample] Falied to create UART_Task!\n");
}
}*/
void HelloWorld(void)
{
MessageTestTask();
}
//SYS_RUN(HelloWorld);
APP_FEATURE_INIT(HelloWorld);
经历了各种错误,无论是栈溢出还是别的。。。。。到现在为止UART任务已经差不多结束了,也能成功发送出去数据,然后MQTT那边目前用的是样例里面自带的receiver,接下来的任务就是把那边衔接上
- 历经千辛万苦地修改,最终实现的第一次成功通信:
#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 CONNECT_SSID ""
#define CONNECT_PASSWORD ""
#define CONNECT_IP ""
#define CONNECT_PORT 1883
#define UART_TASK_STACK_SIZE 1024 * 8
#define UART_TASK_PRIO 25
#define UART_BUFF_SIZE 1000
#define WIFI_IOT_UART_IDX_1 1
#define NUM 1
#define OS_DELAY 5
#define OS_DELAY_F 3
#define OS_DELAY_S 20
#define OS_DELAY_T 80
#define ATTR_STACK_SIZE 1024*20
#define QUEUE_SIZE 3
static const char *data = "Hello, Hispark!\r\n";
static unsigned char sendBuf[1000];
static unsigned char readBuf[1000];
Network network;
typedef struct {
osThreadId_t tid;
int count;
} message_entry;
osMessageQueueId_t qid;
MQTTClient client;
/*
void sender_thread(void)
{
static int count = 0;
message_entry sentry;
//(int)arg;
while (NUM) {
sentry.tid = osThreadGetId();
printf("[Message Test] %s send %d to message queue.\r\n", osThreadGetName(osThreadGetId()), count);
osMessageQueuePut(qid, (const int *)&sentry, 0, osWaitForever);
count++;
osDelay(OS_DELAY);
}
}
*/
void receiver_thread(void)
{
//(int)arg;
message_entry rentry;
while (NUM) {
osMessageQueueGet(qid, (int *)&rentry, NULL, osWaitForever);
printf("[Message Test] %s get %d from %s by message queue.\r\n",
osThreadGetName(osThreadGetId()), rentry.count, osThreadGetName(rentry.tid));
osDelay(OS_DELAY_F);
}
}
void Do_MQTT_Task(message_entry rentry)
{
start:
printf("MQTTClientInit ...\n");
MQTTClientInit(&client, &network, 2000, sendBuf, sizeof(sendBuf), readBuf, sizeof(readBuf));
MQTTString clientId = MQTTString_initializer;
clientId.cstring = "hispark";
MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
data.clientID = clientId;
data.willFlag = 0;
data.MQTTVersion = 3;
data.keepAliveInterval = 0;
data.cleansession = 1;
int rc = -1;
printf("MQTTConnect ...\n");
rc = MQTTConnect(&client, &data);
printf("rc:%d\n",rc);
if (rc != 0)
{
printf("MQTT ERROR!\n");
//NetworkDisconnect(&network);
MQTTDisconnect(&client);
osDelay(200);
goto start;
}
else
{
printf("MQTT Successfully Connect!\n");
while(1)
{
printf("send!\n");
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("Return code from MQTT publish is %d\n", rc);
//NetworkDisconnect(&network);
MQTTDisconnect(&client);
goto start;
}else{
printf("send finish!\n");
}
osDelay(50);
}
}
}
void receiver_thread1(void)
{
//(int)arg;
message_entry rentry;
while (NUM)
{
osMessageQueueGet(qid, (int *)&rentry, NULL, osWaitForever);
printf("[Message Test] %s get %d from %s by message queue.\r\n",
osThreadGetName(osThreadGetId()), rentry.count, osThreadGetName(rentry.tid));
osDelay(OS_DELAY_F);
if(rentry.count > 0)
{
Do_MQTT_Task(rentry);
}
}
}
/*
osThreadId_t newThread(char *name, osThreadFunc_t func, int *arg)
{
osThreadAttr_t attr = {
name, 0, NULL, 0, NULL, 1024*2, osPriorityNormal, 0, 0
};
osThreadId_t tid = osThreadNew(func, arg, &attr);
if (tid == NULL) {
printf("[Message Test] osThreadNew(%s) failed.\r\n", name);
} else {
printf("[Message Test] osThreadNew(%s) success, thread id: %d.\r\n", name, tid);
}
return tid;
}*/
void messageArrived(MessageData* data)
{
printf("Message arrived on topic %.*s: %.*s\n", data->topicName->lenstring.len, data->topicName->lenstring.data,
data->message->payloadlen, data->message->payload);
}
static void MQTT_DemoTask(void)
{
message_entry rentry;
WifiConnect(CONNECT_SSID,CONNECT_PASSWORD);
printf("Starting ...\n");
int rc;
NetworkInit(&network);
printf("NetworkConnect ...\n");
begin:
NetworkConnect(&network, CONNECT_IP, CONNECT_PORT);
printf("MQTTClientInit ...\n");
MQTTClientInit(&client, &network, 2000, sendBuf, sizeof(sendBuf), readBuf, sizeof(readBuf));
MQTTString clientId = MQTTString_initializer;
clientId.cstring = "hispark";
MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
data.clientID = clientId;
data.willFlag = 0;
data.MQTTVersion = 3;
data.keepAliveInterval = 0;
data.cleansession = 1;
printf("MQTTConnect ...\n");
rc = MQTTConnect(&client, &data);
if (rc != 0) {
printf("MQTTConnect: %d\n", rc);
NetworkDisconnect(&network);
MQTTDisconnect(&client);
osDelay(200);
goto begin;
}
while(1){
osMessageQueueGet(qid, (int *)&rentry, NULL, osWaitForever);
printf("[Message Test] %s get %d from %s by message queue.\r\n",
osThreadGetName(osThreadGetId()), rentry.count, osThreadGetName(rentry.tid));
//osDelay(OS_DELAY_F);
MQTTMessage message;
char payload[30];
message.qos = 2;
message.retained = 0;
message.payload = payload;
sprintf(payload, "message number %d", 1314);
message.payloadlen = strlen(payload);
/*
printf("MQTTSubscribe ...\n");
rc = MQTTSubscribe(&client, "substopic", 2, messageArrived);
if (rc != 0) {
printf("MQTTSubscribe: %d\n", rc);
osDelay(200);
goto begin;
}*/
if ((rc = MQTTPublish(&client, "pubtopic", &message)) != 0){
printf("Return code from MQTT publish is %d\n", rc);
NetworkDisconnect(&network);
MQTTDisconnect(&client);
goto begin;
}
osDelay(50);
}
}
static void UART_Task(void)
{
uint8_t uart_buff[UART_BUFF_SIZE] = {0};
uint8_t *uart_buff_ptr = uart_buff;
uint32_t ret;
message_entry sentry;
//WifiIotUartAttribute uart_attr = {
IotUartAttribute uart_attr = {
.baudRate = 9600,
.dataBits = 8,
.stopBits = 1,
.parity = 0,
};
ret = IoTUartInit(WIFI_IOT_UART_IDX_1, &uart_attr);
if (ret != IOT_SUCCESS)
{
printf("Failed to init uart! Err code = %d\n", ret);
return;
}
printf("UART START\n");
while (1)
{
//IoTUartWrite(WIFI_IOT_UART_IDX_1, (unsigned char *)data, strlen(data));
//通过串口1接收数据
int length = IoTUartRead(WIFI_IOT_UART_IDX_1, uart_buff_ptr, UART_BUFF_SIZE);
if(length > 0)
{
printf("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("[Message Test] %s send %d to message queue.\r\n", osThreadGetName(osThreadGetId()), res);
osMessageQueuePut(qid, (const int *)&sentry, 0, osWaitForever);
osDelay(10);
//}
}
}
}
}
usleep(500000);
}
}
osThreadId_t newMQTTThread()
{
osThreadAttr_t attr;
attr.name = "MQTT_DemoTask";
attr.attr_bits = 0U;
attr.cb_mem = NULL;
attr.cb_size = 0U;
attr.stack_mem = NULL;
attr.stack_size = 10240;
attr.priority = osPriorityNormal;
printf("MQTT Thread Create!\n");
osThreadId_t tid = osThreadNew((osThreadFunc_t)receiver_thread1, NULL, &attr);
if (tid == NULL) {
printf("[Message Test] osThreadNew(MQTT) failed.\r\n");
} else {
printf("[Message Test] osThreadNew(MQTT) success, thread id: %d.\r\n",tid);
}
return tid;
}
osThreadId_t newUARTThread()
{
osThreadAttr_t attr;
attr.name = "UART_Task";
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 Thread Create!\n");
osThreadId_t tid = osThreadNew((osThreadFunc_t)UART_Task, NULL, &attr);
if (tid == NULL) {
printf("[Message Test] osThreadNew(UART) failed.\r\n");
} else {
printf("[Message Test] osThreadNew(UART) success, thread id: %d.\r\n",tid);
}
return tid;
}
void WIFI_Prepare(void)
{
printf("NETWORKCONNECT...\n");
WifiConnect(CONNECT_SSID,CONNECT_PASSWORD);
NetworkInit(&network);
NetworkConnect(&network, CONNECT_IP, CONNECT_PORT);
}
void rtosv2_msgq_main(int *arg)
{
WIFI_Prepare();
qid = osMessageQueueNew(QUEUE_SIZE, sizeof(message_entry), NULL);
osThreadId_t ctid = newMQTTThread();
osThreadId_t ptid = newUARTThread();
osDelay(OS_DELAY_S);
uint32_t cap = osMessageQueueGetCapacity(qid);
printf("[Message Test] osMessageQueueGetCapacity, capacity: %u.\r\n", cap);
uint32_t msg_size = osMessageQueueGetMsgSize(qid);
printf("[Message Test] osMessageQueueGetMsgSize, size: %u.\r\n", msg_size);
uint32_t count = osMessageQueueGetCount(qid);
printf("[Message Test] osMessageQueueGetCount, count: %u.\r\n", count);
uint32_t space = osMessageQueueGetSpace(qid);
printf("[Message Test] osMessageQueueGetSpace, space: %u.\r\n", space);
osDelay(OS_DELAY_T);
osThreadTerminate(ctid);
osThreadTerminate(ptid);
osMessageQueueDelete(qid);
}
static void MessageTestTask(void)
{
osThreadAttr_t attr;
attr.name = "rtosv2_msgq_main";
attr.attr_bits = 0U;
attr.cb_mem = NULL;
attr.cb_size = 0U;
attr.stack_mem = NULL;
attr.stack_size = ATTR_STACK_SIZE;
attr.priority = osPriorityNormal;
if (osThreadNew((osThreadFunc_t)rtosv2_msgq_main, NULL, &attr) == NULL) {
printf("[MessageTestTask] Failed to create rtosv2_msgq_main!\n");
}
}
/*
void MQTTINIT(void)
{
osThreadAttr_t attr;
attr.name = "MQTT_DemoTask";
attr.attr_bits = 0U;
attr.cb_mem = NULL;
attr.cb_size = 0U;
attr.stack_mem = NULL;
attr.stack_size = 10240;
attr.priority = osPriorityNormal;
if (osThreadNew((osThreadFunc_t)MQTT_DemoTask, NULL, &attr) == NULL) {
printf("[MQTT_Demo] Falied to create MQTT_DemoTask!\n");
}
}*/
/*
void UARTINIT(void)
{
osThreadAttr_t attr;
attr.name = "UART_Task";
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;
if (osThreadNew((osThreadFunc_t)UART_Task, NULL, &attr) == NULL)
{
printf("[ADCExample] Falied to create UART_Task!\n");
}
}*/
void HelloWorld(void)
{
MessageTestTask();
}
//SYS_RUN(HelloWorld);
APP_FEATURE_INIT(HelloWorld);
总结一下之前遇到的问题:
消息队列样例里面的线程开的内存太小了,导致无法进行wifi连接操作,而openharmony的奇怪机制导致它停留但是不执行,并没有进行栈溢出的报错。这就是为什么之前只执行了几次之后就结束了,什么都没输出
接下来需要解决的问题:
多次执行,让输入不断变化时可以实现反复通信与交互
- 附上一次成功的记录:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了