Issue with esp now and delaying data send/received

2 Arduino sketches, one sends a number to the second sketch and I want it to turn on a relay for a specified time and then turn it off. My problem is the sending sketch keeps sending the same number so the relay never turns off

Send sketch

#include "DFRobot_DF2301Q.h"
#include <esp_now.h>
#include <WiFi.h>
uint8_t broadcastAddress[] = { 0x14, 0x08, 0x08, 0xa5, 0xd7, 0x68 };
DFRobot_DF2301Q_UART DF2301Q(/*hardSerial =*/&Serial1, /*rx =*/D7, /*tx =*/D6);
#define Rgt 1
#define Lft 0
int Rcnt = 0;
int RbuttonPin = 10;
int RbuttonPushCounter = 0;  // counter for the number of button presses
int RbuttonState = 0;        // current state of the button
int RlastButtonState = 0;

int Lcnt = 0;
int LbuttonPin = 9;
int LbuttonPushCounter = 0;  // counter for the number of button presses
int LbuttonState = 0;        // current state of the button
int LlastButtonState = 0;

int buttonPin = 4;  // Pin connected to the push button

typedef struct struct_message {
int b;
} struct_message;
struct_message myData;
esp_now_peer_info_t peerInfo;
DFRobot_DF2301Q_I2C asr;
void OnDataSent(const esp_now_send_info_t *tx_info, esp_now_send_status_t status) {
if (status == ESP_NOW_SEND_SUCCESS) {
Serial.println("Send Success");
} else {
Serial.println("Send Fail");
}
///////////

////////////////
}

void setup() {
Serial.begin(115200);
myData.b = 6;
WiFi.mode(WIFI_STA);
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
esp_now_register_send_cb(OnDataSent);
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
Serial.println("Failed to add peer");
return;
}
pinMode(buttonPin, INPUT);
pinMode(Rgt, OUTPUT);
pinMode(Lft, OUTPUT);
while (!(asr.begin())) {
Serial.println("Communication with device failed, please check connection");
delay(3000);
}
Serial.println("Begin ok!");
asr.setVolume(7);
asr.setMuteMode(0);
asr.setWakeTime(255);

uint8_t wakeTime = 0;
wakeTime = asr.getWakeTime();
Serial.print("wakeTime 4= ");
Serial.println(wakeTime);

// asr.playByCMDID(1);   // Wake-up command

/**
@brief Play the corresponding reply audio according to the ID

 CMDID - command word ID
*/

////////////////////
pinMode(RbuttonPin, INPUT);
pinMode(LbuttonPin, INPUT);
//////////////////////////////
}

void loop() {
int buttonState = digitalRead(buttonPin);

// If the button is pressed
if (buttonState == LOW) {
asr.playByCMDID(1);  // Trigger the specific command/wake function (ID 1 as an example)
delay(500);          // Debounce to prevent multiple triggers
}
//////////////////////////////////
RbuttonState = digitalRead(RbuttonPin);
if (RbuttonState != RlastButtonState) {
RbuttonPushCounter++;
}
if (RbuttonPushCounter >= 2) {
RbuttonPushCounter = 0;
digitalWrite(Rgt, LOW);
}
LbuttonState = digitalRead(LbuttonPin);
if (LbuttonState != LlastButtonState) {
LbuttonPushCounter++;
}
if (LbuttonPushCounter >= 2) {
LbuttonPushCounter = 0;
digitalWrite(Lft, LOW);
}
//////////////////////////////////
uint8_t CMDID = 0;
CMDID = DF2301Q.getCMDID();
if (0 != CMDID) {
Serial.print("CMDID = ");
Serial.println(CMDID);
if (CMDID == 5) {
digitalWrite(Rgt, HIGH);  // Turn Relay ON
digitalWrite(Lft, LOW);
Serial.println("Relay Activated");
//delay(1200);
}
if (CMDID == 6) {
digitalWrite(Lft, HIGH);  // Turn Relay ON
Serial.println("Relay Activated");
digitalWrite(Rgt, LOW);
}
if (CMDID == 7) {
digitalWrite(Rgt, LOW);  // Turn Relay OFF
digitalWrite(Lft, LOW);
Serial.println("Relay Deactivated");
}
delay(500);
(CMDID == 0);
//GREEN = 8, WHITE = 9, RED = 10, CRUISE ON = 11, CRUISE SET = 12, ADAPT ON = 13, NEAR = 14, FAR = 15
if ( CMDID == 8 || CMDID == 9 || CMDID == 10 || CMDID == 11 || CMDID == 12 || CMDID == 13 || CMDID == 14 || CMDID == 15 || CMDID == 16) {
(myData.b = CMDID);

  Serial.println(myData.b);
  uint8_t data\[sizeof(myData)\];
  memcpy(data, &myData, sizeof(myData));
  esp_err_t result = esp_now_send(broadcastAddress, (uint8_t \*)&myData, sizeof(myData));
  //       esp_now_send(peerAddress, data, sizeof(data));
  if (result == ESP_OK) {
    Serial.println("Sent with success");
  } else {
    Serial.println("Error sending the data");
  }
}
delay(1000);

}
}

