test html

This commit is contained in:
ok-home
2023-09-24 11:28:09 +07:00
parent 9dc7641c60
commit 5c6932902e
10 changed files with 4202 additions and 62 deletions

9
dependencies.lock Normal file
View File

@@ -0,0 +1,9 @@
dependencies:
idf:
component_hash: null
source:
type: idf
version: 5.2.0
manifest_hash: 2ac596d0d5a708817329b21531cbf3574c7c8daaf36dca979b5dd32a78fc2857
target: esp32s3
version: 1.0.0

View File

@@ -3,8 +3,18 @@
idf_component_register( idf_component_register(
SRCS SRCS
example_ota_ws.c example_ota_ws.c
../source/ota_ws_esp.c
../source/ota_ws_http.c
INCLUDE_DIRS INCLUDE_DIRS
"." ../include
#EMBED_FILES PRIV_INCLUDE_DIRS
ota_ws.html ../private_include
REQUIRES
prv_Wifi_Connect
esp_http_server
mdns
app_update
esp_wifi
EMBED_FILES
../source/ota_ws.html
) )

View File

@@ -14,44 +14,45 @@
#include "esp_ota_ops.h" #include "esp_ota_ops.h"
#include "esp_wifi.h" #include "esp_wifi.h"
//#include <esp_log.h> // #include <esp_log.h>
#include "prv_wifi_connect.h" #include "prv_wifi_connect.h"
#include "ota_ws.h"
static const char *TAG = "ota_ws"; static const char *TAG = "ota_ws";
#define MDNS #define MDNS
#ifdef MDNS #ifdef MDNS
#define HOST_NAME "esp-ota"
#include "mdns.h" #include "mdns.h"
#include "lwip/apps/netbiosns.h" #include "lwip/apps/netbiosns.h"
#endif //MDNS #endif // MDNS
#ifdef MDNS #ifdef MDNS
static void initialise_mdns(void) static void initialise_mdns(void)
{ {
mdns_init(); mdns_init();
mdns_hostname_set("esp"); mdns_hostname_set(HOST_NAME);
mdns_instance_name_set("esp home web server"); mdns_instance_name_set("esp home web server");
mdns_txt_item_t serviceTxtData[] = { mdns_txt_item_t serviceTxtData[] = {
{"board", "esp32"}, {"board", "esp32"},
{"path", "/"} {"path", "/"}};
};
ESP_ERROR_CHECK(mdns_service_add("ESP32-WebServer", "_http", "_tcp", 80, serviceTxtData, ESP_ERROR_CHECK(mdns_service_add("ESP32-WebServer", "_http", "_tcp", 80, serviceTxtData,
sizeof(serviceTxtData) / sizeof(serviceTxtData[0]))); sizeof(serviceTxtData) / sizeof(serviceTxtData[0])));
netbiosns_init();
netbiosns_set_name(HOST_NAME);
} }
#endif // MDNS #endif // MDNS
void app_main(void) void app_main(void)
{ {
prv_wifi_connect(); // return with error ? prv_wifi_connect(); // return with error ?
#ifdef MDNS #ifdef MDNS
initialise_mdns(); initialise_mdns();
netbiosns_init();
netbiosns_set_name("esp");
#endif // MDNS #endif // MDNS
prv_start_http_server(PRV_MODE_STAY_ACTIVE,NULL); // run server prv_start_http_server(PRV_MODE_STAY_ACTIVE, ota_ws_register_uri_handler); // run server
//example_echo_ws_server(); // example_echo_ws_server();
} }

6
partitions.csv Normal file
View File

@@ -0,0 +1,6 @@
# Name, Type, SubType, Offset, Size, Flags
nvs, data, nvs, 0x9000, 0x4000
otadata, data, ota, , 0x2000
phy_init, data, phy, , 0x1000
ota_0, app, ota_0, , 1M
ota_1, app, ota_1, , 1M
1 # Name, Type, SubType, Offset, Size, Flags
2 nvs, data, nvs, 0x9000, 0x4000
3 otadata, data, ota, , 0x2000
4 phy_init, data, phy, , 0x1000
5 ota_0, app, ota_0, , 1M
6 ota_1, app, ota_1, , 1M

View File

