-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEspCamSend.ino
135 lines (120 loc) · 3.54 KB
/
EspCamSend.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
/*
Copyright (C) 2021 SFini
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file EspCamSend.ino
*
* Main module with setup() and loop()
*/
#include "Battery.h"
#include "Camera.h"
#include "Config.h"
#include "ConfigOverride.h" // uncomment this
#include "DeepSleep.h"
#include "SendTo.h"
#include "Led.h"
#include "bmm8563.h"
#include "WiFi.h"
#define DEEP_SLEEP_TIME (60 * 60) // Deep sleep: 1 hour
// #define DEEP_SLEEP_TIME (45 * 60) // Deep sleep: 45 minutes
// #define DEEP_SLEEP_TIME (30 * 60) // 30 minutes
// #define DEEP_SLEEP_TIME (1 * 60) // 1 minute
// #define DEEP_SLEEP_TIME 2700
// #define DEEP_SLEEP_TIME 3600
/** Helper function to set the RTC clock. */
void SetClock()
{
rtc_date_t date;
date.year = 2020;
date.month = 6;
date.day = 3;
date.hour = 10;
date.minute = 22;
date.second = 00;
bmm8563_setTime(&date);
}
/**
* Main function to get the greyscale imgage, analyse and compare it with the previous
* and if it is different and not at night take a high resolution image and send it
* with additional information to the raspberry server.
*/
void CaptureCompareAndSend()
{
StartCameraGreyScale();
if (CaptureGreyScaleImage()) {
ReadOldFrameFromNVS();
float frameDiff = GetFrameDiff();
float frameAvg = GetFrameAvg();
float frameSum = GetFrameSum();
bool hasDiff = frameDiff > 100;
bool daylight = frameAvg >= 2.2;
bool onlyWithDiff = false;
bool onlyDaylight = true;
// Only when changed and not at night
if (onlyWithDiff && !hasDiff) { // nothing changed
IncrementEqualCount();
} else if (!onlyDaylight || daylight) { // only at daylight
float voltage = 0.0;
int capacity = 0;
WriteNewFrameToNVS();
StopCamera();
StartCameraJpg();
if (StartWiFi()) {
int rssi = GetWifiRssi();
if (ReadBatterie(voltage, capacity)) {
SendInfo(voltage, capacity, frameDiff, frameAvg, frameSum, rssi);
}
if (CaptureJpgImage()) {
SendImage();
ReleaseJpgImage();
}
StopWiFi();
}
}
}
StopCamera();
}
/** setup() function. Starts everything and go to deep sleep. */
void setup()
{
Serial.begin(115200);
NVS.begin();
// NVS.eraseAll();
InitBattery();
bmm8563_init();
bmm8563_setDateIRQ(1, 0, 0, 0);
bmm8563_setTimerIRQ(DEEP_SLEEP_TIME);
pinMode(CAMERA_LED_GPIO, OUTPUT);
digitalWrite(CAMERA_LED_GPIO, HIGH);
//SetClock();
IncrementWakeupCount();
CaptureCompareAndSend();
IncrementAllActiveTime();
NVS.close();
StopBatteryOutput();
DeepSleep(DEEP_SLEEP_TIME);
}
/**
* loop() function.
* Should never reached
* This is only for checking
*/
void loop()
{
Serial.println("loop()");
if (StartWiFi()) {
SendInfo(0.0, 0, 0, 0, 0, 0);
StopWiFi();
}
delay(30000);
}