Home > OS >  Sending data using esp_now
Sending data using esp_now

Time:05-13

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();
}      

CodePudding user response:

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.

  • Related