@@ -1,7 +1,19 @@
#pragma once #pragma once
#include "esp_event.h"
#include "freertos/event_groups.h"
#include <esp_log.h> #include <esp_log.h>
#include <esp_system.h> #include <esp_system.h>
#include "esp_http_server.h" #include "esp_http_server.h"
#define OTA_RESTART_ESP "otaRestartEsp"
#define OTA_SIZE_START "otaSize"
#define OTA_SET_CHUNK_SIZE "otaSetChunkSize"
#define OTA_GET_CHUNK "otaGetChunk"
#define OTA_END "otaEnd"
#define OTA_ERROR "otaError"
#define OTA_CANCEL "otaCancel"
#define OTA_CHUNK_SIZE 4096
esp_err_t start_ota_ws(void);
esp_err_t write_ota_ws(int data_read, uint8_t *ota_write_data);
esp_err_t end_ota_ws(void);

2030
sdkconfig Normal file

File diff suppressed because it is too large Load Diff

2030
sdkconfig.old Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -92,7 +92,7 @@
<script> <script>
let otaData; let otaData;
let otaSetchunksSize = 0; let otaSetChunkSize = 0;
let otaStartsegment = 0; let otaStartsegment = 0;
let otaStarted = 0; let otaStarted = 0;
@@ -121,19 +121,20 @@
document.getElementById("otaStartCancel").addEventListener("click", function (e) { document.getElementById("otaStartCancel").addEventListener("click", function (e) {
if (otaData.length > 0 && otaStarted == 0) { if (otaData.length > 0 && otaStarted == 0) {
//socket.send(JSON.stringify({ name: "otasize", msg: otaData.length })); socket.send(JSON.stringify({ name: "otaSize", value: otaData.length }));
console.log(JSON.stringify({ name: "otasize", msg: otaData.length })); //console.log(JSON.stringify({ name: "otaSize", value: otaData.length }));
otaStarted = 1; otaStarted = 1;
this.innerHTML = "Click to Cancel"; this.innerHTML = "Click to Cancel";
document.getElementById("otaFileSelect").disabled = true; document.getElementById("otaFileSelect").disabled = true;
document.getElementById("otaProgressVisible").style.display = "block"; document.getElementById("otaProgressVisible").style.display = "block";
document.getElementById("otaPogress").max = otaData.length; document.getElementById("otaPogress").max = otaData.length;
tstReceive(); //tstReceive();
} }
else { else {
otaStarted = 0; otaStarted = 0;
receiveWsData(JSON.stringify({ name: "otaCancel", msg: "Cancel" })); //receiveWsData(JSON.stringify({ name: "otaCancel", value: "Cancel" }));
socket.send(JSON.stringify({ name: "otaCancel", value: "Cancel" }));
} }
}); });
@@ -143,8 +144,8 @@
window.location.href = '/'; window.location.href = '/';
}); });
document.getElementById("otaReStart").addEventListener("click", function (e) { document.getElementById("otaReStart").addEventListener("click", function (e) {
//socket.send(JSON.stringify({ name: "otarestartesp", msg: "restart" })); socket.send(JSON.stringify({ name: "otaRestartEsp", value: "restart" }));
console.log(JSON.stringify({ name: "otarestartesp", msg: "restart" })); //console.log(JSON.stringify({ name: "otaRestartEsp", value: "restart" }));
}); });
// //
@@ -152,18 +153,18 @@
return new Promise(resolve => setTimeout(resolve, ms)); return new Promise(resolve => setTimeout(resolve, ms));
} }
async function tstReceive() { async function tstReceive() {
receiveWsData(JSON.stringify({ name: "otaSetchunksSize", msg: 1024 })); receiveWsData(JSON.stringify({ name: "otaSetChunkSize", value: 1024 }));
while (otaStartsegment + otaSetchunksSize <= otaData.length && otaStarted == 1) { while (otaStartsegment + otaSetChunkSize <= otaData.length && otaStarted == 1) {
await sleep(1000); await sleep(1000);
if(otaStarted == 1){ if(otaStarted == 1){
receiveWsData(JSON.stringify({ name: "otaGetChunk", msg: otaStartsegment })); receiveWsData(JSON.stringify({ name: "otaGetChunk", value: otaStartsegment }));
} }
otaStartsegment += otaSetchunksSize; otaStartsegment += otaSetChunkSize;
} }
//console.log(otaStartsegment + " " + otaSetchunksSize + " " + otaData.length + " " + (otaData.length - otaStartsegment)); //console.log(otaStartsegment + " " + otaSetChunkSize + " " + otaData.length + " " + (otaData.length - otaStartsegment));
if (otaStarted == 1) { if (otaStarted == 1) {
receiveWsData(JSON.stringify({ name: "otaGetChunk", msg: otaStartsegment })); receiveWsData(JSON.stringify({ name: "otaGetChunk", value: otaStartsegment }));
receiveWsData(JSON.stringify({ name: "otaEnd", msg: "OK" })); receiveWsData(JSON.stringify({ name: "otaEnd", value: "OK" }));
} }
} }
// //
@@ -172,15 +173,15 @@
let obj = JSON.parse(data); let obj = JSON.parse(data);
console.log(data); console.log(data);
switch (obj.name) { switch (obj.name) {
case "otaSetchunksSize": case "otaSetChunkSize":
otaSetchunksSize = obj.msg; otaSetChunkSize = obj.value;
break; break;
case "otaGetChunk": case "otaGetChunk":
let otaDataSend = otaData.subarray(obj.msg, obj.msg + otaSetchunksSize); let otaDataSend = otaData.subarray(obj.value, obj.value + otaSetChunkSize);
document.getElementById("otaPogress").value = obj.msg; document.getElementById("otaPogress").value = obj.value;
document.getElementById("otaStartCancel").innerHTML = "Ota Transfer. Size = " + otaData.length + " Segment = " + obj.msg + " Click to Cancel"; document.getElementById("otaStartCancel").innerHTML = "Ota Transfer. Size = " + otaData.length + " Segment = " + obj.value + " Click to Cancel";
console.log("sock send " + obj.msg + " " + otaDataSend.length); console.log("sock send " + obj.value + " " + otaDataSend.length);
//socket.send(otaDataSend); socket.send(otaDataSend);
break; break;
case "otaEnd": case "otaEnd":
otaStartsegment = 0; otaStartsegment = 0;
@@ -202,7 +203,7 @@
document.getElementById("otaPogress").value = otaData.length; document.getElementById("otaPogress").value = otaData.length;
document.getElementById("otaFileSelect").disabled = false; document.getElementById("otaFileSelect").disabled = false;
document.getElementById("otaReStartVisible").style.display = "block"; document.getElementById("otaReStartVisible").style.display = "block";
document.getElementById("otaReStart").innerHTML = "Transfer Cancel " + obj.msg; document.getElementById("otaReStart").innerHTML = "Transfer Cancel " + obj.value;
document.getElementById("otaReStart").disabled = true; document.getElementById("otaReStart").disabled = true;
} }
} }

