From f867c62b8bc11f32ce65c5d079a2082d6b08e4e9 Mon Sep 17 00:00:00 2001 From: Jaro Date: Fri, 5 Nov 2021 06:18:40 +0100 Subject: [PATCH] Init --- .gitignore | 6 ++ .vscode/extensions.json | 7 ++ include/README | 39 +++++++++ platformio.ini | 22 +++++ src/main.cpp | 178 ++++++++++++++++++++++++++++++++++++++++ test/README | 11 +++ 6 files changed, 263 insertions(+) create mode 100644 .gitignore create mode 100644 .vscode/extensions.json create mode 100644 include/README create mode 100644 platformio.ini create mode 100644 src/main.cpp create mode 100644 test/README diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..362e2bd --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch +lib/* \ No newline at end of file diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..e80666b --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,7 @@ +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ] +} diff --git a/include/README b/include/README new file mode 100644 index 0000000..194dcd4 --- /dev/null +++ b/include/README @@ -0,0 +1,39 @@ + +This directory is intended for project header files. + +A header file is a file containing C declarations and macro definitions +to be shared between several project source files. You request the use of a +header file in your project source file (C, C++, etc) located in `src` folder +by including it, with the C preprocessing directive `#include'. + +```src/main.c + +#include "header.h" + +int main (void) +{ + ... +} +``` + +Including a header file produces the same results as copying the header file +into each source file that needs it. Such copying would be time-consuming +and error-prone. With a header file, the related declarations appear +in only one place. If they need to be changed, they can be changed in one +place, and programs that include the header file will automatically use the +new version when next recompiled. The header file eliminates the labor of +finding and changing all the copies as well as the risk that a failure to +find one copy will result in inconsistencies within a program. + +In C, the usual convention is to give header files names that end with `.h'. +It is most portable to use only letters, digits, dashes, and underscores in +header file names, and at most one dot. + +Read more about using header files in official GCC documentation: + +* Include Syntax +* Include Operation +* Once-Only Headers +* Computed Includes + +https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html diff --git a/platformio.ini b/platformio.ini new file mode 100644 index 0000000..800a132 --- /dev/null +++ b/platformio.ini @@ -0,0 +1,22 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:esp-wrover-kit] +platform = espressif32 +board = esp-wrover-kit +framework = arduino +build_flags = -DBOARD_HAS_PSRAM -mfix-esp32-psram-cache-issue +monitor_port = COM[13678] +monitor_speed = 115200 +monitor_rts = 0 +monitor_dtr = 0 +lib_deps = + yoursunny/esp32cam@^0.0.20210226 + naguissa/uTimerLib@^1.6.7 diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..0c96bc2 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,178 @@ +#define CAMERA_MODEL_AI_THINKER +#include +#include +#include +#include "uTimerLib.h" + +const char* WIFI_SSID = "CAMAP"; +const char* WIFI_PASS = "camera4okutt*8"; +const uint8_t port = 12; +const uint16_t pulseTimeout = 4; + +WebServer server(80); + +static auto loRes = esp32cam::Resolution::find(320, 240); +static auto hiRes = esp32cam::Resolution::find(800, 600); + +void +handleBmp() +{ + if (!esp32cam::Camera.changeResolution(loRes)) { + Serial.println("SET-LO-RES FAIL"); + } + + auto frame = esp32cam::capture(); + if (frame == nullptr) { + Serial.println("CAPTURE FAIL"); + server.send(503, "", ""); + return; + } + Serial.printf("CAPTURE OK %dx%d %db\n", frame->getWidth(), frame->getHeight(), + static_cast(frame->size())); + + if (!frame->toBmp()) { + Serial.println("CONVERT FAIL"); + server.send(503, "", ""); + return; + } + Serial.printf("CONVERT OK %dx%d %db\n", frame->getWidth(), frame->getHeight(), + static_cast(frame->size())); + + server.setContentLength(frame->size()); + server.send(200, "image/bmp"); + WiFiClient client = server.client(); + frame->writeTo(client); +} + +void +serveJpg() +{ + auto frame = esp32cam::capture(); + if (frame == nullptr) { + Serial.println("CAPTURE FAIL"); + server.send(503, "", ""); + return; + } + Serial.printf("CAPTURE OK %dx%d %db\n", frame->getWidth(), frame->getHeight(), + static_cast(frame->size())); + + server.setContentLength(frame->size()); + server.send(200, "image/jpeg"); + WiFiClient client = server.client(); + frame->writeTo(client); +} + +void +handleJpgLo() +{ + if (!esp32cam::Camera.changeResolution(loRes)) { + Serial.println("SET-LO-RES FAIL"); + } + serveJpg(); +} + +void +handleJpgHi() +{ + if (!esp32cam::Camera.changeResolution(hiRes)) { + Serial.println("SET-HI-RES FAIL"); + } + serveJpg(); +} + +void +handleJpg() +{ + server.sendHeader("Location", "/cam-hi.jpg"); + server.send(302, "", ""); +} + +void +handleMjpeg() +{ + if (!esp32cam::Camera.changeResolution(hiRes)) { + Serial.println("SET-HI-RES FAIL"); + } + + Serial.println("STREAM BEGIN"); + WiFiClient client = server.client(); + auto startTime = millis(); + int res = esp32cam::Camera.streamMjpeg(client); + if (res <= 0) { + Serial.printf("STREAM ERROR %d\n", res); + return; + } + auto duration = millis() - startTime; + Serial.printf("STREAM END %dfrm %0.2ffps\n", res, 1000.0 * res / duration); +} + +void handleOne() +{ + digitalWrite(port,1); +} + +void handleZero() +{ + digitalWrite(port,0); +} + +void handlePulse() +{ + handleOne(); + + TimerLib.setTimeout_s(handleZero,pulseTimeout); +} + +void +setup() +{ + Serial.begin(115200); + Serial.println(); + + { + using namespace esp32cam; + Config cfg; + cfg.setPins(pins::AiThinker); + cfg.setResolution(hiRes); + cfg.setBufferCount(2); + cfg.setJpeg(80); + + bool ok = Camera.begin(cfg); + Serial.println(ok ? "CAMERA OK" : "CAMERA FAIL"); + + pinMode(port, OUTPUT); + Serial.println("SET PORT OUTPUT\n"); + + } + + WiFi.persistent(false); + WiFi.mode(WIFI_STA); + WiFi.begin(WIFI_SSID, WIFI_PASS); + while (WiFi.status() != WL_CONNECTED) { + delay(500); + } + + Serial.print("http://"); + Serial.println(WiFi.localIP()); + Serial.println(" /cam.bmp"); + Serial.println(" /cam-lo.jpg"); + Serial.println(" /cam-hi.jpg"); + Serial.println(" /cam.mjpeg"); + + server.on("/cam.bmp", handleBmp); + server.on("/cam-lo.jpg", handleJpgLo); + server.on("/cam-hi.jpg", handleJpgHi); + server.on("/cam.jpg", handleJpg); + server.on("/cam.mjpeg", handleMjpeg); + server.on("/send.zero",handleZero); + server.on("/send.one",handleOne); + server.on("/send.pulse",handlePulse); + + server.begin(); +} + +void +loop() +{ + server.handleClient(); +} \ No newline at end of file diff --git a/test/README b/test/README new file mode 100644 index 0000000..b94d089 --- /dev/null +++ b/test/README @@ -0,0 +1,11 @@ + +This directory is intended for PlatformIO Unit Testing and project tests. + +Unit Testing is a software testing method by which individual units of +source code, sets of one or more MCU program modules together with associated +control data, usage procedures, and operating procedures, are tested to +determine whether they are fit for use. Unit testing finds problems early +in the development cycle. + +More information about PlatformIO Unit Testing: +- https://docs.platformio.org/page/plus/unit-testing.html