Unable to send to more than five slaves in ESPNOW

VipinVibhute
Posts: 2
Joined: Wed Feb 07, 2024 5:25 am

Unable to send to more than five slaves in ESPNOW

Postby VipinVibhute » Wed Mar 27, 2024 5:38 am

#include <TinyGPSPlus.h> // include TinyGPS++ library
#include <TimeLib.h> // include Arduino time library
#include <SoftwareSerial.h> // include software serial library
#include <esp_now.h>
#include <WiFi.h>

// REPLACE WITH YOUR ESP RECEIVER'S MAC ADDRESS

uint8_t broadcastAddress[8][6] = {{ 0xA8, 0x42, 0xE3, 0xA8, 0xE2, 0xC4 },
{ 0xD4, 0x8A, 0xFC, 0xA4, 0xD9, 0xA8 },
{ 0xFC, 0xB4, 0x67, 0xF4, 0xAC, 0x8C },
{ 0xE4, 0x65, 0xB8, 0x71, 0x40, 0xAC },
{ 0xD8, 0xBC, 0x38, 0xF9, 0xC8, 0xB0 },
{ 0xD8, 0xBC, 0x38, 0xFB, 0x58, 0x70 },
{ 0xD8, 0xBC, 0x38, 0xFB, 0x3B, 0x98 },
{ 0xE4, 0x65, 0xB8, 0x71, 0x3E, 0x4C }};
esp_now_peer_info_t peerInfo [8];

typedef struct test_struct {
int hour1;
int hour2;
int minute1;
int minute2;
} test_struct;

test_struct test;

void zero();
void one();
void two();
void three();
void four();
void five();
void six();
void seven();
void eight();
void nine();
void display();
void turnOffAllSegments();
void digit1();
void digit2();
void digit3();
void digit4();
void digit5();
void digit6();
void all6Digitsoff();
void display_error();
void pickMsg(int x);
void pickDigit(int x);
void pickNumber(int x);



int pinA = 12;
int pinB = 13;
int pinC = 25;
int pinD = 33;
int pinE = 32;
int pinF = 14;
int pinG = 27;
//int pinDP = 25;

int D1 = 5;
int D2 = 18;
int D3 = 21;
int D4 = 22;
int D5 = 23;
int D6 = 19;

//int i = 0;

int d6_value = 6;
int d5_value = 5;
int d4_value = 4;
int d3_value = 3;
int d2_value = 2;
int d1_value = 1;

//SevSeg sevseg;
TinyGPSPlus gps;

#define S_RX 16 // define software serial RX pin
#define S_TX 17 // define software serial TX pin

SoftwareSerial SoftSerial(S_RX, S_TX); // configure SoftSerial library

int h, s, m;
int h1 = 0;
int h2 = 0;
int m1 = 0;
int m2 = 0;
int s1 = 0;
int s2 = 0;
int previousTime = 0;
int startTime;
int interval;
int timeFlag;
//int time = 15;

unsigned long previousMillis = 0UL;
int timerDigit[6]; // array to store individual timer digits
unsigned long currentMillis;


// callback when data is sent
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
char macStr[18];
//display();
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], mac_addr[6], mac_addr[7], mac_addr[8], mac_addr[9], mac_addr[10], mac_addr[11], mac_addr[12], mac_addr[13], mac_addr[14], mac_addr[15]);
Serial.print(macStr);
Serial.print(" send status:\t");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
}

void setup(void) {
Serial.begin(115200);
while (!Serial)
;
delay(1000);

SoftSerial.begin(9600); // initialize software serial at 9600 baud
delay(3000);

pinMode(pinA, OUTPUT);
pinMode(pinB, OUTPUT);
pinMode(pinC, OUTPUT);
pinMode(pinD, OUTPUT);
pinMode(pinE, OUTPUT);
pinMode(pinF, OUTPUT);
pinMode(pinG, OUTPUT);
// pinMode(pinDP, OUTPUT);
pinMode(D1, OUTPUT);
pinMode(D2, OUTPUT);
pinMode(D3, OUTPUT);
pinMode(D4, OUTPUT);
pinMode(D5, OUTPUT);
pinMode(D6, OUTPUT);

pinMode(S_RX, INPUT);
pinMode(S_TX, OUTPUT);
turnOffAllSegments();
all6Digitsoff();

WiFi.mode(WIFI_STA);

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

esp_now_register_send_cb(OnDataSent);

for(int i=0; i<8; i++){
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;
}
}

}

