@@ -565,12 +565,11 @@ void Send10Seconds2Cloud() {
565
565
acceleration[" y" ].add (AccelRecord.y );
566
566
acceleration[" z" ].add (AccelRecord.z );
567
567
}
568
-
569
568
}
570
569
571
570
// Serialize the History Json object into a string to be transmitted
572
571
// serializeJson(historydoc,Serial); // print to console
573
- static char historymsg[16384 ];;
572
+ static char historymsg[16384 ];
574
573
serializeJson (historydoc, historymsg, 16383 );
575
574
576
575
int jsonSize = measureJson (historydoc);
@@ -585,6 +584,7 @@ void Send10Seconds2Cloud() {
585
584
NeoPixelStatus ( LED_CONNECTED ); // Success - blink cyan
586
585
}
587
586
587
+ mqtt.setBufferSize ( 2000 ); // reset the MQTT buffer size
588
588
historydoc.clear ();
589
589
}
590
590
@@ -848,17 +848,25 @@ void loop() {
848
848
adxstatus = adxl355.getStatus ();
849
849
850
850
if (adxstatus & Adxl355::STATUS_VALUES::FIFO_FULL) {
851
+ // Keep track of the heap in case heap fragmentation returns
852
+ // Serial.println( xPortGetFreeHeapSize() );
851
853
int numEntriesFifo = adxl355.readFifoEntries ( (long *)fifoOut ) ;
852
854
if ( numEntriesFifo != -1 ) {
853
855
// Generate an array of json objects that contain x,y,z arrays of 32 floats.
854
856
// [{"x":[],"y":[],"z":[]},{"x":[],"y":[],"z":[]}]
855
857
JsonObject acceleration = traces.createNestedObject ();
856
858
859
+ // Declare one AccelReading structure for this iteration of loop()
860
+ // so it doesn't need to go in and out of scope in various for() loops below
861
+ // typedef struct AccelXYZ {
862
+ // double x; double y; double z;
863
+ // } AccelReading ;
864
+ AccelReading AccelRecord;
865
+
857
866
// [{"x":[9.479,0],"y":[0.128,-1.113],"z":[-0.185,123.321]},{"x":[9.479,0],"y":[0.128,-1.113],"z":[-0.185,123.321]}]
858
867
double gal;
859
868
double x, y, z;
860
869
for (int i = 0 ; i < numEntriesFifo; i++) {
861
- AccelReading AccelRecord;
862
870
gal = adxl355.valueToGals (fifoOut[i][0 ]);
863
871
x = round (gal*1000 )/1000 ;
864
872
acceleration[" x" ].add (x);
@@ -878,13 +886,14 @@ void loop() {
878
886
}
879
887
880
888
// Do some STA / LTA math here...
881
- // ...
889
+ char mathmsg[65 ];
890
+ sprintf (mathmsg, " Calculating STA/LTA from %d accelerometer readings" , StaLtaQue.getCount ());
891
+ // Serial.println(mathmsg);
882
892
if ( StaLtaQue.isFull () ) {
883
893
// ///////////////// find offset ////////////////
884
894
int queCount = StaLtaQue.getCount ( );
885
895
886
896
for (int idx = 0 ; idx < queCount; idx++) {
887
- AccelReading AccelRecord;
888
897
if ( StaLtaQue.peekIdx ( &AccelRecord, idx) ) {
889
898
sample[0 ] = AccelRecord.x ;
890
899
sample[1 ] = AccelRecord.y ;
@@ -897,13 +906,12 @@ void loop() {
897
906
for (int j = 0 ; j < 3 ; j++) {
898
907
offset[j] = sampleSUM[j] / (QUE_len);
899
908
}
900
-
909
+
901
910
// ///////////////// find lta /////////////////
902
911
sampleSUM[0 ] = 0 ;
903
912
sampleSUM[1 ] = 0 ;
904
913
sampleSUM[2 ] = 0 ;
905
914
for (int idx = 0 ; idx < LTA_len; idx++) {
906
- AccelReading AccelRecord;
907
915
if ( StaLtaQue.peekIdx ( &AccelRecord, idx) ) {
908
916
sampleABS[0 ] = abs ( AccelRecord.x - offset[0 ] );
909
917
sampleABS[1 ] = abs ( AccelRecord.y - offset[1 ] );
@@ -916,13 +924,12 @@ void loop() {
916
924
for (int j = 0 ; j < 3 ; j++) {
917
925
ltav[j] = sampleSUM[j] / (LTA_len);
918
926
}
919
-
927
+
920
928
// ////////////////// find sta ///////////////////////
921
929
sampleSUM[0 ] = 0 ;
922
930
sampleSUM[1 ] = 0 ;
923
931
sampleSUM[2 ] = 0 ;
924
932
for (int idx = LTA_len-STA_len ; idx < LTA_len; idx++) {
925
- AccelReading AccelRecord;
926
933
if ( StaLtaQue.peekIdx ( &AccelRecord, idx) ) {
927
934
sampleABS[0 ] = abs ( AccelRecord.x - offset[0 ] );
928
935
sampleABS[1 ] = abs ( AccelRecord.y - offset[1 ] );
@@ -939,15 +946,14 @@ void loop() {
939
946
if ( stalta[j] >= thresh ) {
940
947
// Whoa - STA/LTA algorithm detected some anomalous shaking
941
948
Serial.printf (" STA/LTA = %f = %f / %f (%i)\n " , stalta[j], stav[j], ltav[j], j );
942
- bPossibleEarthQuake = true ;
949
+ bPossibleEarthQuake = true ;
943
950
}
944
951
}
945
952
}
946
953
947
954
// // find STA/LTA for the other 31 samples but without doing the summing again
948
955
949
956
for (int idx = LTA_len+1 ; idx < QUE_len; idx++) {
950
- AccelReading AccelRecord;
951
957
if ( StaLtaQue.peekIdx ( &AccelRecord, idx) ) {
952
958
sample[0 ] = AccelRecord.x ;
953
959
sample[1 ] = AccelRecord.y ;
@@ -981,34 +987,41 @@ void loop() {
981
987
}
982
988
}
983
989
984
- // If STA/LTA algorithm detected some anomalous shaking
985
- if ( bPossibleEarthQuake ) {
990
+ if ( numSecsOfAccelReadings > 0 ) {
991
+ SendLiveData2Cloud ();
992
+ numSecsOfAccelReadings-- ;
993
+ bPossibleEarthQuake=false ;
994
+ } else if ( bPossibleEarthQuake ) {
995
+ // The STA/LTA algorithm detected some anomalous shaking
996
+ // If this is continued shaking, the above SendLiveData2Cloud()
997
+ // function has already sent current accelerometer data
998
+ // so don't send it again.
986
999
bPossibleEarthQuake=false ;
1000
+
987
1001
// Start sending 5 minutes of live accelerometer data
1002
+ Serial.println (" Start sending 5 minutes of live accelerometer data" );
988
1003
numSecsOfAccelReadings = 300 ;
1004
+
989
1005
// Send the previous 10 seconds of history to the cloud
990
1006
Send10Seconds2Cloud ();
991
1007
}
992
- char mathmsg[65 ];
993
- sprintf (mathmsg, " %d accelerometer readings on the StaLta Queue" , StaLtaQue.getCount ());
994
- Serial.println (mathmsg);
995
- // When the math is done, drop 32 records off the queue
1008
+
1009
+ // When this loop is done, drop 32 records off the queue
996
1010
if ( StaLtaQue.isFull () ) {
997
1011
for ( int i=0 ; i < 32 ; i++ )
998
1012
StaLtaQue.drop ();
999
1013
}
1000
1014
1001
- if ( numSecsOfAccelReadings > 0 ) {
1002
- SendLiveData2Cloud ();
1003
- numSecsOfAccelReadings-- ;
1004
- }
1005
1015
// Clear & Reset JsonArrays
1006
1016
jsonTraces.clear ();
1007
1017
traces = jsonTraces.to <JsonArray>();
1008
1018
1009
1019
// Switch the direction of the LEDs
1010
1020
breathedirection = breathedirection ? false : true ;
1011
1021
}
1022
+
1023
+ // Keep track of the heap in case heap fragmentation returns
1024
+ // Serial.println( xPortGetFreeHeapSize() );
1012
1025
}
1013
1026
}
1014
1027
0 commit comments