Recording data from two adxl345 sensors

Source Audio's icon

Your last question (if I understand it correctly)

if M1 value changed from 0.2 >= 0.5, or better say more then set amount like 0.3 that is not going to work like that because sensor will never jump, but change values step wise. how would you set starting points ?Maybe if within last 10 or 100 readings average value changed more then 0.2 ...

say zl.stream > zl.median then pass that again through the if threshold thing.or something else ? how do you imagine that ?

values that do not pass change threshold don't get registered at all.so your solution will depend on the reality - I mean what really is to be expected from your sensors.

Source Audio's icon

I actually discovered code you posted for ESP now, you allready there use smoothing, threshold etc.

why do that twice ?

and where is that humidity sensor getting in ?

As you are sending a list, why do you also need IDs , like X1, Y1 etc ?

one can split a list without that and needs less bytes ...

ESP32 code seems to be running strictly on ESP32 3> library,

there are places in your code that need a FIX.

I see the goal to send individual sensors with real threshold and "if changed" state from ESP32 in order to preserve power (if run on battery) which would mean that you need ID for each of them from beginning.

humidity sensor must be using a different board as it is not a part of that code you posted.

receiving ESP should then again get and route that mesages to max the way it is proper for your entire project.

F_Dos's icon

would you have that max patch active whole year 24/7 ???

Not the whole here but eventually it should run on a small pc for a few month (this is in the future). It must connect to max in real time so it will effect some sounds.

and where is that humidity sensor getting in ?

I am using two ESP32S3 - one called "sender" that the ADXL345 sensor is attached to it and sends that sensor value to the other ESP32S3 called "Receiver" . to that receiver I also attached the analog moisture sensor (M1) via analog pin. This receiver is connected to the computer via USB and sends all data to MAX (ADXL345 sensor x y z values, and M1 analog value)

As you are sending a list, why do you also need IDs , like X1, Y1 etc ?

I'm using this because I might attached more adxl345 sensor in the future

there are places in your code that need a FIX.

could you give an example?

humidity sensor must be using a different board as it is not a part of that code you posted.

True. It is attached to the receiver board as I write above. Here is the Receiver board code:

#include <WiFi.h>
#include <esp_now.h>

typedef struct {
  float x;
  float y;
  float z;
} SensorData;

SensorData receivedData;
bool newDataReceived = false;

#define MOISTURE_PIN A10
#define LED_PIN LED_BUILTIN

unsigned long lastMoistureRead = 0;
const int MOISTURE_INTERVAL = 500; // read every 500ms

// LED state
bool ledState = false;
unsigned long lastLedToggle = 0;
unsigned long lastReceiveTime = 0;
const unsigned long COMM_TIMEOUT = 2000;      // ms without data = "waiting" mode
const unsigned long BLINK_WAITING = 300;     // heartbeat interval when idle
const unsigned long BLINK_ACTIVE = 1000;       // interval when receiving data

void onReceive(const esp_now_recv_info *info, const uint8_t *data, int len) {
  memcpy(&receivedData, data, sizeof(receivedData));
  newDataReceived = true;
  lastReceiveTime = millis();
}

void setup() {
  Serial.begin(115200);
  delay(1000);

  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, LOW);

  WiFi.mode(WIFI_STA);
  if (esp_now_init() != ESP_OK) { Serial.println("ESP-NOW init failed"); while(1); }
  esp_now_register_recv_cb(onReceive);

  Serial.println("Ready");
}

void loop() {
  unsigned long now = millis();

  // Decide current blink mode based on recent communication
  bool commActive = (now - lastReceiveTime) < COMM_TIMEOUT;
  unsigned long blinkInterval = commActive ? BLINK_ACTIVE : BLINK_WAITING;

  // Toggle LED at the appropriate interval
  if (now - lastLedToggle >= blinkInterval) {
    ledState = !ledState;
    digitalWrite(LED_PIN, ledState);
    lastLedToggle = now;
  }

  // Read moisture every 500ms without blocking
  if (now - lastMoistureRead >= MOISTURE_INTERVAL) {
    int raw = analogRead(MOISTURE_PIN);
    float moisture = 1.0 - (raw / 4095.0);
    Serial.print("M1 "); Serial.println(moisture, 3);
    lastMoistureRead = now;
  }

  // Process ESP-NOW data immediately when received
  if (newDataReceived) {
    Serial.print("X1 "); Serial.println(receivedData.x, 3);
    Serial.print("Y1 "); Serial.println(receivedData.y, 3);
    Serial.print("Z1 "); Serial.println(receivedData.z, 3);
    newDataReceived = false;
  }
}

