ESP NOW from ESP8266 to ESP32

Hi all,
I’m sending a long variable from an ESP8266(TX/master) to an ESP32(RX/slave).

this is the TX code:

#include <ESP8266WiFi.h>
extern "C" {
#include <espnow.h>
}

#define WIFI_CHANNEL 1

// must match the slave struct
struct __attribute__((packed)) DataStruct {
  char text[32];
  unsigned long time;
};

DataStruct myData;

unsigned long lastSentMillis;
unsigned long sendIntervalMillis = 10;
unsigned long sentMicros;
unsigned long ackMicros;


//MAC ADDRESS OF THE DEVICE YOU ARE SENDING TO
byte remoteMac[] = {0x80, 0x7D, 0x3A, 0xDC, 0xB3, 0x95};

const byte dataLength = 7;
byte cnt = 0;
byte rcvData[dataLength];
byte result;

#include <NewPingESP8266.h>
#define TRIGGER_PIN  D5  // 14 Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     D6  // 12 Arduino pin tied to echo pin on the ultrasonic sensor.

NewPingESP8266 sonar(TRIGGER_PIN, ECHO_PIN);

long echoTime;

void setup()
{
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  WiFi.begin();
  Serial.print("\r\n\r\nDevice MAC: ");
  Serial.println(WiFi.macAddress());
  Serial.println("\r\nESP_Now Controller.");
  esp_now_init();
  delay(10);
  esp_now_set_self_role(ESP_NOW_ROLE_CONTROLLER);
  esp_now_add_peer(remoteMac, ESP_NOW_ROLE_COMBO, WIFI_CHANNEL, NULL, 0);
}

void loop()
{
  echoTime = (sonar.ping_median(10));
  sendData();
}



void sendData() {
  if (millis() - lastSentMillis >= sendIntervalMillis) {
    lastSentMillis += sendIntervalMillis;
    myData.time = echoTime;
    uint8_t bs[sizeof(myData)];
    memcpy(bs, &myData, sizeof(myData));
    sentMicros = micros();
    esp_now_send(NULL, bs, sizeof(myData)); // NULL means send to all peers
    Serial.println(echoTime);
    Serial.println("sent data");
    Serial.println();
  }
}

and this is TX serial monitor output:

Device MAC: 60:01:94:29:DC:D7

ESP_Now Controller.
7117
sent data

7241
sent data

7316
sent data

this is the RX code:

#include <esp_now.h>
#include <WiFi.h>
#include <M5Stack.h>

#define WIFI_CHANNEL 1
uint8_t localCustomMac[] = {0x36, 0x33, 0x33, 0x33, 0x33, 0x33};
const byte maxDataFrameSize = 200;


// must match the controller struct
struct __attribute__((packed)) DataStruct {
  char text[32];
  unsigned int time;
};

DataStruct myData;



void setup()
{
  Serial.begin(115200);

  M5.begin();

  Serial.print("\r\n\r\n");
  WiFi.mode(WIFI_AP);
  Serial.println( WiFi.softAPmacAddress() );
  WiFi.disconnect();
  if (esp_now_init() == ESP_OK)
  {
    Serial.println("ESPNow Init Success!");
  }
  else
  {
    Serial.println("ESPNow Init Failed....");
  }
  esp_now_register_recv_cb(OnDataRecv);
}

void loop()
{
  yield();
}

void OnDataRecv(const uint8_t *mac_addr, const uint8_t *incomingData, int data_len)

{

  memcpy(&myData, incomingData, sizeof(myData));
  int cm = myData.time / 29 / 2;
  int inc = myData.time / 74 / 2;
  Serial.print("NewMsg ");
  Serial.print("MacAddr ");
  for (byte n = 0; n < 6; n++) {
    Serial.print (mac_addr[n], HEX);
  }
  Serial.print("  MsgLen ");
  Serial.print(data_len);
  Serial.print("  Text ");
  Serial.print(myData.text);
  Serial.print("  Time ");
  Serial.print(myData.time);
  Serial.print("  CM = ");
  Serial.print(cm);
  Serial.print("   IN = ");
  Serial.print(inc);
  Serial.println();
}

… and his serial monitor output:

M5Stack initializing...OK


80:7D:3A:DC:B3:95
ESPNow Init Success!
NewMsg MacAddr 6019429DCD7  MsgLen 36  Text   Time 793  CM = 13   IN = 5
NewMsg MacAddr 6019429DCD7  MsgLen 36  Text   Time 841  CM = 14   IN = 5
NewMsg MacAddr 6019429DCD7  MsgLen 36  Text   Time 866  CM = 14   IN = 5
NewMsg MacAddr 6019429DCD7  MsgLen 36  Text   Time 818  CM = 14   IN = 5

it’s working for me, but I would like to know if:

can I reduce sent packets dimensions?
transmission could be faster?

thanks a lot

can I reduce sent packets dimensions?
transmission could be faster?

 char text[32];

Why do you send 32 bytes of nothing?

The maximum length of data must be less than ESP_NOW_MAX_DATA_LEN - so 250 bytes - but you can make it as small as you need (but may be still the full frame of 255 bytes is sent anyway - it's unclear in the doc)