I now have to read and download a different file from Amazon S3 in order to flash it onto an AVR MCU that is attached to a UART. The locations of both firmwares are the same.
I am able to make the connection. I can't read this, however.
The code
- int content_length = esp_http_client_get_content_length(client);
- ESP_LOGI("avr", "Content length: %d", content_length);
- if (content_length < 0)
- {
- ESP_LOGE("avr", "Failed to get content length");
- }
- int buffer_size = snprintf(NULL, 0, "%d", content_length);
- ESP_LOGI("avr", "Buffer size: %d", buffer_size);
- char *buffer = (char *)malloc(content_length);
- if (buffer == NULL)
- {
- ESP_LOGE("avr", "Failed to allocate memory for buffer");
- }
- else
- {
- // Convert the content_length to a string and store it in the buffer
- snprintf(buffer, buffer_size + 1, "%d", content_length+1);
- ESP_LOGI("avr", "Buffer: %s", buffer);
- }
- ESP_LOGI("avr","block 1");
- int data_read_total = 0;
- while (data_read_total <= content_length)
- {
- int data_read = esp_http_client_read(client, buffer, content_length);
- // int data_read = esp_http_client_read(client, buffer + data_read_total, content_length - data_read_total);
- ESP_LOGI("avr", "Data read: %d", data_read);
- if (data_read <= 0)
- {
- ESP_LOGE("avr", "Failed to read data");
- // break; // Exit the loop if reading fails
- }
- data_read_total += data_read;
- }
- buffer[content_length+1] = '\0'; // Null-terminate the buffer
- printf("Buffer: %s\n", buffer);
- ESP_LOGI("avr", "Data read: %d", data_read_total);
- free(buffer);
- I (69399) avr: HTTP_EVENT_ON_HEADER, key=Server, value=AmazonS3
- I (69399) avr: HTTP_EVENT_ON_HEADER, key=Content-Length, value=27258
- I (69889) avr: HTTP_EVENT_ON_FINISH
- I (69889) avr: Content length: 27258
- I (69899) avr: Buffer size: 5
- I (69899) avr: Buffer: 27259
- I (69899) avr: block 1
- I (69909) avr: Data read: 0
- E (69909) avr: Failed to read data
- Buffer: 27259
- I (69909) avr: Data read: 0