espressif / arduino-esp32

Arduino core for the ESP32
GNU Lesser General Public License v2.1
13.41k stars 7.37k forks source link

ESPnow sending 7 bytes receiving 28 bytes #1019

Closed GrooverFromHolland closed 6 years ago

GrooverFromHolland commented 6 years ago

Hardware:

Board: DOIT ESP32 Devkit V1 Core Installation/update date: 11 ‎december ‎2017 IDE name: Visualmicro Flash Frequency: 80Mhz Upload Speed: 921600

Description:

I am sending a packet of 7 bytes and 28 bytes are received on the slave. send method:

void sendData(uint8_t datatosend[7])
{
    const uint8_t *data[7];
    Serial.print("Sending: ................");
    for (int i = 0; i < 7; i++)
    {
        data[i] = &datatosend[i];
    }   
    for (int i = 0; i < SlaveCnt; i++)
    {
        const uint8_t *peer_addr = slaves[i].peer_addr;
        if (i == 0)
        { 
            Serial.print("Sending: ");
            for (int i = 0; i < 7; i++)
            {
                Serial.println(*data[i]);
            }           
        }
        esp_err_t result = esp_now_send(peer_addr, *data, sizeof(data));
        Serial.print("Send Status: ");
        if (result == ESP_OK)
        {
            Serial.println("Success");
        }       
        tosend = false;
        clearToSend = false;
        }

void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status)
{
    char macStr[18];
    snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
        mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
    Serial.print("Last Packet Sent to: "); Serial.println(macStr);
    Serial.print("Last Packet Send Status: ");
    Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
}

Serialmonitor output: Received from IR-Controller: 218103821 Sending: ................ 2, 40, 10, 0, 0, 0, 3 Send Status: Success Last Packet Sent to: 30:ae:a4:3c:64:b5 Last Packet Send Status: Delivery Success
Receive method:

void OnDataRecv(const uint8_t *mac_addr, const uint8_t *data, int data_len)
{
    char macStr[18];
    snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
        mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
    Serial.print("Last Packet Recv from: ");
    Serial.println(macStr);
    Serial.println("Datalength: ");
    Serial.println(data_len);
    Serial.println("Last Packet  Recv total Data: ");
    for (size_t i = 0; i < data_len; i++)
    {
        recBuffer[i] = data[i];
        Serial.print(data[i]);
        Serial.print(", ");
    }
    Serial.println(" ");
    Serial.println("Last Packet Recv Data: ");
    for (size_t i = 0; i < 7; i++)
    {
        recBuffer[i] = data[i];
        Serial.print(data[i]);
        Serial.print(", ");
    }
    Serial.println(" ");
    if ((data[0] == stx) && (data[2] == dle) && (data[6] == etx))
    {
        dataAvailable = true;
        data = 0;
    }
    else
    {
        Serial.println("Packet not valid");
    }
}

Serial monitor output:

ESPNow/Basic/Slave Example AP Config Success. Broadcasting with AP: Slave_1 AP MAC: 30:AE:A4:3C:64:B5 ESPNow Init Success Last Packet Recv from: 24:0a:c4:13:ae:04 Datalength: 28 Last Packet Recv total Data: 2, 40, 10, 0, 0, 0, 3, 63, 0, 0, 0, 0, 128, 4, 254, 63, 0, 0, 0, 0, 224, 35, 254, 63, 0, 0, 0, 0,
Last Packet Recv Data: 2, 40, 10, 0, 0, 0, 3,

As the data sent is in the first 7 bytes my application is working, but why 28 bytes received and what is in the remaining 21 bytes?

LaurentLouf commented 6 years ago

Well, have you tested Serial.println(sizeof(data)) ? You would have realized that it returns 28, which is 7 (the number of elements) times 4 (the size of an element (which is here a pointer on an uint8_t, so 4 bytes)).

GrooverFromHolland commented 6 years ago

@Loufylouf. Thank you, I ditn't know pointer variables are 32 bits wide, regardless of the type. The only thing I have to do is: esp_err_t result = esp_now_send(peer_addr, *data, 7); in my send method.