ESPNOW send data to different MAC adress

Hi, i wish to use my ESP8266 to send data at two different esp board..
I choose the board writing on Serial monitor "A" or "B"..
I've tried to do that using this sketch

#include <ESP8266WiFi.h>
#include <espnow.h>


// here are the MAC adress
uint8_t broadcastAddressA[]={0x24, 0x6F, 0x28, 0x97, 0x42, 0x80};
uint8_t broadcastAddressB[]={0xfc, 0xf5, 0xc4, 0x65, 0x13, 0x5c};
uint8_t broadcastAddress[6]=;

// Structure example to receive data
// Must match the sender structure
typedef struct struct_message {
    char a[85];
    boolean tratt;
} struct_message;


// Create a struct_message called myData
struct_message myDataSend;

// Callback when data is sent
void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) {
  Serial.print("Last Packet Send Status: ");
  if (sendStatus == 0){
    Serial.println("Delivery success");
  }
  else{
    Serial.println("Delivery fail");
  }
}

 
void setup() {
  // Initialize Serial Monitor
  Serial.begin(115200);
  
  // Set device as a Wi-Fi Station
  WiFi.mode(WIFI_STA);

  // Init ESP-NOW
  if (esp_now_init() != 0) {
    Serial.println("Error initializing ESP-NOW");
    return;
  }

//i've setted to COMBO couse i need to use it also for datarecive
  esp_now_set_self_role(ESP_NOW_ROLE_COMBO);
  
  
}
 
void loop() {
   Serial.println("INSERT LETTER");
   int incomingByte=0;
   if (Serial.available() > 0) {    
   incomingByte = Serial.read(); // read the incoming byte:
   Serial.println("INCOMMING BYTE= ");
   Serial.println(incomingByte);
   if (incomingByte == 65){ //in ASCII 65 = A maiuscolo
    memcpy(broadcastAddressA, broadcastAddress, sizeof broadcastAddressA);
    richiestatrattamento();
   }
   if (incomingByte == 66){ //in ASCII 66 = B maiuscolo
   memcpy(broadcastAddressB, broadcastAddress, sizeof broadcastAddressB);
   richiestatrattamento();
   }
   //Serial.println((broadcastAddress, (uint8_t *));  
}
delay(10000);
}

void richiestatrattamento() {  
  // Register peer        
  esp_now_add_peer(broadcastAddress, ESP_NOW_ROLE_SLAVE, 1, NULL, 0);
  esp_now_register_send_cb(OnDataSent);
 
  myDataSend.tratt= true;
  // Send message via ESP-NOW
  esp_now_send(broadcastAddress, (uint8_t *) &myDataSend, sizeof(myDataSend));
  delay(1000);
}

I have two problems.. first this code

memcpy(broadcastAddressA, broadcastAddress, sizeof broadcastAddressA);

for copy MAC adress doesent work :frowning:
How can i copy the right adress in to the variable?

And second i don't know why after reading the letter it gives me and print the right result
it gives me another value read that i didn't send..

19:29:42.435 -> INSERT LETTER
19:29:42.435 -> INCOMMING BYTE= 
19:29:42.473 -> 65
19:29:52.424 -> INSERT LETTER
19:29:52.424 -> INCOMMING BYTE= 
19:29:52.462 -> 10
19:30:02.439 -> INSERT LETTER
19:30:12.467 -> INSERT LETTER

Why?? I've checked on ascii table and it's a wired character...

Thank you very much

memcpy(broadcastAddressA, broadcastAddress, sizeof broadcastAddressA);

That is copying broadcastAddress to broadcastAddressA using the length of broadcastAddressA to set the number of bytes copied.

Is that what you intended to do ? From the names of the variables it sounds the wrong way round to me

Hi Iteo85,

why would you like to do copying the macadress?

you can simply use the macadress-variable
for sending I use

