diff --git a/example_ota_ws/main/CMakeLists.txt b/example_ota_ws/main/CMakeLists.txt index 3b5187e..f1bbcd1 100644 --- a/example_ota_ws/main/CMakeLists.txt +++ b/example_ota_ws/main/CMakeLists.txt @@ -3,10 +3,13 @@ idf_component_register( SRCS example_ota_ws_update.c + example_echo_ws_server.c REQUIRES ota_ws_update - prv_Wifi_Connect + nvs_wifi_connect esp_http_server mdns esp_wifi + EMBED_FILES + example_echo_ws_server.html ) \ No newline at end of file diff --git a/example_ota_ws/main/example_echo_ws_server.c b/example_ota_ws/main/example_echo_ws_server.c new file mode 100644 index 0000000..89f433a --- /dev/null +++ b/example_ota_ws/main/example_echo_ws_server.c @@ -0,0 +1,176 @@ +/* WebSocket Echo Server Example + + This example code is in the Public Domain (or CC0 licensed, at your option.) + + Unless required by applicable law or agreed to in writing, this + software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + CONDITIONS OF ANY KIND, either express or implied. +*/ + +#include +#include +#include +#include +#include +#include "esp_netif.h" + +#include + +#include "nvs_wifi_connect.h" +#include "ota_ws_update.h" + +/* A simple example that demonstrates using websocket echo server + */ +static const char *TAG = "example_ws_echo_server"; + + +/* + * This handler echos back the received ws data + * and triggers an async send if certain message received + */ +static esp_err_t echo_handler(httpd_req_t *req) +{ + if (req->method == HTTP_GET) { + ESP_LOGI(TAG, "Handshake done, the new connection was opened"); + return ESP_OK; + } + httpd_ws_frame_t ws_pkt; + uint8_t *buf = NULL; + memset(&ws_pkt, 0, sizeof(httpd_ws_frame_t)); + ws_pkt.type = HTTPD_WS_TYPE_TEXT; + /* Set max_len = 0 to get the frame len */ + esp_err_t ret = httpd_ws_recv_frame(req, &ws_pkt, 0); + if (ret != ESP_OK) { + ESP_LOGE(TAG, "httpd_ws_recv_frame failed to get frame len with %d", ret); + return ret; + } + //ESP_LOGI(TAG, "frame len is %d", ws_pkt.len); + if (ws_pkt.len) { + /* ws_pkt.len + 1 is for NULL termination as we are expecting a string */ + buf = calloc(1, ws_pkt.len + 1); + if (buf == NULL) { + ESP_LOGE(TAG, "Failed to calloc memory for buf"); + return ESP_ERR_NO_MEM; + } + ws_pkt.payload = buf; + /* Set max_len = ws_pkt.len to get the frame payload */ + ret = httpd_ws_recv_frame(req, &ws_pkt, ws_pkt.len); + if (ret != ESP_OK) { + ESP_LOGE(TAG, "httpd_ws_recv_frame failed with %d", ret); + free(buf); + return ret; + } + //ESP_LOGI(TAG, "Got packet with message: %s", ws_pkt.payload); + } + //ESP_LOGI(TAG, "Packet type: %d", ws_pkt.type); + + ret = httpd_ws_send_frame(req, &ws_pkt); + if (ret != ESP_OK) { + ESP_LOGE(TAG, "httpd_ws_send_frame failed with %d", ret); + } + free(buf); + return ret; +} + +static const httpd_uri_t example_ws = { + .uri = "/ws", + .method = HTTP_GET, + .handler = echo_handler, + .user_ctx = NULL, + .is_websocket = true +}; + +static esp_err_t get_handler(httpd_req_t *req) +{ + extern const unsigned char example_echo_ws_server_html_start[] asm("_binary_example_echo_ws_server_html_start"); + extern const unsigned char example_echo_ws_server_html_end[] asm("_binary_example_echo_ws_server_html_end"); + const size_t example_echo_ws_server_html_size = (example_echo_ws_server_html_end - example_echo_ws_server_html_start); + + httpd_resp_send_chunk(req, (const char *)example_echo_ws_server_html_start, example_echo_ws_server_html_size); + httpd_resp_sendstr_chunk(req, NULL); + return ESP_OK; +} +static const httpd_uri_t example_gh = { + .uri = "/", + .method = HTTP_GET, + .handler = get_handler, + .user_ctx = NULL}; + +esp_err_t example_register_uri_handler(httpd_handle_t server); + +static httpd_handle_t start_webserver(void) +{ + httpd_handle_t server = NULL; + httpd_config_t config = HTTPD_DEFAULT_CONFIG(); + + // Start the httpd server + ESP_LOGI(TAG, "Starting server on port: '%d'", config.server_port); + if (httpd_start(&server, &config) == ESP_OK) { + // Registering the ws handler + ESP_LOGI(TAG, "Registering URI handlers"); + example_register_uri_handler(server); + nvs_wifi_connect_register_uri_handler(server); + ota_ws_register_uri_handler(server); + return server; + } + + ESP_LOGI(TAG, "Error starting server!"); + return NULL; +} + +static esp_err_t stop_webserver(httpd_handle_t server) +{ + // Stop the httpd server + return httpd_stop(server); +} + +static void disconnect_handler(void* arg, esp_event_base_t event_base, + int32_t event_id, void* event_data) +{ + httpd_handle_t* server = (httpd_handle_t*) arg; + if (*server) { + ESP_LOGI(TAG, "Stopping webserver"); + if (stop_webserver(*server) == ESP_OK) { + *server = NULL; + } else { + ESP_LOGE(TAG, "Failed to stop http server"); + } + } +} + +static void connect_handler(void* arg, esp_event_base_t event_base, + int32_t event_id, void* event_data) +{ + httpd_handle_t* server = (httpd_handle_t*) arg; + if (*server == NULL) { + ESP_LOGI(TAG, "Starting webserver"); + *server = start_webserver(); + } +} + + +void example_echo_ws_server(void) +{ + static httpd_handle_t server = NULL; + + ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &connect_handler, &server)); + ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &disconnect_handler, &server)); + ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_AP_STACONNECTED, &connect_handler, &server)); + ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_AP_STADISCONNECTED, &disconnect_handler, &server)); + + /* Start the server for the first time */ + server = start_webserver(); +} + +esp_err_t example_register_uri_handler(httpd_handle_t server) +{ + esp_err_t ret = ESP_OK; + ret = httpd_register_uri_handler(server, &example_gh); + if (ret) + goto _ret; + ret = httpd_register_uri_handler(server, &example_ws); + if (ret) + goto _ret; +_ret: + return ret; +} diff --git a/example_ota_ws/main/example_echo_ws_server.html b/example_ota_ws/main/example_echo_ws_server.html new file mode 100644 index 0000000..bbe8257 --- /dev/null +++ b/example_ota_ws/main/example_echo_ws_server.html @@ -0,0 +1,131 @@ + + + + + + Example Websocket Echo Server + + + + + + + +
Example Websocket Echo Server
+
+ +
+
+ +
+ +
+ + +
+ +
+ + +
+ + + + + + + + + + + + \ No newline at end of file diff --git a/example_ota_ws/main/example_ota_ws_update.c b/example_ota_ws/main/example_ota_ws_update.c index d95d1ed..772f860 100644 --- a/example_ota_ws/main/example_ota_ws_update.c +++ b/example_ota_ws/main/example_ota_ws_update.c @@ -13,12 +13,16 @@ #include "esp_log.h" #include "esp_ota_ops.h" #include "esp_wifi.h" +#include "esp_http_server.h" // #include -#include "prv_wifi_connect.h" +#include "nvs_wifi_connect.h" #include "ota_ws_update.h" +extern esp_err_t example_register_uri_handler(httpd_handle_t server); +void example_echo_ws_server(void); + //static const char *TAG = "ota_ws"; #define MDNS @@ -45,13 +49,16 @@ static void initialise_mdns(void) } #endif // MDNS + + void app_main(void) { - prv_wifi_connect(); // return with error ? + nvs_wifi_connect(); // return with error ? #ifdef MDNS initialise_mdns(); #endif // MDNS - prv_start_http_server(PRV_MODE_STAY_ACTIVE, ota_ws_register_uri_handler); // run server + example_echo_ws_server(); + //prv_start_http_server(PRV_MODE_STAY_ACTIVE, ota_ws_register_uri_handler); // run server } diff --git a/source/ota_ws_update.html b/source/ota_ws_update.html index 1d00c7c..e1cf70f 100644 --- a/source/ota_ws_update.html +++ b/source/ota_ws_update.html @@ -247,11 +247,11 @@ console.log("connect ws"); }; socket.onclose = function (event) { - document.location.reload(); + //document.location.reload(); console.log("close ws"); }; socket.onerror = function () { - document.location.reload(); + //document.location.reload(); console.log("error ws"); }; socket.onmessage = function (event) {