-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathesp32-cam-motion-detector.ino
586 lines (506 loc) · 15.9 KB
/
esp32-cam-motion-detector.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
#include <LoopbackStream.h>
#include <PipedStream.h>
#include <StreamUtils.h>
#include <ArduinoJson.h>
/**
* ESP32 Camera as Motion Detector
* Based on https://eloquentarduino.com/projects/esp32-arduino-motion-detection
* Tested using ESP32 wrover dev board w/ qqvga camera only
*
* In Arduino IDE you need the following extra libraries installed:
* - EloquantArduino
* - PubSubClient
*/
// REQUIRED SETTINGS:
// Include WiFi and MQTT PubSub
#include <WiFi.h>
#include <PubSubClient.h>
// WiFi Setup *REQUIRED*
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";
// MQTT Broker Setup *REQUIRED*
const char* mqtt_server = "YOUR_MQTT_BROKER"; // MQTT Broker IP/URL
const int mqtt_port = 1883; // MQTT Broker Port
const char* uniqueID = "OfficeMultiSensor"; // Unique Client Name
// Home Assistant Setup
// State/Attrib Topics
const char* haStateTopic = "homeassistant/sensor/office_multi_sensor/state";
const char* haAttribTopic = "homeassistant/sensor/office_multi_sensor/state";
//Motion Sensor
const char* haMotionConfigTopic = "homeassistant/binary_sensor/office_motion_sensor/config";
const char* haMotionName = "Office Motion Sensor";
// Temp Sensor
const char* haTempStateTopic = "homeassistant/sensor/office_multi_sensor/state";
const char* haTempConfigTopic = "homeassistant/sensor/office_temp_sensor/config";
const char* haTempName = "Office Temperature Sensor";
// RH Sensor
const char* haRHStateTopic = "homeassistant/sensor/office_multi_sensor/state";
const char* haRHConfigTopic = "homeassistant/sensor/office_humidity_sensor/config";
const char* haRHName = "Office Humidity Sensor";
//MQTT Topics to trigger action
const char* MQTTledEnable = "motion/office/settings/ledEnable"; // enable/disable LED
const char* MQTTheartbeat = "motion/office/settings/heartbeat"; // set heartbeat interval
const char* MQTTmotionenable = "motion/office/settings/enable"; // "enable" or "disable" motion detection
const char* MQTTmotionlockout = "motion/office/settings/lockout"; // time in ms to keep motion triggered
const char* MQTTstatus = "motion/office/settings/status"; // JSON status message
// Support Variables
const int MQTTheartbeatMIN = 59999; // 1 minute
const int MQTTheartbeatMAX = 300001; // 5 minutes
const int MQTTmotionlockoutMIN = 999;
const int MQTTmotionlockoutMAX = 60001;
// MQTT Sub Topics
const char* subtopic = "motion/office/settings/+";
// LED Setup
#define LEDPIN 12
//DHT Setup
#define DHT_PIN 13
#define DHT_TYPE DHT11
bool tempEnabled = true;
bool humidityEnabled = true;
bool reportTempF = true;
unsigned long dhtWait = 60000; // in miliseconds
// NO NEED TO EDIT BELOW THIS LINE UNLESS YOU WANT TO CHANGE THINGS
#include "eloquent.h"
#include "eloquent/vision/motion/naive.h"
#include "DHT.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"
// Setup Detection Variables
bool ledEnable = true;
bool motionEnabled = true;
bool detection = false;
bool motionEvent = false;
unsigned long motionLockoutTime = 5000;
unsigned long detectionStartTime = millis();
unsigned long lastHeartbeat = millis();
bool initialHeartbeat = false;
unsigned long heartbeatInterval = 60000; // Heartbeat interval in ms (60000 = 60 seconds)
// Setup DHT
DHT dht(DHT_PIN, DHT_TYPE);
unsigned long dhtLastRead = millis();
float dhtF = 0;
float dhtT = 0;
float dhtH = 0;
float dhtHIF = 0;
float dhtHIC = 0;
// Setup Debugging
bool debug = false;
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
#define THUMB_WIDTH 32
#define THUMB_HEIGHT 24
Eloquent::Vision::Motion::Naive<THUMB_WIDTH, THUMB_HEIGHT> detector;
void setup() {
dht.begin();
pinMode(LEDPIN, OUTPUT);
digitalWrite(LEDPIN, LOW);
delay(4000);
Serial.begin(115200);
// turn on high freq for fast streaming speed
camera.setHighFreq();
if (!camera.begin())
eloquent::abort(Serial, "Camera init error");
if (debug){
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());
}
const char* True = "true";
const char* False = "false";
void publishHATempConfig(){
/*
* Publish sensor status to MQTT
*/
// Create Buffer
char buffer[256]; // Used for all publish commands
// Temp Sensor
// Build JSON
StaticJsonDocument<1000> haTemp;
// Add Values
haTemp["device_class"] = "temperature";
haTemp["name"] = haTempName;
haTemp["state_topic"] = haStateTopic;
haTemp["value_template"] = "{{value_json.temperature}}";
haTemp["json_attributes_topic"] = haAttribTopic;
// Publish haTemp Config
serializeJsonPretty(haTemp, buffer);
client.publish(haTempConfigTopic,buffer,true);
}
void publishHARHConfig(){
/*
* Publish sensor status to MQTT
*/
// Create Buffer
char buffer[1000]; // Used for all publish commands
// Humidity Sensor
// Build JSON
StaticJsonDocument<1000> haRH;
// Add Values
haRH["device_class"] = "humidity";
haRH["name"] = haRHName;
haRH["state_topic"] = haStateTopic;
haRH["value_template"] = "{{value_json.humidity}}";
haRH["json_attributes_topic"] = haAttribTopic;
// Publish haRH Config
serializeJsonPretty(haRH, buffer);
client.publish(haRHConfigTopic,buffer,true);
}
void publishHAMotionConfig(){
/*
* Publish sensor status to MQTT
*/
// Create Buffer
char buffer[1000]; // Used for all publish commands
// Motion Sensor
// Build JSON
StaticJsonDocument<1000> haMotion;
// Add Values
haMotion["device_class"] = "motion";
haMotion["name"] = haMotionName;
haMotion["state_topic"] = haStateTopic;
haMotion["value_template"] = "{{value_json.motion}}";
haMotion["json_attributes_topic"] = haAttribTopic;
//Publish haMotion Config
serializeJsonPretty(haMotion, buffer);
client.publish(haMotionConfigTopic,buffer,true);
}
void publishState(){
/*
* Publish sensor status to MQTT
*/
Serial.println("Executing publishState()");
// Build JSON
// Allocate memory for the document
StaticJsonDocument<1000> haState;
// Add some values
haState["heartbeat"] = lastHeartbeat;
haState["heartbeatInterval"] = heartbeatInterval;
haState["motionLockoutTime"] = motionLockoutTime;
haState["ledEnable"] = ledEnable?"Enabled":"Disabled";
haState["motionEnabled"]= motionEnabled?"Enabled":"Disabled";
if (reportTempF){
haState["temperature"] = dhtF;
haState["HeatIndex"] = dhtHIF;
haState["units"] = "°F";
}
else{
haState["temperatureCelsius"] = dhtT;
haState["HeatIndexCelsius"] = dhtHIC;
haState["units"] = "°C";
}
haState["humidity"] = dhtH;
haState["motion"] = detection?"ON":"OFF";
// Create Buffer
Serial.println("creating buffer");
char buffer[1000];
Serial.println("buffer created");
serializeJsonPretty(haState, buffer);
Serial.print("Publish State: ");
Serial.println(client.publish(haStateTopic,buffer)?"OK":"Error");
Serial.println(buffer);
}
void callback(char* topic, byte* message, unsigned int length) {
if (debug){
Serial.print("Message arrived on topic: ");
Serial.print(topic);
Serial.print(". Message: ");
}
String messageTemp;
for (int i = 0; i < length; i++) {
if (debug){
Serial.print((char)message[i]);
}
messageTemp += (char)message[i];
}
if (debug){
Serial.println();
}
// MQTT Actions
// LED Enable/Disable
if (String(topic) == MQTTledEnable){
if (messageTemp == "true"){
if (ledEnable == false){
if (debug){
Serial.println("LED disabled");
}
ledEnable = true;
}
if (messageTemp == "false"){
if (ledEnable == true){
ledEnable = false;
}
}
}
}
// Heartbeat Interval
if (String(topic) == MQTTheartbeat){
unsigned long newHeartbeatInterval = strtoul(messageTemp.c_str(),NULL,0);
if (debug){
Serial.print("Received new Heartbeat Interval: ");
Serial.println(newHeartbeatInterval);
}
if ((newHeartbeatInterval-MQTTheartbeatMIN)*(newHeartbeatInterval-MQTTheartbeatMAX)){
heartbeatInterval = newHeartbeatInterval;
if (debug){
Serial.print("Heartbeat Interval Set To :");
Serial.print(newHeartbeatInterval);
Serial.println(" via MQTT");
client.publish(heartbeatIntervalPub,messageTemp.c_str());
}
}
else{
if (debug){
Serial.print("New Heartbeat Interval Must be > ");
Serial.println(MQTTheartbeatMIN);
Serial.print("Heartbeat Interval Remains: ");
Serial.println(heartbeatInterval);
}
}
}
// Motion Lockout Time
if (String(topic) == MQTTmotionlockout){
unsigned long newLockoutTime = strtoul(messageTemp.c_str(),NULL,0);
if (debug){
Serial.print("Received new lockout: ");
Serial.println(newLockoutTime);
}
if ((newLockoutTime-MQTTmotionlockoutMIN)*(newLockoutTime-MQTTmotionlockoutMAX)){
motionLockoutTime = newLockoutTime;
if (debug){
Serial.print("Motion Lockout Time Set To :");
Serial.print(newLockoutTime);
Serial.println(" via MQTT");
client.publish(lockoutTimePub,messageTemp.c_str());
}
}
else{
if (debug){
Serial.print("New Lockout Time Must be > ");
Serial.println(MQTTmotionlockoutMIN);
Serial.print("Lockout Time Remains: ");
Serial.println(motionLockoutTime);
}
}
}
// Motion Detection Enable/Disable
if (String(topic) == MQTTmotionenable){
if (messageTemp == "enable"){
if (motionEnabled == false){
motionEnabled = true;
if (debug){
Serial.println("Motion Sensor Enabled");
client.publish(MQTTmotionenable, "Enabled");
}
}
}
if (messageTemp == "disable"){
if (motionEnabled == true){
motionEnabled = false;
if (debug){
Serial.println("MotionSensor Disabled");
client.publish(MQTTmotionenable, "Disabled");
}
}
}
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect(uniqueID)) {
client.setBufferSize(1024);
Serial.print ("connected as");
Serial.println(uniqueID);
// Subscribe
client.subscribe(subtopic);
Serial.print("Subscribed to ");
Serial.println(subtopic);
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void detect_motion(){
if (camera.capture()){
// 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()) {
detection = true;
}
else{
detection = false;
}
// release memory
camera.free();
}
else {
delay(1000);
detect_motion();
}
}
int readDHT(){
if (debug){
Serial.println();
Serial.print(millis());
Serial.println(": Reading DHT11");
}
dhtH = dht.readHumidity();
dhtT = dht.readTemperature();
dhtF = dht.readTemperature(true);
if (isnan(dhtH) || isnan(dhtT) || isnan(dhtF)){
Serial.print("ERROR: failed to read DHT11 on Pin #");
Serial.println(DHT_PIN);
}
else{
dhtLastRead = millis();
dhtHIF = dht.computeHeatIndex(dhtF,dhtH);
dhtHIC = dht.computeHeatIndex(dhtT,dhtH);
if (debug){
Serial.println("DHT Sensor Readings:");
Serial.print("Humidity: ");
Serial.print(dhtH);
Serial.println("%RH");
Serial.print("Temp: ");
Serial.print(dhtF);
Serial.print("°F ");
Serial.print(dhtT);
Serial.println("°C");
Serial.print("Heat Index: ");
Serial.print(dhtHIF);
Serial.print("°F ");
Serial.print(dhtHIC);
Serial.print("°C");
}
return 0;
}
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop(); // Listen to MQTT Broker
// Get current execution time
unsigned long currentTime = millis(); // Used for HEARTBEAT and MOTION DETECTION
// BEGIN HEARTBEAT
if (!initialHeartbeat){
// This allows us to do initial setup of the sensor upon the first heartbeat...
// Publish Home Assistant MQTT Config
}
else{
// Send First Heartbeat
unsigned long timeoutHeartbeat = lastHeartbeat + heartbeatInterval;
if (currentTime > timeoutHeartbeat){
if (debug){
Serial.print("Heartbeat: ");
Serial.println(currentTime);
}
publishState();
lastHeartbeat = millis();
}
}
if (!initialHeartbeat){
if (debug){
Serial.print("Heartbeat: ");
Serial.println(currentTime);
}
// Publish Home Assistant Configs
publishHATempConfig();
publishHARHConfig();
publishHAMotionConfig();
// Publish Initial State
publishState();
lastHeartbeat = millis();
initialHeartbeat = true;
}
// END HEARTBEAT
// BEGIN MOTION DETECTION
if (motionEnabled){
if (motionEvent){
unsigned long timeout = detectionStartTime + motionLockoutTime;
if (currentTime > timeout){
detect_motion();
if (!detection){
publishState();
digitalWrite(LEDPIN, LOW); // Turn off LED
motionEvent = false;
}
}
}
else{
detect_motion();
if (detection){
publishState();
digitalWrite(LEDPIN, HIGH); // Turn on LED
detectionStartTime = millis();
motionEvent = true;
}
}
}
// END MOTION DETECTION
// BEGIN DHT
if (tempEnabled || humidityEnabled){
unsigned long dhtTimeout = dhtLastRead + dhtWait;
if (currentTime >= dhtTimeout || dhtLastRead == 0){
readDHT();
}
}
// END DHT
}