mirror of
https://github.com/ok-home/ota_ws_update.git
synced 2025-11-13 22:03:27 +03:00
update example
This commit is contained in:
@@ -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
|
||||
)
|
||||
176
example_ota_ws/main/example_echo_ws_server.c
Normal file
176
example_ota_ws/main/example_echo_ws_server.c
Normal file
@@ -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 <esp_wifi.h>
|
||||
#include <esp_event.h>
|
||||
#include <esp_log.h>
|
||||
#include <esp_system.h>
|
||||
#include <sys/param.h>
|
||||
#include "esp_netif.h"
|
||||
|
||||
#include <esp_http_server.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
131
example_ota_ws/main/example_echo_ws_server.html
Normal file
131
example_ota_ws/main/example_echo_ws_server.html
Normal file
@@ -0,0 +1,131 @@
|
||||
<!DOCTYPE html>
|
||||
|
||||
<html lang="ru">
|
||||
|
||||
<head>
|
||||
<title>Example Websocket Echo Server</title>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
|
||||
<style>
|
||||
.column {
|
||||
float: left;
|
||||
width: 100%;
|
||||
margin-top: 2px;
|
||||
margin-bottom: 2px;
|
||||
}
|
||||
|
||||
.btn {
|
||||
float: left;
|
||||
width: 100%;
|
||||
margin: 2px;
|
||||
|
||||
}
|
||||
|
||||
.cl1 {
|
||||
float: left;
|
||||
width: 100%;
|
||||
margin: 2px;
|
||||
margin-top: 2px;
|
||||
margin-bottom: 2px;
|
||||
}
|
||||
|
||||
.cl01 {
|
||||
float: left;
|
||||
width: 100%;
|
||||
text-align: center;
|
||||
margin-top: 2px;
|
||||
margin-bottom: 2px;
|
||||
}
|
||||
|
||||
.cl02 {
|
||||
float: left;
|
||||
width: 100%;
|
||||
text-align: center;
|
||||
margin-top: 2px;
|
||||
margin-bottom: 2px;
|
||||
}
|
||||
|
||||
.hdr {
|
||||
float: left;
|
||||
width: 100%;
|
||||
text-align: center;
|
||||
color: white;
|
||||
background-color: blue;
|
||||
padding: 5px;
|
||||
margin: 5px;
|
||||
}
|
||||
|
||||
.logstr {
|
||||
width: 100%;
|
||||
float: left;
|
||||
}
|
||||
</style>
|
||||
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div class="hdr">Example Websocket Echo Server </div>
|
||||
<div class="column">
|
||||
<button class="btn" id="goWifi" onclick="window.location.href = '/wifi'" >Set WiFi SSID/Password -> /wifi</button>
|
||||
</div>
|
||||
<div class="column">
|
||||
<button class="btn" id="goOta" onclick="window.location.href = '/ota'" >Ota update -> /ota</button>
|
||||
</div>
|
||||
|
||||
<div class="cl1">
|
||||
<label class="cl01" for="exampleSend">Input string for Echo</label>
|
||||
<input class="cl02" type="text" id="exampleSend" placeholder="input">
|
||||
</div>
|
||||
|
||||
<div class="cl1">
|
||||
<label class="cl01" for="exampleEcho">Echo string</label>
|
||||
<input class="cl02" type="text" id="exampleEcho" placeholder="output">
|
||||
</div>
|
||||
|
||||
<script>
|
||||
let exSend = document.getElementById("exampleSend");
|
||||
exSend.addEventListener("input", function (e) {
|
||||
//console.log(exSend.id + " " + exSend.value );
|
||||
socket.send(JSON.stringify({ name: exSend.id, msg: exSend.value }));
|
||||
});
|
||||
function receiveWsData(data) {
|
||||
//console.log(data);
|
||||
try {
|
||||
let obj = JSON.parse(data);
|
||||
let exEcho = document.getElementById("exampleEcho");
|
||||
exEcho.value = obj.msg;
|
||||
}
|
||||
catch {
|
||||
console.log(data + " catch");
|
||||
}
|
||||
};
|
||||
</script>
|
||||
|
||||
<script> // Прием, обработка и отправка данных в WS
|
||||
</script>
|
||||
|
||||
<script> // основной старт скрипта, открыть сокет
|
||||
// создать сокет по адресу
|
||||
let wsHostStr = "ws://" + document.location.host + document.location.pathname;
|
||||
wsHostStr += (document.location.pathname == '/') ? "ws" : "/ws";
|
||||
var socket = new WebSocket(wsHostStr);
|
||||
</script>
|
||||
|
||||
<script> // события WS
|
||||
socket.onopen = function () {
|
||||
console.log("connect");
|
||||
};
|
||||
socket.onclose = function (event) {
|
||||
console.log("close");
|
||||
};
|
||||
socket.onerror = function () {
|
||||
console.log("error");
|
||||
};
|
||||
socket.onmessage = function (event) {
|
||||
receiveWsData(event.data);
|
||||
};
|
||||
</script>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
||||
@@ -13,12 +13,16 @@
|
||||
#include "esp_log.h"
|
||||
#include "esp_ota_ops.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "esp_http_server.h"
|
||||
|
||||
// #include <esp_log.h>
|
||||
|
||||
#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
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user