void loop() {
display();
timeFlag = 0;
//Serial.println(timeFlag);
while (SoftSerial.available() > 0) {
//Serial.println(SoftSerial.available());
//display();
if (gps.encode(SoftSerial.read())) {
//display();
// get time from GPS module
if (gps.time.isValid()) {
timeFlag = 01;
//Serial.println(timeFlag);
h = gps.time.hour() + 5; //adjust UTC
if (h > 23) {
h = h - 24;
}
//Serial.println(hour);
// Minute (0-59) (u8)

m = gps.time.minute() + 30;
if (m > 59) {
m = m - 60;
h = h + 1;
}
//Serial.print("Hour = ");
Serial.print(h);
Serial.print(F(":"));
//Serial.print("Minute = ");
Serial.print(m);
Serial.print(F(":"));
//Second (0-59) (u8)
//Serial.print("Second = ");
s = gps.time.second();
Serial.println(s);


h1 = h / 10; //12 / 10 is 1.
h2 = h % 10; //12 % 10 is 2.
m1 = m / 10;
m2 = m % 10;
s1 = s / 10;
s2 = s % 10;

display();
//display();

test.hour1 = h1;
test.hour2 = h2;
test.minute1 = m1;
test.minute2 = m2;

esp_err_t result = esp_now_send(0, (uint8_t *)&test, sizeof(test_struct));

if (result == ESP_OK) {
Serial.println("Sent with success");
} else {
Serial.println("Error sending the data");
}
}
}
}

//display_error();
}


//function for display
void display() {

interval = 0;
if ((currentMillis - previousTime >= interval)) { //&& (digitalRead(D1) == LOW)) {
previousTime = currentMillis;
unsigned long currentMillis = millis();
timerDigit[0] = h1;
timerDigit[1] = h2;
timerDigit[2] = m1;
timerDigit[3] = m2;
timerDigit[4] = s1;
timerDigit[5] = s2;

for (int i = 1; i <= 6; i++) { //cycling through each digit to be displayed
pickDigit(i);
pickNumber(timerDigit);
delayMicroseconds(2500);
turnOffAllSegments();
all6Digitsoff();
}
}
}




void display_error() {
interval = 1;
if ((currentMillis - previousTime >= interval)) { //&& (digitalRead(D1) == LOW)) {
previousTime = currentMillis;
unsigned long currentMillis = millis();

for (int i = 1; i <= 6; i++) { //cycling through each digit to be displayed
pickDigit(i);
pickMsg(i);
delayMicroseconds(2500);
turnOffAllSegments();
all6Digitsoff();
}
}
}

//functions to select individual digits on the display
void pickDigit(int x) //changes digit
{


switch (x) {
case 1:
digitalWrite(D1, LOW);
digitalWrite(D2, HIGH);
digitalWrite(D3, HIGH);
digitalWrite(D4, HIGH);
digitalWrite(D5, HIGH);
digitalWrite(D6, HIGH);
break;

case 2:
digitalWrite(D1, HIGH);
digitalWrite(D2, LOW);
digitalWrite(D3, HIGH);
digitalWrite(D4, HIGH);
digitalWrite(D5, HIGH);
digitalWrite(D6, HIGH);
break;

case 3:
digitalWrite(D1, HIGH);
digitalWrite(D2, HIGH);
digitalWrite(D3, LOW);
digitalWrite(D4, HIGH);
digitalWrite(D5, HIGH);
digitalWrite(D6, HIGH);
break;

case 4:
digitalWrite(D1, HIGH);
digitalWrite(D2, HIGH);
digitalWrite(D3, HIGH);
digitalWrite(D4, LOW);
digitalWrite(D5, HIGH);
digitalWrite(D6, HIGH);
break;

case 5:
digitalWrite(D1, HIGH);
digitalWrite(D2, HIGH);
digitalWrite(D3, HIGH);
digitalWrite(D4, HIGH);
digitalWrite(D5, LOW);
digitalWrite(D6, HIGH);
break;

case 6:
digitalWrite(D1, HIGH);
digitalWrite(D2, HIGH);
digitalWrite(D3, HIGH);
digitalWrite(D4, HIGH);
digitalWrite(D5, HIGH);
digitalWrite(D6, LOW);
break;

default:
digitalWrite(D1, HIGH);
digitalWrite(D2, HIGH);
digitalWrite(D3, HIGH);
digitalWrite(D4, HIGH);
digitalWrite(D5, HIGH);
digitalWrite(D6, HIGH);
}
}

void pickNumber(int x) //changes value of number
{
switch (x) {
default:
zero();
break;
case 1:
one();
break;
case 2:
two();
break;
case 3:
three();
break;
case 4:
four();
break;
case 5:
five();
break;
case 6:
six();
break;
case 7:
seven();
break;
case 8:
eight();
break;
case 9:
nine();
break;
}
}

void pickMsg(int x) //changes value of number
{
switch (x) {
default:
G();
break;
case 1:
P();
break;
case 2:
five();
break;
case 3:
zero();
break;
case 4:
E();
break;
case 5:
R();
break;
}
}

//functions representing numbers 0-9
void zero() {
digitalWrite(pinA, HIGH);
digitalWrite(pinB, HIGH);
digitalWrite(pinC, HIGH);
digitalWrite(pinD, HIGH);
digitalWrite(pinE, HIGH);
digitalWrite(pinF, HIGH);
digitalWrite(pinG, LOW);
}

