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 ouput below. What could be the reason?
- char* prev_msg;
- char msg_dat[] = "";
- char dst[2048];
- char msg_buffer[4096];
- if(unsend_data == true)
- {
- ESP_LOGI("test","inside logic");
- if(sd_card == true)
- {
- xSemaphoreTake(sdcard_mutex,pdMS_TO_TICKS(1000));
- FILE* fu = fopen("/sdcard/unsend.txt","r");
- if (fu == NULL){
- goto jump;
- }
- else
- {
- fread(msg_buffer,1,sizeof(msg_buffer)-1,fu);
- ESP_LOGI("unsend_data","%s",msg_buffer);
- }
- fclose(fu);
- xSemaphoreGive(sdcard_mutex);
- strcpy(msg_dat,msg_buffer);
- printf("%s\n",msg_dat);
- char *p = strtok(msg_dat,"|");
- while(p!=0)
- {
- prev_msg = p;
- p = strtok(0,"|");
- if(prev_msg != 0)
- {
- printf("Before publishing: %s\n",prev_msg);
- strcpy(dst, prev_msg);
- xResult = AzureIoTHubClient_SendTelemetry( &xAzureIoTHubClient,
- (uint8_t *)dst, strlen(dst)+1,
- NULL, eAzureIoTHubMessageQoS1, NULL );
- vTaskDelay(pdMS_TO_TICKS(1000));
- printf("After publishing: %s\n",dst);
- memset(dst, 0, sizeof(dst));
- vTaskDelay(pdMS_TO_TICKS(30000));*/
- }
- else
- {
- unsend_data = false;
- }
- }
- xSemaphoreTake(sdcard_mutex,pdMS_TO_TICKS(5000));
- if(unlink("/sdcard/unsend.txt") != 0)
- {
- ESP_LOGE("Previous_msg","deleting unsend.txt failed");
- }
- xSemaphoreGive(sdcard_mutex);
- }
- }