Compare commits

...

4 Commits

Author SHA1 Message Date
6a79a887a6 print more about http responses from server 2020-10-04 07:58:28 -05:00
688a156ec2 could transmit a photo finally 2020-10-04 07:39:33 -05:00
778189ce5f rename camera driver module url 2020-10-03 17:12:39 -05:00
465def9b54 driver module 2020-10-03 17:11:02 -05:00
3 changed files with 61 additions and 33 deletions

3
.gitmodules vendored Normal file
View File

@@ -0,0 +1,3 @@
[submodule "components/esp32-camera"]
path = components/esp32-camera
url = ssh://git@git.gminus2.duckdns.org:9822/nam/esp32-camera-driver.git

View File

@@ -119,7 +119,7 @@ static const uint32_t CHUNK_SIZE = 1024;
static const uint32_t MSG_SIZE = 256;
static const char * uploadPath = "/upload";
static const char * server = "192.168.1.107:3001";
static const char * boundary = "01234567899876543210";
static const char * boundary = "boundary";
static void post_data(camera_fb_t * pic){
char url[128];
@@ -129,11 +129,11 @@ static void post_data(camera_fb_t * pic){
esp_http_client_handle_t client = esp_http_client_init(&config);
esp_http_client_set_method(client, HTTP_METHOD_POST);
char header[CHUNK_SIZE];
char head[MSG_SIZE];
char tail[MSG_SIZE];
sprintf(head, "--%s\r\n"
sprintf(head,
"--%s\r\n"
"Content-Disposition: form-data; name=\"photo\"; filename=\"esp32.jpg\"\r\n"
"Content-Type: image/jpeg\r\n\r\n", boundary);
sprintf(tail, "\r\n--%s--\r\n", boundary);
@@ -142,48 +142,64 @@ static void post_data(camera_fb_t * pic){
char tmpStr[MSG_SIZE];
// send header so that the server knows what to do with chunks after this
sprintf(header, "POST %s HTTP/1.1\r\nHost: %s\r\n"
"Content-Length: %u\r\n"
"Expect: 100-continue\r\n"
"Accept: */*\r\n"
"Content-Type: multipart/form-data; boundary=%s\r\n\r\n"
, uploadPath, server, pic->len, boundary);
ESP_LOGI(TAG, "header to be sent:\n%s", header);
sprintf(tmpStr, "%u", totalLen);
esp_http_client_set_header(client, "Content-Length", tmpStr);
ESP_LOGI(TAG, "Content-Length: %s", tmpStr);
esp_http_client_open(client, totalLen);
sprintf(tmpStr, "multipart/form-data; boundary=%s", boundary);
esp_http_client_set_header(client, "Content-Type", tmpStr);
ESP_LOGI(TAG, "Content-Type: %s", tmpStr);
esp_http_client_set_header(client, "Accept", "*/*");
esp_http_client_set_header(client, "Expect", "100-continue");
err_t err;
err = esp_http_client_open(client, totalLen);
if (err != ESP_OK) {
ESP_LOGE(TAG, "esp_http_client_open failed");
esp_http_client_cleanup(client);
return;
}
// send the first header
esp_http_client_write(client, header, strlen(header));
vTaskDelay(100 / portTICK_RATE_MS);
// 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);
/* char resp[CHUNK_SIZE]; */
/* esp_http_client_read(client, resp, CHUNK_SIZE); */
/* ESP_LOGI(TAG, "%s", resp); */
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);
}
// send head
// 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){
@@ -191,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);
}