lena
lena

Reputation: 727

Sending data using esp_now

Normally when I send and receive data using a send_now I use a structure as in this tutorial code. In this way, I face a problem, which is declaring an array where I need to declare size inside the structure to ensure that the sending and receiving process is done correctly. While sometimes I need to send an array I don't know what its size is, for example, this array

  double *arry=(double*)malloc(size*sizeof(double));

So can I send without using struct? or can I declare the arry[] inside struct without its size? and then specify it size?

sender struct:

typedef struct test_struct {
  int arry[];  I need to send this array but I do not know its size yet
} test_struct;

receiver struct:

typedef struct test_struct {
  int arry[];
} test_struct;

UPDATE: I trying to like this but I received uncorrect values in sender

int sizee=100;
double *by=(double*)malloc(sizee*sizeof(double));
  for (int i=0; i < 100; i++) {
       by[i]=i;
       Serial.println(by[i]);
  }
  esp_err_t result = esp_now_send(0, (uint8_t *) &by, sizeof(double));

In receiver

int sizee=100;
  double *by=(double*)malloc(sizee*sizeof(double));
  Serial.print("rearray=: ");
for(int i=0;i<100;i++){
    Serial.print(by[i]);
}
  Serial.println();
}      

Upvotes: 0

Views: 1533

Answers (2)

Bruce Gennette
Bruce Gennette

Reputation: 1

Version 2 of ESP-NOW allows 1490 bytes.
Change your library and re-compile.

A little more - V2 will automatically adjust power and TX rate, OR you can over-ride this and choose a slow, high power connection if plenty of power is available, or choose fast, low power if battery operation from nearby.
Or whatever suits your project.

Upvotes: 0

Tim Roberts
Tim Roberts

Reputation: 54867

If you are simply trying to send 100 double-precision floats, this will do it:

int sizee=100;
double *by=(double*)malloc(sizee*sizeof(double));

for (int i=0; i < sizee; i++) {
    by[i]=i;
    Serial.println(by[i]);
}
esp_err_t result = esp_now_send(0, (uint8_t *) by, sizee*sizeof(double));

The receiver has to be a callback, right? Something like this:

void OnDataRecv(const uint8_t * mac_addr, const uint8_t *incomingData, int len) {
  double *by=(double*)incomingData;
  Serial.print("rearray=: ");
  for(int i=0;i<100;i++){
    Serial.print(by[i]);
  }
  Serial.println();
} 

I don't know whether Serial.print can handle doubles. You'll have to check that.

Upvotes: 2

Related Questions