I'm having problem successfully saving http post data to spiffs file system. I'm receiving data in parts (chunks) and saving it to spiffs. However, I'm receiving huge amount of data, 48kB to be exact that I'm supposed to send from http event handler with xQueueSendFromISR to task that should save all that data without interrupting other tasks.
Here is my code:
/http_client.c
Code: Select all
esp_err_t _http_event_handler_backup_file(esp_http_client_event_t *evt)
{
switch(evt->event_id) {
case HTTP_EVENT_ERROR:
ESP_LOGD("[ HTTP_CLIENT ]", "HTTP_EVENT_ERROR");
break;
case HTTP_EVENT_ON_CONNECTED:
ESP_LOGD("[ HTTP_CLIENT ]", "HTTP_EVENT_ON_CONNECTED");
break;
case HTTP_EVENT_HEADER_SENT:
ESP_LOGD("[ HTTP_CLIENT ]", "HTTP_EVENT_HEADER_SENT");
break;
case HTTP_EVENT_ON_HEADER:
ESP_LOGD("[ HTTP_CLIENT ]", "HTTP_EVENT_ON_HEADER, key=%s, value=%s", evt->header_key, evt->header_value);
break;
case HTTP_EVENT_ON_DATA:
ESP_LOGD("[ HTTP_CLIENT ]", "HTTP_EVENT_ON_DATA, len=%d", evt->data_len);
if (!esp_http_client_is_chunked_response(evt->client)) {
//ESP_LOGI("[ HTTP_CLIENT_BACKUP_FILE ]", "Response: %.*s, strlen: %d, sizeof: %d", evt->data_len, (char*)evt->data, strlen((char*)evt->data), sizeof((char*)evt->data));
//ESP_LOGI("[ HTTP_CLIENT_BACKUP_FILE HANDLER]", "Response: data_len: %d, strlen: %d, sizeof: %d", evt->data_len, strlen((char*)evt->data), sizeof((char*)evt->data));
DataBaseRx DB;
strcpy(DB.message, (char*)evt->data);
DB.length = evt->data_len; //evt->data_len;
xHigherPriorityTaskWoken = pdFALSE;
//evt->data = 0;
xQueueSendFromISR( backupQueue, &DB, xHigherPriorityTaskWoken);
}
break;
case HTTP_EVENT_ON_FINISH:
ESP_LOGD("[ HTTP_CLIENT ]", "HTTP_EVENT_ON_FINISH");
break;
case HTTP_EVENT_DISCONNECTED:
ESP_LOGD("[ HTTP_CLIENT ]", "HTTP_EVENT_DISCONNECTED");
break;
}
return ESP_OK;
}
//GET BACKUP FILE
void requestBackupFile(char *param)
{
esp_http_client_config_t config = {
.url = "http://192.168.1.20/api/backup",
.event_handler = _http_event_handler_backup_file,
};
esp_http_client_handle_t client = esp_http_client_init(&config);
char *post_data = "file=backup/filename";
esp_http_client_set_method(client, HTTP_METHOD_POST);
esp_http_client_set_post_field(client, post_data, strlen(post_data));
/* esp_http_client_set_header(client, "Content-Type", "text/plain");
esp_http_client_set_header(client, "Accept", "text/plain");
esp_http_client_set_header(client, "cache-control", "no-cache");*/
esp_err_t err = esp_http_client_perform(client);
if (err == ESP_OK) {
ESP_LOGI("[ HTTP_CLIENT ]", "HTTP chunk encoding Status = %d, content_length = %d",
esp_http_client_get_status_code(client),
esp_http_client_get_content_length(client));
} else {
ESP_LOGE("[ HTTP_CLIENT ]", "Error perform http request %s", esp_err_to_name(err));
}
//esp_http_client_close(client);
esp_http_client_cleanup(client);
}
Code: Select all
void backupSystemTask( void *pvParameters )
{
DataBaseRx DB;
while(1)
{
TIMERG0.wdt_wprotect=TIMG_WDT_WKEY_VALUE;
TIMERG0.wdt_feed=1;
TIMERG0.wdt_wprotect=0;
if( xQueueReceive( backupQueue, ( void * ) &DB, portMAX_DELAY) != pdFALSE){
//ESP_LOGI("[ HTTP_CLIENT_BACKUP_FILE ]", "%s, length %d", DB.message, DB.length );
printf("%s", DB.message);
//spiffs_append(DB.message, DB.length);
strcpy(DB.message, "");
DB.length = 0;
}
//vTaskDelay(10/portTICK_RATE_MS);
}
}
Code: Select all
void backupSystem_init( void )
{
xTaskCreate(&backupSystemTask, "backupSystemTask", 1024 * 50, NULL, 5, NULL);
}
/spiffs.c
void spiffs_append(char *dataToAppend, int size)
{
FILE *file;
if ((file = fopen("/spiffs/foo.txt", "r")))
{
fclose(file);
}else{
spiffs_create_file();
}
file = fopen("/spiffs/foo.txt", "a");
fwrite(dataToAppend, 1, size, SP.f);
fclose(file);
}