Receiving code

#include <esp_now.h>
#include <WiFi.h>
const int RelGrn = 36; //*Yellow x
const int RelWht = 16; //*Brown x
const int RelRed = 37; //*Blue x
const int RelCrOn = 38; //*Purple x
const int RelCrSet = 39; //*Gray x
const int RelTrl = 32;//*White
const int RelCls = 33; //*Oange
const int RelFar = 34; //*Green
// Structure exampl to receive data
// Must match the sender structure
typedef struct struct_message {

int b;

} struct_message;

// Create a struct_message called myData
struct_message myData;

// callback function that will be executed when data is received
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
memcpy(&myData, incomingData, sizeof(myData));

Serial.print("Int: ");
Serial.println(myData.b);

}
void setup() {
Serial.begin(115200);
pinMode(RelGrn, OUTPUT);
pinMode(RelWht, OUTPUT);
pinMode(RelRed, OUTPUT);
pinMode(RelCrOn, OUTPUT);
pinMode(RelCrSet, OUTPUT);
pinMode(RelTrl, OUTPUT);
pinMode(RelCls, OUTPUT);
pinMode(RelFar, OUTPUT);

digitalWrite(RelGrn, LOW);
digitalWrite(RelWht, LOW);
digitalWrite(RelRed, LOW);
digitalWrite(RelCrOn, LOW);
digitalWrite(RelCrSet, LOW);
digitalWrite(RelTrl, LOW);
digitalWrite(RelCls, LOW);
digitalWrite(RelFar, LOW);

// Set device as a Wi-Fi Station
WiFi.mode(WIFI_STA);

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

// Once ESPNow is successfully Init, we will register for recv CB to
// get recv packer info
esp_now_register_recv_cb(esp_now_recv_cb_t(OnDataRecv));

}

void loop() {
if (myData.b == 8) {
digitalWrite(RelGrn, HIGH);
delay(2000);
digitalWrite(RelGrn, LOW);
} else if (myData.b == 9) {
digitalWrite(RelWht, HIGH);
delay(2000);
digitalWrite(RelWht, LOW);
} else if (myData.b == 10) {
digitalWrite(RelRed, HIGH);
delay(2000);
digitalWrite(RelRed, LOW);
} else if (myData.b == 11) {
digitalWrite(RelCrOn, HIGH);
delay(2000);
digitalWrite(RelCrOn, LOW);
} else if (myData.b == 12) {
digitalWrite(RelCrSet, HIGH);
delay(2000);
digitalWrite(RelCrSet, LOW);
} else if (myData.b == 13) {
digitalWrite(RelTrl, HIGH);
delay(2000);
digitalWrite(RelTrl, LOW);
} else if (myData.b == 14) {
digitalWrite(RelCls, HIGH);
delay(2000);
digitalWrite(RelCls, LOW);
} else if (myData.b == 15) {
digitalWrite(RelFar, HIGH);
delay(2000);
digitalWrite(RelFar, LOW);
}
}
        (CMDID == 0);