F_Dos's icon

Your last question (if I understand it correctly)

if M1 value changed from 0.2 >= 0.5, or better say more then set amount like 0.3 that is not going to work like that because sensor will never jump, but change values step wise. how would you set starting points ?Maybe if within last 10 or 100 readings average value changed more then 0.2 ...

say zl.stream > zl.median then pass that again through the if threshold thing.or something else ? how do you imagine that ?

values that do not pass change threshold don't get registered at all.so your solution will depend on the reality - I mean what really is to be expected from your sensors.

Thanks, that's a good point — you're right, the sensor won't jump like that, it changes gradually. I'll think about how to set the threshold based on what's realistic for the sensor. If you have a suggestion for how you'd set it up, I'd be glad to hear it.

As for what's realistic to expect from the sensor: moisture changes are slow, not instant — when the ground gets watered, the reading drifts up over roughly a few minutes rather than jumping, and then stays relatively stable (with small noise) until it dries out again over a much longer stretch of time (hours). So a fast, single-reading threshold probably isn't the right fit here.

Source Audio's icon

Listen, I would help you with the code, if you provide more infos, like where is that humidity sensor connected, what model it is etc.

for example DHT22 takes about 2000 ms to perform a single internal measurement.

that is too slow compared to accel if it moves.

at beginning you wanted 2 acccel sensors, is that still in play ?

how many ESP32 are in play, etc etc

sorry , I posted before having your answer above.

it explains few things.

I would let ESP32 send 3 accel values separated, each one only if it changes and passes threshold. that gives you best real time capture in receiving ESP32.

there you can add M1 and M2 and another ESP32 sender with another sensor if needed.

( if you capture a tree, it makes no sense to place 2 accel sensors close on same tree, i2c bus needs a very short wire ...)

in max you get them again prepended with IDs , I would rather use numeric 1 2 3 4 5 6 7 8 instead of X1 X2 etc.

then you can process them in whatever way you want.

F_Dos's icon

Listen, I would help you with the code, if you provide more infos, like where is that humidity sensor connected, what model it is etc.

Capacitive Soil Moisture Sensor V2.0 . This sensoe is attached to analog pin of the receiver ESP32S3

at beginning you wanted 2 acccel sensors, is that still in play ?

how many ESP32 are in play, etc etc

For the testing I'm doing now I'm using:
1 sender ESP for 1 ADXL345 sensor
1 receiver ESP for receiving the data from the sender ESP and reading the data from the moisture sensor.
In the future (perhaps in a week or two) I would like to connect at least another ADXL345 to its own ESP32 (Sender 2)

Source Audio's icon

ok, to your code, you seem to use strictly esp32 lib v3 , which would not compile on older versions,

which actually are more efficient.

then you have Serial.end at the setup() end, and to my understanding it woujd disturb USB connection.

delay(20) is not good in the loop, rather use non blocking millis.

the treshold and "if changed" check make no benefit if you send a list with 3 values on ANY change.

F_Dos's icon
F_Dos's icon

I would let ESP32 send 3 accel values separated, each one only if it changes and passes threshold. that gives you best real time capture in receiving ESP32.

OK that sounds good!

in max you get them again prepended with IDs , I would rather use numeric 1 2 3 4 5 6 7 8 instead of X1 X2 etc.

why numeric? I think it might be easier to understand the value if using x y and z .

( if you capture a tree, it makes no sense to place 2 accel sensors close on same tree, i2c bus needs a very short wire ...)

I will try to capture data from 1 or 2 different branches of the same tree, as I think I won't get much data if I place the sensor on the tree trunk. (Ideally I wanted to capture all sorts of hidden data from a tree, but I decided to start with these two types of sensors since I couldn't think of any other ideas, and I wanted to start experimenting with something.) So if you have any ideas for different sensors, I'd be glad to hear them.

Regarding the I2C — I know it needs to be a maximum of 20cm, which is why I'm using an ESP32, so I can keep the sensor very close to it and then send the data to the receiver, which will be down the tree, close to the computer.