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);
}
}