What do you think this line of code does ?

i wanted to fail all the ifs on the rec side and it would allow the prior sent number to full execute and then the rec code would wait for a valid number

What do you want the value of CMDID to be after that line of code ?

Have you tried printing it to see whether its value is what you want ?

I just did, looks like it has no impact. Commented it out no change

What do you want the value of CMDID to be after that line ?

(CMDID == 0);

This compares CMDID to 0. Since that operation has no effect on anything, the optimizer probably eliminates the line. Use = to set CMDID to zero.

I was hoping to lead @cdb0ewm to that conclusion under his/her own steam rather than giving the game away, but never mind

thank you both

I assume that you understand the problem and have fixed it

yes but still have another issue - been working for 3 day

sendworks_copy_20260730182332.ino (4.4 KB)

When CMDID = 5 I want to check if a button was not pressed twice and CMDID is not 7. If thats ok then I want cycle Rgt on and off until the button is pressed twice or CMDID is 7. I can't get the on /off working nor can I get CMDID to acknowledge 7 in this loop

Please post your full sketch here, using code tags

#include "DFRobot_DF2301Q.h"
#include <esp_now.h>
#include <WiFi.h>
uint8_t broadcastAddress[] = { 0x14, 0x08, 0x08, 0xa5, 0xd7, 0x68 };
DFRobot_DF2301Q_UART DF2301Q(/*hardSerial =*/&Serial1, /*rx =*/D7, /*tx =*/D6);
#define Rgt D0
#define Lft D1

int RbuttonPin = 10;
int RbuttonPushCounter = 0;  // counter for the number of button presses
int RbuttonState = 0;        // current state of the button
int RlastButtonState = 0;

int LbuttonPin = 9;
int LbuttonPushCounter = 0;  // counter for the number of button presses
int LbuttonState = 0;        // current state of the button
int LlastButtonState = 0;

int buttonPin = 4;  // Pin connected to the push button

typedef struct struct_message {
  int b;
} struct_message;
struct_message myData;
esp_now_peer_info_t peerInfo;
DFRobot_DF2301Q_I2C asr;
void OnDataSent(const esp_now_send_info_t *tx_info, esp_now_send_status_t status) {
  if (status == ESP_NOW_SEND_SUCCESS) {
    Serial.println("Send Success");
  } else {
    Serial.println("Send Fail");
  }
}

void setup() {
  Serial.begin(115200);
  myData.b = 0;
  WiFi.mode(WIFI_STA);
  if (esp_now_init() != ESP_OK) {
    Serial.println("Error initializing ESP-NOW");
    return;
  }
  esp_now_register_send_cb(OnDataSent);
  memcpy(peerInfo.peer_addr, broadcastAddress, 6);
  peerInfo.channel = 0;
  peerInfo.encrypt = false;
  if (esp_now_add_peer(&peerInfo) != ESP_OK) {
    Serial.println("Failed to add peer");
    return;
  }
  pinMode(buttonPin, INPUT);
  pinMode(Rgt, OUTPUT);
  pinMode(Lft, OUTPUT);
  while (!(asr.begin())) {
    Serial.println("Communication with device failed, please check connection");
    delay(3000);
  }
  Serial.println("Begin ok!");
  asr.setVolume(7);
  asr.setMuteMode(0);
  asr.setWakeTime(255);

  uint8_t wakeTime = 0;
  wakeTime = asr.getWakeTime();
  Serial.print("wakeTime 4= ");
  Serial.println(wakeTime);

  // asr.playByCMDID(1);   // Wake-up command

  /**
     @brief Play the corresponding reply audio according to the ID
     @param CMDID - command word ID
  */

  ////////////////////
  pinMode(RbuttonPin, INPUT);
  pinMode(LbuttonPin, INPUT);
  //////////////////////////////
}

