From 84505eef0aaadfba568e40f38258660b046e9d7f Mon Sep 17 00:00:00 2001 From: Rene Zeldenthuis Date: Tue, 20 Sep 2022 11:33:46 +0200 Subject: [PATCH 1/2] Added OTA updating --- include/settings.h | 2 ++ src/main.cpp | 36 ++++++++++++++++++++++++++++++------ 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/include/settings.h b/include/settings.h index 73dac60..4f98553 100644 --- a/include/settings.h +++ b/include/settings.h @@ -7,6 +7,8 @@ #define WIFI_PASSWORD nullptr #define CONFIG_VERSION "1.1" +#define OTA_PASSWORD "ESP32CAM-RTSP" + #define RTSP_PORT 554 #define DEFAULT_CAMERA_CONFIG "AI THINKER" #define DEFAULT_FRAME_DURATION "20" diff --git a/src/main.cpp b/src/main.cpp index 540e3c7..c733649 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -133,20 +134,20 @@ void handle_snapshot() log_v("handle_jpg"); if (camera_init_result != ESP_OK) { - web_server.send(404, "text/plain", "Camera is not initialized"); - return; + web_server.send(404, "text/plain", "Camera is not initialized"); + return; } - + web_server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate"); web_server.sendContent("HTTP/1.1 200 OK\r\n" "Content-Disposition: inline; filename=snapshot.jpg\r\n" "Content-Type: image/jpeg\r\n\r\n"); - // Make a copy in memory to prevent interaction with RTSP + // Make a copy in memory to prevent interaction with RTSP auto fb_len = cam.getSize(); cam.run(); - auto fb = (uint8_t*)memcpy(new uint8_t[cam.getSize()], cam.getfb(), fb_len); + auto fb = (uint8_t *)memcpy(new uint8_t[cam.getSize()], cam.getfb(), fb_len); web_server.sendContent(reinterpret_cast(fb), fb_len); - delete []fb; + delete[] fb; } void on_config_saved() @@ -246,11 +247,34 @@ void setup() MDNS.begin(iotWebConf.getThingName()); // Add service to mDNS - http MDNS.addService("http", "tcp", 80); + + ArduinoOTA + .onStart([]() + { log_w("Starting OTA update: %s", ArduinoOTA.getCommand() == U_FLASH ? "sketch" : "filesystem"); }) + .onEnd([]() + { log_w("OTA update done!"); }) + .onProgress([](unsigned int progress, unsigned int total) + { log_i("OTA Progress: %u%%\r", (progress / (total / 100))); }) + .onError([](ota_error_t error) + { + switch (error) + { + case OTA_AUTH_ERROR: log_e("OTA: Auth Failed"); break; + case OTA_BEGIN_ERROR: log_e("OTA: Begin Failed"); break; + case OTA_CONNECT_ERROR: log_e("OTA: Connect Failed"); break; + case OTA_RECEIVE_ERROR: log_e("OTA: Receive Failed"); break; + case OTA_END_ERROR: log_e("OTA: End Failed"); break; + default: log_e("OTA error: %u", error); + } }); + // Start (OTA) Over The Air programming + ArduinoOTA.setPassword(OTA_PASSWORD); + ArduinoOTA.begin(); } void loop() { iotWebConf.doLoop(); + ArduinoOTA.handle(); if (camera_server) camera_server->doLoop(); From cc132876e78f8a8a6313e1065e2ef030465660b1 Mon Sep 17 00:00:00 2001 From: Rene Zeldenthuis Date: Tue, 20 Sep 2022 13:59:47 +0200 Subject: [PATCH 2/2] MDNS already done by iotConf. Start OTA when connected --- src/main.cpp | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index c733649..ade5581 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -243,11 +243,6 @@ void setup() web_server.onNotFound([]() { iotWebConf.handleNotFound(); }); - // Set DNS to thing name - MDNS.begin(iotWebConf.getThingName()); - // Add service to mDNS - http - MDNS.addService("http", "tcp", 80); - ArduinoOTA .onStart([]() { log_w("Starting OTA update: %s", ArduinoOTA.getCommand() == U_FLASH ? "sketch" : "filesystem"); }) @@ -266,9 +261,11 @@ void setup() case OTA_END_ERROR: log_e("OTA: End Failed"); break; default: log_e("OTA error: %u", error); } }); - // Start (OTA) Over The Air programming ArduinoOTA.setPassword(OTA_PASSWORD); - ArduinoOTA.begin(); + + // Start (OTA) Over The Air programming when connected + iotWebConf.setWifiConnectionCallback([]() + { ArduinoOTA.begin(); }); } void loop()