From 2e98e1fffe33e54ad09c46d5748e141745e3d610 Mon Sep 17 00:00:00 2001 From: abratchik Date: Fri, 2 Dec 2022 11:29:56 +0400 Subject: [PATCH] version 5.0 beta 2 --- .gitignore | 1 + API.md | 44 ++++---- CONTRIBUTING.md | 31 +++--- README.md | 224 ++++++++++++++++++++++++++++------------ data/default_cam.json | 5 +- data/www/dump.html | 15 ++- esp32-cam-webserver.ino | 1 + src/app_config.h | 9 +- src/storage.cpp | 27 ++--- src/storage.h | 19 ++-- 10 files changed, 240 insertions(+), 136 deletions(-) diff --git a/.gitignore b/.gitignore index f8f210b..22006d5 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ .pio .vscode .DS_Store +.idea *.code-workspace /data/conn.json /data/cam.json diff --git a/API.md b/API.md index 390f311..6fa1db7 100644 --- a/API.md +++ b/API.md @@ -1,27 +1,24 @@ # Basic HTTP Commands; -The WebUI and camera server communicate entirely via HTTP requests and responses; this makes controlling all functions of the camera via GET requests possible. +The WebUI and camera server communicate entirely via HTTP requests and responses; +this makes controlling all functions of the camera via GET requests possible. ## URI's -### Http Port -* `/` - Default index (portal) +### Web UI pages +* `/` or `/portal` - Default index (portal) * `/view?mode=full|simple|stream|still` - Go direct to specific page: * - full: taking still and video with complete set of camera controls * - simple: taking still and video with limited set of camera controls * - stream: starting video capture with full screen mode * - still: taking a still image with full screen mode -* `/status` - Returns a JSON string with all camera status / pairs listed -* `/control?var=&val=` - Set `` to `` -* `/dump` - Status page -* `/stop` - End all active streams +* `/dump` - Status page (automatically refreshed every 5 sec) +### Special *key / val* settings and commands -## *key / val* settings and commands +* `/control?var=&val=` - Set a Control Variable specified by `` to `` +* `/status` - JSON response containing camera settings +* `/system` - JSON response containing all parameters displayed on the `/dump` page -Call the `/status` URI to recieve a JSON response containing all the available settings and current value. - -Call `/control?var=&val=` with a settings key and value to set camera properties or trigger actions. - -#### Settings +#### Supported Control Variables: ``` lamp - Lamp value in percent; integer, 0 - 100 (-1 = disabled) framesize - See below @@ -56,13 +53,7 @@ colorbar - Overlays a color test pattern on the stream; integer, 1 = enab face_detect - Face Detection; 1 = enabled, Only settable if framesize <= 4 (CIF) face_recognize - Face recognition; 1 = enabled, only settable if Face detection is already enabled ``` -#### Read Only -These values are returned in the `/status` JSON response, but cannot be set via the `/control` URI. -``` -cam_name - Camera Name; String -code_ver - Code compile date and time; String -stream_url - Raw stream URL; string -``` + ##### Framesize values These may vary between different ESP framework releases ``` @@ -82,13 +73,16 @@ Only for 3Mp+ camera modules: 14 - FHD (1920x1080) 17 - QXGA (2048x1536) ``` + #### Commands -These are commands; they can be sent by calling the `/control` URI with them as the `` *(a `` must also be supplied, but can be any value and is ignored)*. +These are commands; they can be sent by calling the `/control` URI with them as +the `` *(a `` must also be supplied, but can be any value and is ignored)*. ``` save_prefs - Saves preferences file clear_prefs - Deletes the preferences file -reboot - Reboots the camera +reboot - Reboots the board ``` + ## Examples * Flash light: on/mid/off * `http:///control?var=lamp&val=100` @@ -97,11 +91,13 @@ reboot - Reboots the camera * Set resolution to VGA * `http:///control?var=framesize&val=8` * Show camera details and settings - * All settings are returned via single `status` call in [JSON](https://www.json.org/) format. + * All settings are returned via single `status` call in [JSON](https://www.json.org/) + format. * `http:///status` * Returns: ``` {"lamp":0,"autolamp":0,"frame_rate":0,"framesize":9,"quality":10,"xclk":8,"brightness":0,"contrast":0,"saturation":0,"sharpness":0,"special_effect":0,"wb_mode":0,"awb":1,"awb_gain":1,"aec":1,"aec2":0,"ae_level":0,"aec_value":204,"agc":1,"agc_gain":0,"gainceiling":0,"bpc":0,"wpc":1,"raw_gma":1,"lenc":1,"vflip":1,"hmirror":1,"dcw":1,"colorbar":0,"cam_name":"ESP32 test camera","code_ver":"Mar 10 2022 @ 14:00:45","rotate":"0","stream_url":"ws:///ws"}``` * Reboot the camera * `http:///control?var=reboot&val=0` -You can try these yourself in a browser address bar, from the commandline with `curl` and co. or use them programatically from your scripting language of choice. +You can try these yourself in a browser address bar, from the commandline with `curl` +and co. or use them programatically from your scripting language of choice. diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 4a4b8bf..18961d0 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,5 +1,6 @@ # Contributing to ESP32-CAM revisited -I love your input! and want to make contributing to this project as easy and transparent as possible, whether it's: +We love your input and want to make contributing to this project as easy and transparent +as possible, whether it's: - Reporting a bug - Discussing the current state of the code @@ -7,11 +8,14 @@ I love your input! and want to make contributing to this project as easy and tra - Proposing new features - Becoming a maintainer -## I Develop with Github -I use github to host code, to track issues and feature requests, as well as accept pull requests. +## We Develop with Github +We use github to host code, to track issues and feature requests, as well as accept pull +requests. -## I Use [Github Flow](https://guides.github.com/introduction/flow/index.html), So All Code Changes Happen Through Pull Requests -Pull requests are the best way to propose changes to the codebase (I use [Github Flow](https://guides.github.com/introduction/flow/index.html)). I actively welcome your pull requests: +## We Use [Github Flow](https://guides.github.com/introduction/flow/index.html) +So All Code Changes Happen Through Pull Requests. Pull requests are the best way to +propose changes to the codebase (use [Github Flow](https://guides.github.com/introduction/flow/index.html)). +We actively welcome your pull requests: 1. Fork the repo and create your branch from `master`. 2. Give your branch a clear descriptive name and do your changes there. @@ -20,10 +24,12 @@ Pull requests are the best way to propose changes to the codebase (I use [Github 5. Clearly describe your changes and the reason for them in the pull request. ## Any contributions you make will be under the GNU Lesser General Public License v2.1 -In short, when you submit code changes, your submissions are understood to be under the same [License](./LICENSE) that covers the project. +In short, when you submit code changes, your submissions are understood to be under +the same [License](./LICENSE) that covers the project. -## Report bugs using Github's [issues](https://github.com/abratchik/esp32-cam-webserver/issues) -We use GitHub issues to track public bugs. Report a bug by opening a new issue; it's that easy! +## Report bugs using Github's issues +We use GitHub issues to track public bugs. Report a bug by opening a new issue; it's that +easy! ## Write bug reports with detail, background, and sample code @@ -34,15 +40,16 @@ We use GitHub issues to track public bugs. Report a bug by opening a new issue; - Be specific! - What you expected would happen - What actually happens -- Notes (possibly including why you think this might be happening, or stuff you tried that didn't work) - -People *love* thorough bug reports. I'm not even kidding. +- Notes (possibly including why you think this might be happening, or stuff you + tried that didn't work) + ## Use a Consistent Coding Style * 4 spaces for indentation rather than tabs in the main code ## License -By contributing, you agree that your contributions will be licensed under its GNU Lesser General Public License v2.1 +By contributing, you agree that your contributions will be licensed under its GNU +Lesser General Public License v2.1 diff --git a/README.md b/README.md index 6b34f8a..7b2f21f 100644 --- a/README.md +++ b/README.md @@ -1,65 +1,71 @@ -# ESP32-CAM example revisited.     [![CI Status](https://travis-ci.com/easytarget/esp32-cam-webserver.svg?branch=master)](https://travis-ci.com/github/easytarget/esp32-cam-webserver)    ![ESP-EYE logo](data/www/logo.svg) +# ESP32-CAM Example Revisited2        ![ESP-EYE logo](data/www/logo.svg) ## Taken from the ESP examples, and expanded -This sketch is a extension/expansion/rework of the 'official' ESP32 Camera example sketch from Espressif and its modification by -@easytarget: +This sketch is a extension/expansion/rework of the 'official' ESP32 Camera example +sketch from Espressif: * Original [CameraWebServer](https://github.com/espressif/arduino-esp32/tree/master/libraries/ESP32/examples/Camera/CameraWebServer) -* Expanded [esp32-cam-webserver](https://github.com/easytarget/esp32-cam-webserver) ### Key features: ### * Extended options for default network and camera settings -* Save and restore settings +* Save and restore settings in JSON configuration files * Dedicated standalone stream viewer * Over The Air firmware updates * Optimizing the way how the video stream is processed, thus allowing higher frame rates on high resolution. * Using just one IP port instead of two. * Porting the web server to [ESP Async Web Server](https://github.com/me-no-dev/ESPAsyncWebServer). * Storing web pages as separate HTML/CSS/JS files on the SD drive, which greatly simplifies development of the interface. Basically, you can swap the face of this project just by replacing files on SD card. +* Reduced size of the sketch and improving memory utilization +* Porting the code from basic C to C++ object hierarchy, eliminating extensive use of global variables * Lots of minor fixes and tweaks, documentation etc. -### Key principle of rework ### +### Key principles ### +There are many other variants of a webcam server for these modules online, +but most are created for a specific scenario and not good for general, casual, +webcam use. -1. Any idea can be killed by unnecessary features -2. See [this tutorial video](https://www.youtube.com/watch?v=iMULJIXPxK4). - -Summary of reductions (compared to the official ESP32CAM web server example): -* Face recogniton features (dropped by @easytarget). -* The other reduction is the flash lamp. I mean, it is nice that you can flash with it, but the lamp is sharing one of the lines with SD card data. So I unsoldered it for now. May be will implement some workaround later. -* PlatformIO is nice, but had to go to the horizont. May be it is useful for some but adds nothing to this particular project, only adds another layer of compexity. - -The original example, is a bit incomprehensible and hard to modify as supplied. It is very focused on showing off the face recognition capabilities, and forgets the 'webcam' part. -The example extended by @easytarget improves the situation big time, but still follows the arcitecture design of the original and uses a lot of boilerplate code, which is hard to modify and extend. Besides, it does not support smooth video in high resolution. -* There are many other variants of a webcam server for these modules online, but most are created for a specific scenario and not good for general, casual, webcam use. +Hopefully this expanded example is more useful for those users who wish to set up +a simple ESP32 based webcam using the cheap(ish) modules freely available online. -Hopefully this expanded example is more useful for those users who wish to set up a simple ESP32 based webcam using the cheap(ish) modules freely available online. Especially the AI-THINKER board: +### Summary of reductions ### +When re-desiginig and refactoring the original ESP32 Camera web server example from +Espressve, the following key principles were followed: -#### AI-THINKER ESP32-CAM vs Other Modules: - -I have four [AI-THINKER ESP32-CAM](https://github.com/raphaelbs/esp32-cam-ai-thinker/blob/master/assets/ESP32-CAM_Product_Specification.pdf) boards, so the descriptions below are for that board. But I took care to leave the default definitions and controls for other boards in the example intact. You may need to adjust the programming method to suit the your board, look for examples online. +1. Any idea can be killed by unnecessary features +2. See [this tutorial video](https://www.youtube.com/watch?v=iMULJIXPxK4). -* For some other good examples and information on ESP32 based webcams I also recommend the sketches here: -https://github.com/raphaelbs/esp32-cam-ai-thinker -* The AI thinker wiki can be quite informative, when run through an online translator and read sensibly: -https://wiki.ai-thinker.com/esp32-cam -* Default pinouts are also included for WRover Kit, ESP Eye and M5Stack esp32 camera modules. - I do not have any of these boards, so they are untested by me. Please [let me know](https://github.com/abratchik/esp32-cam-webserver/issues) if you find issues or have a board not [in the list](./camera_pins.h). +Given the above, face recognition feature was removed. The main purpose of this +sketch is to make the camera web server easily configurable and reusable. +The original example, is a bit incomprehensible and hard to modify as supplied. +It is very focused on showing off the face recognition capabilities, and forgets +the 'webcam' part. + +### Supported development boards ### +The sketch has been tested on the [AI Thinker ESP32-CAM](https://github.com/raphaelbs/esp32-cam-ai-thinker/blob/master/assets/ESP32-CAM_Product_Specification.pdf) +module. Other ESP32 boards equipped with camera may be compatible but not guaranteed. ### Known Issues -The ESP32 itself is susceptible to the usual list of WiFi problems, not helped by having small antennas, older designs, congested airwaves and demanding users. The majority of disconnects, stutters and other comms problems are simply due to 'WiFi issues'. The AI-THINKER camera module & esp32 combination is quite susceptible to power supply problems affecting both WiFi conctivity and Video quality; short cabling and decent power supplies are your friend here; also well cooled cases and, if you have the time, decoupling capacitors on the power lines. +The ESP32 itself is susceptible to the usual list of WiFi problems, not helped by having +small antennas, older designs, congested airwaves and demanding users. The majority of +disconnects, stutters and other communication problems are simply due to 'WiFi issues'. -A basic limitation of the sketch is that it can can only support one stream at a time. If you try to connect to a cam that is already streaming (or attempting to stream, the first steam will freeze. The stream itself is just a series of JPEG images sent over the Web Socket protocol. This allows to relieve the client from necessity to query the server for new frames - server pushes it directly to the front end. This is also much faster than MJPEG since the server is not busy with processing client queries on every frame. +The AI-THINKER camera module & esp32 combination is quite susceptible to power supply +problems affecting both WiFi conctivity and Video quality; short cabling and decent +power supplies are your friend here; also well cooled cases and, if you have the time, +decoupling capacitors on the power lines. -This release also doesnt support OV3660 camera. I do not have it so testing is problematic. May be some time in the future. +A basic limitation of the sketch is that it can can only support one stream at a time. +If you try to connect to a cam that is already streaming (or attempting to stream, +the first steam will freeze. + +Currently, camera modules other than ov2640 are not supported. ## Setup: -* For programming you will need a suitable development environment, I use the Visual Studio Code, but this code should work in the Arduino Studio or Espressif development environment too. -* Make sure you are using the [latest version](https://www.arduino.cc/en/main/software#download) of the IDE and then follow [This Guide](https://docs.espressif.com/projects/arduino-esp32/en/latest/installing.html) to set up the Espressif Arduino core for the IDE. -* _I do not recommend or support running with development builds of either the IDE or the ESP arduino core._ -* If you have a development board (anything that can be programmed via a standard USB cable/jack on the board itself) you are in luck. Just plug it in and skip ahead to the [config](#config) section. Remember to set your board model. +* For programming you will need a suitable development environment. Possible options + include Visual Studio Code, Arduino Studio or Espressif development environment . ### Wiring for AI-THINKER Boards (and similar clone-alikes) @@ -67,62 +73,146 @@ Is pretty simple, You just need jumper wires, no soldering really required, see ![Hoockup](Docs/hookup.png) * Connect the **RX** line from the serial adapter to the **TX** pin on ESP32 * The adapters **TX** line goes to the ESP32 **RX** pin -* The **GPIO0** pin of the ESP32 must be held LOW (to ground) when the unit is powered up to allow it to enter it's programming mode. This can be done with simple jumper cable connected at poweron, fitting a switch for this is useful if you will be reprogramming a lot. -* You must supply 5v to the ESP32 in order to power it during programming, the FTDI board alone fails to supply this sometimes. The ESP32 CAM board is very sensitive to the quality of power source. Decoupling capacitors are very much reccomended. +* The **GPIO0** pin of the ESP32 must be held LOW (to ground) when the unit is + powered up to allow it to enter it's programming mode. This can be done with simple + jumper cable connected at poweron, fitting a switch for this is useful if you + will be reprogramming a lot. +* You will to supply 5v to the ESP32 in order to power it during programming; the FTDI + board alone fails to supply this sometimes. The ESP32 CAM board is very sensitive + to the quality of power source. Decoupling capacitors are very much recommended. ### Download the Sketch, Unpack and Rename -Download the latest release of the sketch from https://github.com/abratchik/esp32-cam-webserver. +Download the latest release of the sketch this repository. Once you have done that you +can open the sketch in the IDE by going to the `esp32-cam-webserver` sketch folder and +selecting `esp32-cam-webserver.ino`. -Once you have done that you can open the sketch in the IDE by going to the `esp32-cam-webserver` sketch folder and selecting `esp32-cam-webserver.ino`. +You also need to copy the content of the **data** folder from this repository to a micro +SD flash memory card (must be formatted as FAT32) and insert it into the micro SD slot of +the board. -You also need to copy the content of the **data** folder from this repository to an SD flash card (must be formatted as FAT32) and insert it into the SD slot of the board. Without the SD card, the sketch will not start. I'm using 4GB MicroSD flash memory card, which is mentioned as a maximum suppoted capacity for ESP32-CAM board. Higher capacity SD card may not work. +Without the SD card, the sketch will not start. Please ensure the size of the card does +not exceed 4GB, which is a maximum supported capacity for ESP32-CAM board. +Higher capacity SD card may not work. ### Config -By default the sketch assumes you have an AI-THINKER board, it creates an AccessPoint called `ESP32-CAM-CONNECT` and with the password `InsecurePassword`; connect to that and then browse to [`http://192.168.4.1/`](http://192.168.4.1/). This is nice and easy for testing and demo purposes. - -To make a permanent config with your home wifi settings, different defaults or a different board; copy (or rename) the file `myconfig.sample.h` in the sketch folder to `myconfig.h` and edit that, all the usable defaults are in that file. Because this is your private copy of the config it will not get overwritten if you update the main sketch! +You will need to configure the web server with your WiFi settings. In order to do so, +you will need to create a config file in the root folder of your SD card named `conn.json` +and format it as follows: + +```json +{ + "mdns_name":"YOUR_MDNS_NAME", + "stations":[ + {"ssid": "YOUR_SSID", "pass":"YOUR_WIFI_PASSWORD", "dhcp": true} + ], + "http_port":80, + "ota_enabled":true, + "ota_password":"YOUR_OTA_PASSWORD", + "ap_ssid":"esp32cam", + "ap_pass":"123456789", + "ap_ip": {"ip":"192.168.4.1", "netmask":"255.255.255.0"}, + "ap_dhcp":true, + "ntp_server":"pool.ntp.org", + "gmt_offset":14400, + "dst_offset":0, + "debug_mode": false +} +``` +Replace the WiFi and OTA parameters with your settings and save. PLease note that the sketch +will not boot properly if WiFi connection is established. + +Web server name can configured by creating another config file, `httpd.json`, in the root +folder of the SD card: + +```json +{ + "my_name": "MY_NAME", + "debug_mode": false +} +``` + +Similarly, default camera configuration parameters can be set by creating the file `cam.json`: + +```json +{ + "lamp":-1, + "autolamp":0, + "framesize":8, + "quality":12, + "xclk":8, + "frame_rate":25, + "brightness":0, + "contrast":0, + "saturation":0, + "special_effect":0, + "wb_mode":0,"awb":1, + "awb_gain":1, + "aec":1, + "aec2":0, + "ae_level":0, + "aec_value":204, + "agc":1, + "agc_gain":0, + "gainceiling":0, + "bpc":0, + "wpc":1, + "raw_gma":1, + "lenc":1, + "vflip":0, + "hmirror":0, + "dcw":1, + "colorbar":0, + "rotate":"0", + "debug_mode": false +} +``` ### Programming -Assuming you are using the latest Espressif Arduino core the `ESP32 Dev Module` board will appear in the ESP32 Arduino section of the boards list. Select this (do not use the `AI-THINKER` entry listed in the boiards menu, it is not OTA compatible, and will caus the module to crash and reboot rather than updating if you use it. +Assuming you are using the latest Espressif Arduino core the `ESP32 Dev Module` board +will appear in the ESP32 Arduino section of the boards list. Select this (do not use +the `AI-THINKER` entry listed in the boiards menu, it is not OTA compatible, and will +cause the module to crash and reboot rather than updating if you use it. ![IDE board config](Docs/ota-board-selection.png) -Make sure you select the `Minimal SPIFFS (1.9MB APP with OTA/190KB SPIFFS)` partition scheme and turn `PSRAM` on. - -The first time you program (or if OTA is failing) you need to compile and upload the code from the IDE, and when the `Connecting...` appears in the console reboot the ESP32 module while keeping **GPIO0** grounded. You can release GPO0 once the sketch is uploading, most boards have a 'boot' button to trigger a reboot. - -Once the upload completes (be patient, it can be a bit slow) open the serial monitor in the IDE and reboot the board again without GPIO0 grounded. In the serial monitor you should see the board start, connect to the wifi and then report the IP address it has been assigned. +Make sure you select the `Minimal SPIFFS (1.9MB APP with OTA/190KB SPIFFS)` partition +cheme and turn `PSRAM` on. -Once you have the initial upload done and the board is connected to the wifi network you should see it appearing in the `network ports` list of the IDE, and you can upload wirelessly. +The first time you program (or if OTA is failing) you need to compile and upload the +code from the IDE, and when the `Connecting...` appears in the console reboot the ESP32 +module while keeping **GPIO0** grounded. You can release GPO0 once the sketch is +uploading, most boards have a 'boot' button to trigger a reboot. -If you have a status LED configured it will give a double flash when it begins attempting to conenct to WiFi, and five short flashes once it has succeeded. It will also flash briefly when you access the camera to change settings. +Once the upload completes (be patient, it can be a bit slow) open the serial monitor +in the IDE and reboot the board again without GPIO0 grounded. In the serial monitor +you should see the board start, connect to the wifi and then report the IP address +it has been assigned. -Go to the URL given in the serial output, the web UI should appear with the settings panel open. Click away! +Once you have the initial upload done and the board is connected to the wifi network +you should see it appearing in the `network ports` list of the IDE, and you can upload +wirelessly. -## My Modifications: +If you have a status LED configured it will give a double flash when it begins +attempting to conenct to WiFi, and five short flashes once it has succeeded. It will +also flash briefly when you access the camera to change settings. -A lot more to come. Please stay tuned +Go to the URL given in the serial output, the web UI should appear with the settings +panel open. Click away! ### API -The communications between the web browser and the camera module can also be used to send commands directly to the camera (eg to automate it, etc) and form, in effect, an API for the camera. -* I have [documented this here](https://github.com/abratchik/esp32-cam-webserver/blob/master/API.md). - -## Notes: - -* I only have AI-THINKER modules with OV2640 camera installed; so I have only been able to test with this combination. I have attempted to preserve all the code for other boards and the OV3660 module, and I have merged all changes for the WebUI etc, but I cannot guarantee operation for these. -* I use a standard breadboard for programming. Connect your ESP32-CAM to a stable 5V, connect to FTDI (or use OTA) and dont forget about decoupling capacitors. +The communications between the web browser and the camera module can also be used to +send commands directly to the camera (eg to automate it, etc) and form, in effect, +an API for the camera. +* [ESP32 Camera Web Server JSON API](API.md). ## Contributing Contributions are welcome; please see the [Contribution guidelines](CONTRIBUTING.md). -## Plans - -Time allowing; my Current plan is: +## Future plans -1. Continue refactoring the project. The main goal is to simplify. -2. Try to fix the support for SPIFFS. It should work but it doesnt live well with ESP Async WebServer, not sure, why. I mean, using SD has its own advantages but sometimes you may want to keep the flash lamp. -3. Experiment with different cameras. -4. Explore how to improve the video quality and reduce requirements to resources. +1. Support of LittleFS. +3. Support of other boards and cameras. +4. Explore how to improve the video quality and further reduce requirements to resources. diff --git a/data/default_cam.json b/data/default_cam.json index 6197919..720ec47 100755 --- a/data/default_cam.json +++ b/data/default_cam.json @@ -1,4 +1,5 @@ -{ "lamp":-1, +{ + "lamp":-1, "autolamp":0, "framesize":8, "quality":12, @@ -27,4 +28,4 @@ "colorbar":0, "rotate":"0", "debug_mode": false -} \ No newline at end of file +} diff --git a/data/www/dump.html b/data/www/dump.html index 5ce5cf4..04a844b 100644 --- a/data/www/dump.html +++ b/data/www/dump.html @@ -26,9 +26,19 @@

