diff --git a/bathroom-esp32/.vscode/settings.json b/bathroom-esp32/.vscode/settings.json index b7fa1c1..6713db9 100755 --- a/bathroom-esp32/.vscode/settings.json +++ b/bathroom-esp32/.vscode/settings.json @@ -12,8 +12,11 @@ "interface/ftdi/esp32_devkitj_v1.cfg", "target/esp32.cfg" ], - "idf.portWin": "COM7", + "idf.portWin": "COM8", "idf.pythonBinPathWin": "C:\\Projects\\Esp32\\espressif\\python_env\\idf5.3_py3.11_env\\Scripts\\python.exe", "idf.toolsPathWin": "C:\\Projects\\Esp32\\espressif", - "idf.flashType": "UART" + "idf.flashType": "UART", + "files.associations": { + "led_strip.h": "c" + } } diff --git a/bathroom-esp32/.vscode/settings.json b/bathroom-esp32/.vscode/settings.json index b7fa1c1..6713db9 100755 --- a/bathroom-esp32/.vscode/settings.json +++ b/bathroom-esp32/.vscode/settings.json @@ -12,8 +12,11 @@ "interface/ftdi/esp32_devkitj_v1.cfg", "target/esp32.cfg" ], - "idf.portWin": "COM7", + "idf.portWin": "COM8", "idf.pythonBinPathWin": "C:\\Projects\\Esp32\\espressif\\python_env\\idf5.3_py3.11_env\\Scripts\\python.exe", "idf.toolsPathWin": "C:\\Projects\\Esp32\\espressif", - "idf.flashType": "UART" + "idf.flashType": "UART", + "files.associations": { + "led_strip.h": "c" + } } diff --git a/bathroom-esp32/main/CMakeLists.txt b/bathroom-esp32/main/CMakeLists.txt index 92a6850..646f273 100755 --- a/bathroom-esp32/main/CMakeLists.txt +++ b/bathroom-esp32/main/CMakeLists.txt @@ -1,6 +1,3 @@ idf_component_register(SRCS "main.c" INCLUDE_DIRS "." - REQUIRES driver - REQUIRES esp_wifi - REQUIRES nvs_flash - REQUIRES mbedtls) \ No newline at end of file + REQUIRES driver esp_wifi nvs_flash esp_http_client esp-tls) \ No newline at end of file diff --git a/bathroom-esp32/.vscode/settings.json b/bathroom-esp32/.vscode/settings.json index b7fa1c1..6713db9 100755 --- a/bathroom-esp32/.vscode/settings.json +++ b/bathroom-esp32/.vscode/settings.json @@ -12,8 +12,11 @@ "interface/ftdi/esp32_devkitj_v1.cfg", "target/esp32.cfg" ], - "idf.portWin": "COM7", + "idf.portWin": "COM8", "idf.pythonBinPathWin": "C:\\Projects\\Esp32\\espressif\\python_env\\idf5.3_py3.11_env\\Scripts\\python.exe", "idf.toolsPathWin": "C:\\Projects\\Esp32\\espressif", - "idf.flashType": "UART" + "idf.flashType": "UART", + "files.associations": { + "led_strip.h": "c" + } } diff --git a/bathroom-esp32/main/CMakeLists.txt b/bathroom-esp32/main/CMakeLists.txt index 92a6850..646f273 100755 --- a/bathroom-esp32/main/CMakeLists.txt +++ b/bathroom-esp32/main/CMakeLists.txt @@ -1,6 +1,3 @@ idf_component_register(SRCS "main.c" INCLUDE_DIRS "." - REQUIRES driver - REQUIRES esp_wifi - REQUIRES nvs_flash - REQUIRES mbedtls) \ No newline at end of file + REQUIRES driver esp_wifi nvs_flash esp_http_client esp-tls) \ No newline at end of file diff --git a/bathroom-esp32/main/main.c b/bathroom-esp32/main/main.c index 5751149..fa9483a 100755 --- a/bathroom-esp32/main/main.c +++ b/bathroom-esp32/main/main.c @@ -17,6 +17,7 @@ */ #include #include +#include #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "driver/gpio.h" @@ -28,13 +29,8 @@ #include "esp_event.h" #include "esp_err.h" #include "nvs_flash.h" - -#include "mbedtls/pk.h" -#include "mbedtls/rsa.h" -#include "mbedtls/error.h" -#include "mbedtls/base64.h" -#include "mbedtls/entropy.h" -#include "mbedtls/ctr_drbg.h" +#include "esp_http_client.h" +#include "esp_tls.h" static const char *TAG = "example"; @@ -177,8 +173,8 @@ wifi_config_t wifi_config = { .sta = { - .ssid = "DJArtsGames", - .password = "yubyubechubab", + .ssid = "Test", + .password = "password", /* Authmode threshold resets to WPA2 as default if password matches WPA2 standards (pasword len => 8). * If you want to connect the device to deprecated WEP/WPA networks, Please set the threshold value * to WIFI_AUTH_WEP/WIFI_AUTH_WPA_PSK and set the password with length and format matching to @@ -216,33 +212,97 @@ } } -// From: https://gist.github.com/VNovytskyi/e6d29b3d473a41141e27059a25e64100 -size_t get_decoded_size(const char *base64_str_p) +#define MAX_HTTP_OUTPUT_BUFFER 2048 + +esp_err_t _http_event_handler(esp_http_client_event_t *evt) { - if (base64_str_p == NULL) - { - return 0; + static char *output_buffer; // Buffer to store response of http request from event handler + static int output_len; // Stores number of bytes read + switch(evt->event_id) { + case HTTP_EVENT_ERROR: + ESP_LOGD(TAG, "HTTP_EVENT_ERROR"); + break; + case HTTP_EVENT_ON_CONNECTED: + ESP_LOGD(TAG, "HTTP_EVENT_ON_CONNECTED"); + break; + case HTTP_EVENT_HEADER_SENT: + ESP_LOGD(TAG, "HTTP_EVENT_HEADER_SENT"); + break; + case HTTP_EVENT_ON_HEADER: + ESP_LOGD(TAG, "HTTP_EVENT_ON_HEADER, key=%s, value=%s", evt->header_key, evt->header_value); + break; + case HTTP_EVENT_ON_DATA: + ESP_LOGD(TAG, "HTTP_EVENT_ON_DATA, len=%d", evt->data_len); + // Clean the buffer in case of a new request + if (output_len == 0 && evt->user_data) { + // we are just starting to copy the output data into the use + memset(evt->user_data, 0, MAX_HTTP_OUTPUT_BUFFER); + } + /* + * Check for chunked encoding is added as the URL for chunked encoding used in this example returns binary data. + * However, event handler can also be used in case chunked encoding is used. + */ + if (!esp_http_client_is_chunked_response(evt->client)) { + // If user_data buffer is configured, copy the response into the buffer + int copy_len = 0; + if (evt->user_data) { + // The last byte in evt->user_data is kept for the NULL character in case of out-of-bound access. + copy_len = MIN(evt->data_len, (MAX_HTTP_OUTPUT_BUFFER - output_len)); + if (copy_len) { + memcpy(evt->user_data + output_len, evt->data, copy_len); + } + } else { + int content_len = esp_http_client_get_content_length(evt->client); + if (output_buffer == NULL) { + // We initialize output_buffer with 0 because it is used by strlen() and similar functions therefore should be null terminated. + output_buffer = (char *) calloc(content_len + 1, sizeof(char)); + output_len = 0; + if (output_buffer == NULL) { + ESP_LOGE(TAG, "Failed to allocate memory for output buffer"); + return ESP_FAIL; + } + } + copy_len = MIN(evt->data_len, (content_len - output_len)); + if (copy_len) { + memcpy(output_buffer + output_len, evt->data, copy_len); + } + } + output_len += copy_len; + } + + break; + case HTTP_EVENT_ON_FINISH: + ESP_LOGD(TAG, "HTTP_EVENT_ON_FINISH"); + if (output_buffer != NULL) { + // Response is accumulated in output_buffer. Uncomment the below line to print the accumulated response + // ESP_LOG_BUFFER_HEX(TAG, output_buffer, output_len); + free(output_buffer); + output_buffer = NULL; + } + output_len = 0; + break; + case HTTP_EVENT_DISCONNECTED: + ESP_LOGI(TAG, "HTTP_EVENT_DISCONNECTED"); + int mbedtls_err = 0; + esp_err_t err = esp_tls_get_and_clear_last_error((esp_tls_error_handle_t)evt->data, &mbedtls_err, NULL); + if (err != 0) { + ESP_LOGI(TAG, "Last esp error code: 0x%x", err); + ESP_LOGI(TAG, "Last mbedtls failure: 0x%x", mbedtls_err); + } + if (output_buffer != NULL) { + free(output_buffer); + output_buffer = NULL; + } + output_len = 0; + break; + case HTTP_EVENT_REDIRECT: + ESP_LOGD(TAG, "HTTP_EVENT_REDIRECT"); + esp_http_client_set_header(evt->client, "From", "user@example.com"); + esp_http_client_set_header(evt->client, "Accept", "text/html"); + esp_http_client_set_redirection(evt->client); + break; } - - size_t pad_char_count = 0; - size_t base64_str_len = strlen(base64_str_p); - - if ((base64_str_len < 4) || (base64_str_len % 4 != 0)) - { - return 0; - } - - if (base64_str_p[base64_str_len - 1] == '=') - { - pad_char_count++; - } - - if (base64_str_p[base64_str_len - 2] == '=') - { - pad_char_count++; - } - - return (3 * (base64_str_len / 4)) - pad_char_count; + return ESP_OK; } void app_main(void) @@ -257,6 +317,22 @@ configure_wifi(); + esp_http_client_config_t config = { + .url = "https://mattermost.djartsgames.ca/plugins/com.mattermost.bathroom/settings", + .event_handler = _http_event_handler, + }; + esp_http_client_handle_t client = esp_http_client_init(&config); + esp_err_t err = esp_http_client_perform(client); + + if (err == ESP_OK) { + ESP_LOGI(TAG, "HTTPS Status = %d, content_length = %"PRId64, + esp_http_client_get_status_code(client), + esp_http_client_get_content_length(client)); + } else { + ESP_LOGE(TAG, "Error perform http request %s", esp_err_to_name(err)); + } + esp_http_client_cleanup(client); + /* Configure the peripheral according to the LED type */ configure_led(); diff --git a/bathroom-esp32/.vscode/settings.json b/bathroom-esp32/.vscode/settings.json index b7fa1c1..6713db9 100755 --- a/bathroom-esp32/.vscode/settings.json +++ b/bathroom-esp32/.vscode/settings.json @@ -12,8 +12,11 @@ "interface/ftdi/esp32_devkitj_v1.cfg", "target/esp32.cfg" ], - "idf.portWin": "COM7", + "idf.portWin": "COM8", "idf.pythonBinPathWin": "C:\\Projects\\Esp32\\espressif\\python_env\\idf5.3_py3.11_env\\Scripts\\python.exe", "idf.toolsPathWin": "C:\\Projects\\Esp32\\espressif", - "idf.flashType": "UART" + "idf.flashType": "UART", + "files.associations": { + "led_strip.h": "c" + } } diff --git a/bathroom-esp32/main/CMakeLists.txt b/bathroom-esp32/main/CMakeLists.txt index 92a6850..646f273 100755 --- a/bathroom-esp32/main/CMakeLists.txt +++ b/bathroom-esp32/main/CMakeLists.txt @@ -1,6 +1,3 @@ idf_component_register(SRCS "main.c" INCLUDE_DIRS "." - REQUIRES driver - REQUIRES esp_wifi - REQUIRES nvs_flash - REQUIRES mbedtls) \ No newline at end of file + REQUIRES driver esp_wifi nvs_flash esp_http_client esp-tls) \ No newline at end of file diff --git a/bathroom-esp32/main/main.c b/bathroom-esp32/main/main.c index 5751149..fa9483a 100755 --- a/bathroom-esp32/main/main.c +++ b/bathroom-esp32/main/main.c @@ -17,6 +17,7 @@ */ #include #include +#include #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "driver/gpio.h" @@ -28,13 +29,8 @@ #include "esp_event.h" #include "esp_err.h" #include "nvs_flash.h" - -#include "mbedtls/pk.h" -#include "mbedtls/rsa.h" -#include "mbedtls/error.h" -#include "mbedtls/base64.h" -#include "mbedtls/entropy.h" -#include "mbedtls/ctr_drbg.h" +#include "esp_http_client.h" +#include "esp_tls.h" static const char *TAG = "example"; @@ -177,8 +173,8 @@ wifi_config_t wifi_config = { .sta = { - .ssid = "DJArtsGames", - .password = "yubyubechubab", + .ssid = "Test", + .password = "password", /* Authmode threshold resets to WPA2 as default if password matches WPA2 standards (pasword len => 8). * If you want to connect the device to deprecated WEP/WPA networks, Please set the threshold value * to WIFI_AUTH_WEP/WIFI_AUTH_WPA_PSK and set the password with length and format matching to @@ -216,33 +212,97 @@ } } -// From: https://gist.github.com/VNovytskyi/e6d29b3d473a41141e27059a25e64100 -size_t get_decoded_size(const char *base64_str_p) +#define MAX_HTTP_OUTPUT_BUFFER 2048 + +esp_err_t _http_event_handler(esp_http_client_event_t *evt) { - if (base64_str_p == NULL) - { - return 0; + static char *output_buffer; // Buffer to store response of http request from event handler + static int output_len; // Stores number of bytes read + switch(evt->event_id) { + case HTTP_EVENT_ERROR: + ESP_LOGD(TAG, "HTTP_EVENT_ERROR"); + break; + case HTTP_EVENT_ON_CONNECTED: + ESP_LOGD(TAG, "HTTP_EVENT_ON_CONNECTED"); + break; + case HTTP_EVENT_HEADER_SENT: + ESP_LOGD(TAG, "HTTP_EVENT_HEADER_SENT"); + break; + case HTTP_EVENT_ON_HEADER: + ESP_LOGD(TAG, "HTTP_EVENT_ON_HEADER, key=%s, value=%s", evt->header_key, evt->header_value); + break; + case HTTP_EVENT_ON_DATA: + ESP_LOGD(TAG, "HTTP_EVENT_ON_DATA, len=%d", evt->data_len); + // Clean the buffer in case of a new request + if (output_len == 0 && evt->user_data) { + // we are just starting to copy the output data into the use + memset(evt->user_data, 0, MAX_HTTP_OUTPUT_BUFFER); + } + /* + * Check for chunked encoding is added as the URL for chunked encoding used in this example returns binary data. + * However, event handler can also be used in case chunked encoding is used. + */ + if (!esp_http_client_is_chunked_response(evt->client)) { + // If user_data buffer is configured, copy the response into the buffer + int copy_len = 0; + if (evt->user_data) { + // The last byte in evt->user_data is kept for the NULL character in case of out-of-bound access. + copy_len = MIN(evt->data_len, (MAX_HTTP_OUTPUT_BUFFER - output_len)); + if (copy_len) { + memcpy(evt->user_data + output_len, evt->data, copy_len); + } + } else { + int content_len = esp_http_client_get_content_length(evt->client); + if (output_buffer == NULL) { + // We initialize output_buffer with 0 because it is used by strlen() and similar functions therefore should be null terminated. + output_buffer = (char *) calloc(content_len + 1, sizeof(char)); + output_len = 0; + if (output_buffer == NULL) { + ESP_LOGE(TAG, "Failed to allocate memory for output buffer"); + return ESP_FAIL; + } + } + copy_len = MIN(evt->data_len, (content_len - output_len)); + if (copy_len) { + memcpy(output_buffer + output_len, evt->data, copy_len); + } + } + output_len += copy_len; + } + + break; + case HTTP_EVENT_ON_FINISH: + ESP_LOGD(TAG, "HTTP_EVENT_ON_FINISH"); + if (output_buffer != NULL) { + // Response is accumulated in output_buffer. Uncomment the below line to print the accumulated response + // ESP_LOG_BUFFER_HEX(TAG, output_buffer, output_len); + free(output_buffer); + output_buffer = NULL; + } + output_len = 0; + break; + case HTTP_EVENT_DISCONNECTED: + ESP_LOGI(TAG, "HTTP_EVENT_DISCONNECTED"); + int mbedtls_err = 0; + esp_err_t err = esp_tls_get_and_clear_last_error((esp_tls_error_handle_t)evt->data, &mbedtls_err, NULL); + if (err != 0) { + ESP_LOGI(TAG, "Last esp error code: 0x%x", err); + ESP_LOGI(TAG, "Last mbedtls failure: 0x%x", mbedtls_err); + } + if (output_buffer != NULL) { + free(output_buffer); + output_buffer = NULL; + } + output_len = 0; + break; + case HTTP_EVENT_REDIRECT: + ESP_LOGD(TAG, "HTTP_EVENT_REDIRECT"); + esp_http_client_set_header(evt->client, "From", "user@example.com"); + esp_http_client_set_header(evt->client, "Accept", "text/html"); + esp_http_client_set_redirection(evt->client); + break; } - - size_t pad_char_count = 0; - size_t base64_str_len = strlen(base64_str_p); - - if ((base64_str_len < 4) || (base64_str_len % 4 != 0)) - { - return 0; - } - - if (base64_str_p[base64_str_len - 1] == '=') - { - pad_char_count++; - } - - if (base64_str_p[base64_str_len - 2] == '=') - { - pad_char_count++; - } - - return (3 * (base64_str_len / 4)) - pad_char_count; + return ESP_OK; } void app_main(void) @@ -257,6 +317,22 @@ configure_wifi(); + esp_http_client_config_t config = { + .url = "https://mattermost.djartsgames.ca/plugins/com.mattermost.bathroom/settings", + .event_handler = _http_event_handler, + }; + esp_http_client_handle_t client = esp_http_client_init(&config); + esp_err_t err = esp_http_client_perform(client); + + if (err == ESP_OK) { + ESP_LOGI(TAG, "HTTPS Status = %d, content_length = %"PRId64, + esp_http_client_get_status_code(client), + esp_http_client_get_content_length(client)); + } else { + ESP_LOGE(TAG, "Error perform http request %s", esp_err_to_name(err)); + } + esp_http_client_cleanup(client); + /* Configure the peripheral according to the LED type */ configure_led(); diff --git a/bathroom-esp32/sdkconfig b/bathroom-esp32/sdkconfig index 4c220cf..383b711 100755 --- a/bathroom-esp32/sdkconfig +++ b/bathroom-esp32/sdkconfig @@ -539,7 +539,8 @@ # CONFIG_ESP_TLS_SERVER_CERT_SELECT_HOOK is not set # CONFIG_ESP_TLS_SERVER_MIN_AUTH_MODE_OPTIONAL is not set # CONFIG_ESP_TLS_PSK_VERIFICATION is not set -# CONFIG_ESP_TLS_INSECURE is not set +CONFIG_ESP_TLS_INSECURE=y +CONFIG_ESP_TLS_SKIP_SERVER_CERT_VERIFY=y # end of ESP-TLS #