Message doesn't get published

random_fmad
Posts: 3
Joined: Tue Oct 25, 2022 10:46 am

Message doesn't get published

Postby random_fmad » Tue Oct 25, 2022 11:05 am

Hi Friends,

I am working on pushing some telemetry value to MQTT hub in Azure IoT. The firmware is written in ESP IDF 4.41 and code is running on ESP32-WROOM-32D. Pushing telemetry to Azure IoT using MQTT is working fine.

Thinking of taking the project to next stage by improving reliability introduced few changes by logging Telemetry to text file in SD card and then reading the Telemetry from SD card and pushing it to Azure IoT Hub so that it can withstand issues incase Wi-Fi is not accessible or down.

So have designed an asynchronous logger where telemetry messages are written on to the sd card as a .txt file . Each message is separated by a delimiter and pipe character "|" is used as delimiter.

The code reads message and then sperate message based on delimiter and then publish the first message which works fine. But when publishing the second message some special character gets appended to the message.

I have attached the output below. How can the problem be resolved?
  1.     char* prev_msg;
  2.     char msg_dat[] = "";
  3.     char dst[2048];
  4.     char msg_buffer[4096];
  5.  
  6.     if(unsend_data  == true)
  7.         {
  8.             ESP_LOGI("test","inside logic");
  9.             if(sd_card == true)
  10.             {
  11.                 xSemaphoreTake(sdcard_mutex,pdMS_TO_TICKS(1000));
  12.                 FILE* fu = fopen("/sdcard/unsend.txt","r");
  13.                 if (fu == NULL){
  14.                     goto jump;
  15.                 }
  16.                 else
  17.                 {
  18.                     fread(msg_buffer,1,sizeof(msg_buffer)-1,fu);
  19.                     ESP_LOGI("unsend_data","%s",msg_buffer);
  20.                 }
  21.                 fclose(fu);
  22.                 xSemaphoreGive(sdcard_mutex);
  23.                 strcpy(msg_dat,msg_buffer);
  24.                 printf("%s\n",msg_dat);
  25.                 char *p = strtok(msg_dat,"|");
  26.                 while(p!=0)
  27.                 {
  28.                    
  29.                     prev_msg = p;
  30.                     p = strtok(0,"|");  
  31.                     if(prev_msg != 0)
  32.                     {
  33.                         printf("Before publishing: %s\n",prev_msg);
  34.                         strcpy(dst, prev_msg);
  35.                         xResult = AzureIoTHubClient_SendTelemetry( &xAzureIoTHubClient,
  36.                                                            (uint8_t *)dst, strlen(dst)+1,
  37.                                                            NULL, eAzureIoTHubMessageQoS1, NULL );
  38.                         vTaskDelay(pdMS_TO_TICKS(1000));
  39.                         printf("After publishing: %s\n",dst);
  40.                         memset(dst, 0, sizeof(dst));
  41.                         vTaskDelay(pdMS_TO_TICKS(30000));*/
  42.                                  
  43.                     }
  44.                     else
  45.                     {
  46.                         unsend_data = false;
  47.                     }
  48.                 }
  49.                 xSemaphoreTake(sdcard_mutex,pdMS_TO_TICKS(5000));
  50.                 if(unlink("/sdcard/unsend.txt") != 0)
  51.                 {
  52.                     ESP_LOGE("Previous_msg","deleting unsend.txt failed");
  53.                 }
  54.                 xSemaphoreGive(sdcard_mutex);
  55.             }
  56.         }
Attachments
output.txt
(2.74 KiB) Downloaded 118 times

Who is online

Users browsing this forum: Baidu [Spider] and 357 guests