void one() {
digitalWrite(pinA, LOW);
digitalWrite(pinB, HIGH);
digitalWrite(pinC, HIGH);
digitalWrite(pinD, LOW);
digitalWrite(pinE, LOW);
digitalWrite(pinF, LOW);
digitalWrite(pinG, LOW);
}

void two() {
digitalWrite(pinA, HIGH);
digitalWrite(pinB, HIGH);
digitalWrite(pinC, LOW);
digitalWrite(pinD, HIGH);
digitalWrite(pinE, HIGH);
digitalWrite(pinF, LOW);
digitalWrite(pinG, HIGH);
}

void three() {
digitalWrite(pinA, HIGH);
digitalWrite(pinB, HIGH);
digitalWrite(pinC, HIGH);
digitalWrite(pinD, HIGH);
digitalWrite(pinE, LOW);
digitalWrite(pinF, LOW);
digitalWrite(pinG, HIGH);
}

void four() {
digitalWrite(pinA, LOW);
digitalWrite(pinB, HIGH);
digitalWrite(pinC, HIGH);
digitalWrite(pinD, LOW);
digitalWrite(pinE, LOW);
digitalWrite(pinF, HIGH);
digitalWrite(pinG, HIGH);
}

void five() {
digitalWrite(pinA, HIGH);
digitalWrite(pinB, LOW);
digitalWrite(pinC, HIGH);
digitalWrite(pinD, HIGH);
digitalWrite(pinE, LOW);
digitalWrite(pinF, HIGH);
digitalWrite(pinG, HIGH);
}

void six() {
digitalWrite(pinA, HIGH);
digitalWrite(pinB, LOW);
digitalWrite(pinC, HIGH);
digitalWrite(pinD, HIGH);
digitalWrite(pinE, HIGH);
digitalWrite(pinF, HIGH);
digitalWrite(pinG, HIGH);
}

void seven() {
digitalWrite(pinA, HIGH);
digitalWrite(pinB, HIGH);
digitalWrite(pinC, HIGH);
digitalWrite(pinD, LOW);
digitalWrite(pinE, LOW);
digitalWrite(pinF, LOW);
digitalWrite(pinG, LOW);
}

void eight() {
digitalWrite(pinA, HIGH);
digitalWrite(pinB, HIGH);
digitalWrite(pinC, HIGH);
digitalWrite(pinD, HIGH);
digitalWrite(pinE, HIGH);
digitalWrite(pinF, HIGH);
digitalWrite(pinG, HIGH);
}

void nine() {
digitalWrite(pinA, HIGH);
digitalWrite(pinB, HIGH);
digitalWrite(pinC, HIGH);
digitalWrite(pinD, LOW);
digitalWrite(pinE, LOW);
digitalWrite(pinF, HIGH);
digitalWrite(pinG, HIGH);
}

void G() {
digitalWrite(pinA, HIGH);
digitalWrite(pinB, HIGH);
digitalWrite(pinC, HIGH);
digitalWrite(pinD, HIGH);
digitalWrite(pinE, LOW);
digitalWrite(pinF, HIGH);
digitalWrite(pinG, HIGH);
}

void P() {
digitalWrite(pinA, HIGH);
digitalWrite(pinB, HIGH);
digitalWrite(pinC, LOW);
digitalWrite(pinD, LOW);
digitalWrite(pinE, HIGH);
digitalWrite(pinF, HIGH);
digitalWrite(pinG, HIGH);
}

void E() {
digitalWrite(pinA, HIGH);
digitalWrite(pinB, LOW);
digitalWrite(pinC, LOW);
digitalWrite(pinD, HIGH);
digitalWrite(pinE, HIGH);
digitalWrite(pinF, HIGH);
digitalWrite(pinG, HIGH);
}

void R() {
digitalWrite(pinA, LOW);
digitalWrite(pinB, LOW);
digitalWrite(pinC, LOW);
digitalWrite(pinD, LOW);
digitalWrite(pinE, HIGH);
digitalWrite(pinF, LOW);
digitalWrite(pinG, HIGH);
}

void turnOffAllSegments() {
digitalWrite(pinA, LOW);
digitalWrite(pinB, LOW);
digitalWrite(pinC, LOW);
digitalWrite(pinD, LOW);
digitalWrite(pinE, LOW);
digitalWrite(pinF, LOW);
digitalWrite(pinG, LOW);
}




void all4Digits() {
digitalWrite(D1, HIGH);
digitalWrite(D2, HIGH);
digitalWrite(D3, HIGH);
digitalWrite(D4, HIGH);
}

void all6DigitsON() {
digitalWrite(D1, LOW);
digitalWrite(D2, LOW);
digitalWrite(D3, LOW);
digitalWrite(D4, LOW);
digitalWrite(D5, LOW);
digitalWrite(D6, LOW);
}

void all6Digitsoff() {
digitalWrite(D1, HIGH);
digitalWrite(D2, HIGH);
digitalWrite(D3, HIGH);
digitalWrite(D4, HIGH);
digitalWrite(D5, HIGH);
digitalWrite(D6, HIGH);
}

//end of code

Who is online

Users browsing this forum: No registered users and 86 guests