ESP32 Cam Webserver

setInterval(function(){ fetchData(); - }, 15000); + }, 5000); function fetchData() { + console.log('Query serial'); + fetch('/control?var=cmdout&val=P') + .then(response => { + if(!response.ok) { + throw new Error(`Failed to retrieve serial data: ${response.status}`); + } + return response.text; + }) + .catch(error=> console.log(error)); + console.log('Start fetching data'); fetch('/system') .then(function (response) { @@ -115,6 +125,9 @@

ESP32 Cam Webserver

bodyHtml += 'Filesystem: ' + data.storage_size + data.storage_units + ', used: ' + data.storage_used + data.storage_units + '
'; + bodyHtml += '

Serial

'; + bodyHtml += data.serial_buf; + bodyHtml += '

'; contentBody.innerHTML = bodyHtml; diff --git a/esp32-cam-webserver.ino b/esp32-cam-webserver.ino index d65bdee..8bc75a8 100644 --- a/esp32-cam-webserver.ino +++ b/esp32-cam-webserver.ino @@ -147,6 +147,7 @@ void handleSerial() { // Rceiving commands and data from serial. Any input, which doesnt start from '#' is ignored. if (cmd == '#' ) { String rsp = Serial.readStringUntil('\n'); + rsp.trim(); sprintf(AppHttpd.getSerialBuffer(), rsp.c_str()); } } diff --git a/src/app_config.h b/src/app_config.h index fa9d535..e444451 100644 --- a/src/app_config.h +++ b/src/app_config.h @@ -37,8 +37,13 @@ // Uncomment to disable the illumination lamp features #define LAMP_DISABLE -// Uncomment this line to use SD instead of SPIFFS -#define USE_SD +// Uncomment this line to use LittleFS instead of SD. +// NOTE! +// LittleFS is still experimental, not recommended. The 'official' library installed from the Library Manager +// seems to be broken, but fixed in this PR: https://github.com/lorol/LITTLEFS/pull/56 +// To install it, please navigate to you /libraries sub-folder of your sketch location and then execute +// git clone https://github.com/Michael2MacDonald/LITTLEFS. +// #define USE_LittleFS // Define the startup lamp power setting (as a percentage, defaults to 0%) // Saved user settings will override this diff --git a/src/storage.cpp b/src/storage.cpp index 5ab2559..1bdd4b6 100644 --- a/src/storage.cpp +++ b/src/storage.cpp @@ -40,8 +40,8 @@ void listDir(const char * dirname, uint8_t levels){ /// @brief file storage initialization /// @return true if success, or false otherwise. bool init_storage() { -#ifndef USE_SD - return fsStorage->begin(FORMAT_SPIFFS_IF_FAILED); +#ifdef USE_LittleFS + return fsStorage->begin(FORMAT_LITTLEFS_IF_FAILED); #else if(!fsStorage->begin("/root", true, false, SDMMC_FREQ_DEFAULT)) return false; @@ -65,8 +65,7 @@ bool init_storage() { break; } - uint64_t cardSize = SD_MMC.cardSize() / (1024 * 1024); - Serial.printf(" card size: %lluMB\n", cardSize); + Serial.printf(" card size: %lluMB\n", storageSize()); return true; #endif @@ -78,7 +77,7 @@ bool init_storage() { /// @return OK(0) or FAIL(1) int readFileToString(char *path, String *s) { - File file = SD_MMC.open(path); + File file = fsStorage->open(path); if (!file) return FAIL; @@ -92,25 +91,13 @@ int readFileToString(char *path, String *s) } int storageSize() { -#ifndef USE_SD - return fsStorage->totalBytes(); -#else - return (int) (fsStorage->totalBytes() / (1024 * 1024)); -#endif + return (int) (fsStorage->totalBytes() / pow(1024, STORAGE_UNITS)); } int storageUsed() { -#ifndef USE_SD - return fsStorage->usedBytes(); -#else - return (int) (fsStorage->usedBytes() / (1024 * 1024)); -#endif + return (int) (fsStorage->usedBytes() / pow(1024, STORAGE_UNITS)); } int capacityUnits() { -#ifndef USE_SD - return STORAGE_UNITS_BT; -#else - return STORAGE_UNITS_MB; -#endif + return STORAGE_UNITS; } diff --git a/src/storage.h b/src/storage.h index bd75417..e5841b6 100644 --- a/src/storage.h +++ b/src/storage.h @@ -5,18 +5,21 @@ #include "app_config.h" -#ifndef USE_SD -#include "SPIFFS.h" -fs::SPIFFSFS * const fsStorage = &SPIFFS; -#define FORMAT_SPIFFS_IF_FAILED true +#define STORAGE_UNITS_BT 0 +#define STORAGE_UNITS_MB 2 + +#ifdef USE_LittleFS +#include +fs::LITTLEFSFS * const fsStorage = &LITTLEFS; +#define FORMAT_LITTLEFS_IF_FAILED true +#define STORAGE_UNITS STORAGE_UNITS_BT #else #include "SD_MMC.h" -fs::SDMMCFS * const fsStorage = &SD_MMC; -#define FORMAT_SPIFFS_IF_FAILED +fs::SDMMCFS * const fsStorage = &SD_MMC; +#define STORAGE_UNITS STORAGE_UNITS_MB #endif -#define STORAGE_UNITS_BT 0 -#define STORAGE_UNITS_MB 2 + int readFileToString(char *path, String *s);