-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWildfire_Detection_System_Program.ino
More file actions
1843 lines (1489 loc) · 45.6 KB
/
Wildfire_Detection_System_Program.ino
File metadata and controls
1843 lines (1489 loc) · 45.6 KB
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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//libraries
#include <Adafruit_HTU21DF.h>
#include <ArduCAM.h>
#include <ArduinoHttpClient.h>
#include <MKRGSM.h>
#include <PubSubClient.h>
#include "secrets.h"
#include <SD.h>
#include <SPI.h>
#include <stdint.h>
#include <TinyGPS++.h>
#include <Wire.h>
//global variables
const String DeviceName = "Device 1";
const char* pin = SECRET_PINNUMBER;
const char* apn = SECRET_GPRS_APN;
const char* login = SECRET_GPRS_LOGIN;
const char* password = SECRET_GPRS_PASSWORD;
const char* mqtt_server = IOT_SERVER;
const int mqtt_port = MQTT_PORT;
const char* http_server = S3_SERVER;
const int http_port = HTTP_PORT;
//Objects
GSM gsmAccess;
GPRS gprsAccess;
GSMClient Gclient;
HttpClient client = HttpClient(Gclient, http_server, http_port);
PubSubClient mqttClient(Gclient);
char incomingByte;
#define photoRelayPin 1
#define ionRelayPin 2
//GPSSerial object created, linked to serial port of MKR GSM 1400
#define GPSSerial Serial1
//baud rate for the GPS
int GPSBaud = 9600;
// Create a TinyGPS++ object
TinyGPSPlus gps;
//Object created for location
GSMLocation location;
File videoFile;
char str[8];
const int bufferSize = 10240;
uint8_t buffer[bufferSize];
String videoName;
const char outTopic[] = TOPIC;
const int SIZE = 512;
String postData;
//Temp/Humi Sensor object
Adafruit_HTU21DF htu = Adafruit_HTU21DF();
float temp = 0, humi = 0;
int photoAlarm = 0;
long time = 0;
long searchTime = 30000;
//PWM pin for fan
const int FAN_PIN = 3;
//Fan speed var
float fanSpeed = 255;
//Anemometer variables
int anemPin = A4;
float anemVolt = 0, anemSpeed = 0;
//Wind vane variables
float vanVolt = 0;
String myDirection = "ERROR";
int vanPin = A6;
//variables for LED
int led1State = LOW; // ledState used to set the LED
unsigned long currentMillis;
unsigned long previousMillis = 0; // will store last time LED was updated
const long interval = 1000; // interval at which to blink (milliseconds)
int debugFlag = 1;
//variables for ion and photo sensor values
int photoSensorValue;
int ionSensorValue;
String photoSensor = "false", ionSensor = "false";
//variables for longitude and lattitude (GPS)
float GPSlongi = 0;
float GPSlati = 0;
//variables for timing getting coordinates
unsigned long startTime = 0; //approx time when we started
unsigned long finishTime = 0; //approx time we ended when we looked for a coordinate
unsigned long elapsedTime = 0; //interval of elapsed time
//variables for GSM coordinates
float gsmLati = 0; //holds first reading
float gsmLongi = 0; //holds first reading
float gsmLati2 = 0; //holds second more accurate reading
float gsmLongi2 = 0; //holds second more accurate reading
int GPSkey = 0; //key so we only get GPS coordinates once
int GSMkey = 0; //key so we only iterate through the cell tower triangulation twice
//coordinates to be used, decided between either GPS or cell tower triangulation coordinates
float mainLati = 0;
float mainLongi = 0;
int pollSensorsFlag = 0;
int pauseTime = 120; //seconds
// DEFINES
#if !(defined(OV5640_MINI_5MP_PLUS) || defined(OV5642_MINI_5MP_PLUS))
#error Please select the hardware platform and camera module in the ../libraries/ArduCAM/memorysaver.h file
#endif
//#define FISHINO_UNO // Nice UNO board with integrated RTC, microSD, WiFi
#define SERIAL_SPEED 115200
#define BUFFSIZE 512 // 512 is a good buffer size for SD writing. 4096 would be better, on boards with enough RAM (not Arduino Uno of course)
#define FRAME_SIZE OV5642_320x240
#define WIDTH_1 0x40 // Video width in pixel, hex. Here we set 320 (Big Endian: 320 = 0x01 0x40 -> 0x40 0x01). For 640: 0x80
#define WIDTH_2 0x01 // For 640: 0x02
#define HEIGHT_1 0xF0 // 240 pixels height (0x00 0xF0 -> 0xF0 0x00). For 480: 0xE0
#define HEIGHT_2 0x00 // For 480: 0x01
#define FPS 0x0F // 15 FPS. Placeholder: will be overwritten at runtime based upon real FPS attained
#define TOTAL_FRAMES 111 // Number of frames to be recorded. If < 256, easier to recognize in header (for manual hex debug)
//set pin 7 as the slave select for SPI:
#define SPI_CS 7
//ETCG Notes -- SD Pin
#define SD_CS 4 // 9 on Arducam adapter Uno and SD shields
//set this to 10 due to OV5642 Model
//if using FISHINO_UNO
#ifdef FISHINO_UNO
#define SD_AUX 10 // Needs to be Output on Fishino Uno for the integrated SD card to work
#endif
#define AVIOFFSET 240 // AVI main header length
//ionization sensor read voltage
const int voltage = A0;
// GLOBALS
unsigned long movi_size = 0;
unsigned long jpeg_size = 0;
const char zero_buf[4] = { 0x00, 0x00, 0x00, 0x00 };
const int avi_header[AVIOFFSET] PROGMEM = {
0x52,
0x49,
0x46,
0x46,
0xD8,
0x01,
0x0E,
0x00,
0x41,
0x56,
0x49,
0x20,
0x4C,
0x49,
0x53,
0x54,
0xD0,
0x00,
0x00,
0x00,
0x68,
0x64,
0x72,
0x6C,
0x61,
0x76,
0x69,
0x68,
0x38,
0x00,
0x00,
0x00,
0xA0,
0x86,
0x01,
0x00,
0x80,
0x66,
0x01,
0x00,
0x00,
0x00,
0x00,
0x00,
0x10,
0x00,
0x00,
0x00,
0x64,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x01,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
WIDTH_1,
WIDTH_2,
0x00,
0x00,
HEIGHT_1,
HEIGHT_2,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x4C,
0x49,
0x53,
0x54,
0x84,
0x00,
0x00,
0x00,
0x73,
0x74,
0x72,
0x6C,
0x73,
0x74,
0x72,
0x68,
0x30,
0x00,
0x00,
0x00,
0x76,
0x69,
0x64,
0x73,
0x4D,
0x4A,
0x50,
0x47,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x01,
0x00,
0x00,
0x00,
FPS,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x0A,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x73,
0x74,
0x72,
0x66,
0x28,
0x00,
0x00,
0x00,
0x28,
0x00,
0x00,
0x00,
WIDTH_1,
WIDTH_2,
0x00,
0x00,
HEIGHT_1,
HEIGHT_2,
0x00,
0x00,
0x01,
0x00,
0x18,
0x00,
0x4D,
0x4A,
0x50,
0x47,
0x00,
0x84,
0x03,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x4C,
0x49,
0x53,
0x54,
0x10,
0x00,
0x00,
0x00,
0x6F,
0x64,
0x6D,
0x6C,
0x64,
0x6D,
0x6C,
0x68,
0x04,
0x00,
0x00,
0x00,
0x64,
0x00,
0x00,
0x00,
0x4C,
0x49,
0x53,
0x54,
0x00,
0x01,
0x0E,
0x00,
0x6D,
0x6F,
0x76,
0x69,
};
//edited to this from past code, works better and sense OV5642 camera
#if defined(OV5640_MINI_5MP_PLUS)
ArduCAM myCAM(OV5640, SPI_CS);
#else
ArduCAM myCAM(OV5642, SPI_CS);
#endif
// END GLOBALS
static void inline print_quartet(unsigned long i, File fd) { // Writes an uint32_t in Big Endian at current file position
fd.write(i % 0x100);
i = i >> 8; //i /= 0x100;
fd.write(i % 0x100);
i = i >> 8; //i /= 0x100;
fd.write(i % 0x100);
i = i >> 8; //i /= 0x100;
fd.write(i % 0x100);
}
static void Video2SD() { // We don't enforce FPS: we just record and save frames as fast as possible
// Then we compute the attained FPS and update the AVI header accordingly
char str[8];
uint16_t n;
File outFile;
byte buf[BUFFSIZE];
static int i = 0;
uint8_t temp = 0, temp_last = 0;
unsigned long fileposition = 0;
uint16_t frame_cnt = 0;
uint16_t remnant = 0;
uint32_t length = 0;
uint32_t startms;
uint32_t elapsedms;
uint32_t uVideoLen = 0;
bool is_header = false;
#ifndef DISABLE_SD
//ETCG Notes -- Create File Name
// Create a avi file. Name should be unique-ish, but short
digitalWrite(SD_CS, HIGH);
randomSeed(analogRead(0) * millis());
n = (random(2, 999)); // Don't use 1.avi: was the default in old code, we don't want to overwrite old recordings
itoa(n, str, 10);
strcat(str, ".avi");
//Open the new file
outFile = SD.open(str, O_WRITE | O_CREAT | O_TRUNC);
if (!outFile) {
Serial.println(F("File open failed"));
while (1)
;
return;
}
videoName = String(str);
#endif
//Write AVI Main Header
// Some entries will be overwritten later
for (i = 0; i < AVIOFFSET; i++) {
char ch = pgm_read_byte(&avi_header[i]);
buf[i] = ch;
}
#ifndef DISABLE_SD
outFile.write(buf, AVIOFFSET);
#endif
Serial.print(F("\nRecording "));
Serial.print(TOTAL_FRAMES);
Serial.println(F(" video frames: please wait...\n"));
startms = millis();
//Write video data, frame by frame
for (frame_cnt = 0; frame_cnt < TOTAL_FRAMES; frame_cnt++) {
mqttClient.loop();
#if defined(ESP8266)
yield();
#endif
temp_last = 0;
temp = 0;
//Capture a frame
//Flush the FIFO
myCAM.flush_fifo();
//Clear the capture done flag
myCAM.clear_fifo_flag();
//Start capture
myCAM.start_capture();
// Wait for frame ready
while (!myCAM.get_bit(ARDUCHIP_TRIG, CAP_DONE_MASK))
;
length = myCAM.read_fifo_length(); // Length of FIFO buffer. In general, it contains more than 1 JPEG frame;
// so we'll have to check JPEG markers to save a single JPEG frame
#if defined(SPI_HAS_TRANSACTION)
SPI.beginTransaction(SPISettings(8000000, MSBFIRST, SPI_MODE0));
#endif
#ifndef DISABLE_SD
// Write segment. We store 1 frame for each segment (video chunk)
outFile.write("00dc"); // "start of video data chunk" (00 = data stream #0, d = video, c = "compressed")
outFile.write(zero_buf, 4); // Placeholder for actual JPEG frame size, to be overwritten later
#endif
i = 0;
jpeg_size = 0;
// Deassert camera Chip Select to start SPI transfer
myCAM.CS_LOW();
// Set FIFO to burst read mode
myCAM.set_fifo_burst();
// Transfer data, a byte at a time
while (length--) { // For every byte in the FIFO...
#if defined(ESP8266)
yield();
#endif
// We always need the last 2 bytes, to check for JPEG begin/end markers
temp_last = temp; // Save current temp value
temp = SPI.transfer(0x00); // Overwrite temp with 1 byte from FIFO (0x00 is dummy byte for the slave: we are reading, the slave will ignore it)
#if defined(SPI_HAS_TRANSACTION)
SPI.endTransaction();
#endif
// a JPEG ends with the two bytes 0xFF, 0xD9
if ((temp == 0xD9) && (temp_last == 0xFF)) // End of the image
{
buf[i++] = temp; // Add this last byte to the buffer
myCAM.CS_HIGH(); // End of transfer: re-assert Slave Select
#ifndef DISABLE_SD
// Write the buffer to file
outFile.write(buf, i);
#endif
is_header = false; // We are at the last byte of the JPEG: sure is not the header :)
jpeg_size += i; // Update total jpeg size with this last buffer size
i = 0; // Reset byte counter (restart writing from the first element of the buffer)
}
if (is_header == true) // Not at end of JPEG, yet
{
//Write image data to buffer if not full
if (i < BUFFSIZE)
buf[i++] = temp;
else { // Buffer is full: transfer to file
myCAM.CS_HIGH(); // End SPI transfer
#ifndef DISABLE_SD
//Write BUFFSIZE bytes image data to file
outFile.write(buf, BUFFSIZE);
#endif
i = 0; // Restart writing from the first element
buf[i++] = temp; // Save current byte as first in "new" buffer
myCAM.CS_LOW(); // Re-enable SPI transfer
myCAM.set_fifo_burst(); // Set FIFO to burst read mode
jpeg_size += BUFFSIZE;
}
} else if ((temp == 0xD8) & (temp_last == 0xFF)) { // A JPEG starts with the two bytes 0xFF, 0XD8; so here we are at the beginning of the JPEG
is_header = true;
buf[i++] = temp_last; // Save the first two bytes (off-cycle)
buf[i++] = temp;
}
} // end loop over each byte in the FIFO: JPEG is complete
// Padding
remnant = jpeg_size & 0x00000001; // Align to 16 bit: add 0 or 1 "0x00" bytes
#ifndef DISABLE_SD
if (remnant > 0) {
outFile.write(zero_buf, remnant); // see https://docs.microsoft.com/en-us/windows/desktop/directshow/avi-riff-file-reference
}
#endif
movi_size += jpeg_size; // Update totals
uVideoLen += jpeg_size; // <- This is for statistics only
// Now we have the real frame size in bytes. Time to overwrite the placeholder
#ifndef DISABLE_SD
fileposition = outFile.position(); // Here, we are at end of chunk (after padding)
outFile.seek(fileposition - jpeg_size - remnant - 4); // Here we are the the 4-bytes blank placeholder
print_quartet(jpeg_size, outFile); // Overwrite placeholder with actual frame size (without padding)
outFile.seek(fileposition - jpeg_size - remnant + 2); // Here is the FOURCC "JFIF" (JPEG header)
outFile.write("AVI1", 4); // Overwrite "JFIF" (still images) with more appropriate "AVI1"
// Return to end of JPEG, ready for next chunk
outFile.seek(fileposition);
#endif
} // End cycle for all frames
// END CAPTURE
// Compute statistics
elapsedms = millis() - startms;
float fRealFPS = (1000.0f * (float)frame_cnt) / ((float)elapsedms);
float fmicroseconds_per_frame = 1000000.0f / fRealFPS;
uint8_t iAttainedFPS = round(fRealFPS); // Will overwrite AVI header placeholder
uint32_t us_per_frame = round(fmicroseconds_per_frame); // Will overwrite AVI header placeholder
#ifndef DISABLE_SD
//Modify the MJPEG header from the beginning of the file, overwriting various placeholders
outFile.seek(4);
print_quartet(movi_size + 12 * frame_cnt + 4, outFile); // riff file size
//movie_size = movi_size;
//frame_count = frame_cnt;
//overwrite hdrl
//hdrl.avih.us_per_frame:
outFile.seek(0x20);
print_quartet(us_per_frame, outFile);
unsigned long max_bytes_per_sec = movi_size * iAttainedFPS / frame_cnt; //hdrl.avih.max_bytes_per_sec
outFile.seek(0x24);
print_quartet(max_bytes_per_sec, outFile);
//hdrl.avih.tot_frames
outFile.seek(0x30);
print_quartet(frame_cnt, outFile);
outFile.seek(0x84);
print_quartet((int)iAttainedFPS, outFile);
//hdrl.strl.list_odml.frames
outFile.seek(0xe0);
print_quartet(frame_cnt, outFile);
outFile.seek(0xe8);
print_quartet(movi_size, outFile); // size again
myCAM.CS_HIGH();
//Close the file
outFile.close();
#endif
Serial.println(F("\n*** Video recorded and saved ***\n"));
Serial.print(F("Recorded "));
Serial.print(elapsedms / 1000);
Serial.print(F("s in "));
Serial.print(frame_cnt);
Serial.print(F(" frames\nFile size is "));
Serial.print(movi_size + 12 * frame_cnt + 4);
Serial.print(F(" bytes\nActual FPS is "));
Serial.print(fRealFPS, 2);
Serial.print(F("\nMax data rate is "));
Serial.print(max_bytes_per_sec);
Serial.print(F(" byte/s\nFrame duration is "));
Serial.print(us_per_frame);
Serial.println(F(" us"));
Serial.print(F("Average frame length is "));
Serial.print(uVideoLen / TOTAL_FRAMES);
Serial.println(F(" bytes"));
}
void setup() {
uint8_t vid, pid;
uint8_t temp;
Wire.begin();
Serial.begin(SERIAL_SPEED); //115200
GPSSerial.begin(GPSBaud);
pinMode(voltage, INPUT); //voltage read
pinMode(FAN_PIN, OUTPUT);
pinMode(INPUT, vanPin);
pinMode(INPUT, anemPin);
pinMode(5, OUTPUT); // led pin
pinMode(1, OUTPUT); //relay 1 pin
pinMode(2, OUTPUT); //relay 2 pin
digitalWrite(1, HIGH); //relay is high, or engaged
digitalWrite(2, HIGH); //relay is high, or engaged
analogWrite(FAN_PIN, fanSpeed);
htu.begin();
delay(5000);
Serial.println(F("\nWILDFIRE DETECTION SYSTEM BEGIN\n"));
digitalWrite(5, HIGH);
ConnectGSM();
delay(3000);
GSMcoordinates();
delay(3000);
GPScoordinates();
delay(3000);
GSMorGPS();
delay(5000); // Gain time to start logic analyzer
#ifndef DISABLE_SD
// set the SPI_CS as an output:
pinMode(SD_CS, OUTPUT);
digitalWrite(SD_CS, HIGH);
#ifdef FISHINO_UNO
pinMode(SD_AUX, OUTPUT);
#endif
#endif
delay(1000);
// initialize SPI:
SPI.begin();
#ifndef DISABLE_SD
//Initialize SD Card
while (!SD.begin(SD_CS)) {
Serial.println(F("SD Card Error!"));
delay(1000);
}
Serial.println(F("SD Card detected."));
delay(200);
#endif
//Reset the CPLD
myCAM.write_reg(0x07, 0x80);
delay(100);
myCAM.write_reg(0x07, 0x00);
delay(200);
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;
}
}
delay(100);
//edited in order to detect OV5642
#if defined(OV5640_MINI_5MP_PLUS)
while (1) {
//Check if the camera module type is OV5640
myCAM.rdSensorReg16_8(OV5640_CHIPID_HIGH, &vid);
myCAM.rdSensorReg16_8(OV5640_CHIPID_LOW, &pid);
if ((vid != 0x56) || (pid != 0x40)) {
Serial.println(F("Can't find OV5640 module!"));
delay(1000);
continue;
} else {
Serial.println(F("OV5640 detected."));
break;
}
}
#else
while (1) {
//Check if the camera module type is OV5642
myCAM.rdSensorReg16_8(OV5642_CHIPID_HIGH, &vid);
myCAM.rdSensorReg16_8(OV5642_CHIPID_LOW, &pid);
if ((vid != 0x56) || (pid != 0x42)) {
Serial.println(F("Can't find OV5642 module!"));
delay(1000);
continue;
} else {
Serial.println(F("OV5642 detected."));
break;
}
}
#endif
delay(1000);
myCAM.set_format(JPEG);
myCAM.InitCAM();
//myCAM.OV5642_set_JPEG_size(FRAME_SIZE);
myCAM.write_reg(ARDUCHIP_TIM, VSYNC_LEVEL_MASK);
myCAM.OV5642_set_JPEG_size(FRAME_SIZE);
}
void loop() {
digitalWrite(5, HIGH);
ConnectGSM();
ConnectMQTT();
mqttClient.loop();
DebugHandler();
if (debugFlag == 0) {
Serial.println(ionSensor + ", " + photoSensor + ", " + String(humi) + ", " + String(temp));
Serial.println("Alarm: " + String(photoAlarm));
pollSensorsFlag = 0;
PollSensors();
}
TempHumiFan();
AnemVane();
Photo();
Ion();
if (ionSensor == "true" || photoSensor == "true") {
photoAlarm = 2;
} else if (temp > 57 && humi < 15) {
photoAlarm = 1;
} else {
photoAlarm = 0;
}
if (photoAlarm == 2) {
Serial.println("\n****************");
Serial.println("FIRE DETECTED!!!");
Serial.println("****************");
Video2SD();
delay(1000);
OpenVideo(videoName);
PostVideo(videoName);
ConnectMQTT();
mqttClient.loop();
PublishData();
videoFile.close();
DeleteVideo();
} else {
ConnectMQTT();
mqttClient.loop();
PublishData();
}
Serial.println("---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n");
}
void TempHumiFan() {
//Poll sensors
temp = htu.readTemperature(); //C
humi = htu.readHumidity();
if (temp < 24) {
fanSpeed = 64;
} else if (temp > 23 && temp < 76) {
fanSpeed = (3.82 * temp - 27.68);
} else {
fanSpeed = 255;
}
analogWrite(FAN_PIN, fanSpeed);
}
void ConnectGSM() {
//Connecting GSM network
if (gsmAccess.status() != GSM_READY) {
Serial.print("Connecting GSM: ");
while (gsmAccess.begin(pin) != GSM_READY) {
Serial.print(".");
delay(1000);
}
Serial.println("Connected");
}
//Connecting GPRS network
if (gprsAccess.status() != GPRS_READY) {
Serial.print("Connecting GPRS: ");
while (gprsAccess.attachGPRS(apn, login, password) != GPRS_READY) {
Serial.print(".");
delay(1000);
}
Serial.println("Connected");
}
}
void ConnectMQTT() {
//Connect to mqtt broker
mqttClient.setServer(mqtt_server, mqtt_port);
if (!mqttClient.connected()) {
// MQTT client is disconnected, connect
Serial.print("Attempting to MQTT broker: ");
Serial.print(mqtt_server);
while (!mqttClient.connect("MKR_client")) {
// failed, retry
Serial.print(".");
//ConnectGSM();
Blink(5, 330, 3, 0);
}
Serial.println();
Serial.println("You're connected to the MQTT broker");
Serial.println();
}
}
void AnemVane() {
//Vane voltage
vanVolt = analogRead(vanPin) * 5.0 / 1023.0;
//Anemomter voltage
anemVolt = analogRead(anemPin) * 2.5 / 1023.0;
float vanMax = 4.0;
//myDirections expressed as a range of voltages
if (vanVolt <= vanMax * 1 / 16 || vanVolt >= vanMax * 15 / 16) {
myDirection = "N";
} else if (vanVolt > vanMax * 1 / 16 && vanVolt <= vanMax * 3 / 16) {
myDirection = "NE";
} else if (vanVolt > vanMax * 3 / 16 && vanVolt <= vanMax * 5 / 16) {
myDirection = "E";
} else if (vanVolt > vanMax * 5 / 16 && vanVolt <= vanMax * 7 / 16) {
myDirection = "SE";
} else if (vanVolt > vanMax * 7 / 16 && vanVolt <= vanMax * 9 / 16) {
myDirection = "S";
} else if (vanVolt > vanMax * 9 / 16 && vanVolt <= vanMax * 11 / 16) {
myDirection = "SW";
} else if (vanVolt > vanMax * 11 / 16 && vanVolt <= vanMax * 13 / 16) {
myDirection = "W";
} else if (vanVolt > vanMax * 13 / 16 && vanVolt < vanMax * 15 / 16) {
myDirection = "NW";
} else {
myDirection = "ERROR";
}
anemSpeed = 12.0 * anemVolt * 3.0;
if (debugFlag == 0) {
//Wind vane voltage
Serial.print("Voltage: " + String(vanVolt) + " V - ");
//Wind myDirection output
Serial.print("Direction: " + String(myDirection) + "\t");
//Anemomter voltage output
Serial.print("Voltage: ");
Serial.print(anemVolt);
Serial.print(" V - ");
//Wind speed output
Serial.print("Speed: " + String(anemSpeed));
Serial.println(" mph");
}
}
String SubName(String videoName) {
String filename = videoName; // Your original filename
if (filename.endsWith(".AVI") || filename.endsWith(".avi")) {
filename = filename.substring(0, filename.length() - 4);
// Removes the last 4 characters (i.e. ".AVI" or ".avi")
}
// Now, the value of filename will be "example"
return filename;
}