void loop() {
  int buttonState = digitalRead(buttonPin);

  // If the button is pressed
  if (buttonState == LOW) {
    asr.playByCMDID(1);  // Trigger the specific command/wake function (ID 1 as an example)
    delay(500);          // Debounce to prevent multiple triggers
  }
  //////////////////////////////////
  RbuttonState = digitalRead(RbuttonPin);
  if (RbuttonState != RlastButtonState) {
    RbuttonPushCounter++;
  }
  if (RbuttonPushCounter >= 2) {
    RbuttonPushCounter = 0;
    digitalWrite(Rgt, LOW);
  }
  LbuttonState = digitalRead(LbuttonPin);
  if (LbuttonState != LlastButtonState) {
    LbuttonPushCounter++;
  }
  if (LbuttonPushCounter >= 2) {
  }
  //////////////////////////////////
  uint8_t CMDID = 0;
  CMDID = DF2301Q.getCMDID();
  Serial.print("CMDID = ");
  Serial.println(CMDID);
  if (CMDID == 5) {
    digitalWrite(Lft, LOW);
  while ((RbuttonPushCounter < 2) && (CMDID != 7)) {
      digitalWrite(Rgt, HIGH);
      delay(1000);
      digitalWrite(Rgt, LOW);
      if ((RbuttonPushCounter >= 2) || (CMDID == 7)) {
        break;
      }
      RbuttonPushCounter = 0;
      digitalWrite(Rgt, LOW);
  }
  }
   if (CMDID == 6) {
    digitalWrite(Rgt, LOW);
    while ((LbuttonPushCounter < 2) && (CMDID != 7)) {
      digitalWrite(Lft, HIGH);
      delay(1000);
      digitalWrite(Lft, LOW);
      if ((LbuttonPushCounter >= 2) || (CMDID == 7)) {
        break;
      }
      LbuttonPushCounter = 0;
      digitalWrite(Lft, LOW);
    }
  }
  if (CMDID == 7) {
    digitalWrite(Rgt, LOW);  // Turn Relay OFF
    digitalWrite(Lft, LOW);
    Serial.println("Relay Deactivated");
  }
  delay(500);
  //GREEN = 8, WHITE = 9, RED = 10, CRUISE ON = 11, CRUISE SET = 12, ADAPT ON = 13, NEAR = 14, FAR = 15
  if (CMDID == 0 || CMDID == 8 || CMDID == 9 || CMDID == 10 || CMDID == 11 || CMDID == 12 || CMDID == 13 || CMDID == 14 || CMDID == 15 || CMDID == 16) {
    (myData.b = CMDID);
    (CMDID == 0);
    Serial.println(myData.b);
    uint8_t data[sizeof(myData)];
    memcpy(data, &myData, sizeof(myData));
    esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *)&myData, sizeof(myData));
    //       esp_now_send(peerAddress, data, sizeof(data));
    if (result == ESP_OK) {
      Serial.println("Sent with success");
    } else {
      Serial.println("Error sending the data");
    }
  }
  delay(1000);
}

This is the section of code which is causing your problem

    if (CMDID == 5)
    {
        digitalWrite(Lft, LOW);
        while ((RbuttonPushCounter < 2) && (CMDID != 7))
        {
            digitalWrite(Rgt, HIGH);
            delay(1000);
            digitalWrite(Rgt, LOW);
            if ((RbuttonPushCounter >= 2) || (CMDID == 7))
            {
                break;
            }
            RbuttonPushCounter = 0;
            digitalWrite(Rgt, LOW);
        }
    }

In order to enter it CMDID must equal 5

In it there is a while loop that is only entered if RbuttonPushCounter is smaller than 2 and CMDID is not 7

Once in that while loop the value of CMDID never changes and you repeatedly set RbuttonPushCounter to zero

How will the code ever exit the while loop ?