//I define arrays of byte in the same way as you do
uint8_t ESP_NOW_MAC_adrOfRecv[] = {0x18, 0xFE, 0x34, 0xCE, 0xD2, 0xBB};

// add_peer with the variablename itself
esp_now_add_peer(ESP_NOW_MAC_adrOfRecv, ESP_NOW_ROLE_COMBO, 1, NULL, 0);

// and then use this variable in the call of the esp_now_send-function
esp_now_send(ESP_NOW_MAC_adrOfRecv, (uint8_t *) &myESP_NOW_Data, sizeof(myESP_NOW_Data));

So if you would wrap your if-statements around two calls for esp_now_send-commands you could use
the variables directly

or if you would like to keep the memcpy

memcpy(destination, source, sizeof (source) );

//sizeof is a function itself and need the argument in brackets ()
memcpy(broadcastAddress, broadcastAddress, sizeof (broadcastAddressA)  );

I would use variable-names that differ fundamentaly from each-other not only in the trailing last letter

the word "boardcast" means send all over (like a whatsApp-broad-cast to multiple persons)

A good description of the meaning of the macadress here is "mac-adress-of-the-receiving-unit"
So if these boards have different functions I would rename the variables from

uint8_t broadcastAddressA[]={0x24, 0x6F, 0x28, 0x97, 0x42, 0x80};[color=#222222][/color]
uint8_t broadcastAddressB[]={0xfc, 0xf5, 0xc4, 0x65, 0x13, 0x5c};

let's assume your board "A" has the the function "OpenDoor"
your board "B" has the function "SwitchLight"

I would name the variables for the mac-adresses

uint8_t OpenDoor_MAC_Addr[]   ={0x24, 0x6F, 0x28, 0x97, 0x42, 0x80};[color=#222222][/color]
uint8_t SwitchLight_MAC_Addr[]={0xfc, 0xf5, 0xc4, 0x65, 0x13, 0x5c};

if you write
broadcastAddressA/B[] your brain has to do the additional "work" of translating "A" is "OpenDoor"
and "B" is "SwitchLight"

Taking time to think about well chosen self-explaining names
saves time everytime you have to re-read your code

best regards

Stefan

UKHeliBob:

memcpy(broadcastAddressA, broadcastAddress, sizeof broadcastAddressA);

That is copying broadcastAddress to broadcastAddressA using the length of broadcastAddressA to set the number of bytes copied.

Is that what you intended to do ? From the names of the variables it sounds the wrong way round to me

Yes! thank you i've wrong the order..now it works.. it only remains the problem with serial.read..why does it read "INCOMMING BYTE= 10" without any imput after read an imput ??

StefanL38:
Hi Iteo85,

why would you like to do copying the macadress?

you can simply use the macadress-variable

In this way i should have just a function for send esp-now message and i need just to change the adress.. in my project all espnow will do the same thing i just choose wich esp now do the thing so i think the name will be "arnia1" "arnia2" ecc :wink: (arnia in italian means hive :wink: ) and i hope some days it will be 10 esp doing the job ;).

thank you so much! Please help me with serial reading :)!

why does it read "INCOMMING BYTE= 10" without any imput after read an imput ??

Receiving a value of 10 seems to me to indicate that a linefeed was received. What have you got the Line ending set to in the Serial monitor ?

there is a variant of the serialread-command

Serial.readStringUntil('\n');

This means read from serial until the character in the brackets is received as indicator for characterstransmission completetd.
This readStringuntil has a timeout standard-timeout is 1000 millicseconds
you can change this with

Serial.setTimeout(2000);

readStringuntil can read multiple bytes so it must be assigned to a "multibyte-variable = array
You should NOT assign it to a variable of type String. Strings eat up memory over time and cause the code to crash
when the repeated string-assigments start to overwrite your code.

So you better use array of char or the library "PString"

by the way what are your ESPs doing. Simply send to one receiver? or send and receive all cross over to all?

best regards

Stefan