Hi all,
I do have ESP32 cam with no PIR but still would like to have motion detection. My host running HA is pretty loaded and i found the below cool project with Arduino code where all processing is happening in the ESP32 cam, great!
It’s as easy as installing the motioneye hassio addon and configuring it. You can detect motion on many streams your system can handle. For esphome you need to use the esp32 camera web server to work together with motioneye.
Briefly tested this out. It publishes “motion detected” to MQTT and turns on an optional LED on pin 4
Then you would have to setup an MQTT sensor in HA. More to come… I like this idea for presence detection. I may add snapshots to it along with a temp sensor just for the fun of it…
/**
* Camera motion detection demo
*/
#include "eloquent.h"
#include "eloquent/vision/motion/naive.h"
// uncomment based on your camera and resolution
//#include "eloquent/vision/camera/ov767x/gray/vga.h"
//#include "eloquent/vision/camera/ov767x/gray/qvga.h"
//#include "eloquent/vision/camera/ov767x/gray/qqvga.h"
//#include "eloquent/vision/camera/esp32/aithinker/gray/vga.h"
//#include "eloquent/vision/camera/esp32/aithinker/gray/qvga.h"
//#include "eloquent/vision/camera/esp32/aithinker/gray/qqvga.h"
//#include "eloquent/vision/camera/esp32/wrover/gray/vga.h"
//#include "eloquent/vision/camera/esp32/wrover/gray/qvga.h"
#include "eloquent/vision/camera/esp32/wrover/gray/qqvga.h"
//#include "eloquent/vision/camera/esp32/eye/gray/vga.h"
//#include "eloquent/vision/camera/esp32/eye/gray/qvga.h"
//#include "eloquent/vision/camera/esp32/eye/gray/qqvga.h"
//#include "eloquent/vision/camera/esp32/m5/gray/vga.h"
//#include "eloquent/vision/camera/esp32/m5/gray/qvga.h"
//#include "eloquent/vision/camera/esp32/m5/gray/qqvga.h"
//#include "eloquent/vision/camera/esp32/m5wide/gray/vga.h"
//#include "eloquent/vision/camera/esp32/m5wide/gray/qvga.h"
//#include "eloquent/vision/camera/esp32/m5wide/gray/qqvga.h"
// WiFi and MQTT PubSub
#include <WiFi.h>
#include <PubSubClient.h>
// Replace the next variable with the MQTT Topic for this device
const char* pubtopic = "motion/ENTER_DEVICE_NAME";
// Replace the next variables with your SSID/Password combination
const char* ssid = "ENTER_YOUR_SSID";
const char* password = "ENTER_YOUR_PASSWORD";
// Add your MQTT Broker IP address, example:
//const char* mqtt_server = "192.168.1.144";
const char* mqtt_server = "ENTER_YOUR_MQTT_BROKER_ADDRESS";
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
// LED Pin
const int ledPin = 4;
#define THUMB_WIDTH 32
#define THUMB_HEIGHT 24
Eloquent::Vision::Motion::Naive<THUMB_WIDTH, THUMB_HEIGHT> detector;
void setup() {
delay(4000);
Serial.begin(115200);
// turn on high freq for fast streaming speed
camera.setHighFreq();
if (!camera.begin())
eloquent::abort(Serial, "Camera init error");
Serial.println("Camera init OK");
// wait for at least 10 frames to be processed before starting to detect
// motion (false triggers at start)
// then, when motion is detected, don't trigger for the next 10 frames
detector.startSinceFrameNumber(10);
detector.debounceMotionTriggerEvery(10);
// or, in one call
detector.throttle(10);
// trigger motion when at least 10% of pixels change intensity by
// at least 15 out of 255
detector.setPixelChangesThreshold(0.1);
detector.setIntensityChangeThreshold(15);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* message, unsigned int length) {
Serial.print("Message arrived on topic: ");
Serial.print(topic);
Serial.print(". Message: ");
String messageTemp;
for (int i = 0; i < length; i++) {
Serial.print((char)message[i]);
messageTemp += (char)message[i];
}
Serial.println();
// Feel free to add more if statements to control more GPIOs with MQTT
// If a message is received on the topic esp32/output, you check if the message is either "on" or "off".
// Changes the output state according to the message
if (String(topic) == "esp32/output") {
Serial.print("Changing output to ");
if(messageTemp == "on"){
Serial.println("on");
digitalWrite(ledPin, HIGH);
}
else if(messageTemp == "off"){
Serial.println("off");
digitalWrite(ledPin, LOW);
}
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("ESP8266Client")) {
Serial.println("connected");
// Subscribe
client.subscribe("esp32/output");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
if (!camera.capture()) {
Serial.println(camera.getErrorMessage());
delay(1000);
return;
}
// perform motion detection on resized image for fast detection
camera.image.resize<THUMB_WIDTH, THUMB_HEIGHT>();
camera.image.printAsJsonTo(Serial);
detector.update(camera.image);
// if motion is detected, print coordinates to serial in JSON format
if (detector.isMotionDetected()) {
client.publish(pubtopic, "Motion Detected");
detector.printAsJsonTo(Serial);
}
// release memory
camera.free();
}
Since i find PIRs super unreliable and slow, would it be possible to use the camera feed to motion eye to set up zones to detect motion and set it up so on motion detection it triggers event in HA?
zigbee: all of the above + becomes stuck
bt: slow, but so far best of the battery powered, can do sensing in the shape that it does nto react to the motion
wifi - i did found some diy, finished shelly maybe? but rather expensive to just try if it works by a chance
diy wifi - i did not found any i could adjust sensitivity and detect only certain angle etc. using eshome, all had mechanical adjustements and some mechancialy set timeout - not really sensor.
That is why i’m now thinking about camera with set detection zone or mmwave