Hi!
I have been using ESP NOW for some time now but i can't seem to find the information to send the same message to various esps.
I am able to send to 4 different but from there it doesnt send to the rest (total 6). I have added all the peers and have tried using a different line to each one of them but it doesnt seem to work. On Espressif Systems it says that we can send up to 20 devices but from 4 i cant send anymore.
This is the master code that is trying to send to all 6 of them:
#include <esp_now.h>
#include <WiFi.h>
#include "BluetoothSerial.h"
#include <HardwareSerial.h>
HardwareSerial SerialPort(2); // use UART2
// init Class:
BluetoothSerial ESP_BT;
int sair;
int incoming = 0;
int golos = 0;
String golo;
String bolas;
String tempo;
String bolac;
String goloc;
bool flag15 = false;
bool flag30 = false;
bool flag45 = false;
bool flag60 = false;
bool flag90 = false;
bool flag120 = false;
bool flag5 = false;
bool flag10 = false;
bool flag14 = false;
bool flag20 = false;
bool flag29 = false;
bool flag50 = false;
long tempoatual;
long tempoagr;
long tempodecorrido;
int num_gerado;
// REPLACE WITH YOUR RECEIVER MAC Address
uint8_t broadcastAddress1[] = { 0x3C, 0xE9, 0x0E, 0x08, 0xF5, 0xE0 };
uint8_t broadcastAddress2[] = { 0x94, 0xB9, 0x7E, 0x59, 0x40, 0xF4 };
uint8_t broadcastAddress3[] = { 0x3C, 0xE9, 0x0E, 0x08, 0xF5, 0x2C };
uint8_t broadcastAddress4[] = { 0x0C, 0xB8, 0x15, 0xD5, 0x28, 0xB4 };
uint8_t broadcastAddress5[] = { 0x10, 0x97, 0xBD, 0xD4, 0xB4, 0x54 };
uint8_t broadcastAddress6[] = { 0x10, 0x97, 0xBD, 0xD4, 0xD1, 0x68 };
// Define variables to store BME280 readings to be sent
int ze;
// Define variables to store incoming readings
int zerecebe;
// Variable to store if sending data was successful
String success;
esp_err_t result1;
//Structure example to send data
//Must match the receiver structure
typedef struct struct_message {
int ze;
} struct_message;
// Create a struct_message called BME280Readings to hold sensor readings
struct_message mensagem_enviada;
// Create a struct_message to hold incoming sensor readings
struct_message mensagem_recebida;
esp_now_peer_info_t peerInfo;
// callback when data is sent
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
char macStr[18];
Serial.print("Packet to: ");
// 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");
}
// callback when data is received
void OnDataRecv(const uint8_t *mac, const uint8_t *incomingData, int len) {
memcpy(&mensagem_recebida, incomingData, sizeof(mensagem_recebida));
Serial.print("Bytes received: ");
Serial.println(len);
zerecebe = mensagem_recebida.ze;
Serial.print("ze");
Serial.println(zerecebe);
}
void setup() {
Serial.begin(115200);
SerialPort.begin(115200, SERIAL_8N1, 16, 17);
WiFi.mode(WIFI_STA);
ESP_BT.begin("ESP32_Control");
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
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;
}
/// register third peer
memcpy(peerInfo.peer_addr, broadcastAddress3, 6);
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
Serial.println("Failed to add peer");
return;
}
memcpy(peerInfo.peer_addr, broadcastAddress4, 6);
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
Serial.println("Failed to add peer");
return;
}
memcpy(peerInfo.peer_addr, broadcastAddress5, 6);
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
Serial.println("Failed to add peer");
return;
}
memcpy(peerInfo.peer_addr, broadcastAddress6, 6);
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
Serial.println("Failed to add peer");
return;
}
// Register for a callback function that will be called when data is received
esp_now_register_recv_cb(OnDataRecv);
}
void contagem_golo() {
golos++;
}
void num_endereco() {
if (num_gerado == 1) {
mensagem_enviada.ze = 1;
result1 = esp_now_send(broadcastAddress1, (uint8_t *)&mensagem_enviada, sizeof(mensagem_enviada));
zerecebe = 0;
num_gerado = 0;
}
if (num_gerado == 2) {
mensagem_enviada.ze = 2;
result1 = esp_now_send(broadcastAddress2, (uint8_t *)&mensagem_enviada, sizeof(mensagem_enviada));
zerecebe = 0;
num_gerado = 0;
}
if (num_gerado == 3) {
mensagem_enviada.ze = 3;
result1 = esp_now_send(broadcastAddress3, (uint8_t *)&mensagem_enviada, sizeof(mensagem_enviada));
zerecebe = 0;
num_gerado = 0;
}
if (num_gerado == 4) {
mensagem_enviada.ze = 4;
result1 = esp_now_send(broadcastAddress4, (uint8_t *)&mensagem_enviada, sizeof(mensagem_enviada));
zerecebe = 0;
num_gerado = 0;
}
if (num_gerado == 5) {
mensagem_enviada.ze = 5;
result1 = esp_now_send(broadcastAddress5, (uint8_t *)&mensagem_enviada, sizeof(mensagem_enviada));
zerecebe = 0;
num_gerado = 0;
}
if (num_gerado == 6) {
mensagem_enviada.ze = 6;
result1 = esp_now_send(broadcastAddress6, (uint8_t *)&mensagem_enviada, sizeof(mensagem_enviada));
zerecebe = 0;
num_gerado = 0;
}
}
void num_espera() {
esp_err_t result1 = esp_now_send(0, (uint8_t *) &mensagem_enviada, sizeof(mensagem_enviada));
zerecebe = 2;
num_gerado = 0;
}
void loop() {
if (ESP_BT.available()) {
incoming = ESP_BT.read(); //Read what we receive
if (incoming == 5 || incoming == 10 || incoming == 15 || incoming == 20 || incoming == 25 || incoming == 30 || incoming == 35 || incoming == 40 || incoming == 45 || incoming == 50 || incoming == 55 || incoming == 60 || incoming == 65 || incoming == 70 || incoming == 75 || incoming == 80 || incoming == 85 || incoming == 90 || incoming == 95 || incoming == 100) {
if ((incoming < 100) && (incoming >= 10)) {
bolas = String(incoming);
bolac = ("0" + bolas);
} else if ((incoming < 10) && (incoming >= 1)) {
bolas = String(incoming);
bolac = ("00" + bolas);
} else if (incoming == 0) {
bolas = String("000");
} else {
bolas = String(incoming);
}
}
if (incoming == 240) {
Serial.print("app:");
Serial.println(incoming);
golos = 0;
}
if (incoming == 241) {
flag15 = true;
Serial.print("app:");
Serial.println(incoming);
}
if (incoming == 242) {
flag30 = true;
Serial.print("app:");
Serial.println(incoming);
}
if (incoming == 243) {
flag45 = true;
Serial.print("app:");
Serial.println(incoming);
}
if (incoming == 244) {
flag60 = true;
Serial.print("app:");
Serial.println(incoming);
}
if (incoming == 245) {
flag90 = true;
Serial.print("app:");
Serial.println(incoming);
}
if (incoming == 246) {
flag120 = true;
Serial.print("app:");
Serial.println(incoming);
}
if (incoming == 247) {
flag5 = true;
Serial.print("app:");
Serial.println(incoming);
}
if (incoming == 248) {
flag10 = true;
Serial.print("app:");
Serial.println(incoming);
}
if (incoming == 249) {
flag14 = true;
Serial.print("app:");
Serial.println(incoming);
}
if (incoming == 250) {
flag20 = true;
Serial.print("app:");
Serial.println(incoming);
}
if (incoming == 251) {
flag30 = true;
Serial.print("app:");
Serial.println(incoming);
}
if (incoming == 252) {
flag50 = true;
Serial.print("app:");
Serial.println(incoming);
}
if (flag15 == true) {
flag15 = false;
incoming = 0;
tempodecorrido = 0;
tempoagr = millis();
zerecebe = 3;
while (tempodecorrido < 5000) {
sair = ESP_BT.read();
if (sair == 253) {
return loop();
}
tempoatual = millis();
tempodecorrido = tempoatual - tempoagr;
if (zerecebe == 3) {
mensagem_enviada.ze = 10;
num_espera();
}
}
tempodecorrido = 0;
tempoagr = millis();
SerialPort.print("pega");
while (tempodecorrido < 15000) {
sair = ESP_BT.read();
if (sair == 253) {
return loop();
}
tempoatual = millis();
tempodecorrido = tempoatual - tempoagr;
if (zerecebe == 2 || zerecebe == 4) {
if (zerecebe == 4) {
contagem_golo();
}
num_gerado = random(6, 7);
Serial.print("número aleatório:");
Serial.println(num_gerado);
delay(250);
num_endereco();
}
}
if ((golos < 100) && (golos >= 10)) {
golo = String(golos);
goloc = ("0" + golo);
} else if ((golos < 10) && (golos >= 1)) {
golo = String(golos);
goloc = ("00" + golo);
} else if (golos == 0) {
goloc = String("000");
} else {
goloc = String(golos);
}
SerialPort.print(bolac + goloc);
zerecebe = 0;
}
}
}
and this is the receivers one:
#include <esp_now.h>
#include <WiFi.h>
#include <NeoPixelBus.h>
const int sensor1 = 15;
const int sensor2 = 2;
int valor1 = 0;
int fim_baliza;
const uint16_t PixelCount = 30;
const uint8_t PixelPin = 5;
NeoPixelBus<NeoGrbFeature, Neo800KbpsMethod> strip(PixelCount, PixelPin);
RgbColor red(255, 0, 0);
RgbColor green(0, 255, 0);
RgbColor blue(0, 0, 255);
RgbColor white(255);
RgbColor black(0, 0, 0);
HslColor hslRed(red);
HslColor hslGreen(green);
HslColor hslBlue(blue);
HslColor hslWhite(white);
HslColor hslBlack(black);
long tempoatual;
long tempoagr;
long tempodecorrido;
// REPLACE WITH THE MAC Address of your receiver
uint8_t broadcastAddress[] = { 0x10, 0x97, 0xBD, 0xD4, 0xE8, 0x18 };
// Define variables to store BME280 readings to be sent
int ze;
// Define variables to store incoming readings
int zerecebe;
// Variable to store if sending data was successful
String success;
//Structure example to send data
//Must match the receiver structure
typedef struct struct_message {
int ze;
} struct_message;
// Create a struct_message called BME280Readings to hold sensor readings
struct_message mensagem_enviada;
// Create a struct_message to hold incoming sensor readings
struct_message mensagem_recebida;
esp_now_peer_info_t peerInfo;
// Callback when data is sent
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
Serial.print("\r\nLast Packet Send Status:\t");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
if (status == 0) {
success = "Delivery Success :)";
} else {
success = "Delivery Fail :(";
}
}
// Callback when data is received
void OnDataRecv(const uint8_t *mac, const uint8_t *incomingData, int len) {
memcpy(&mensagem_recebida, incomingData, sizeof(mensagem_recebida));
Serial.print("Bytes received: ");
Serial.println(len);
zerecebe = mensagem_recebida.ze;
Serial.print("ze:"), Serial.println(zerecebe);
}
void setup() {
// Init Serial Monitor
Serial.begin(115200);
// 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 Send CB to
// get the status of Trasnmitted packet
esp_now_register_send_cb(OnDataSent);
// Register peer
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
// Add peer
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
Serial.println("Failed to add peer");
return;
}
// Register for a callback function that will be called when data is received
esp_now_register_recv_cb(OnDataRecv);
pinMode(sensor1, INPUT_PULLUP);
pinMode(sensor2, INPUT_PULLUP);
pinMode(PixelPin, OUTPUT);
strip.Begin();
strip.Show();
}
void espera() {
strip.SetPixelColor(0, red);
strip.SetPixelColor(1, red);
strip.SetPixelColor(2, red);
strip.SetPixelColor(3, red);
strip.SetPixelColor(4, red);
strip.SetPixelColor(5, red);
strip.SetPixelColor(6, red);
strip.SetPixelColor(7, red);
strip.SetPixelColor(8, red);
strip.SetPixelColor(9, red);
strip.SetPixelColor(10, red);
strip.SetPixelColor(11, red);
strip.SetPixelColor(12, red);
strip.SetPixelColor(13, red);
strip.SetPixelColor(14, red);
strip.SetPixelColor(15, red);
strip.SetPixelColor(16, red);
strip.SetPixelColor(17, red);
strip.SetPixelColor(18, red);
strip.SetPixelColor(19, red);
strip.SetPixelColor(20, red);
strip.SetPixelColor(21, red);
strip.SetPixelColor(22, red);
strip.SetPixelColor(23, red);
strip.SetPixelColor(24, red);
strip.SetPixelColor(25, red);
strip.SetPixelColor(26, red);
strip.SetPixelColor(27, red);
strip.SetPixelColor(28, red);
strip.SetPixelColor(29, red);
strip.SetPixelColor(30, red);
strip.Show();
}
void led_on() {
strip.SetPixelColor(0, green);
strip.SetPixelColor(1, green);
strip.SetPixelColor(2, green);
strip.SetPixelColor(3, green);
strip.SetPixelColor(4, green);
strip.SetPixelColor(5, green);
strip.SetPixelColor(6, green);
strip.SetPixelColor(7, green);
strip.SetPixelColor(8, green);
strip.SetPixelColor(9, green);
strip.SetPixelColor(10, green);
strip.SetPixelColor(11, green);
strip.SetPixelColor(12, green);
strip.SetPixelColor(13, green);
strip.SetPixelColor(14, green);
strip.SetPixelColor(15, green);
strip.SetPixelColor(16, green);
strip.SetPixelColor(17, green);
strip.SetPixelColor(18, green);
strip.SetPixelColor(19, green);
strip.SetPixelColor(20, green);
strip.SetPixelColor(21, green);
strip.SetPixelColor(22, green);
strip.SetPixelColor(23, green);
strip.SetPixelColor(24, green);
strip.SetPixelColor(25, green);
strip.SetPixelColor(26, green);
strip.SetPixelColor(27, green);
strip.SetPixelColor(28, green);
strip.SetPixelColor(29, green);
strip.SetPixelColor(30, green);
strip.Show();
}
void led_off() {
strip.SetPixelColor(0, black);
strip.SetPixelColor(1, black);
strip.SetPixelColor(2, black);
strip.SetPixelColor(3, black);
strip.SetPixelColor(4, black);
strip.SetPixelColor(5, black);
strip.SetPixelColor(6, black);
strip.SetPixelColor(7, black);
strip.SetPixelColor(8, black);
strip.SetPixelColor(9, black);
strip.SetPixelColor(10, black);
strip.SetPixelColor(11, black);
strip.SetPixelColor(12, black);
strip.SetPixelColor(13, black);
strip.SetPixelColor(14, black);
strip.SetPixelColor(15, black);
strip.SetPixelColor(16, black);
strip.SetPixelColor(17, black);
strip.SetPixelColor(18, black);
strip.SetPixelColor(19, black);
strip.SetPixelColor(20, black);
strip.SetPixelColor(21, black);
strip.SetPixelColor(22, black);
strip.SetPixelColor(23, black);
strip.SetPixelColor(24, black);
strip.SetPixelColor(25, black);
strip.SetPixelColor(26, black);
strip.SetPixelColor(27, black);
strip.SetPixelColor(28, black);
strip.SetPixelColor(29, black);
strip.SetPixelColor(30, black);
strip.Show();
}
void loop() {
if (zerecebe == 6) {
zerecebe = 0;
tempodecorrido = 0;
fim_baliza = 0;
tempoagr = millis();
led_on();
while (tempodecorrido < 5000) {
valor1 = digitalRead(sensor1) || digitalRead(sensor2);
//Serial.println(valor1);
if (valor1 == 1) {
fim_baliza = 4;
tempodecorrido = 5000;
delay(250);
} else {
tempoatual = millis();
tempodecorrido = tempoatual - tempoagr;
fim_baliza = 2;
}
}
delay(100);
led_off();
// Set values to send
mensagem_enviada.ze = fim_baliza;
// Send message via ESP-NOW
esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *)&mensagem_enviada, sizeof(mensagem_enviada));
if (result == ESP_OK) {
Serial.println("Sent with success");
} else {
Serial.println("Error sending the data");
}
} else {
led_off();
Serial.println("5");
delay(250);
}
if (zerecebe == 10) {
fim_baliza = 0;
espera();
tempodecorrido = 0;
tempoagr = millis();
Serial.println("1");
while (tempodecorrido < 5000) {
tempoatual = millis();
tempodecorrido = tempoatual - tempoagr;
fim_baliza = 2;
Serial.println("2");
delay(250);
}
Serial.println("3");
// Set values to send
mensagem_enviada.ze = fim_baliza;
// Send message via ESP-NOW
esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *)&mensagem_enviada, sizeof(mensagem_enviada));
if (result == ESP_OK) {
Serial.println("Sent with success");
} else {
Serial.println("Error sending the data");
}
zerecebe = 0;
} else {
led_off();
Serial.println("5");
delay(250);
}
}
they all are the same just the first cycle in the loop is a different number like instead of 6 is 1 and it goes like that
