I am using ESP_NOW to send speed and direction commands to a radio controlled tugboat and a radio controlled car. If I send the commands individually, just one mac address, it works fine.
I'm now trying to send the commands to both at the same time, one sending ESP32 to two receive ESP32. It will send the commands to the first address but not the second. In my code I have the addresses as an array, uint8_t broadcastAddress1[], and uint8_t broadcastAddress2[] , if I just swap the 1 and 2 in those the the receiver that wasn't working is now but the other one is not, again sends to the first address but not the second. I get the following messages on the serial monitor.
Packet from: FC:E8:C0:7C:A2:40 send status: Delivery Success
Error sending data
All 3 ESP32's are ESP32 Dev Module
My code is below, and thanks for any help
John
[code]
/* Started with sipPuffToyCar_Mod example
added NRF24L01 communications
with corrections from forum
Rev 3 added steering state to receiver
Rev 4 Added sip/puff delay on steering
Rev 5 Changed to ESP_NOW
Rev 6 Added crawler controlS
Can switch, by commenting out, tug and crawler
*/
// Include Libraries
#include <esp_now.h>
#include <WiFi.h>
#include <SafeString.h>
// MAC Address of responder - edit as required
uint8_t broadcastAddress1[] = {0xFC, 0xE8, 0xC0, 0x7C, 0xA2, 0x40}; // Tug MAC address
uint8_t broadcastAddress2[] = {0xFC, 0xE8, 0xC0, 0x7B, 0x09, 0x88}; // Crawler MAC address
cSF(drvText_SS, 24);
cSF(strText_SS, 24);
#define SENSOR_PIN 34
#define redLED 22
#define grnLED 23
#define SIP_THRESHOLD 2000 //analog val, 450
#define PUFF_THRESHOLD 3000 //analog val
#define POLL_INTERVAL 10 //ms
#define SHORT_ACTION_MIN 100 //ms
#define SHORT_ACTION_MAX 750 //ms
#define ACTION_SPACE_MIN 50 //ms
#define ACTION_SPACE_MAX 750 //ms
uint32_t sipStartTime;
uint8_t sipStarted = 0;
uint32_t lastSipTime;
uint32_t lastSipDuration;
uint32_t puffStartTime;
uint8_t puffStarted = 0;
uint32_t lastPuffTime;
uint32_t lastPuffDuration;
uint8_t ThrottleOutValue = 0;
uint8_t ThrottleTrimValue = 0;
uint8_t SteeringOutValue = 135;
uint8_t SteeringTrimValue = 0;
uint32_t SteeringDelay = 0;
uint32_t previousMillis = 0;
uint32_t StartSteeringDelay = 50;
uint8_t SteeringPause = 0;
uint32_t SteeringPauseDelay = 500;
uint32_t SteeringPauseStart = 0;
enum DRIVE_STATE {FORWARD, REVERSE, IDLE}; // Enumeration, FORWARD - 1, REVERSE = 2, IDLE = 3
char *DRIVE_STATE_STRS[] = {"forward", "reverse", "idle"}; // ? Defines words so that when Drive state = 1 Serial monitor will display Forward, etc
uint8_t driveState = IDLE;
// Define a data structure
typedef struct struct_message {
int steering;
int drive;
} struct_message;
// Create a structured object
struct_message myData;
// Peer info
esp_now_peer_info_t peerInfo;
// Callback function called when data is sent
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
char macStr[18];
Serial.print("Packet from: ");
// Copies the sender mac address to a string
snprintf(macStr, sizeof(macStr),
"%02x:%02x:%02x:%02x:%02x:%02x",
mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
Serial.print(macStr);
Serial.print(" send status:\t");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");}
//
void setDrive(uint8_t state) {
// pinMode(FORWARD_PIN, OUTPUT);
// pinMode(REVERSE_PIN, OUTPUT);
if (driveState == state)
return;
if (state == FORWARD) {
myData.drive = 1;
digitalWrite(grnLED, HIGH);
digitalWrite(redLED, LOW);
// digitalWrite(FORWARD_PIN, HIGH);
//Serial.print (" Forward Pause = ");
//Serial.print (SteeringPause);
//Serial.print (" Time = ");
//Serial.println (SteeringPauseStart);
}
else if (state == REVERSE) {
myData.drive = 2;
digitalWrite(grnLED, LOW);
digitalWrite(redLED, HIGH);
//digitalWrite (REVERSE_PIN, HIGH);
//Serial.print (" Reverse Pause = ");
//Serial.print (SteeringPause);
//Serial.print (" Time = ");
//Serial.println (SteeringPauseStart);
}
else {
myData.drive = 0;
digitalWrite(grnLED, LOW);
digitalWrite(redLED, LOW);
// digitalWrite (FORWARD_PIN, LOW);
// digitalWrite (REVERSE_PIN, LOW);
}
driveState = state;
}
//
void toggleReverse() {
if (driveState == REVERSE)
return;
else if (driveState == IDLE)
setDrive(REVERSE);
else
setDrive(IDLE);
}
void toggleForward() {
if (driveState == FORWARD)
return;
else if (driveState == IDLE)
setDrive(FORWARD);
else
setDrive(IDLE);
}
enum STEERING_STATE {LEFT, RIGHT, CENTER};
char *STEERING_STATE_STRS[] = {"left", "right", "center"};
uint8_t steeringState = CENTER;
void setSteering(uint8_t state) {
//pinMode(LEFT_PIN, OUTPUT);
//pinMode(RIGHT_PIN, OUTPUT);
if (steeringState == state)
return;
if (state == LEFT) {
myData.steering = 1;
// && (millis() - SteeringPauseStart) > SteeringPauseDelay
// This line was added to the If condition statement above
// Serial.print (" Left Pause = ");
// Serial.print (SteeringPause);
// Serial.print ("Pause Time = ");
// Serial.println (millis() - SteeringPauseStart);
// digitalWrite(LEFT_PIN, HIGH);
}
else if (state == RIGHT) {
myData.steering = 2;
// && (millis() - SteeringPauseStart) > SteeringPauseDelay
// This line was added to the If condition statement above
// Serial.print (" Right Pause = ");
// Serial.print (SteeringPause);
// Serial.print (" Pause Time = ");
// Serial.println (millis() - SteeringPauseStart);
// digitalWrite(RIGHT_PIN, HIGH);
}
else {
myData.steering = 0;
// digitalWrite(LEFT_PIN, LOW);
// digitalWrite(RIGHT_PIN, LOW);
SteeringPauseStart = millis();
SteeringPause = 0;
// Serial.print (" Idle Pause = ");
// Serial.print (SteeringPause);
// Serial.print (" Pause Time = ");
// Serial.println (millis() - SteeringPauseStart);
}
steeringState = state;
} // end void setSteering(uint8_t state)
void setup() {
Serial.begin(115200);
pinMode(redLED, OUTPUT);
pinMode(grnLED, OUTPUT);
// Set ESP32 as a Wi-Fi Station
WiFi.mode(WIFI_STA);
// Initilize ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
// Register the send callback
esp_now_register_send_cb(OnDataSent);
// register peer
peerInfo.channel = 0;
peerInfo.encrypt = false;
// register first peer
memcpy(peerInfo.peer_addr, broadcastAddress1, 6);
if (esp_now_add_peer(&peerInfo) != ESP_OK){
Serial.println("Failed to add peer");
return;
}
// register second peer
memcpy(peerInfo.peer_addr, broadcastAddress2, 6);
if (esp_now_add_peer(&peerInfo) != ESP_OK){
Serial.println("Failed to add peer");
return;
}
} // End ot Setup Loop
void loop() {
// loop is doing this Constantly repeating
// while (1) { // Constantly run While Loop
int16_t val = analogRead(SENSOR_PIN); // Read Pressure Switch < 450 = SIP, > 550 = Puff
// Serial.println(val);
if (!sipStarted && val < SIP_THRESHOLD) {
SteeringPause = 1;
sipStarted = 1;
sipStartTime = millis(); //Store Sip Start Time
}
else if (sipStarted && val > SIP_THRESHOLD) {// SIP has Stopped
sipStarted = 0;
uint32_t duration = millis() - sipStartTime;
if (duration > SHORT_ACTION_MIN) {
uint32_t prevLastSipTime = lastSipTime;
uint32_t prevLastSipDuration = lastSipDuration;
lastSipTime = millis();
lastSipDuration = duration;
uint32_t space = sipStartTime - prevLastSipTime;
//two shorts in a row
if (prevLastSipDuration < SHORT_ACTION_MAX &&
lastSipDuration < SHORT_ACTION_MAX &&
space > ACTION_SPACE_MIN && space < ACTION_SPACE_MAX) {
toggleReverse();
}
}
}
if (!puffStarted && val > PUFF_THRESHOLD) {
SteeringPause = 1;
puffStarted = 1;
puffStartTime = millis();
}
else if (puffStarted && val < PUFF_THRESHOLD) {
puffStarted = 0;
uint32_t duration = millis() - puffStartTime;
if (duration > SHORT_ACTION_MIN) {
uint32_t prevLastPuffTime = lastPuffTime;
uint32_t prevLastPuffDuration = lastPuffDuration;
lastPuffTime = millis();
lastPuffDuration = duration;
uint32_t space = puffStartTime - prevLastPuffTime;
//two shorts in a row
if (prevLastPuffDuration < SHORT_ACTION_MAX &&
lastPuffDuration < SHORT_ACTION_MAX &&
space > ACTION_SPACE_MIN && space < ACTION_SPACE_MAX) {
toggleForward();
}
}
}
//update steering
if (sipStarted && (millis() > (sipStartTime + SteeringPauseDelay)))
setSteering(LEFT);
else if (puffStarted && (millis() > (puffStartTime + SteeringPauseDelay)))
setSteering(RIGHT);
else
setSteering(CENTER);
// Set Throttle Output with Trim
// ThrottleTrimValue = map(analogRead(ThrottleTrimInput), 0, 1024, 85, 135) - 110;
if (DRIVE_STATE_STRS[driveState] == "forward") {
// ThrottleOutValue = (240 - ThrottleTrimValue) ;
//analogWrite(ThrottleOutput, ThrottleOutValue);
// Serial.print(" Throttle Out = ");
// Serial.print(ThrottleOutValue);
}
else if (DRIVE_STATE_STRS[driveState] == "reverse") {
// ThrottleOutValue = (30 - ThrottleTrimValue);
// analogWrite(ThrottleOutput, ThrottleOutValue);
// Serial.print(" Throttle Out = ");
// Serial.print(ThrottleOutValue);
}
else {
// ThrottleOutValue = (135 - ThrottleTrimValue);
//analogWrite(ThrottleOutput, ThrottleOutValue);
// Serial.print(" Throttle Out = ");
// Serial.print(ThrottleOutValue);
}
// Set Steeering Output with Trim
// SteeringTrimValue = map(analogRead(SteeringTrimInput), 0, 1024, 85, 135) - 110;
// Delay for Steering change
unsigned long currentmillis = millis();
int CenterStop ;
if (currentmillis - previousMillis > StartSteeringDelay) {
previousMillis = currentmillis;
CenterStop = (135 + SteeringTrimValue);
if ((STEERING_STATE_STRS[steeringState] == "right") && (SteeringOutValue < 230)) {
// SteeringOutValue = ((SteeringOutValue + 10) );
// analogWrite(SteeringOutput, SteeringOutValue);
}
if ((STEERING_STATE_STRS[steeringState] == "left") && (SteeringOutValue > 40)) {
// SteeringOutValue = ((SteeringOutValue - 10) );
//analogWrite(SteeringOutput, SteeringOutValue);
}
if (STEERING_STATE_STRS[steeringState] == "center") {
if (SteeringOutValue - CenterStop > 2) {
// SteeringOutValue = ((SteeringOutValue) - 5);
// analogWrite(SteeringOutput, SteeringOutValue);
}
if (SteeringOutValue - CenterStop < -2) {
// SteeringOutValue = ((SteeringOutValue) + 5);
// analogWrite(SteeringOutput, SteeringOutValue);
}
} // End of CENTER if
} // End of Main Steering If loop
/*
Serial.print(" Steering State = ");
Serial.print(myData.steering);
Serial.print("\t");
Serial.print(" Drive State = ");
Serial.println(myData.drive);
*/
strText_SS = STEERING_STATE_STRS[steeringState];
// Send message via ESP-NOW
esp_err_t result = esp_now_send(0, (uint8_t *) &myData,
sizeof(struct_message));
if (result == ESP_OK) {
Serial.println("Sent with success");
}
else {
Serial.println("Error sending data");
}
delay(POLL_INTERVAL);
// if (sipStarted == 1){
// Serial.println ("Sip Started");
// StartSteeringDelay = 0;
// }
// else if (puffStarted ==1){
// Serial.println ("Puff Started");
// }
// else {}
// }
}
[/code]