Help sending 3 floats with xQueueSend

Hi,

Forgive me, I am still new to C++ and am learning FreeRTOS.

I am trying to send 3 float values using xQueueSend.
The problem is that only the first float is being successfully received.
Looking for some help please.

This running on an ESP32 using PlatformIO to compile, the other code runs fine, it’s just passing these values that does not.

Here’s the code.

void ReadBME280(void *parameter)
{
  float test[] = { 10.1, 20.2, 30,3 };
  xQueueSend(TemperatureQueue, &test, portMAX_DELAY);
  vTaskDelete(NULL);
}

void SendOverWiFi(void *parameter)
{
  float Readings[3];
  xQueueReceive(TemperatureQueue, &Readings, portMAX_DELAY);
  Serial.println(Readings[0]);
  Serial.println(Readings[1]);
  Serial.println(Readings[2]);
 vTaskDelete(NULL);
}

void setup()
{
  TemperatureQueue = xQueueCreate(3, sizeof(float));
  TaskHandle_t Task4; // SendOverWifi
  TaskHandle_t Task8; // ReadBME280
  xTaskCreate(ReadBME280, "Task8", 2500, NULL, 0, &Task8);
  xTaskCreate(SendOverWiFi, "Task4", 2500, NULL, 0, &Task4);
}

The output shows only the first float.
10.10
-0.00
-0.00

Thanks !!

To send 3 queue items you’ve to call xQueueSend 3 times.
You’ve created the queue for with item size 1 float and depth 3, which means you send/receive 1 float items per call and the queue can hold/buffer max. 3 float items.
See also https://freertos.org/Embedded-RTOS-Queues.html and e.g. xQueueCreate API description.

1 Like

Hi @cloudgenki,

You need to call the xQueueSend()/xQueueReceive() once for each item as far as I understand.

void ReadBME280(void *parameter)
{
    float test[] = { 10.1, 20.2, 30.3 };
    for( int i =0; i <3; i++ ) 
    {
        xQueueSend(TemperatureQueue, &test[i], portMAX_DELAY);
    }
vTaskDelete(NULL);
}

void SendOverWiFi(void *parameter)
{
    float Readings[3];
    for(int i = 0; i < 3; i++ ) {
        xQueueReceive(TemperatureQueue, &Readings[i], portMAX_DELAY);
    }
    Serial.println(Readings[0]);
    Serial.println(Readings[1]);
    Serial.println(Readings[2]);
    vTaskDelete(NULL);
}

I hope that helps.

Kind Regards,

Pete

1 Like

Wow that was a quick reply. I didn’t even think of calling the xQueueSend for each element in the array… DOH !!

Thanks I will try it now.

Thanks pete-pjb that was the solution !!

:grinning:

1 Like

Hi hs2,
Thank you for the help, yes I tried and your suggestion works. I will check out the link you sent Thanks!

The other option would be to make the element size of the queue to be 3*sizeof(float)

Hi,

I actually thought I had made the element size of the queue to be 3 floats with this statement
TemperatureQueue = xQueueCreate(3, sizeof(float));

I didn’t think about iterating over the Queue to fill it.

I had hoped that I could create a struct containing 3 floats and call the XQueueSend only 1 time filling it with the 3 floats… it seemed cleaner / faster

Thx

Sure you can send structs. As described in the examples you find following the links already posted. In fact FreeRTOS queues handle any fixed sized binary items or packets you want. Using the docs save a lot of time :wink:

The first parameter is basically the number of ‘Send’ the queue can hold.
The second parameter is how much to send with each ‘Send’ call.

If you want to send 3 float at once, then it can be 3*sizeof(float), to send an array.