Merge pull request #9 from rzeldent/feature/ota

Added OTA updating
This commit is contained in:
rzeldent
2022-09-20 15:07:19 +02:00
committed by GitHub
2 changed files with 33 additions and 10 deletions

View File

@@ -7,6 +7,8 @@
#define WIFI_PASSWORD nullptr #define WIFI_PASSWORD nullptr
#define CONFIG_VERSION "1.1" #define CONFIG_VERSION "1.1"
#define OTA_PASSWORD "ESP32CAM-RTSP"
#define RTSP_PORT 554 #define RTSP_PORT 554
#define DEFAULT_CAMERA_CONFIG "AI THINKER" #define DEFAULT_CAMERA_CONFIG "AI THINKER"
#define DEFAULT_FRAME_DURATION "20" #define DEFAULT_FRAME_DURATION "20"

View File

@@ -1,4 +1,5 @@
#include <Arduino.h> #include <Arduino.h>
#include <ArduinoOTA.h>
#include <soc/rtc_cntl_reg.h> #include <soc/rtc_cntl_reg.h>
#include <IotWebConf.h> #include <IotWebConf.h>
#include <IotWebConfTParameter.h> #include <IotWebConfTParameter.h>
@@ -133,20 +134,20 @@ void handle_snapshot()
log_v("handle_jpg"); log_v("handle_jpg");
if (camera_init_result != ESP_OK) if (camera_init_result != ESP_OK)
{ {
web_server.send(404, "text/plain", "Camera is not initialized"); web_server.send(404, "text/plain", "Camera is not initialized");
return; return;
} }
web_server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate"); web_server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate");
web_server.sendContent("HTTP/1.1 200 OK\r\n" web_server.sendContent("HTTP/1.1 200 OK\r\n"
"Content-Disposition: inline; filename=snapshot.jpg\r\n" "Content-Disposition: inline; filename=snapshot.jpg\r\n"
"Content-Type: image/jpeg\r\n\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(); auto fb_len = cam.getSize();
cam.run(); 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<const char *>(fb), fb_len); web_server.sendContent(reinterpret_cast<const char *>(fb), fb_len);
delete []fb; delete[] fb;
} }
void on_config_saved() void on_config_saved()
@@ -242,15 +243,35 @@ void setup()
web_server.onNotFound([]() web_server.onNotFound([]()
{ iotWebConf.handleNotFound(); }); { iotWebConf.handleNotFound(); });
// Set DNS to thing name ArduinoOTA
MDNS.begin(iotWebConf.getThingName()); .onStart([]()
// Add service to mDNS - http { log_w("Starting OTA update: %s", ArduinoOTA.getCommand() == U_FLASH ? "sketch" : "filesystem"); })
MDNS.addService("http", "tcp", 80); .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);
} });
ArduinoOTA.setPassword(OTA_PASSWORD);
// Start (OTA) Over The Air programming when connected
iotWebConf.setWifiConnectionCallback([]()
{ ArduinoOTA.begin(); });
} }
void loop() void loop()
{ {
iotWebConf.doLoop(); iotWebConf.doLoop();
ArduinoOTA.handle();
if (camera_server) if (camera_server)
camera_server->doLoop(); camera_server->doLoop();