Mismatch in sent and received data of ESP-NOW

h19l71
Posts: 1
Joined: Tue Jan 25, 2022 10:10 am

Mismatch in sent and received data of ESP-NOW

Postby h19l71 » Tue Jan 25, 2022 10:53 am

Hello,
I'm trying to run esp-now example given in esp-idf: <https://github.com/espressif/esp-idf/bl ... ple_main.c> on 2 ESP32 devkits with the following changes in data_parse() and data_prepare():
  1. /* Parse received ESPNOW data. */
  2. int example_espnow_data_parse(uint8_t *data, uint16_t data_len, uint8_t *state, uint16_t *seq, int *magic)
  3. {
  4.     example_espnow_data_t *buf = (example_espnow_data_t *)data;
  5.     uint16_t crc, crc_cal = 0;
  6.     uint8_t i;
  7.     if (data_len < sizeof(example_espnow_data_t)) {
  8.         ESP_LOGE(TAG, "Receive ESPNOW data too short, len:%d", data_len);
  9.         return -1;
  10.     }
  11.     *state = buf->state;
  12.     *seq = buf->seq_num;
  13.     *magic = buf->magic;
  14.     crc = buf->crc;
  15.     buf->crc = 0;
  16.     crc_cal = esp_crc16_le(UINT16_MAX, (uint8_t const *)buf, data_len);
  17.     if (crc_cal == crc) {
  18.         for(i=0;i<data_len;i++){
  19.             ESP_LOGI(TAG, "||| Payload: %02x |||", buf->payload[i]);
  20.         }
  21.         ESP_LOGI(TAG, "<<< Type: %02x | State: %02x | Seq_num: %04x | CRC: %04x | Magic: %08x", buf->type, buf->state, buf->seq_num, crc_cal, buf->magic);
  22.         return buf->type;
  23.     }
  24.     return -1;
  25. }
  26. /* Prepare ESPNOW data to be sent. */
  27. void example_espnow_data_prepare(example_espnow_send_param_t *send_param)
  28. {
  29.     example_espnow_data_t *buf = (example_espnow_data_t *)send_param->buffer;
  30.     uint8_t i;
  31.     assert(send_param->len >= sizeof(example_espnow_data_t));
  32.     buf->type = IS_BROADCAST_ADDR(send_param->dest_mac) ? EXAMPLE_ESPNOW_DATA_BROADCAST : EXAMPLE_ESPNOW_DATA_UNICAST;
  33.     buf->state = send_param->state;
  34.     buf->seq_num = s_example_espnow_seq[buf->type]++;
  35.     buf->crc = 0;
  36.     buf->magic = send_param->magic;
  37.     *buf->payload = 0x32;
  38.     // for(i=0;i<send_param->len;i++){
  39.     //     *(buf->payload + i) = 0x32 + i;
  40.     // }
  41.     /* Fill all remaining bytes after the data with random values */
  42.     esp_fill_random(buf->payload, send_param->len - sizeof(example_espnow_data_t));
  43.     buf->crc = esp_crc16_le(UINT16_MAX, (uint8_t const *)buf, send_param->len);
  44.     for(i=0;i<send_param->len;i++){
  45.         ESP_LOGI(TAG, "||| Payload: %02x |||", buf->payload[i]);
  46.     }
  47.     ESP_LOGI(TAG, ">>> Size: %d | Type: %02x | State: %02x | Seq_num: %04x | CRC: %04x | Magic: %08x", sizeof(buf->payload), buf->type, buf->state, buf->seq_num, buf->crc, buf->magic);
  48. }
I've also added their resp. target MAC addresses and the receiver is set to be a softAP and the transmitter to a station.

And this is the output:
sender side:

Code: Select all

I (304238) espnow_example: send data to 9c:9c:1f:c9:47:15
I (304238) espnow_example: ||| Payload: 32 |||
I (304238) espnow_example: ||| Payload: 3f |||
I (304238) espnow_example: ||| Payload: 00 |||
I (304248) espnow_example: ||| Payload: 08 |||
I (304248) espnow_example: ||| Payload: 00 |||
I (304258) espnow_example: ||| Payload: 00 |||
I (304258) espnow_example: ||| Payload: a5 |||
I (304268) espnow_example: ||| Payload: a5 |||
I (304268) espnow_example: ||| Payload: a5 |||
I (304278) espnow_example: ||| Payload: a5 |||
I (304278) espnow_example: >>> Size: 0 | Type: 00 | State: 01 | Seq_num: 011c | CRC: e969 | Magic: 29d68dfc
receiver side:

Code: Select all

I (302476) espnow_example: ||| Payload: 00 |||
I (302476) espnow_example: ||| Payload: 00 |||
I (302476) espnow_example: ||| Payload: bc |||
I (302476) espnow_example: ||| Payload: 68 |||
I (302486) espnow_example: ||| Payload: fc |||
I (302486) espnow_example: ||| Payload: 3f |||
I (302496) espnow_example: ||| Payload: 40 |||
I (302496) espnow_example: ||| Payload: 00 |||
I (302506) espnow_example: ||| Payload: 00 |||
I (302506) espnow_example: ||| Payload: 00 |||
I (302516) espnow_example: <<< Type: 00 | State: 01 | Seq_num: 011c | CRC: e969 | Magic: 29d68dfc
I (302526) espnow_example: Receive 284th broadcast data from: 84:cc:a8:2e:05:b8, len: 10
As you can see, there is a mismatch in the sent and received payloads, but the CRC matches! I've verified configs. of both the ESPs, and they're the same.

And even if the payload is set to random right now, it remains the same as mentioned above on their resp. sides. Also, when I uncomment the lines 38-40 to change the data being sent, it executes once and then the ESP keeps on going into a panic state and it resets.

What is causing these problems? And how to solve them?

Who is online

Users browsing this forum: davidbetz, Google [Bot] and 49 guests