Transferring Values between Esps Using esp_now issue.....second Esp cannot receive the data

engkhaledeisa
Posts: 14
Joined: Mon Feb 26, 2024 10:21 am

Transferring Values between Esps Using esp_now issue.....second Esp cannot receive the data

Postby engkhaledeisa » Fri Sep 20, 2024 11:50 am

hi ....how are you ....my project is agility light ....and I stuck in code that I use espnow function to transfer the needed value from esp to another but the other esp cannot receive the data

I cut these piece of code from the main one as the main one is really big and I put the pieces that relate to the transferring part

OF COURSE you will find missing parts in these pieces like but be sure the missing variable or function are already declared and defined ....that is the main code if any one want to help

https://github.com/projectswithalex/Rea ... ing-Module

.just stay with and see with me the flow of logic or states to see why?

So why the packetAlone didnot transferred ? the size of the struct like you see is 2 ...but when this value transferred to datalength unfortunetly case 2 didnot called?

the other point I doubt is the receiver address as in the main code there are different packet of data with different name but itis already send when the the place of the receiver_addresss has a vlaue of NULL ?

the both pieces have the same init functions

[Codebox]

Code: Select all

#define NEWTRAININGMAXTIME 4
#define MY_ROLE ESP_NOW_ROLE_COMBO        // set the role of this device: CONTROLLER, SLAVE, COMBO
/ #define MY_ECU 1 // and MY_ECU= 2 in the second part
#define RECEIVER_ROLE ESP_NOW_ROLE_COMBO  // set the role of the receiver
/*replaceValueHere*    //ECU number
#define WIFI_CHANNEL 1
#define MACADDRESSSIZE 6                       //Mac address size
#define NO_ECU 0                               //No ecu with the define MY_ECU 0
#define RGBCLEARDELAY 100                      //delay to be used with RGB clear ?TBD
  /*replaceValueHere*/ #define AVAILABLEECU 4  //Nr of ECUs to be used
#define MAXAVAILABLEECU 10 
     struct __attribute__((packed)) dataPacketAlone {
  uint8_t LED_Token;  // Token for activating ECUs
  uint8_t counterExerciseData;
};
dataPacketAlone packetAlone = { 1, 0 };
 

uint8_t receiverAddress1[] = { 0xF4, 0xCF, 0xA2, 0x5D, 0x75, 0x28 };    
uint8_t receiverAddress2[] = {0xAC,0x0B,0xFB,0xDA,0xE0,0x11};
uint8_t receiverECU_Address[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };  //Placeholder for the receiver address

uint8_t receiverArray[MAXAVAILABLEECU][MACADDRESSSIZE];
 

#define MAXAVAILABLEECU 10   



void initReceiverAddress(void) {

  switch (training_NrOfEcus) {

    case 2:

      memcpy(&receiverArray[1], receiverAddress1, 6);
      esp_now_add_peer(receiverAddress1, RECEIVER_ROLE, WIFI_CHANNEL, NULL, 0);
      break;

    case 3:

      memcpy(&receiverArray[1], receiverAddress1, 6);
      memcpy(&receiverArray[2], receiverAddress2, 6);
      esp_now_add_peer(receiverAddress1, RECEIVER_ROLE, WIFI_CHANNEL, NULL, 0);
      esp_now_add_peer(receiverAddress2, RECEIVER_ROLE, WIFI_CHANNEL, NULL, 0);
      break;


}
 
 
 
 
void initESPNOWcomm(void) {
  WiFi.mode(WIFI_STA);
  WiFi.disconnect();  // we do not want to connect to a WiFi network

  if (esp_now_init() != 0) {
    Serial.println("ESP-NOW initialization failed");
    return;
  }

  Serial.print("ESP Board MAC Address:  ");
  Serial.println(WiFi.macAddress());

  esp_now_set_self_role(MY_ROLE);
  esp_now_register_send_cb(transmissionComplete);  
  esp_now_register_recv_cb(dataReceived);        

 
  esp_now_add_peer(receiverAddress1, RECEIVER_ROLE, WIFI_CHANNEL, NULL, 0);  

esp_now_add_peer(receiverAddress2, RECEIVER_ROLE, WIFI_CHANNEL, NULL, 0);   memcpy(&receiverArray[2], receiverAddress2, 6);

memcpy(&receiverArray[1], receiverAddress1, 6);
    Serial.println("initESPNOWcomm");
}
 
uint8_t randomECUselect(void) {

  randomSeed(millis());
  uint8_t returnValue = 0;
  uint8_t randomNumber = 0;
  while (returnValue == 0) {
    randomNumber = random(0, training_NrOfEcus + 2);  //we have +2 because 1 is master and the function is exclusive

    if ((randomNumber != MY_ECU) && (randomNumber != NO_ECU)) {
      returnValue = randomNumber;
    }
  }
    Serial.println("randomECUselect");
    delay(500);
  return returnValue;
}
 
 
 
void selectECU_number(uint8_t ECU) {
  memcpy(&receiverECU_Address, receiverArray[ECU], MACADDRESSSIZE);
  packetAlone.LED_Token = ECU;
  TransmisionStatus = SENDDATA_en;
  Serial.print("selectECU_number");
 
          delay(500);
}
[/Codebox]



in the first piece


Code: Select all

[Codebox]     randomECUSelection = randomECUselect();
selectECU_number(randomECUSelection);
        esp_now_send(receiverECU_Address, (uint8_t *)&packetAlone, sizeof(packetAlone));[/Codebox]

In the second piece

Code: Select all

[Codebox]void dataReceived(uint8_t *senderMac, uint8_t *data, uint8_t dataLength) {
  char macStr[18];
  snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x", senderMac[0], senderMac[1], senderMac[2], senderMac[3], senderMac[4], senderMac[5]);
 
  Serial.println();
  Serial.print("Received data from: ");
  Serial.println(macStr);
  switch (dataLength) {
    case 2:
      Serial.println(" case 2 ");
      memcpy(&packetAlone, data, sizeof(packetAlone));
      if(packetSettings.training_trainingType==TRAINING_TIMERMODE && packetAlone.LED_Token==MY_ECU) {
        timer1_write(randomTimerInterval());
      }
      Serial.print(packetAlone.counterExerciseData);
            Serial.print(packetAlone.LED_Token);
      break;

  TransmisionStatus = DATARECEIVED_en;
}



[/Codebox]

Who is online

Users browsing this forum: No registered users and 10 guests