GOT IT..CHANGED THE CODE TO STOP SETTING RbuttonPushCounter to 0 but still no looping Rgt HIGH, delay LOW

  if (CMDID == 5) {
    digitalWrite(Lft, LOW);
    while ((RbuttonPushCounter < 2) && (CMDID != 7)) {
      digitalWrite(Rgt, HIGH);
      delay(1000);
      digitalWrite(Rgt, LOW);
      if ((RbuttonPushCounter >= 2) || (CMDID == 7)) {
        break;
      }
    }
  }

How long will Rgt stay LOW before going HIGH again when in the while loop ?

How ill the code ever leave the while loop when the values of RbuttonPushCounter and CMDID do not change inside it ?

Thanks to you questions I got the loop working with the counter - eliminated the CMDID =7 but I can't get it to work if I add it back

  if (CMDID == 5) {
    digitalWrite(Lft, LOW);
    while ((RbuttonPushCounter < 2)) {
      digitalWrite(Rgt, HIGH);
      delay(1000);
      digitalWrite(Rgt, LOW);
      delay(1000);
      RbuttonState = digitalRead(RbuttonPin);
      Serial.print("now ");
      Serial.println(RbuttonState);
      Serial.print("last ");
      Serial.println(RlastButtonState);
      if (RbuttonState != RlastButtonState) {
        RbuttonPushCounter++;
        (RlastButtonState = RbuttonState);
        Serial.print("CNT = ");
        Serial.println(RbuttonPushCounter);
      }
      if ((RbuttonPushCounter >= 2)) {
        RbuttonPushCounter = 0;
        digitalWrite(Rgt, LOW);
        break;
      }
    }
  }

  if (CMDID == 7) {
    digitalWrite(Rgt, LOW);  // Turn Relay OFF
    digitalWrite(Lft, LOW);
    Serial.println("Relay Deactivated");
  }

Are you saying that if you have the code that tests for CMDID equalling 7 in the sketch then the code that tests for CMDID equalling 5 does not work ?

Please clarify that and post the whole sketch as it is now. The structure of your sketch is much clearer if you put each curly bracket on its own line. That way the structure of the sketch is easier to see, like this

if (CMDID == 5)
{
    digitalWrite(Lft, LOW);
    while ((RbuttonPushCounter < 2))
    {
        digitalWrite(Rgt, HIGH);
        delay(1000);
        digitalWrite(Rgt, LOW);
        delay(1000);
        RbuttonState = digitalRead(RbuttonPin);
        Serial.print("now ");
        Serial.println(RbuttonState);
        Serial.print("last ");
        Serial.println(RlastButtonState);
        if (RbuttonState != RlastButtonState)
        {
            RbuttonPushCounter++;
            (RlastButtonState = RbuttonState);
            Serial.print("CNT = ");
            Serial.println(RbuttonPushCounter);
        }
        if ((RbuttonPushCounter >= 2))
        {
            RbuttonPushCounter = 0;
            digitalWrite(Rgt, LOW);
            break;
        }
    }
}

if (CMDID == 7)
{
    digitalWrite(Rgt, LOW);  // Turn Relay OFF
    digitalWrite(Lft, LOW);
    Serial.println("Relay Deactivated");
}

5 works, the issue is using 7 to break the 5 loop.

#include "DFRobot_DF2301Q.h"
#include <esp_now.h>
#include <WiFi.h>
uint8_t broadcastAddress[] = { 0x14, 0x08, 0x08, 0xa5, 0xd7, 0x68 };
DFRobot_DF2301Q_UART DF2301Q(/*hardSerial =*/&Serial1, /*rx =*/D7, /*tx =*/D6);
#define Rgt D0
#define Lft D1

int RbuttonPin = D9;
int RbuttonPushCounter = 0;  // counter for the number of button presses
int RbuttonState = 0;        // current state of the button
int RlastButtonState = 0;

int LbuttonPin = 29;
int LbuttonPushCounter = 0;  // counter for the number of button presses
int LbuttonState = 0;        // current state of the button
int LlastButtonState = 0;

