-
Notifications
You must be signed in to change notification settings - Fork 0
/
CloudCamMotionControl.ino
398 lines (327 loc) · 12.5 KB
/
CloudCamMotionControl.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
#include <AsyncTCP.h>
#include <PubSubClient.h>
#include <WiFi.h>
const char* const TAG = "CAM_CONTROL";
// LED pins.
const uint8_t RED_LED_PIN = GPIO_NUM_3;
const uint8_t GREEN_LED_PIN = GPIO_NUM_4;
const uint8_t BLUE_LED_PIN = GPIO_NUM_5;
/**************************************************************************************/
/* Common constants */
// Common WiFi settings.
const char* const WIFI_SSID = "<WIFI_SSID>";
const char* const WIFI_PWD = "<WIFI_PASSWORD>";
// Common MQTT settings.
const uint16_t MQTT_PORT = 1883;
const char* const MQTT_SERVER = "<MQTT_SERVER_IP>";
const char* const MQTT_CLIENT_ID = "<MQTT_CLIENT_ID>";
const char* const MQTT_USER_NAME = "<MQTT_USER_NAME>";
const char* const MQTT_PASSWORD = "<MQTT_PASSWORD>";
const char* const MQTT_MOTION_ON_MESSAGE = "ON";
const char* const MQTT_MOTION_OFF_MESSAGE = "OFF";
// Common camera settings.
const uint16_t CAMERA_EVENT_PORT = 3201;
const char* const CAMERA_MOTION_EVENT = "EVENT: MOTION detect";
// Individual camera settings.
const char* const INDOOR_CAMERA_IP = "<FIRST_CAMERA_IP>";
const char* const INDOOR_CAMERA_MOTION_TOPIC = "indoorcam/motion";
const uint8_t INDOOR_CAMERA_MAX_MOTION_EVENTS = 1;
const uint32_t INDOOR_CAMERA_MOTION_COUNTER_RESET_TIMEOUT = 5000;
const uint32_t INDOOR_CAMERA_MOTION_RESET_TIMEOUT = 10000;
const uint8_t INDOOR_CAMERA_MOTION_DETECTED_LED = BLUE_LED_PIN;
const char* const OUTDOOR_CAMERA_IP = "<SECOND_CAMERA_IP>";
const char* const outDOOR_CAMERA_MOTION_TOPIC = "doorcam/motion";
const uint8_t OUTDOOR_CAMERA_MAX_MOTION_EVENTS = 2;
const uint32_t OUTDOOR_CAMERA_MOTION_COUNTER_RESET_TIMEOUT = 10000;
const uint32_t OUTDOOR_CAMERA_MOTION_RESET_TIMEOUT = 15000;
const uint8_t OUTDOOR_CAMERA_MOTION_DETECTED_LED = GREEN_LED_PIN;
/**************************************************************************************/
/**************************************************************************************/
/* Data types */
// The record describes remote IP camera.
struct CAMERA_DATA
{
// Camera IP address.
const char* Address;
// Camera motion detection MQTT topic.
const char* Topic;
// Number of motion detected message must be received before MQTT motion
// message will be send. Set to 1 to send MQTT message on first motion event.
const uint8_t MaxMotionEvents;
// Timeout after which motion event counter should be reset. Ignored if
// MaxMotionEvents == 1
const uint32_t MotionCounterResetTimout;
// Timeout after which MQTT motion reset will be send.
const uint32_t MotionResetTimeout;
// Motion indication PIN. 0 - disabled.
const uint8_t MotionIndicationPin;
// Motion detection status. True if camera ready for motion
// detection.
bool Ready;
// Current camera event message text.
String EventText;
// Motion detection event counter.
uint8_t MotionMessageCounter;
// True if motion detected.
bool MotionDetected;
// Millis when last time motion detected.
uint32_t MotionMillis;
// Camera TCP client instance.
AsyncClient* Client;
};
/**************************************************************************************/
/**************************************************************************************/
/* Global variables */
// Cameras array.
CAMERA_DATA Cameras[] = {
{
INDOOR_CAMERA_IP,
INDOOR_CAMERA_MOTION_TOPIC,
INDOOR_CAMERA_MAX_MOTION_EVENTS,
INDOOR_CAMERA_MOTION_COUNTER_RESET_TIMEOUT,
INDOOR_CAMERA_MOTION_RESET_TIMEOUT,
INDOOR_CAMERA_MOTION_DETECTED_LED,
false, "", 0, false, 0, new AsyncClient()
},
{
OUTDOOR_CAMERA_IP,
outDOOR_CAMERA_MOTION_TOPIC,
OUTDOOR_CAMERA_MAX_MOTION_EVENTS,
OUTDOOR_CAMERA_MOTION_COUNTER_RESET_TIMEOUT,
OUTDOOR_CAMERA_MOTION_RESET_TIMEOUT,
OUTDOOR_CAMERA_MOTION_DETECTED_LED,
false, "", 0, false, 0, new AsyncClient()
}
};
const size_t CamsCount = sizeof(Cameras) / sizeof(Cameras[0]);
// WiFiClient for MQTT connection.
WiFiClient _WiFiClient;
// MQTT client.
PubSubClient _MqttClient(_WiFiClient);
/**************************************************************************************/
/**************************************************************************************/
/* MQTT routines */
// Checks MQTT connection status and sends specified MQTT message with
// specified MQTT topic.
void SendMqttMessage(const char* const Topic, const char* const Message)
{
ESP_LOGI(TAG, "Check WiFi connection status");
if (WiFi.status() != WL_CONNECTED)
{
ESP_LOGI(TAG, "WiFi not connected");
return;
}
ESP_LOGI(TAG, "Check MQTT connection status");
if (!_MqttClient.connected())
{
ESP_LOGI(TAG, "MQTT not connected, try to connect");
_MqttClient.connect(MQTT_CLIENT_ID, MQTT_USER_NAME, MQTT_PASSWORD);
}
if (!_MqttClient.connected())
{
ESP_LOGE(TAG, "MQTT connect failed");
return;
}
ESP_LOGI(TAG, "MQTT connected. Send MQTT message (%s : %s)", Topic, Message);
if (!_MqttClient.publish(Topic, Message))
{
ESP_LOGE(TAG, "MQTT message send failed");
return;
}
ESP_LOGI(TAG, "MQTT message sent");
}
void SendMotionOnMessage(const CAMERA_DATA* const CamData)
{
if (CamData->MotionIndicationPin != 0)
digitalWrite(CamData->MotionIndicationPin, HIGH);
SendMqttMessage(CamData->Topic, MQTT_MOTION_ON_MESSAGE);
}
void SendMotionOffMessage(const CAMERA_DATA* const CamData)
{
if (CamData->MotionIndicationPin != 0)
digitalWrite(CamData->MotionIndicationPin, LOW);
SendMqttMessage(CamData->Topic, MQTT_MOTION_OFF_MESSAGE);
}
/**************************************************************************************/
/**************************************************************************************/
/* TCP clients event handlers */
void ResetCameraData(CAMERA_DATA* const CamData)
{
CamData->Ready = false;
CamData->EventText = "";
CamData->MotionMessageCounter = 0;
CamData->MotionDetected = false;
CamData->MotionMillis = 0;
}
void TcpConnect(CAMERA_DATA* const CamData)
{
ResetCameraData(CamData);
if (WiFi.status() != WL_CONNECTED)
{
ESP_LOGI(TAG, "WiFi not connected");
return;
}
if (CamData->Client->connect(CamData->Address, CAMERA_EVENT_PORT))
{
ESP_LOGI(TAG, "Connection to %s started", CamData->Address);
return;
}
ESP_LOGE(TAG, "Start connection to %s failed", CamData->Address);
}
// The event called when TCP connection established.
void ClientConnect(void* Args, AsyncClient* Client)
{
CAMERA_DATA* CamData = (CAMERA_DATA*)Args;
ESP_LOGI(TAG, "Connected to camera: %s", CamData->Address);
}
// The event called when TCP connection error appeared.
void ClientError(void* Args, AsyncClient* Client, int8_t Error)
{
CAMERA_DATA* CamData = (CAMERA_DATA*)Args;
ESP_LOGE(TAG, "Connect to camera %s failed: %d", CamData->Address, Error);
Client->close(true);
TcpConnect(CamData);
}
void ClientTimeout(void* Args, AsyncClient* Client, uint32_t Time)
{
CAMERA_DATA* CamData = (CAMERA_DATA*)Args;
ESP_LOGI(TAG, "Connect to camera %s timeout %d", CamData->Address, Time);
Client->close(true);
TcpConnect(CamData);
}
// The event called when TCP connection closed.
void ClientDisconnect(void* Args, AsyncClient* Client)
{
CAMERA_DATA* CamData = (CAMERA_DATA*)Args;
ESP_LOGI(TAG, "Disconnected from camera: %s", CamData->Address);
Client->close(true);
TcpConnect(CamData);
}
// The event called when data from camera received.
void ClientData(void* Args, AsyncClient* Client, void* Data, size_t Len)
{
if (Len == 0)
return;
CAMERA_DATA* CamData = (CAMERA_DATA*)Args;
if (!CamData->Ready)
{
if (Len > 17)
return;
CamData->Ready = true;
}
size_t i = 0;
char* StrData = (char*)Data;
while (i < Len)
{
char c = StrData[i];
i++;
if (StrData[i] != 0x0A)
{
CamData->EventText += c;
continue;
}
if (CamData->EventText.indexOf(CAMERA_MOTION_EVENT) > -1)
{
CamData->MotionMillis = millis();
if (!CamData->MotionDetected)
CamData->MotionMessageCounter++;
ESP_LOGI(TAG, "Motion event received from %s (%d)", CamData->Address,
CamData->MotionMessageCounter);
}
CamData->EventText = "";
}
}
/**************************************************************************************/
/**************************************************************************************/
/* WiFi event handlers */
// The event called when connection to WiFI network has been established.
void WiFiConnected(WiFiEvent_t Event, WiFiEventInfo_t EventInfo)
{
ESP_LOGI(TAG, "WiFi network connected");
}
// The event called when IP address assigned.
void WiFiIpAssigned(WiFiEvent_t Event, WiFiEventInfo_t EventInfo)
{
ESP_LOGI(TAG, "IP address assigned: %s", WiFi.localIP().toString());
for (size_t i = 0; i < CamsCount; i++)
TcpConnect(&Cameras[i]);
}
// The event called when WiFi network disconnected.
void WiFiDisconnected(WiFiEvent_t Event, WiFiEventInfo_t EventInfo)
{
ESP_LOGI(TAG, "WiFi network disconnected: %d",
EventInfo.wifi_sta_disconnected.reason);
}
/**************************************************************************************/
/**************************************************************************************/
/* Arduino routines */
void setup()
{
// As always, first initialize debug UART.
Serial.begin(115200);
// Wait UART startup (actually we need this delay for UART connection).
delay(1000);
// Init LED pins.
pinMode(RED_LED_PIN, OUTPUT);
pinMode(GREEN_LED_PIN, OUTPUT);
pinMode(BLUE_LED_PIN, OUTPUT);
// Turn all LEDs off.
digitalWrite(RED_LED_PIN, LOW);
digitalWrite(GREEN_LED_PIN, LOW);
digitalWrite(BLUE_LED_PIN, LOW);
// Add WiFi event handlers.
WiFi.onEvent(WiFiConnected, WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_CONNECTED);
WiFi.onEvent(WiFiIpAssigned, WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_GOT_IP);
WiFi.onEvent(WiFiDisconnected, WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_DISCONNECTED);
// Add TCP clients event handlers.
for (size_t i = 0; i < CamsCount; i++)
{
Cameras[i].Client->onConnect(ClientConnect, (void*)&Cameras[i]);
Cameras[i].Client->onError(ClientError, (void*)&Cameras[i]);
Cameras[i].Client->onTimeout(ClientTimeout, (void*)&Cameras[i]);
Cameras[i].Client->onData(ClientData, (void*)&Cameras[i]);
// Allocate memory for event text.
Cameras[i].EventText.reserve(255);
}
// Configure MQTT server.
_MqttClient.setServer(MQTT_SERVER, MQTT_PORT);
// Enable WiFi auto-reconnect.
WiFi.setAutoReconnect(true);
// Start WiFi connection.
WiFi.begin(WIFI_SSID, WIFI_PWD);
}
void loop()
{
for (size_t i = 0; i < CamsCount; i++)
{
CAMERA_DATA* CamData = &Cameras[i];
if (CamData->MotionDetected)
{
if (millis() - CamData->MotionMillis >= CamData->MotionResetTimeout)
{
ESP_LOGI(TAG, "Motion reset on camera %s", CamData->Address);
SendMotionOffMessage(CamData);
CamData->MotionMessageCounter = 0;
CamData->MotionDetected = false;
CamData->MotionMillis = 0;
}
continue;
}
if (CamData->MotionMessageCounter >= CamData->MaxMotionEvents)
{
ESP_LOGI(TAG, "Motion detected on camera %s", CamData->Address);
CamData->MotionDetected = true;
SendMotionOnMessage(CamData);
continue;
}
if (CamData->MotionMessageCounter > 0)
{
if (millis() - CamData->MotionMillis >= CamData->MotionCounterResetTimout)
{
ESP_LOGI(TAG, "Motion counter reset for %s", CamData->Address);
CamData->MotionMessageCounter = 0;
CamData->MotionMillis = 0;
}
}
}
}