View File

@@ -2,8 +2,11 @@
#include "esp_ota_ops.h" #include "esp_ota_ops.h"
#include "esp_flash_partitions.h" #include "esp_flash_partitions.h"
#include "esp_partition.h" #include "esp_partition.h"
#include "esp_image_format.h"
static const char *TAG = "ota_esp"; #include "ota_ws_private.h"
static const char *TAG = "ota_ws_esp";
/*an ota data write buffer ready to write to the flash*/ /*an ota data write buffer ready to write to the flash*/
//static char ota_write_data[BUFFSIZE + 1] = {0}; //static char ota_write_data[BUFFSIZE + 1] = {0};
static const esp_partition_t *update_partition = NULL; static const esp_partition_t *update_partition = NULL;
@@ -11,9 +14,9 @@ static bool image_header_was_checked = false;
static int binary_file_length = 0; static int binary_file_length = 0;
static esp_ota_handle_t update_handle = 0; static esp_ota_handle_t update_handle = 0;
esp_err_t start_ota(void) esp_err_t start_ota_ws(void)
{ {
// return ESP_OK; // debug return return ESP_OK; // debug return
esp_err_t err; esp_err_t err;
ESP_LOGI(TAG, "Starting OTA"); ESP_LOGI(TAG, "Starting OTA");
@@ -23,22 +26,22 @@ esp_err_t start_ota(void)
if (configured != running) if (configured != running)
{ {
ESP_LOGW(TAG, "Configured OTA boot partition at offset 0x%08x, but running from offset 0x%08x", ESP_LOGW(TAG, "Configured OTA boot partition at offset 0x%08lx, but running from offset 0x%08lx",
configured->address, running->address); configured->address, running->address);
ESP_LOGW(TAG, "(This can happen if either the OTA boot data or preferred boot image become corrupted somehow.)"); ESP_LOGW(TAG, "(This can happen if either the OTA boot data or preferred boot image become corrupted somehow.)");
} }
ESP_LOGI(TAG, "Running partition type %d subtype %d (offset 0x%08x)", ESP_LOGI(TAG, "Running partition type %d subtype %d (offset 0x%08lx)",
running->type, running->subtype, running->address); running->type, running->subtype, running->address);
update_partition = esp_ota_get_next_update_partition(NULL); update_partition = esp_ota_get_next_update_partition(NULL);
ESP_LOGI(TAG, "Writing to partition subtype %d at offset 0x%x", ESP_LOGI(TAG, "Writing to partition subtype %d at offset 0x%lx",
update_partition->subtype, update_partition->address); update_partition->subtype, update_partition->address);
err = esp_ota_begin(update_partition, OTA_SIZE_UNKNOWN, &update_handle); err = esp_ota_begin(update_partition, OTA_SIZE_UNKNOWN, &update_handle);
if (err != ESP_OK) if (err != ESP_OK)
{ {
ESP_LOGI(TAG, "esp_ota_begin failed "); ESP_LOGE(TAG, "esp_ota_begin failed ");
return -1; return ESP_FAIL;
} }
ESP_LOGI(TAG, "esp_ota_begin succeeded"); ESP_LOGI(TAG, "esp_ota_begin succeeded");
@@ -46,9 +49,10 @@ esp_err_t start_ota(void)
return ESP_OK; return ESP_OK;
} }
esp_err_t write_ota(int data_read, uint8_t *ota_write_data) esp_err_t write_ota_ws(int data_read, uint8_t *ota_write_data)
{ {
// return data_read; // debug return return ESP_OK; // debug return
if (image_header_was_checked == false) // first segment if (image_header_was_checked == false) // first segment
{ {
@@ -64,33 +68,35 @@ esp_err_t write_ota(int data_read, uint8_t *ota_write_data)
else else
{ {
ESP_LOGE(TAG, "received package is not fit len"); ESP_LOGE(TAG, "received package is not fit len");
return -1; return ESP_FAIL;
} }
} }
esp_err_t err = esp_ota_write(update_handle, (const void *)ota_write_data, data_read); esp_err_t err = esp_ota_write(update_handle, (const void *)ota_write_data, data_read);
if (err != ESP_OK) if (err != ESP_OK)
{ {
return -1; return ESP_FAIL;
} }
binary_file_length += data_read; binary_file_length += data_read;
ESP_LOGD(TAG, "Written image length %d", binary_file_length); ESP_LOGD(TAG, "Written image length %d", binary_file_length);
return ESP_OK; return ESP_OK;
} }
esp_err_t end_ota(void) esp_err_t end_ota_ws(void)
{ {
return ESP_OK; // debug return
esp_err_t err = esp_ota_end(update_handle); esp_err_t err = esp_ota_end(update_handle);
if (err != ESP_OK) { if (err != ESP_OK) {
if (err == ESP_ERR_OTA_VALIDATE_FAILED) { if (err == ESP_ERR_OTA_VALIDATE_FAILED) {
ESP_LOGE(TAG, "Image validation failed, image is corrupted"); ESP_LOGE(TAG, "Image validation failed, image is corrupted");
} }
ESP_LOGE(TAG, "esp_ota_end failed (%s)!", esp_err_to_name(err)); ESP_LOGE(TAG, "esp_ota_end failed (%s)!", esp_err_to_name(err));
return -1; return ESP_FAIL;
} }
err = esp_ota_set_boot_partition(update_partition); err = esp_ota_set_boot_partition(update_partition);
if (err != ESP_OK) { if (err != ESP_OK) {
ESP_LOGE(TAG, "esp_ota_set_boot_partition failed (%s)!", esp_err_to_name(err)); ESP_LOGE(TAG, "esp_ota_set_boot_partition failed (%s)!", esp_err_to_name(err));
return -1; return ESP_FAIL;
} }
return ESP_OK; return ESP_OK;
} }

