-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataLoggingCamera4.ino
251 lines (237 loc) · 6.32 KB
/
DataLoggingCamera4.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
#include <Wire.h>
#include <ArduCAM.h>
#include <SPI.h>
#include <SD.h>
#include <LoRa.h>
#include <avr/interrupt.h>
#include "memorysaver.h"
//This demo can only work on OV5640_MINI_5MP_PLUS or OV5642_MINI_5MP_PLUS platform.
#define SS 8
#define RST 9
#define DIO0 2
#if !(defined (OV2640_MINI_2MP_PLUS))
#error Please select the hardware platform and camera module in the ../libraries/ArduCAM/memorysaver.h file
#endif
#define FRAMES_NUM 0x00
// set pin 7 as the slave select for the digital pot:
const int CS = 7;
#define SD_CS 10
bool is_header = false;
int total_time = 0;
#if defined (OV2640_MINI_2MP_PLUS)
ArduCAM myCAM( OV2640, CS );
#endif
uint8_t read_fifo_burst(ArduCAM myCAM);
//BMP and RTC
#include <RTClib.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>
Adafruit_BMP280 bmp1(4); // hardware SPI
RTC_PCF8523 rtc;
File myFile;
//BMP and RTC END
void setup() {
// put your setup code here, to run once:
uint8_t vid, pid;
uint8_t temp;
#if defined(__SAM3X8E__)
Wire1.begin();
#else
Wire.begin();
#endif
Serial.begin(115200);
Serial.println(F("ArduCAM Start!"));
// set the CS as an output:
pinMode(CS, OUTPUT);
digitalWrite(CS, HIGH);
pinMode(3, OUTPUT);
// initialize SPI:
SPI.begin();
//Reset the CPLD
myCAM.write_reg(0x07, 0x80);
delay(100);
myCAM.write_reg(0x07, 0x00);
delay(100);
while (1) {
//Check if the ArduCAM SPI bus is OK
myCAM.write_reg(ARDUCHIP_TEST1, 0x55);
temp = myCAM.read_reg(ARDUCHIP_TEST1);
if (temp != 0x55)
{
Serial.println(F("SPI interface Error!"));
delay(1000); continue;
} else {
Serial.println(F("SPI interface OK.")); break;
}
}
#if defined (OV2640_MINI_2MP_PLUS)
while (1) {
//Check if the camera module type is OV2640
myCAM.wrSensorReg8_8(0xff, 0x01);
myCAM.rdSensorReg8_8(OV2640_CHIPID_HIGH, &vid);
myCAM.rdSensorReg8_8(OV2640_CHIPID_LOW, &pid);
if ((vid != 0x26 ) && (( pid != 0x41 ) || ( pid != 0x42 ))) {
Serial.println(F("ACK CMD Can't find OV2640 module!"));
delay(1000); continue;
}
else {
Serial.println(F("ACK CMD OV2640 detected.")); break;
}
}
#endif
//Initialize SD Card
while (!SD.begin(SD_CS))
{
Serial.println(F("SD Card Error!")); delay(1000);
}
Serial.println(F("SD Card detected."));
//Change to JPEG capture mode and initialize the OV5640 module
myCAM.set_format(JPEG);
myCAM.InitCAM();
myCAM.clear_fifo_flag();
myCAM.write_reg(ARDUCHIP_FRAMES, FRAMES_NUM);
// BMP and RTC
if (!bmp1.begin()) {
Serial.println("Sensor BMP280 device 1 was not found.");
while (1);
}
Serial.println("Initialize BMP280 1 completed.");
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
while (1) delay(3);
}
if (! rtc.initialized() || rtc.lostPower()) {
delay(2500);
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
rtc.start();
Serial.println("RTC is running.");
// BMP and RTC END
// LoRa setup
LoRa.setPins(SS, RST, DIO0);
}
void loop() {
myCAM.flush_fifo();
myCAM.clear_fifo_flag();
#if defined (OV2640_MINI_2MP_PLUS)
myCAM.OV2640_set_JPEG_size(OV2640_1600x1200);
#endif
DateTime now = rtc.now();
int pressure = bmp1.readPressure();
int temperature = bmp1.readTemperature()*100;
Serial.println(pressure);
Serial.println(temperature);
LoRa.beginPacket();
LoRa.println(temperature);
LoRa.println(pressure);
LoRa.print(now.hour(), DEC);
LoRa.print(':');
LoRa.print(now.minute(), DEC);
LoRa.print(':');
LoRa.println(now.second(), DEC);
LoRa.endPacket();
//Start capture
myCAM.start_capture();
Serial.println(F("start capture."));
total_time = millis();
while ( !myCAM.get_bit(ARDUCHIP_TRIG, CAP_DONE_MASK));
Serial.println(F("CAM Capture Done."));
total_time = millis() - total_time;
Serial.print(F("capture total_time used (in miliseconds):"));
Serial.println(total_time, DEC);
total_time = millis();
read_fifo_burst(myCAM);
total_time = millis() - total_time;
Serial.print(F("save capture total_time used (in miliseconds):"));
Serial.println(total_time, DEC);
//Clear the capture done flag
myCAM.clear_fifo_flag();
digitalWrite(3, LOW);
delay(1000);
digitalWrite(3, HIGH);
}
uint8_t read_fifo_burst(ArduCAM myCAM)
{
uint8_t temp = 0, temp_last = 0;
uint32_t length = 0;
static int i = 0;
static int k = 0;
char str[16];
File outFile;
byte buf[256];
length = myCAM.read_fifo_length();
Serial.print(F("The fifo length is :"));
Serial.println(length, DEC);
if (length >= MAX_FIFO_SIZE) //8M
{
Serial.println("Over size.");
return 0;
}
if (length == 0 ) //0 kb
{
Serial.println(F("Size is 0."));
return 0;
}
myCAM.CS_LOW();
myCAM.set_fifo_burst();//Set fifo burst mode
i = 0;
while ( length-- )
{
temp_last = temp;
temp = SPI.transfer(0x00);
//Read JPEG data from FIFO
if ( (temp == 0xD9) && (temp_last == 0xFF) ) //If find the end ,break while,
{
buf[i++] = temp; //save the last 0XD9
//Write the remain bytes in the buffer
myCAM.CS_HIGH();
outFile.write(buf, i);
//Close the file
outFile.close();
Serial.println(F("OK"));
is_header = false;
myCAM.CS_LOW();
myCAM.set_fifo_burst();
i = 0;
}
if (is_header == true)
{
//Write image data to buffer if not full
if (i < 256)
buf[i++] = temp;
else
{
//Write 256 bytes image data to file
myCAM.CS_HIGH();
outFile.write(buf, 256);
i = 0;
buf[i++] = temp;
myCAM.CS_LOW();
myCAM.set_fifo_burst();
}
}
else if ((temp == 0xD8) & (temp_last == 0xFF))
{
is_header = true;
myCAM.CS_HIGH();
//Create a avi file
k = k + 1;
itoa(k, str, 10);
strcat(str, ".jpg");
//Open the new file
outFile = SD.open(str, O_WRITE | O_CREAT | O_TRUNC);
if (! outFile)
{
Serial.println(F("File open failed"));
while (1);
}
myCAM.CS_LOW();
myCAM.set_fifo_burst();
buf[i++] = temp_last;
buf[i++] = temp;
}
}
myCAM.CS_HIGH();
return 1;
}