
ESP32-CAM motion detection streaming
In the post about motion detection (you can find a link at the bottom of this page), we inspected the results of motion detection in the Serial Monitor. It would be great if we could visually inspect them, instead. This is indeed possible using a built-in web server that serves both the camera stream and the motion results at the same time.
For motion detection configurations available, refer to the aforementioned post. The only parameter you can configure for the server is the ratio
threshold: above this value (from 0
to 1
), you will see a message on the debugging web page which highlights that motion has been detected.
Here's the sketch.
/**
* Motion detection example, debug server.
*
* Motion detection requires the JPEGDEC library
* (https://github.com/bitbank2/JPEGDEC)
*/
#include <JPEGDEC.h>
#include <eloquent_esp32cam3.h>
using eloquent::camera::Camera;
using eloquent::camera::motion::MjpegServer;
using eloquent::camera::helpers::net::wifiConnect;
using eloquent::camera::helpers::net::setHostname;
Camera camera;
MjpegServer server(camera);
void setup() {
Serial.begin(115200);
Serial.println("Motion detection: debug server");
// see "GetStarted.ino" for more comments
camera.hardware.brownout.disable();
camera.hardware.clock.fast();
camera.hardware.pinout.ask();
camera.frame.pixformat.jpeg();
camera.frame.resolution.vga();
camera.frame.quality.high();
// see "MotionDetectionSimple.ino" for more comments
server.motion.stride(16);
server.motion.differBy(30);
server.motion.smoothBy(0.2);
server.motion.trainFor(10);
// display message on webpage
// when motion ratio is above this value
server.triggerAbove(0.3);
// connect to Wi-Fi
// make webserver available at http://esp32cam.local
wifiConnect("SSID", "PASSWORD");
setHostname("esp32cam");
// init camera
camera.begin();
camera.raise();
// init server
server.begin();
server.raise();
// print server instructions
Serial.println(server.help());
}
void loop() {
// web server runs in background
delay(1000);
}
/**
* Motion detection example, debug server.
*
* Motion detection requires the JPEGDEC library
* (https://github.com/bitbank2/JPEGDEC)
*/
#include <JPEGDEC.h>
#include <eloquent_esp32cam3.h>
using eloquent::camera::Camera;
using eloquent::camera::motion::MjpegServer;
using eloquent::camera::helpers::net::wifiConnect;
using eloquent::camera::helpers::net::setHostname;
Camera camera;
MjpegServer server(camera);
void setup() {
Serial.begin(115200);
Serial.println("Motion detection: debug server");
// see "GetStarted.ino" for more comments
camera.hardware.brownout.disable();
camera.hardware.clock.fast();
camera.hardware.pinout.ask();
camera.frame.pixformat.jpeg();
camera.frame.resolution.vga();
camera.frame.quality.high();
// see "MotionDetectionSimple.ino" for more comments
server.motion.stride(16);
server.motion.differBy(30);
server.motion.smoothBy(0.2);
server.motion.trainFor(10);
// display message on webpage
// when motion ratio is above this value
server.triggerAbove(0.3);
// connect to Wi-Fi
// make webserver available at http://esp32cam.local
wifiConnect("SSID", "PASSWORD");
setHostname("esp32cam");
// init camera
camera.begin();
camera.raise();
// init server
server.begin();
server.raise();
// print server instructions
Serial.println(server.help());
}
void loop() {
// web server runs in background
delay(1000);
}
After the sketch is flashed, open the Serial Monitor. You will find a message similar to the following line:
Motion server available at http://192.168.X.Y
Motion server available at http://192.168.X.Y
The exact IP address will vary depending on your network. If your router supports it (most do), you should also be able to visit