View File

@@ -37,24 +37,29 @@ static esp_err_t json_to_str_parm(char *jsonstr, char *nameStr, char *valStr) //
valStr[0] = 0; valStr[0] = 0;
return ESP_OK; return ESP_OK;
} }
static void send_json_string(char *str, httpd_req_t *req) static esp_err_t send_json_string(char *str, httpd_req_t *req)
{ {
httpd_ws_frame_t ws_pkt; httpd_ws_frame_t ws_pkt;
memset(&ws_pkt, 0, sizeof(httpd_ws_frame_t)); memset(&ws_pkt, 0, sizeof(httpd_ws_frame_t));
ws_pkt.type = HTTPD_WS_TYPE_TEXT; ws_pkt.type = HTTPD_WS_TYPE_TEXT;
ws_pkt.payload = (uint8_t *)str; ws_pkt.payload = (uint8_t *)str;
ws_pkt.len = strlen(str); ws_pkt.len = strlen(str);
httpd_ws_send_frame(req, &ws_pkt); return httpd_ws_send_frame(req, &ws_pkt);
} }
// write wifi data from ws to nvs
static esp_err_t ota_ws_handler(httpd_req_t *req) static esp_err_t ota_ws_handler(httpd_req_t *req)
{ {
static int ota_size;
static int ota_start_chunk;
if (req->method == HTTP_GET) if (req->method == HTTP_GET)
{ {
ESP_LOGI(TAG, "Handshake done, the new connection was opened"); ESP_LOGI(TAG, "Handshake done, the new connection was opened");
send_nvs_data(req); // read & send initial wifi data from nvs
return ESP_OK; return ESP_OK;
} }
char json_key[64]={0};
char json_value[64]={0};
char json_str[64] = {0};
httpd_ws_frame_t ws_pkt; httpd_ws_frame_t ws_pkt;
uint8_t *buf = NULL; uint8_t *buf = NULL;
memset(&ws_pkt, 0, sizeof(httpd_ws_frame_t)); memset(&ws_pkt, 0, sizeof(httpd_ws_frame_t));
@@ -87,11 +92,41 @@ static esp_err_t ota_ws_handler(httpd_req_t *req)
ret = ESP_OK; ret = ESP_OK;
if (ws_pkt.type == HTTPD_WS_TYPE_TEXT) if (ws_pkt.type == HTTPD_WS_TYPE_TEXT)
{ {
// json data ESP_LOGI(TAG, "Rcv txt msg len=%d data=%s", ws_pkt.len, buf);
if (json_to_str_parm((char*)buf,json_key,json_value))
{
ESP_LOGE(TAG,"error json str: %s",buf);
goto _recv_ret;
}
if(strcmp(json_key,OTA_SIZE_START)==0)// start ota
{
ota_size = atoi(json_value);
ota_start_chunk = 0;
snprintf(json_str, sizeof(json_str), "{\"name\":\"%s\",\"value\":%d}", OTA_SET_CHUNK_SIZE, OTA_CHUNK_SIZE);
send_json_string(json_str,req);
snprintf(json_str, sizeof(json_str), "{\"name\":\"%s\",\"value\":%d}", OTA_GET_CHUNK, ota_start_chunk);
send_json_string(json_str,req);
}
} }
else if (ws_pkt.type == HTTPD_WS_TYPE_BIN) else if (ws_pkt.type == HTTPD_WS_TYPE_BINARY)
{ {
// ota data ESP_LOGI(TAG, "Rcv bin msg len=%d", ws_pkt.len);
ota_start_chunk += ws_pkt.len;
if(ota_start_chunk < ota_size)
{
snprintf(json_str, sizeof(json_str), "{\"name\":\"%s\",\"value\": %d }", OTA_GET_CHUNK, ota_start_chunk);
send_json_string(json_str,req);
}
else{
// end ota
ota_size = 0;
ota_start_chunk = 0;
snprintf(json_str, sizeof(json_str), "{\"name\":\"%s\",\"value\":\"%s\" }", OTA_END, "OK");
send_json_string(json_str,req);
}
} }
else else
{ {