Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Main/src/config.h
Original file line number Diff line number Diff line change
@@ -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
Expand Down
142 changes: 100 additions & 42 deletions Main/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include <ArduinoJson.h>
#include <PubSubClient.h>
#include <Adxl355.h> // forked from https://github.com/markrad/esp32-ADXL355
#include <Wire.h>
#include <ADXL345_WE.h> // forked from https://github.com/wollewald/ADXL345_WE
#include <math.h>
#include "config.h"
#include "secrets.h" // MQTT and Sensor information
Expand Down Expand Up @@ -105,6 +107,10 @@ 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);
// --------------------------------------------------------------------------------------------
// Variables to hold accelerometer data
// 10 second FIFO queue for STA / LTA algorithm
Expand Down Expand Up @@ -243,6 +249,15 @@ void StartADXL355()
WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 1); // enable brownout detector
}

void startADXL345()
{
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
void pubSubErr(int8_t MQTTErr)
Expand Down Expand Up @@ -1349,22 +1364,35 @@ void setup()
client.setServer(MQTT_HOST, MQTT_PORT);
client.setCallback(messageReceived);

Wire.begin();
if (!adxl345.init()) // if ADXL345 not connected properly via I2C
{
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
{
pinMode(ADXL345_int2Pin, INPUT);
attachInterrupt(digitalPinToInterrupt(ADXL345_int2Pin), isr_adxl, RISING);
Serial.println("ADXL345 connected!\n");
startADXL345();
DEBUG("ADXL345 setup done!\n")
}

ledcSetup(channel, freq, resolution);
ledcAttachPin(io, channel);
Expand Down Expand Up @@ -1395,52 +1423,82 @@ void loop()
else
{
client.loop();

//====================== ADXL Accelerometer =====================

if (fifoFull)
{
fifoFull = false;
adxstatus = adxl355.getStatus();

if (adxstatus & Adxl355::STATUS_VALUES::FIFO_FULL)
if (!is_adxl345 )
{

adxstatus = adxl355.getStatus();
is_adxl355 = adxstatus & Adxl355::STATUS_VALUES::FIFO_FULL;
}
if ( is_adxl355 || 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)
// {
static int numEntriesFifo = -1;
if (!is_adxl345)
{
numEntriesFifo = adxl355.readFifoEntries((long *)fifoOut);
}

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++)
// debug
// numEntriesFifo = numEntriesFifo-1;

if ( numEntriesFifo != -1 )
{
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);
for(int i=0; i<32; i++){
xyzFloat g = adxl345.getGValues();

DEBUG_IL("g-x = ");
DEBUG_IL(g.x);
AccelRecord.x = g.x;
DEBUG_IL(" | g-y = ");
DEBUG_IL(g.y);
AccelRecord.y = g.y;
DEBUG_IL(" | g-z = ");
DEBUG_IL(g.z);
AccelRecord.z = g.z;

StaLtaQue.push(&AccelRecord);
}
adxl345.readAndClearInterrupts();
adxl345.setMeasureMode(true);
}

if (STALTAMODE)
{
// Do some STA / LTA math here...
Expand Down Expand Up @@ -1607,7 +1665,7 @@ void loop()
for (int i = 0; i < 32; i++)
StaLtaQue.drop();
}
}
// }
}
}

Expand Down
4 changes: 2 additions & 2 deletions Main/src/secrets.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 =======================================
Expand Down