Saving chunked response data to spiffs from ISR

FreakONN
Posts: 2
Joined: Tue Feb 12, 2019 12:22 pm

Saving chunked response data to spiffs from ISR

Postby FreakONN » Tue Feb 12, 2019 12:36 pm

Hello, first time posting here.

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);
}
/backupSystem.c

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);
}

ESP_Sprite
Posts: 9766
Joined: Thu Nov 26, 2015 4:08 am

Re: Saving chunked response data to spiffs from ISR

Postby ESP_Sprite » Wed Feb 13, 2019 3:13 am

1. What is your question?
2. A http callback is not called in an interrupt context and as such is not an ISR. You can use the normal non-fromISR FreeRTOS here.

FreakONN
Posts: 2
Joined: Tue Feb 12, 2019 12:22 pm

Re: Saving chunked response data to spiffs from ISR

Postby FreakONN » Wed Feb 13, 2019 7:54 am

I'm not sure I'm doing it right. I need advice how to do it properly. I'm new to this. How to save 512 bytes received every second from server to spiffs without interrupting other tasks running concurrently.

To paint picture more clearly. I'm working with a6 gsm with mfrc522 on esp-wroom-32u. GSM module and rfid reader should open gates. They do that by sending data to server when phone number or rfid card is found. In case of wifi loss backup is used in spiffs storage. Backup is renewed every hour and as I said it already it should not interrupt other tasks from doing their work while is receiving backup data.

Who is online

Users browsing this forum: Bing [Bot] and 129 guests