Skip to content

Commit f32794c

Browse files
committed
Chase a heap corruption leak
Signed-off-by: John Walicki <[email protected]>
1 parent a68dcdc commit f32794c

File tree

1 file changed

+34
-21
lines changed

1 file changed

+34
-21
lines changed

WatsonIoT/src/main.cpp

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -565,12 +565,11 @@ void Send10Seconds2Cloud() {
565565
acceleration["y"].add(AccelRecord.y);
566566
acceleration["z"].add(AccelRecord.z);
567567
}
568-
569568
}
570569

571570
// Serialize the History Json object into a string to be transmitted
572571
//serializeJson(historydoc,Serial); // print to console
573-
static char historymsg[16384];;
572+
static char historymsg[16384];
574573
serializeJson(historydoc, historymsg, 16383);
575574

576575
int jsonSize = measureJson(historydoc);
@@ -585,6 +584,7 @@ void Send10Seconds2Cloud() {
585584
NeoPixelStatus( LED_CONNECTED ); // Success - blink cyan
586585
}
587586

587+
mqtt.setBufferSize( 2000 ); // reset the MQTT buffer size
588588
historydoc.clear();
589589
}
590590

@@ -848,17 +848,25 @@ void loop() {
848848
adxstatus = adxl355.getStatus();
849849

850850
if (adxstatus & Adxl355::STATUS_VALUES::FIFO_FULL) {
851+
// Keep track of the heap in case heap fragmentation returns
852+
//Serial.println( xPortGetFreeHeapSize() );
851853
int numEntriesFifo = adxl355.readFifoEntries( (long *)fifoOut ) ;
852854
if ( numEntriesFifo != -1 ) {
853855
// Generate an array of json objects that contain x,y,z arrays of 32 floats.
854856
// [{"x":[],"y":[],"z":[]},{"x":[],"y":[],"z":[]}]
855857
JsonObject acceleration = traces.createNestedObject();
856858

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+
857866
// [{"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]}]
858867
double gal;
859868
double x, y, z;
860869
for (int i = 0; i < numEntriesFifo; i++) {
861-
AccelReading AccelRecord;
862870
gal = adxl355.valueToGals(fifoOut[i][0]);
863871
x = round(gal*1000)/1000;
864872
acceleration["x"].add(x);
@@ -878,13 +886,14 @@ void loop() {
878886
}
879887

880888
// 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);
882892
if( StaLtaQue.isFull() ) {
883893
/////////////////// find offset ////////////////
884894
int queCount = StaLtaQue.getCount( );
885895

886896
for (int idx = 0; idx < queCount; idx++) {
887-
AccelReading AccelRecord;
888897
if( StaLtaQue.peekIdx( &AccelRecord, idx) ) {
889898
sample[0] = AccelRecord.x;
890899
sample[1] = AccelRecord.y;
@@ -897,13 +906,12 @@ void loop() {
897906
for (int j = 0; j < 3; j++) {
898907
offset[j] = sampleSUM[j] / (QUE_len);
899908
}
900-
909+
901910
/////////////////// find lta /////////////////
902911
sampleSUM[0] = 0;
903912
sampleSUM[1] = 0;
904913
sampleSUM[2] = 0;
905914
for (int idx = 0; idx < LTA_len; idx++) {
906-
AccelReading AccelRecord;
907915
if( StaLtaQue.peekIdx( &AccelRecord, idx) ) {
908916
sampleABS[0] = abs( AccelRecord.x - offset[0] );
909917
sampleABS[1] = abs( AccelRecord.y - offset[1] );
@@ -916,13 +924,12 @@ void loop() {
916924
for (int j = 0; j < 3; j++) {
917925
ltav[j] = sampleSUM[j] / (LTA_len);
918926
}
919-
927+
920928
//////////////////// find sta ///////////////////////
921929
sampleSUM[0] = 0;
922930
sampleSUM[1] = 0;
923931
sampleSUM[2] = 0;
924932
for (int idx = LTA_len-STA_len ; idx < LTA_len; idx++) {
925-
AccelReading AccelRecord;
926933
if( StaLtaQue.peekIdx( &AccelRecord, idx) ) {
927934
sampleABS[0] = abs( AccelRecord.x - offset[0] );
928935
sampleABS[1] = abs( AccelRecord.y - offset[1] );
@@ -939,15 +946,14 @@ void loop() {
939946
if ( stalta[j] >= thresh ) {
940947
// Whoa - STA/LTA algorithm detected some anomalous shaking
941948
Serial.printf("STA/LTA = %f = %f / %f (%i)\n", stalta[j], stav[j], ltav[j], j );
942-
bPossibleEarthQuake = true ;
949+
bPossibleEarthQuake = true ;
943950
}
944951
}
945952
}
946953

947954
//// find STA/LTA for the other 31 samples but without doing the summing again
948955

949956
for (int idx = LTA_len+1; idx < QUE_len; idx++) {
950-
AccelReading AccelRecord;
951957
if( StaLtaQue.peekIdx( &AccelRecord, idx) ) {
952958
sample[0] = AccelRecord.x;
953959
sample[1] = AccelRecord.y;
@@ -981,34 +987,41 @@ void loop() {
981987
}
982988
}
983989

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.
986999
bPossibleEarthQuake=false;
1000+
9871001
// Start sending 5 minutes of live accelerometer data
1002+
Serial.println("Start sending 5 minutes of live accelerometer data");
9881003
numSecsOfAccelReadings = 300 ;
1004+
9891005
// Send the previous 10 seconds of history to the cloud
9901006
Send10Seconds2Cloud();
9911007
}
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
9961010
if( StaLtaQue.isFull() ) {
9971011
for( int i=0; i < 32; i++ )
9981012
StaLtaQue.drop();
9991013
}
10001014

1001-
if( numSecsOfAccelReadings > 0 ) {
1002-
SendLiveData2Cloud();
1003-
numSecsOfAccelReadings-- ;
1004-
}
10051015
// Clear & Reset JsonArrays
10061016
jsonTraces.clear();
10071017
traces = jsonTraces.to<JsonArray>();
10081018

10091019
//Switch the direction of the LEDs
10101020
breathedirection = breathedirection ? false : true;
10111021
}
1022+
1023+
// Keep track of the heap in case heap fragmentation returns
1024+
//Serial.println( xPortGetFreeHeapSize() );
10121025
}
10131026
}
10141027

0 commit comments

Comments
 (0)