From e6da3982b252e8a23c3e8933272d5d04e648d02c Mon Sep 17 00:00:00 2001 From: Dhruva Gole Date: Mon, 10 Jan 2022 16:35:56 +0530 Subject: [PATCH 1/5] Fix array size error in secrets.h Signed-off-by: Dhruva Gole --- Main/src/secrets.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Main/src/secrets.h b/Main/src/secrets.h index 4805cca..0e1108f 100644 --- a/Main/src/secrets.h +++ b/Main/src/secrets.h @@ -3,8 +3,8 @@ // ========= Add Station Infomation for this sensor =========================================== // This needs to be done for every sensor you flash char user[40] = "AMEIRA"; // add user code,eg JBIEBER -char network[2] = "GR"; // add 2 digit network code, eg GR -char station[5] = "GR002"; // add 5 digit station code, eg ST001 +char network[] = "GR"; // add 2 digit network code, eg GR +char station[] = "GR002"; // add 5 digit station code, eg ST001 // ========================== add your MQTT credentials ======================================= From 78a617d5830384801d8d7e0d4b8b7bd05d9aacce Mon Sep 17 00:00:00 2001 From: Dhruva Gole Date: Mon, 10 Jan 2022 18:05:56 +0530 Subject: [PATCH 2/5] Get ADXL345 working Signed-off-by: Dhruva Gole --- Main/src/main.cpp | 139 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 97 insertions(+), 42 deletions(-) diff --git a/Main/src/main.cpp b/Main/src/main.cpp index 1d598d2..e6e0d86 100644 --- a/Main/src/main.cpp +++ b/Main/src/main.cpp @@ -10,6 +10,8 @@ #include #include #include // forked from https://github.com/markrad/esp32-ADXL355 +#include +#include // forked from https://github.com/wollewald/ADXL345_WE #include #include "config.h" #include "secrets.h" // MQTT and Sensor information @@ -105,6 +107,11 @@ int QUE_len = LTA_len + STA_len; bool STALTAMODE = false; double TrueSampleRate; +// ADXL345 +#define ADXL345_I2CADDR 0x53 // 0x1D if SDO = HIGH +const int ADXL345_int2Pin = 2; +ADXL345_WE adxl345 = ADXL345_WE(ADXL345_I2CADDR); +static bool is_adxl345 = true; // -------------------------------------------------------------------------------------------- // Variables to hold accelerometer data // 10 second FIFO queue for STA / LTA algorithm @@ -243,6 +250,10 @@ void StartADXL355() WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 1); // enable brownout detector } +void startADXL345() +{ + // TODO Calibratiopn +} // Pub-sub errror messages void pubSubErr(int8_t MQTTErr) @@ -1348,23 +1359,40 @@ void setup() client.setServer(MQTT_HOST, MQTT_PORT); client.setCallback(messageReceived); - + Wire.begin(); + pinMode(ADXL345_int2Pin, INPUT); + if (!adxl345.init()) + { + Serial.println("ADXL345 not connected!\n"); #if OPENEEW_SAMPLE_RATE_125 - odr_lpf = Adxl355::ODR_LPF::ODR_125_AND_31_25; + odr_lpf = Adxl355::ODR_LPF::ODR_125_AND_31_25; #endif #if OPENEEW_SAMPLE_RATE_31_25 - odr_lpf = Adxl355::ODR_LPF::ODR_31_25_AND_7_813; + odr_lpf = Adxl355::ODR_LPF::ODR_31_25_AND_7_813; #endif - pinMode(ADXL_INT_PIN, INPUT); - pinMode(CHIP_SELECT_PIN_ADXL, OUTPUT); - attachInterrupt(digitalPinToInterrupt(ADXL_INT_PIN), isr_adxl, FALLING); + pinMode(ADXL_INT_PIN, INPUT); + pinMode(CHIP_SELECT_PIN_ADXL, OUTPUT); + attachInterrupt(digitalPinToInterrupt(ADXL_INT_PIN), isr_adxl, FALLING); - spi1 = new SPIClass(HSPI); - adxl355.initSPI(*spi1); - StartADXL355(); + spi1 = new SPIClass(HSPI); + adxl355.initSPI(*spi1); + StartADXL355(); + } + else + { + Serial.println("ADXL345 connected!\n"); + adxl345.setDataRate(ADXL345_DATA_RATE_12_5); + adxl345.setRange(ADXL345_RANGE_2G); + adxl345.setActivityParameters(ADXL345_DC_MODE, ADXL345_XY0, 0.6); + adxl345.setInterrupt(ADXL345_ACTIVITY, INT_PIN_2); + adxl345.setFifoParameters(ADXL345_TRIGGER_INT_1, 32); + adxl345.setFifoMode(ADXL345_STREAM); + attachInterrupt(digitalPinToInterrupt(ADXL345_int2Pin), isr_adxl, RISING); + DEBUG("ADXL345 setup done!\n") + } ledcSetup(channel, freq, resolution); ledcAttachPin(io, channel); @@ -1395,52 +1423,79 @@ void loop() else { client.loop(); - + //====================== ADXL Accelerometer ===================== - if (fifoFull) + if (fifoFull || true) // true added temporarily { fifoFull = false; - adxstatus = adxl355.getStatus(); - - if (adxstatus & Adxl355::STATUS_VALUES::FIFO_FULL) + if (!is_adxl345) { - + adxstatus = adxl355.getStatus(); + } + if ( (adxstatus & Adxl355::STATUS_VALUES::FIFO_FULL) || is_adxl345 ) + { + AccelReading AccelRecord; // Keep track of the heap in case heap fragmentation returns // Serial.println( xPortGetFreeHeapSize() ); - int numEntriesFifo = adxl355.readFifoEntries((long *)fifoOut); - // numEntriesFifo = numEntriesFifo-1; + if (is_adxl345) + { + int numEntriesFifo = -1; // adxl355.readFifoEntries((long *)fifoOut); // debug + // numEntriesFifo = numEntriesFifo-1; - if (numEntriesFifo != -1) - { - // Declare one AccelReading structure for this iteration of loop() - // so it doesn't need to go in and out of scope in various for() loops below - // typedef struct AccelXYZ { - // double x; double y; double z; - // } AccelReading ; - - AccelReading AccelRecord; - - // [{"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]}] - double gal; - double x, y, z; - for (int i = 0; i < numEntriesFifo; i++) + if ( (numEntriesFifo != -1 ) && false) // false added while debugging ADXL345 { - gal = adxl355.valueToGals(fifoOut[i][0]); - x = round(gal * 1000) / 1000; - AccelRecord.x = x; + // Declare one AccelReading structure for this iteration of loop() + // so it doesn't need to go in and out of scope in various for() loops below + // typedef struct AccelXYZ { + // double x; double y; double z; + // } AccelReading ; + + // [{"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]}] + double gal; + double x, y, z; + for (int i = 0; i < numEntriesFifo; i++) + { + gal = adxl355.valueToGals(fifoOut[i][0]); + x = round(gal * 1000) / 1000; + AccelRecord.x = x; - gal = adxl355.valueToGals(fifoOut[i][1]); - y = round(gal * 1000) / 1000; - AccelRecord.y = y; + gal = adxl355.valueToGals(fifoOut[i][1]); + y = round(gal * 1000) / 1000; + AccelRecord.y = y; - gal = adxl355.valueToGals(fifoOut[i][2]); - z = round(gal * 1000) / 1000; - AccelRecord.z = z; + gal = adxl355.valueToGals(fifoOut[i][2]); + z = round(gal * 1000) / 1000; + AccelRecord.z = z; - StaLtaQue.push(&AccelRecord); + StaLtaQue.push(&AccelRecord); + } } - + else + { + DEBUG("XYZ Entry\n") + adxl345.setMeasureMode(false); + String axes = adxl345.getActTapStatusAsString(); + byte intSource = adxl345.readAndClearInterrupts(); + for(int i=0; i<32; i++){ + xyzFloat g = adxl345.getGValues(); + + DEBUG("g-x = "); + DEBUG(g.x); + AccelRecord.x = g.x; + DEBUG(" | g-y = "); + DEBUG(g.y); + AccelRecord.y = g.y; + DEBUG(" | g-z = "); + DEBUG(g.z); + AccelRecord.z = g.z; + + StaLtaQue.push(&AccelRecord); + } + adxl345.readAndClearInterrupts(); + adxl345.setMeasureMode(true); + } + if (STALTAMODE) { // Do some STA / LTA math here... From a7ce3fa37ecbc947b4974cc792cb65f58e1c9ad1 Mon Sep 17 00:00:00 2001 From: Dhruva Gole Date: Thu, 13 Jan 2022 00:14:34 +0530 Subject: [PATCH 3/5] ADXL345 get readings from FiFo stream - moved the ADXL345 setup stuff into a dedicated startADXL345 function - Now takes readings when fifo is full. The fifoFull interrupt comes on Pin 2 Signed-off-by: Dhruva Gole --- Main/src/main.cpp | 57 ++++++++++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 26 deletions(-) diff --git a/Main/src/main.cpp b/Main/src/main.cpp index e6e0d86..ecccc84 100644 --- a/Main/src/main.cpp +++ b/Main/src/main.cpp @@ -252,7 +252,12 @@ void StartADXL355() void startADXL345() { - // TODO Calibratiopn + adxl345.setDataRate(ADXL345_DATA_RATE_50); + adxl345.setRange(ADXL345_RANGE_2G); + adxl345.setActivityParameters(ADXL345_DC_MODE, ADXL345_XY0, 0.6); + adxl345.setInterrupt(ADXL345_WATERMARK, INT_PIN_2); + adxl345.setFifoParameters(ADXL345_TRIGGER_INT_1, 32); + adxl345.setFifoMode(ADXL345_STREAM); } // Pub-sub errror messages @@ -1383,14 +1388,10 @@ void setup() } else { - Serial.println("ADXL345 connected!\n"); - adxl345.setDataRate(ADXL345_DATA_RATE_12_5); - adxl345.setRange(ADXL345_RANGE_2G); - adxl345.setActivityParameters(ADXL345_DC_MODE, ADXL345_XY0, 0.6); - adxl345.setInterrupt(ADXL345_ACTIVITY, INT_PIN_2); - adxl345.setFifoParameters(ADXL345_TRIGGER_INT_1, 32); - adxl345.setFifoMode(ADXL345_STREAM); + pinMode(ADXL345_int2Pin, INPUT); attachInterrupt(digitalPinToInterrupt(ADXL345_int2Pin), isr_adxl, RISING); + Serial.println("ADXL345 connected!\n"); + startADXL345(); DEBUG("ADXL345 setup done!\n") } @@ -1423,27 +1424,33 @@ void loop() else { client.loop(); - + bool is_adxl355 = false; //====================== ADXL Accelerometer ===================== - - if (fifoFull || true) // true added temporarily + if (fifoFull) { fifoFull = false; - if (!is_adxl345) + if (!is_adxl345 ) { adxstatus = adxl355.getStatus(); + is_adxl355 = adxstatus & Adxl355::STATUS_VALUES::FIFO_FULL; } - if ( (adxstatus & Adxl355::STATUS_VALUES::FIFO_FULL) || is_adxl345 ) + if ( is_adxl355 || is_adxl345 ) { AccelReading AccelRecord; // Keep track of the heap in case heap fragmentation returns // Serial.println( xPortGetFreeHeapSize() ); - if (is_adxl345) - { - int numEntriesFifo = -1; // adxl355.readFifoEntries((long *)fifoOut); // debug + // if (is_adxl345) + // { + static int numEntriesFifo = -1; + if (!is_adxl345) + { + numEntriesFifo = adxl355.readFifoEntries((long *)fifoOut); + } + + // debug // numEntriesFifo = numEntriesFifo-1; - if ( (numEntriesFifo != -1 ) && false) // false added while debugging ADXL345 + if ( numEntriesFifo != -1 ) { // Declare one AccelReading structure for this iteration of loop() // so it doesn't need to go in and out of scope in various for() loops below @@ -1475,19 +1482,17 @@ void loop() { DEBUG("XYZ Entry\n") adxl345.setMeasureMode(false); - String axes = adxl345.getActTapStatusAsString(); - byte intSource = adxl345.readAndClearInterrupts(); for(int i=0; i<32; i++){ xyzFloat g = adxl345.getGValues(); - DEBUG("g-x = "); - DEBUG(g.x); + DEBUG_IL("g-x = "); + DEBUG_IL(g.x); AccelRecord.x = g.x; - DEBUG(" | g-y = "); - DEBUG(g.y); + DEBUG_IL(" | g-y = "); + DEBUG_IL(g.y); AccelRecord.y = g.y; - DEBUG(" | g-z = "); - DEBUG(g.z); + DEBUG_IL(" | g-z = "); + DEBUG_IL(g.z); AccelRecord.z = g.z; StaLtaQue.push(&AccelRecord); @@ -1662,7 +1667,7 @@ void loop() for (int i = 0; i < 32; i++) StaLtaQue.drop(); } - } + // } } } From 1592300941dbcaf2d52189912a551f8958527fe7 Mon Sep 17 00:00:00 2001 From: Dhruva Gole Date: Thu, 13 Jan 2022 00:19:32 +0530 Subject: [PATCH 4/5] main.cpp: deleted redundant pinmode line Signed-off-by: Dhruva Gole --- Main/src/main.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Main/src/main.cpp b/Main/src/main.cpp index ecccc84..9bd43e4 100644 --- a/Main/src/main.cpp +++ b/Main/src/main.cpp @@ -1364,9 +1364,9 @@ void setup() client.setServer(MQTT_HOST, MQTT_PORT); client.setCallback(messageReceived); + Wire.begin(); - pinMode(ADXL345_int2Pin, INPUT); - if (!adxl345.init()) + if (!adxl345.init()) // if ADXL345 not connected properly via I2C { Serial.println("ADXL345 not connected!\n"); From 93747f29cc213d1317f7aaa219e60e830bfaf9c1 Mon Sep 17 00:00:00 2001 From: Dhruva Gole Date: Thu, 13 Jan 2022 00:41:30 +0530 Subject: [PATCH 5/5] Minor cleanup: move adxl flags to config.h Signed-off-by: Dhruva Gole --- Main/src/config.h | 5 ++++- Main/src/main.cpp | 2 -- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Main/src/config.h b/Main/src/config.h index 27f28b6..cfc7ea8 100644 --- a/Main/src/config.h +++ b/Main/src/config.h @@ -1,8 +1,11 @@ - // Add timezone, if not UTC int8_t TIME_ZONE = 0; // UTC uint8_t DST = 0; +// ADXL Flags +static bool is_adxl345 = true; +static bool is_adxl355 = false; + // debugging #define debug true #define LOG_L2 false // Needs to be true to have logging about deep wifi info diff --git a/Main/src/main.cpp b/Main/src/main.cpp index 9bd43e4..12a48d1 100644 --- a/Main/src/main.cpp +++ b/Main/src/main.cpp @@ -111,7 +111,6 @@ double TrueSampleRate; #define ADXL345_I2CADDR 0x53 // 0x1D if SDO = HIGH const int ADXL345_int2Pin = 2; ADXL345_WE adxl345 = ADXL345_WE(ADXL345_I2CADDR); -static bool is_adxl345 = true; // -------------------------------------------------------------------------------------------- // Variables to hold accelerometer data // 10 second FIFO queue for STA / LTA algorithm @@ -1424,7 +1423,6 @@ void loop() else { client.loop(); - bool is_adxl355 = false; //====================== ADXL Accelerometer ===================== if (fifoFull) {