typedef struct struct_message 
{
  int b;
} struct_message;

struct_message myData;
esp_now_peer_info_t peerInfo;
DFRobot_DF2301Q_I2C asr;

void OnDataSent(const esp_now_send_info_t *tx_info, esp_now_send_status_t status) 
{
  if (status == ESP_NOW_SEND_SUCCESS) 
  {
    Serial.println("Send Success");
  } 
  else 
  {
    Serial.println("Send Fail");
  }
}

void setup() 
{
  Serial.begin(115200);
  myData.b = 0;
  WiFi.mode(WIFI_STA);

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

  esp_now_register_send_cb(OnDataSent);
  memcpy(peerInfo.peer_addr, broadcastAddress, 6);
  peerInfo.channel = 0;
  peerInfo.encrypt = false;
  if (esp_now_add_peer(&peerInfo) != ESP_OK)
  {
    Serial.println("Failed to add peer");
    return;
  }
  pinMode(Rgt, OUTPUT);
  pinMode(Lft, OUTPUT);

  while (!(asr.begin()))
  {
    Serial.println("Communication with device failed, please check connection");
    delay(3000);
  }

  Serial.println("Begin ok!");
  asr.setVolume(7);
  asr.setMuteMode(0);
  asr.setWakeTime(255);

  uint8_t wakeTime = 0;
  wakeTime = asr.getWakeTime();
  Serial.print("wakeTime 4= ");
  Serial.println(wakeTime);

  /**
     @brief Play the corresponding reply audio according to the ID
     @param CMDID - command word ID
  */

  pinMode(RbuttonPin, INPUT);
  pinMode(LbuttonPin, INPUT);
  RbuttonPin = HIGH;
  LbuttonPin = HIGH;
}

void loop() 
{
  uint8_t CMDID = 0;
  CMDID = DF2301Q.getCMDID();
  Serial.print("CMDID = ");
  Serial.println(CMDID);
  if (CMDID == 5) 
  {
    digitalWrite(Lft, LOW);
    while ((RbuttonPushCounter < 2)) {
      digitalWrite(Rgt, HIGH);
      delay(1000);
      digitalWrite(Rgt, LOW);
      delay(1000);
      RbuttonState = digitalRead(RbuttonPin);
      Serial.print("now ");
      Serial.println(RbuttonState);
      Serial.print("last ");
      Serial.println(RlastButtonState);

      if (RbuttonState != RlastButtonState) 
      {
        RbuttonPushCounter++;
        (RlastButtonState = RbuttonState);
        Serial.print("CNT = ");
        Serial.println(RbuttonPushCounter);
      }

     if ((RbuttonPushCounter >= 2)) 
     {
        RbuttonPushCounter = 0;
        digitalWrite(Rgt, LOW);
        break;
      }
    
    }
  }

  if (CMDID == 7)
   {
    digitalWrite(Rgt, LOW);  // Turn Relay OFF
    digitalWrite(Lft, LOW);
    Serial.println("Relay Deactivated");
  }

  delay(500);
  //GREEN = 8, WHITE = 9, RED = 10, CRUISE ON = 11, CRUISE SET = 12, ADAPT ON = 13, NEAR = 14, FAR = 15
  if (CMDID == 0 || CMDID == 8 || CMDID == 9 || CMDID == 10 || CMDID == 11 || CMDID == 12 || CMDID == 13 || CMDID == 14 || CMDID == 15 || CMDID == 16)
   {
    (myData.b = CMDID);
    (CMDID == 0);
    Serial.println(myData.b);
    uint8_t data[sizeof(myData)];
    memcpy(data, &myData, sizeof(myData));
    esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *)&myData, sizeof(myData));
    if (result == ESP_OK)
     {
      Serial.println("Sent with success");
     }
      else 
    {
      Serial.println("Error sending the data");
    }
  }
  delay(1000);
}

There is no 5 loop, only while ((RbuttonPushCounter < 2)) inside if (CMDID == 5) { }