diff --git a/main/main.c b/main/main.c index 29863a2..c7a0db3 100644 --- a/main/main.c +++ b/main/main.c @@ -160,35 +160,46 @@ static void post_data(camera_fb_t * pic){ return; } + // the empty body of the header, must be sent to complete the message sprintf(tmpStr, "\r\n"); esp_http_client_write(client, tmpStr, strlen(tmpStr)); + // wait a bit for response, expecting code 100 vTaskDelay(10 / portTICK_RATE_MS); esp_http_client_fetch_headers(client); - ESP_LOGI(TAG, "code %d", esp_http_client_get_status_code(client)); + int http_status_code = esp_http_client_get_status_code(client); - // send head + if (http_status_code != 100) { + ESP_LOGE(TAG, "Expecting HTTP status code 100, received: %u", http_status_code); + return; + } + else { + ESP_LOGI(TAG, "HTTP status code %d, continue sending data ...", http_status_code); + } + + // got 100, proceed to send head esp_http_client_write(client, head, strlen(head)); - ESP_LOGI(TAG, "%s", (char *)head); + ESP_LOGI(TAG, "head: %s", (char *)head); + // data in chunks size_t nChunks = pic->len / CHUNK_SIZE + 1; uint8_t *ptr = pic->buf; size_t fbLen = pic->len; - ESP_LOGI(TAG, "photo size 0x%x, will be posted in %u chunks", fbLen, nChunks); + ESP_LOGI(TAG, "photo size %u bytes, will be posted in %u chunks", fbLen, nChunks); for (size_t n = 0; n < fbLen; n += CHUNK_SIZE) { if (n + CHUNK_SIZE < fbLen) { esp_http_client_write(client, (char *)ptr, CHUNK_SIZE); - if (n<2) { - sprintf(tmpStr, "%u:", n); - for (int i = 0; i < 15; i++) { - char nextChar[4]; - sprintf(nextChar, "%x", ptr[i]); - strcat(tmpStr, nextChar); - } - ESP_LOGI(TAG, "%.16s", tmpStr); - } + //if (n<2) { + // sprintf(tmpStr, "%u:", n); + // for (int i = 0; i < 15; i++) { + // char nextChar[4]; + // sprintf(nextChar, "%x", ptr[i]); + // strcat(tmpStr, nextChar); + // } + // ESP_LOGI(TAG, "%.16s", tmpStr); + //} ptr += CHUNK_SIZE; } else if (fbLen % CHUNK_SIZE > 0){ @@ -196,10 +207,18 @@ static void post_data(camera_fb_t * pic){ } } - ESP_LOGI(TAG, "%s", (char *)tail); + // and the tail + ESP_LOGI(TAG, "tail: %s", (char *)tail); esp_http_client_write(client, tail, strlen(tail)); - esp_http_client_fetch_headers(client); - ESP_LOGI(TAG, "response %d", esp_http_client_get_status_code(client)); + + // check HTTP response + int response_length = esp_http_client_fetch_headers(client); + http_status_code = esp_http_client_get_status_code(client); + ESP_LOGI(TAG, "HTTP status: %d, content-length: %d", http_status_code, response_length); + if (response_length > 0 && response_length < CHUNK_SIZE) { + esp_http_client_read(client, tmpStr, response_length); + ESP_LOGI(TAG, "HTTP response: %s", tmpStr); + } esp_http_client_cleanup(client); }