Not Receiving Correct ESPNOW Payload Values

physiii
Posts: 23
Joined: Fri Nov 17, 2017 9:37 pm

Not Receiving Correct ESPNOW Payload Values

Postby physiii » Sun Oct 04, 2020 10:00 pm

Hello, I am using the current master branch version of espnow and trying to transmit and receive a payload.

However when I print the value I am receiving on the second esp32 in the example_espnow_data_parse function like

Code: Select all

printf("PAYLOAD: %u\n", buf->payload[0]);
I get a repeating value of 251. I am assume esp_fill_random should put random values in buf->payload instead of 251 every time (except when it first connects).

Here is what I am receiving:

Code: Select all

PAYLOAD: 251
I (21815) espnow_example: Receive 0th broadcast data from: 30:ae:a4:8d:ee:2c, len: 10
I (22815) espnow_example: send data to ff:ff:ff:ff:ff:ff
PAYLOAD: 251
I (22815) espnow_example: Receive 1th broadcast data from: 30:ae:a4:8d:ee:2c, len: 10
I (23815) espnow_example: send data to ff:ff:ff:ff:ff:ff
PAYLOAD: 251
I (23815) espnow_example: Receive 2th broadcast data from: 30:ae:a4:8d:ee:2c, len: 10
PAYLOAD: 64
I (23815) espnow_example: Receive 0th unicast data from: 30:ae:a4:8d:ee:2c, len: 10
PAYLOAD: 251
I (24735) espnow_example: Receive 1th unicast data from: 30:ae:a4:8d:ee:2c, len: 10
PAYLOAD: 251
I (25735) espnow_example: Receive 2th unicast data from: 30:ae:a4:8d:ee:2c, len: 10
If I add this to example_espnow_data_prepare and comment out esp_fill_random

Code: Select all

buf->payload[0] = 42;
I still see the same results as above with just getting a value of 251.

How do I pull values from the uint8_t payload array?

physiii
Posts: 23
Joined: Fri Nov 17, 2017 9:37 pm

Re: Not Receiving Correct ESPNOW Payload Values

Postby physiii » Tue Oct 06, 2020 10:32 am

This seems like the most basic thing someone would want to do; send a payload using ESPNOW. I did this over a year ago but can't get it to work now.

Can anyone spot the bug?

ESP_Sprite
Posts: 9730
Joined: Thu Nov 26, 2015 4:08 am

Re: Not Receiving Correct ESPNOW Payload Values

Postby ESP_Sprite » Tue Oct 06, 2020 2:33 pm

Hard to say... those are the only 2 lines you changed? Where exactly did you insert them into the code?

physiii
Posts: 23
Joined: Fri Nov 17, 2017 9:37 pm

Re: Not Receiving Correct ESPNOW Payload Values

Postby physiii » Tue Oct 06, 2020 3:35 pm

Yes, those are the only two lines that I added. I put them in example_espnow_data_parse and example_espnow_data_prepare functions. See line 120 and line 143.

Code: Select all

/* Parse received ESPNOW data. */
int example_espnow_data_parse(uint8_t *data, uint16_t data_len, uint8_t *state, uint16_t *seq, int *magic)
{
    example_espnow_data_t *buf = (example_espnow_data_t *)data;
    uint16_t crc, crc_cal = 0;

    if (data_len < sizeof(example_espnow_data_t)) {
        ESP_LOGE(TAG, "Receive ESPNOW data too short, len:%d", data_len);
        return -1;
    }

    *state = buf->state;
    *seq = buf->seq_num;
    *magic = buf->magic;
    crc = buf->crc;
    buf->crc = 0;
    crc_cal = esp_crc16_le(UINT16_MAX, (uint8_t const *)buf, data_len);
    printf("PAYLOAD: %u\n", buf->payload[0]);

    if (crc_cal == crc) {
        return buf->type;
    }

    return -1;
}

/* Prepare ESPNOW data to be sent. */
void example_espnow_data_prepare(example_espnow_send_param_t *send_param)
{
    example_espnow_data_t *buf = (example_espnow_data_t *)send_param->buffer;

    assert(send_param->len >= sizeof(example_espnow_data_t));

    buf->type = IS_BROADCAST_ADDR(send_param->dest_mac) ? EXAMPLE_ESPNOW_DATA_BROADCAST : EXAMPLE_ESPNOW_DATA_UNICAST;
    buf->state = send_param->state;
    buf->seq_num = s_example_espnow_seq[buf->type]++;
    buf->crc = 0;
    buf->magic = send_param->magic;
    /* Fill all remaining bytes after the data with random values */
    // esp_fill_random(buf->payload, send_param->len - sizeof(example_espnow_data_t));
    buf->payload[0] = 42;
    buf->crc = esp_crc16_le(UINT16_MAX, (uint8_t const *)buf, send_param->len);
}

physiii
Posts: 23
Joined: Fri Nov 17, 2017 9:37 pm

Re: Not Receiving Correct ESPNOW Payload Values

Postby physiii » Wed Oct 07, 2020 5:58 pm

Apparently Send len in the menuconfig refers to the entire struct and not just the payload. So I figured 10 bytes was enough but I had to increase that to account for the payload.

I do not understand why they would have the Send len start at 10 bytes. If I want to send 3 bytes I need to set that to 13. Why not just say that is a Send len of 3?

Who is online

Users browsing this forum: Gaston1980 and 120 guests