Esp32 find multiple esp-now slave MAC addresses automatically

This is part of a larger project that uses the esp32 as a tank level sensor for a boat. But of course, it could be used in any situation that uses non-metallic tanks. This code handles up to three slave cards, but it could be extended to more than three.

The code that does the job is mainly one function, but some variables are set in other places and the standard esp32 connection code has some flags added to it, but is primarily that provided by Espressif (the people who make the esp32). At present, the MAC address of the master card does need to be hard-coded into the sketch for the slave cards and there must be a variable created in the slave code and a corresponding struct_message value set to send the MAC value from the slave to the master, but other than that all the magic happens in code for the master card, which is below. I have bolded the specific areas where the functionality for this capability takes place. But maybe I missed something.

#include <esp_now.h>
#include <WiFi.h>
#include <Preferences.h>  // library to set up write-to-flash function 

Preferences prefString;  // Assignment to set up write-to-flash function
Preferences prefInteger;
String savedThreshHoldValue1;
String savedThreshHoldValue2;
String savedThreshHoldValue3;

#define CHANNEL 1
byte paddingValue = 10;
int doCalibrate1 = 5;
int doCalibrate2 = 5;
int doCalibrate3 = 5;
byte chooseTank = 2;
String dw_M_SSID; // these vars are to capture the mac addresses of the slaves
String macCapture = "00";
String slaveOneMac = "00";
String slaveTwoMac = "00";
String slaveThreeMac = "00";
unsigned int slaveCountNumber;
bool stopFindingMacs = 0;
int gotMacs = 0;
int saved_DoneWithMacs = 0;

String myMac = WiFi.macAddress() + "AB";

// Pins used 
// 18, 19, 21, 22, 23 for level LEDs  26 for Calibrate switch 04 for calibrate indiscator 
// pins 32 33 inupt_pullup used for setting display to tank 1, 2, or 3/
// tank indicator LED 04 16, 17, 

int macLock1 = 1111; // 1111 is UNlocked 9999 is LOCKED 
int macLock2 = 1111;
int macLock3 = 1111;

// REPLACE WITH THE MAC Address of your receiver - as this is the master it is using the broadcast-to-all 0xFF address
uint8_t broadcastAddress[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
//uint8_t broadcastAddress[] = { 0x30,0xC6,0xF7,0x05,0x9B,0x94 };   

//  MASTER - RECIVES DATA FROM THREE SLAVES AND SORTS THE DATA INTO THREE SERIAL OUTPUT DISPLAYS.
// This recives and labels them ONE  TWO AND THREE
// IT IS DEPENDENT ON PUTTING THE RIGHT IDENTIFIER INTO the right var SUCH AS 949494 OR 707070 OR 787878

//unsigned long previousMillis = 0;        // will store last time LED was updated
//const long interval = 10000;
//const char* ssid = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";
String saved_MacValue1;
String saved_MacValue2;
String saved_MacValue3;
unsigned int  count_lookForMacAddr = 0;
char CharFromMstr[32]; // = "123456789 123456789 1234567893";

// Define variables to hold incoming incoming LED status readings
int incomingSlaveNumber;
int incomingSensorVarA1;
int incomingSensorVarA2;
int incomingSensorVarA3;
int incomingSensorVarA4;
int incomingSensorVarA5;
char incomingSlaveMacA;

int incomingSensorVarB1;
int incomingSensorVarB2;
int incomingSensorVarB3;
int incomingSensorVarB4;
int incomingSensorVarB5;

int incomingSensorVarC1;
int incomingSensorVarC2;
int incomingSensorVarC3;
int incomingSensorVarC4;
int incomingSensorVarC5;
String success;  // Variable to store if sending data was successful

void InitESPNow() {  // Init ESP Now with fallback
	WiFi.disconnect();
	if (esp_now_init() == ESP_OK) {
		Serial.println("ESPNow Init Success");
	} else {
		Serial.println("ESPNow Init Failed");
		// Retry InitESPNow, add a counte and then restart?
		// InitESPNow();
		// or Simply Restart
		ESP.restart();
	}
}

// config AP SSID  with indetifying mac
void configDeviceAP() {
	String Prefix = "Master_dw:";
	String Mac = WiFi.macAddress();
	String SSID = Prefix + Mac;
	String Password = "123456789";
	bool result = WiFi.softAP(SSID.c_str(), Password.c_str(), CHANNEL, 0);
	if (!result) {
		Serial.println("AP Config failed.");
	} else {
		Serial.println("AP Config Success. Broadcasting with AP: " + String(SSID));
		dw_M_SSID = String(SSID);
	}
}


//Structure example to send data
//Must match the receiver structure
typedef struct struct_message {
	int slaveNumber;
	int sensor1; // used to send the LED ON/OFF VALUES WE NEED 5 EVEN IN THE ONCE SENSOR VERSION
	int sensor2; // AS THERE ARE STILL 5 LED INDICATORS
	int sensor3;
	int sensor4;
	int sensor5;
  int setEmptyOrFull;
	char CharFromMstr[32];  // used to exchaqnge MAC VALUES
	char CharFromSlave1[32];  // used to exchaqnge MAC VALUES
	char CharFromSlave2[32];// used to exchaqnge MAC VALUES
	char CharFromSlave3[32];// used to exchaqnge MAC VALUES
	byte dw_pad;   // padding value in 5 sensor version, probably obsolete
	int doCalibrate1;  // To send calibration request to slave 
	int doCalibrate2;  // To send calibration request to slave 
	int doCalibrate3;  // To send calibration request to slave 
} struct_message;

// Create a struct_message called BME280Readings to hold VARIABLES TO SEND TO SLAVES
struct_message outgoingMasterToSlave;

// Create a struct_message to hold incoming sensor readings
struct_message incomingFromSlaves;

// 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");
	String macAddress1 = WiFi.macAddress();
	if (status == 0) {
		success = "Delivery Success :)";
		String Mac = WiFi.macAddress();
		Serial.println("AP Config Success. Broadcasting from MASTER with AP: " + String(Mac));
	} else {
		success = " OUTGOING DELIVER FAIL FAIL FAIL FAIL FAIL Delivery Fail :(";
	}
}

// Callback when data is received
void OnDataRecv(const uint8_t* mac, const uint8_t* incomingData, int len) {
	memcpy(&incomingFromSlaves, incomingData, sizeof(incomingFromSlaves));
	char macStr[18];
	snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
			 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
	Serial.print("Bytes received: ");
	Serial.println(len);
	Serial.print("////////////// LAST PACKET RECV FROM  ///////////: "); Serial.println(macStr);
	Serial.print("Last Packet Recv Data: "); Serial.println(*incomingData);
	Serial.print("Bytes received: ");
	Serial.println(len);
	Serial.print(" Mac address from SLAVE as CHAR  >>>>>>>>>>>>>>>: ");
	Serial.println(incomingFromSlaves.CharFromSlave1);
	delay(2000);
	macCapture = macStr; // capture the MAC id of the current slave being read
	findSave_SlaveMacAddr();// collect slave addresses, assign to a tank and then save to flash memory
	// save_SlaveMacAddr();  // we probably don't need this and it should be eliminated.

	save_FoundAllMacs();

	captureAndDisplaySlave1(); // capture incoming sensor values based on the slave slected 
	captureAndDisplaySlave2();
	captureAndDisplaySlave3();

}

void setup() {
	Serial.begin(115200);
	prefString.begin("macAssign", false);//for nonvolitile memory process  NO ERROR REPORTED IF MISSING!
	prefInteger.begin("allMacsSaved", false);//for nonvolitile memory process  NO ERROR REPORTED IF MISSING!

	pinMode(26, INPUT_PULLUP);  // enable internal pull-up  for calibration pushbutton	

	factoryReset();  // Check if -factory reset- button is pressed during startup
	displaySlaveMacAddr(); // This is for testing 

	pinMode(32, INPUT_PULLUP);  // for chooseTank switch 
	pinMode(33, INPUT_PULLUP); // for chooseTank switch 

	pinMode(4, OUTPUT); // this is for LED that indicates button has been pressed long enough
	pinMode(16, OUTPUT);
	pinMode(17, OUTPUT);
	pinMode(18, OUTPUT);
	pinMode(19, OUTPUT);
	pinMode(21, OUTPUT);
	pinMode(22, OUTPUT);
	pinMode(23, OUTPUT);

	WiFi.mode(WIFI_AP_STA);  // configure device AP mode	
	configDeviceAP();
	if (esp_now_init() != ESP_OK) {  // Init ESP-NOW  Set device as a Wi-Fi Station
		Serial.println("Error initializing ESP-NOW");
		return;
	}
	esp_now_register_send_cb(OnDataSent); // register for Send CB to get the status of Trasnmitted packet
	esp_now_peer_info_t peerInfo;  // Register peer
	memcpy(peerInfo.peer_addr, broadcastAddress, 6);
	peerInfo.channel = 0;
	peerInfo.encrypt = false;
	if (esp_now_add_peer(&peerInfo) != ESP_OK) {  // Add peer
		Serial.println("Failed to add peer");
		return;
	}
	esp_now_register_recv_cb(OnDataRecv);// Register callback function that is called when data is received
}

void loop() { // *************************** start loop *******************************

	Serial.println("*************************** start loop *******************************");
	Serial.println("*************************** start loop *******************************");
	Serial.println("*************************** start loop *******************************");
	Serial.print("VALUE OF  >>var<< DONE - WITH - MACS done collecting mac addresses  : ");
	Serial.println(saved_DoneWithMacs);
	Serial.print("VALUE OF  >>GET<<  DONE - WITH - MACS done collecting mac addresses  : ");
	Serial.println(prefInteger.getUInt("GetStatus", gotMacs));
	delay(2000);

	

	int	saved_GetMacsIsDone = prefInteger.getUInt("GetStatus", gotMacs);
	Serial.print("*******       THIS IS THE  VALUE   >>>>>>>>>>>  saved_GetMacsDone <<< >>>>>>> ");
	Serial.println(saved_GetMacsIsDone);
	Serial.print("         THIS IS  prefInteger.getUInt( 'tempValue2', gotMacs) >>>>>> ");
	Serial.println(prefInteger.getUInt("GetStatus", gotMacs));
	Serial.println("  ");

	//	delay(5000);
	displaySlaveMacAddr(); // this is just for testing 

	chooseTank = selectTank(); 	// part of set mac of slave function 

	pressForCalibrate(); //  read pushbutton to send calibrate request to slave

	valuesToSend();  // collect the values that will be sent to slaves
	//delay(5000);
	sendMessageToSlaves(); // send the message to the slave with esp-now

} // **********************************   end loop  **************************************************


void findSave_SlaveMacAddr() {
	Serial.println(" ***************   ARE LOOKING FOR MAC ADDRESSES ?????   ****************");
	Serial.print("   *********  VALUE OF >  ||| STOP FINDING MACS |||           >>>>>> : ");
	Serial.println(prefInteger.getUInt("GetStatus", gotMacs));
	if (stopFindingMacs == 0 && gotMacs == 0 && prefInteger.getUInt("GetStatus", gotMacs) == 0) {
		Serial.println(" *********  WE >>> ARE <<<< LOOKING FOR MAC ADDRESSES * LOOKING * LOOKING *LOOKING **");
		//	delay(5000);
		if (gotMacs == 0) {
			count_lookForMacAddr++;
		} // increment the counter if we got this far
		if (slaveOneMac == "00") {
			slaveOneMac = macCapture;
			prefString.putString("slaveNum1", slaveOneMac); // SAVE VALUE TO PERMANENT FLASH STORAGE
			saved_MacValue1 = prefString.getString("slaveNum1", slaveOneMac);
			macLock1 = 9999;
		}	// set one 
		if (slaveTwoMac == "00" && macLock1 == 9999 && macCapture != slaveOneMac) {
			slaveTwoMac = macCapture; // set two 
			prefString.putString("slaveNum2", slaveTwoMac); // SAVE VALUE TO PERMANENT FLASH STORAGE
			saved_MacValue2 = prefString.getString("slaveNum2", slaveTwoMac);
			macLock2 = 9999;
		}
		if (slaveTwoMac != "00" && macLock2 == 9999 && macLock1 == 9999 && macCapture != slaveOneMac && macCapture != slaveTwoMac) {
			slaveThreeMac = macCapture; // set three
			prefString.putString("slaveNum3", slaveThreeMac); // SAVE VALUE TO PERMANENT FLASH STORAGE
			saved_MacValue3 = prefString.getString("slaveNum3", slaveThreeMac);
			macLock3 = 9999;
		}
		Serial.println("");
		Serial.print("    value of dw_ssid >>>> ");
		Serial.println(dw_M_SSID);
		Serial.println("     ");
		Serial.print("      Value of XXX  >>>> ");
		Serial.println(macCapture);
		Serial.print("                                  Value of slaveOneMac >>>> ");
		Serial.println(slaveOneMac);
		Serial.print("                                  Value of slaveTwoMac >>>> ");
		Serial.println(slaveTwoMac);
		Serial.print("                                Value of slaveThreeMac >>>> ");
		Serial.println(slaveThreeMac);
		Serial.print("                                         value of counter : ");
		Serial.println(count_lookForMacAddr);

		if (slaveOneMac != "00" && slaveTwoMac != "00" && slaveThreeMac != "00") {
			Serial.println("               ");
			Serial.println("**********************  WE CAN SAVE THIS *******************************");
			Serial.println(" ***************  WE HAVE FOUND ALL THE SLAVES - STOP LOOKING !!!! ************");

			stopFindingMacs = 1; //if we got to here in find_SlaveMacAddr, set flag and stop looking for slaves
			 //if we got to here in find_SlaveMacAddr, set flag and stop looking for slaves
			if (stopFindingMacs == 1); {
				gotMacs = 1;  // this will go to nonvolitle stoage 
				Serial.println("stopFindingMacs = 1  && gotMacs = 1 **** NO LONGER LOOKING FOR MACS !!!!  *****");
				Serial.println("stopFindingMacs = 1  && gotMacs = 1 **** NO LONGER LOOKING FOR MACS !!!!  *****");
				displaySlaveMacAddr();
				//	delay(5000);
				Serial.print("                                                                gotMacs = :  ");
				Serial.println(gotMacs);
				//	delay(9000);
			}
		}
	}  //  stop finding if macs == 0
	if (count_lookForMacAddr >= 7) { gotMacs = 1; }
	// save_SlaveMacAddr();
} // end fuction 

void save_FoundAllMacs() { // Just save the flag that says all macs are found and stored
	Serial.println(" SEE IF WE ARE GOING TO SAVE THE DONE-WITH-MACS FLAG  ");
	saved_DoneWithMacs = prefInteger.getUInt("GetStatus", gotMacs);
	//	saved_DoneWithMacs = 0; // THIS IS FOR TESTING, >> TO RESET <<  SHOULD BE REMOVED FOR PRODUCTION
	if (saved_DoneWithMacs == 0) {
		prefInteger.putUInt("GetStatus", gotMacs);
	}
	saved_DoneWithMacs = prefInteger.getUInt("GetStatus", gotMacs);
}

void save_SlaveMacAddr() {  // this may not be needed as it was included in other function
	Serial.println("  BEFORE saving the macs TO FLASH STORAGE   ");

	Serial.print(" STOP FINDING MACS  =  :   ");
	Serial.println(stopFindingMacs);
	Serial.print("          GOT MACS  =  :   ");
	Serial.println(gotMacs);
	Serial.print("  GET U INIT GOT MACS  :   ");
	Serial.println(prefInteger.getUInt("GetStatus", gotMacs));
	delay(7000);
	if (stopFindingMacs == 0 && gotMacs == 0 && prefInteger.getUInt("GetStatus", gotMacs) == 0) {
		Serial.print("    WE NOW SAVING MAC  ADDRESSES ********************** ");
		delay(5000);
		if (slaveOneMac != "00" && slaveTwoMac != "00" && slaveThreeMac != "00") { // if all have macs, skip
			if (slaveOneMac != "00" && gotMacs == 0) {
				prefString.putString("slaveNum1", slaveOneMac); // if slot one is empty SAVE MAC TO FLASH 
				Serial.print(" SAVING * SAVING * SAVING  KEY SlaveNum1 TO FLASH display   saved_MacValue1   : ");
				saved_MacValue1 = prefString.getString("slaveNum1", slaveOneMac);
				Serial.println(saved_MacValue1);
				delay(9000);
			}
			if (slaveTwoMac != "00" && gotMacs == 0) {
				prefString.putString("slaveNum2", slaveTwoMac); // if slot two is empty SAVE MAC TO FLASH
				Serial.print(" SAVING * SAVING * SAVING  KEY SlaveNum2 TO FLASH display   saved_MacValue2   : ");
				saved_MacValue2 = prefString.getString("slaveNum2", slaveTwoMac);
				Serial.println(saved_MacValue2);
				delay(9000);
			}
			if (slaveThreeMac != "00" && gotMacs == 0) {
				prefString.putString("slaveNum3", slaveThreeMac); // if slot three is empty SAVE MAC TO FLASH
				Serial.print(" SAVING * SAVING * SAVING  KEY SlaveNum3 TO FLASH display   saved_MacValue3   : ");
				saved_MacValue3 = prefString.getString("slaveNum3", slaveThreeMac);
				Serial.println(saved_MacValue3);
				delay(9000);
			}
		} //end     of if to see if the slots are already filled.
	} //end    if stop finding macs  == 0 AND all macs have not been found


	/*
	saved_MacValue1 = prefString.getString("slaveNum1", slaveOneMac);
	delay(20);
	saved_MacValue2 = prefString.getString("slaveNum2", slaveTwoMac);
	delay(20);
	saved_MacValue3 = prefString.getString("slaveNum3", slaveThreeMac);
	*/
	//  prefString.end(); //  I don' know what this does
} // end if

void displaySlaveMacAddr() {
	Serial.println("// *********    this is the start of display  *****************************");
	Serial.println("");
	Serial.print(" saved_MacValue1: ");
	Serial.print(saved_MacValue1 + "  slaveOneMac: ");
	Serial.print(slaveOneMac + "  getString(slaveNum1:   ");
	Serial.println(prefString.getString("slaveNum1", slaveOneMac));

	Serial.print(" saved_MacValue2: ");
	Serial.print(saved_MacValue2 + "  slaveTwoMac: ");
	Serial.print(slaveTwoMac +   "  getString(slaveNum2:   ");
	Serial.println(prefString.getString("slaveNum2", slaveTwoMac));

	Serial.print(" saved_MacValue3: ");
	Serial.print(saved_MacValue3 + "slaveThreeMac: ");
	Serial.print(slaveThreeMac + " getString(slaveNum3:    ");
	Serial.println(prefString.getString("slaveNum3", slaveThreeMac));
	Serial.print(" FLASH SAVED VALUE OF |||| saved_DoneWithMacs ||| >>>: ");
	Serial.println(prefInteger.getUInt("GetStatus", gotMacs));
	Serial.print(" VARIABLE       Value of count_lookForMacAddr : ");
	Serial.println(count_lookForMacAddr);
	delay(5000);

	Serial.println("");
	//	prefInt.putUInt("slaveCounter1", count_lookForMacAddr);
	//	storedLookForMacCounter = prefInt.getUInt("slaveCounter1", count_lookForMacAddr);
	delay(20);
	Serial.print(">>>>>> value of saved_DoneWithMacs >>>>> : ");
	Serial.println(saved_DoneWithMacs);
	Serial.println(prefInteger.getUInt("GetStatus", gotMacs));

}

void captureAndDisplaySlave1() {  // capture incoming sensor values based on the slave slected 
	// if (incomingFromSlaves.slaveNumber == 949494 && chooseTank == 1) {
	if (prefString.getString("slaveNum1", slaveOneMac) == macCapture && chooseTank == 1) {
		incomingSlaveNumber = incomingFromSlaves.slaveNumber;
		incomingSensorVarA1 = incomingFromSlaves.sensor1;
		incomingSensorVarA2 = incomingFromSlaves.sensor2;
		incomingSensorVarA3 = incomingFromSlaves.sensor3;
		incomingSensorVarA4 = incomingFromSlaves.sensor4;
		incomingSensorVarA5 = incomingFromSlaves.sensor5;
		incomingSlaveMacA = incomingFromSlaves.CharFromSlave1[32];
				if (incomingSensorVarA1 == 1) { digitalWrite(18, HIGH); }
		if (incomingSensorVarA1 == 0) { digitalWrite(18, LOW); }
		if (incomingSensorVarA2 == 1) { digitalWrite(19, HIGH); }
		if (incomingSensorVarA2 == 0) { digitalWrite(19, LOW); }
		if (incomingSensorVarA3 == 1) { digitalWrite(21, HIGH); }
		if (incomingSensorVarA3 == 0) { digitalWrite(21, LOW); }
		if (incomingSensorVarA4 == 1) { digitalWrite(22, HIGH); }
		if (incomingSensorVarA4 == 0) { digitalWrite(22, LOW); }
		if (incomingSensorVarA5 == 1) { digitalWrite(23, HIGH); }
		if (incomingSensorVarA5 == 0) { digitalWrite(23, LOW); }
		updateDisplay1();
	}
} // end of read the incoming sensor values 

void captureAndDisplaySlave2() {
	if (prefString.getString("slaveNum2", slaveTwoMac) == macCapture && chooseTank == 2) {
		incomingSlaveNumber = incomingFromSlaves.slaveNumber;
		incomingSensorVarB1 = incomingFromSlaves.sensor1;
		incomingSensorVarB2 = incomingFromSlaves.sensor2;
		incomingSensorVarB3 = incomingFromSlaves.sensor3;
		incomingSensorVarB4 = incomingFromSlaves.sensor4;
		incomingSensorVarB5 = incomingFromSlaves.sensor5;
		if (incomingSensorVarB1 == 1) {
			digitalWrite(18, HIGH);
		}
		if (incomingSensorVarB1 == 0) {
			digitalWrite(18, LOW);
		}
		if (incomingSensorVarB2 == 1) {
			digitalWrite(19, HIGH);
		}
		if (incomingSensorVarB2 == 0) {
			digitalWrite(19, LOW);
		}
		if (incomingSensorVarB3 == 1) {
			digitalWrite(21, HIGH);
		}
		if (incomingSensorVarB3 == 0) {
			digitalWrite(21, LOW);
		}
		if (incomingSensorVarB4 == 1) {
			digitalWrite(22, HIGH);
		}
		if (incomingSensorVarB4 == 0) {
			digitalWrite(22, LOW);
		}
		if (incomingSensorVarB5 == 1) {
			digitalWrite(23, HIGH);
		}
		if (incomingSensorVarB5 == 0) {
			digitalWrite(23, LOW);
		}
		updateDisplay2();
	}
}

void captureAndDisplaySlave3() {
	if (prefString.getString("slaveNum3", slaveThreeMac) == macCapture && chooseTank == 3) {
		incomingSlaveNumber = incomingFromSlaves.slaveNumber;
		incomingSensorVarC1 = incomingFromSlaves.sensor1;
		incomingSensorVarC2 = incomingFromSlaves.sensor2;
		incomingSensorVarC3 = incomingFromSlaves.sensor3;
		incomingSensorVarC4 = incomingFromSlaves.sensor4;
		incomingSensorVarC5 = incomingFromSlaves.sensor5;
		if (incomingSensorVarC1 == 1) {
			digitalWrite(18, HIGH);
		}
		if (incomingSensorVarC1 == 0) {
			digitalWrite(18, LOW);
		}
		if (incomingSensorVarC2 == 1) {
			digitalWrite(19, HIGH);
		}
		if (incomingSensorVarC2 == 0) {
			digitalWrite(19, LOW);
		}
		if (incomingSensorVarC3 == 1) {
			digitalWrite(21, HIGH);
		}
		if (incomingSensorVarC3 == 0) {
			digitalWrite(21, LOW);
		}
		if (incomingSensorVarC4 == 1) {
			digitalWrite(22, HIGH);
		}
		if (incomingSensorVarC4 == 0) {
			digitalWrite(22, LOW);
		}
		if (incomingSensorVarC5 == 1) {
			digitalWrite(23, HIGH);
		}
		if (incomingSensorVarC5 == 0) {
			digitalWrite(23, LOW);
		}
		updateDisplay3();
	}
}

byte selectTank() {// function to choose the active tank (tank being displayed) 
	if (digitalRead(32) == 0 && (digitalRead(33) == 0)) { chooseTank = 1; }
	if (digitalRead(32) == 1 && (digitalRead(33) == 1)) { chooseTank = 3; }
	if (digitalRead(32) == 0 && (digitalRead(33) == 1)) { chooseTank = 2; }
	if (digitalRead(32) == 1 && (digitalRead(33) == 0)) { chooseTank = 2; }
	Serial.print(" chooseTank  value  >>>> :  ");
	Serial.println(chooseTank);
	delay(1000);
	if (chooseTank == 1) {
		digitalWrite(16, LOW);
		digitalWrite(17, LOW);
		digitalWrite(4, HIGH);
	}
	if (chooseTank == 2) {
		digitalWrite(4, LOW);
		digitalWrite(17, LOW);
		digitalWrite(16, HIGH);
	}
	if (chooseTank == 3) {
		digitalWrite(16, LOW);
		digitalWrite(4, LOW);
		digitalWrite(17, HIGH);
	}
	return chooseTank;
}

void blinkLED1() { 	 // blink an LED to show request has be registered
	digitalWrite(4, HIGH); 	delay(300); digitalWrite(4, LOW); 	delay(900);
	digitalWrite(4, HIGH); 	delay(300);
	digitalWrite(4, LOW); delay(300); digitalWrite(4, HIGH); delay(300); digitalWrite(4, LOW); 	delay(300);
	digitalWrite(4, HIGH); 	delay(300);	digitalWrite(4, LOW); digitalWrite(4, HIGH); 	delay(500);
	digitalWrite(4, LOW); 	delay(300);	digitalWrite(4, HIGH); 	delay(300);
	digitalWrite(4, LOW); delay(300); digitalWrite(4, HIGH); delay(300); digitalWrite(4, LOW); 	delay(300);
	digitalWrite(4, HIGH); 	delay(300);	digitalWrite(4, LOW);
}
void blinkLED2() { 	 // blink an LED to show request has be registered
	digitalWrite(16, HIGH); 	delay(300); digitalWrite(16, LOW); 	delay(900);
	digitalWrite(16, HIGH); 	delay(300);
	digitalWrite(16, LOW); delay(300); digitalWrite(16, HIGH); delay(300); digitalWrite(16, LOW); 	delay(300);
	digitalWrite(16, HIGH); 	delay(300);	digitalWrite(16, LOW); digitalWrite(16, HIGH); 	delay(500);
	digitalWrite(16, LOW); 	delay(300);	digitalWrite(16, HIGH); 	delay(300);
	digitalWrite(16, LOW); delay(300); digitalWrite(16, HIGH); delay(300); digitalWrite(16, LOW); 	delay(300);
	digitalWrite(16, HIGH); 	delay(300);	digitalWrite(16, LOW);
}
void blinkLED3() { 	 // blink an LED to show request has be registered
	digitalWrite(17, HIGH); 	delay(300); digitalWrite(17, LOW); 	delay(900);
	digitalWrite(17, HIGH); 	delay(300);
	digitalWrite(17, LOW); delay(300); digitalWrite(17, HIGH); delay(300); digitalWrite(17, LOW); 	delay(300);
	digitalWrite(17, HIGH); 	delay(300);	digitalWrite(17, LOW); digitalWrite(17, HIGH); 	delay(500);
	digitalWrite(17, LOW); 	delay(300);	digitalWrite(17, HIGH); 	delay(300);
	digitalWrite(17, LOW); delay(300); digitalWrite(17, HIGH); delay(300); digitalWrite(17, LOW); 	delay(300);
	digitalWrite(17, HIGH); 	delay(300);	digitalWrite(17, LOW);
}

























void factoryReset() {
	Serial.print("   inside    factory reset  BEFORE BUTTON CHECK  ");
	delay(50);
	if (digitalRead(26) == 0) {
		delay(50);
		blinkLED1();
		gotMacs = 0;
		slaveOneMac = "00";
		saved_MacValue1 = "00";
		saved_MacValue2 = "00";
		slaveThreeMac = "00";
		saved_MacValue3 = "00";
		prefInteger.putUInt("GetStatus", gotMacs);
		prefString.putString("slaveNum1", slaveOneMac);
		prefString.putString("slaveNum2", slaveTwoMac);
		prefString.putString("slaveNum3", slaveThreeMac);
		Serial.print("            inside >>> AFTER <<<  BUTTON IS PUSHEED   factory reset  gotMacs  =  >>>> ");
		displaySlaveMacAddr();
		Serial.println(gotMacs);

		Serial.print("                      the get saved value >  value >>>> ");
		Serial.println(prefInteger.getUInt("GetStatus", gotMacs));
		//	delay(6000); // to be able to read the message
	}
}


void pressForCalibrate() {
	if (digitalRead(26) == 0 && chooseTank == 1) {
		blinkLED1();
		doCalibrate1 = 9999;
		Serial.print("            inside    readPushButton() function  Value of doCalibrate1 >>>> ");
		Serial.println(doCalibrate1);
		delay(500); // to be able to read the message
		Serial.print("                                                  alue of doCalibrate1 >>>> ");
		Serial.println(doCalibrate1);
		delay(2000); // to be able to read the message
	}
	if (digitalRead(26) == 0 && chooseTank == 2) {
		blinkLED2();
		doCalibrate2 = 9999;
		Serial.print("            inside    readPushButton() function  Value of doCalibrate1 >>>> ");
		Serial.println(doCalibrate2);
		delay(500); // to be able to read the message
		Serial.print("                                                 Value of doCalibrate1 >>>> ");
		Serial.println(doCalibrate2);
	}
	if (digitalRead(26) == 0 && chooseTank == 3) {
		blinkLED3();
		doCalibrate3 = 9999;
		Serial.print("            inside    readPushButton() function  Value of doCalibrate1 >>>> ");
		Serial.println(doCalibrate3);
		delay(500); // to be able to read the message
		Serial.print("                                                  Value of doCalibrate1 >>>> ");
		Serial.println(doCalibrate3);
	}

} // end of read pushbutton

void valuesToSend() {    // values TO SEND TO SLAVE
	char StingToChar[32]; // ; // " test one in TO SEND";
	myMac.toCharArray(StingToChar, sizeof(StingToChar));
	strcpy(outgoingMasterToSlave.CharFromMstr, StingToChar);

	outgoingMasterToSlave.CharFromMstr[32]; // = "assign var in To Send";

	outgoingMasterToSlave.dw_pad = paddingValue;
	outgoingMasterToSlave.doCalibrate1 = doCalibrate1;
}

void sendMessageToSlaves() {  // Send the message from Master to Slave via ESP-NOW

	Serial.print("sending padding and doCalibrate >>>>>> : ");
	Serial.print(outgoingMasterToSlave.dw_pad);
	Serial.print("  <<<<<<  >>> ");
	Serial.print(outgoingMasterToSlave.doCalibrate1);
	Serial.println("  <<<<<<  sending char from master 32  ");
	Serial.print(outgoingMasterToSlave.CharFromMstr[32]);
	Serial.println("  <<<<<<   ");


	//delay(5000); // to be able to read the message
	esp_err_t result = esp_now_send(broadcastAddress, (uint8_t*)&outgoingMasterToSlave, sizeof(outgoingMasterToSlave));
	if (result == ESP_OK) { // If the message was recived by the slave, do the following
		Serial.println("***************MESSEGE RECEIVED BY A SLAVE with success  ********************");
		if (outgoingMasterToSlave.doCalibrate1 == 9999) { // if 9999 was recieved SUCCESFULLY, reset the value to 1111 
			doCalibrate1 = 1111;
		}
	} else {
		Serial.println("ERROR SENDING DATA TO THE SLAVE ");
		delay(1000); // allow time to read the message
	}
	delay(150); // allow time to read the message
}

void updateDisplay1() { // create function Display Readings in Serial Monitor
	String Mac = WiFi.macAddress();
	//  Serial.print("MAC OF THE MASTER CARD =====================   :  ");
	// Serial.println(Mac);
	Serial.println("======== THIS IS THE MASTER AND LED DISPLAY   >>111  ONE  111<<  ====== (14)");
	Serial.print("SAVED mac value");
	Serial.println(prefString.getString("slaveNum1", slaveOneMac));
	Serial.print("CURRENT  mac value");
	Serial.println(macCapture);
	Serial.print("TANK 1  TANK 1      TANK 1");


	Serial.print("incomingSensorVarA1:   ");
	Serial.println(incomingSensorVarA1);
	Serial.print("incomingSensorVarA2:   ");

	Serial.println(incomingSensorVarA2);
	Serial.print("incomingSensorVarA3:   ");
	Serial.println(incomingSensorVarA3);
	Serial.print("incomingSensorVarA4  : ");
	Serial.println(incomingSensorVarA4);
	Serial.print("incomingSensorVarA5  : ");
	Serial.println(incomingSensorVarA5);
	Serial.print(" Mac address from SLAVE as CHAR  >>>>>>>>>>>>>>>: ");
	Serial.println(incomingFromSlaves.CharFromSlave1);
	Serial.println(incomingSlaveMacA);

	Serial.print("   //////////            Start of loop -- doCalibrate1 =  : "); Serial.println(doCalibrate1);
	Serial.print(" digitalRead(26) ====  : "); Serial.println(digitalRead(26));

	//Serial.print("                             BUTTON STATUS  IS  >>>>>   : ");  Serial.println(pinValue);
//	Serial.print("    outgoing From Master To Slave.reSetCalFlag IS >>>   : ");
//	Serial.println(outgoingMasterToSlave.requestToCalibrateSensors);
	delay(3000);
}

void updateDisplay2() { // create function to Display Readings in Serial Monitor
	String Mac = WiFi.macAddress();
	Serial.print("MAC OF THE MASTER CARD =====================  :  ");
	Serial.println(Mac);
	Serial.println("== THIS IS THE MASTER AND LED DISPLAY     >>TWO<< ");
	Serial.print("SAVED mac value");
	Serial.println(prefString.getString("slaveNum2", slaveTwoMac));
	Serial.print("CURRENT  mac value");
	Serial.println(macCapture);
	Serial.print("TANK 2  TANK 2      TANK 2");
	Serial.print("incomingSensorVarB1:  ");
	Serial.println(incomingSensorVarB1);
	Serial.print("incomingSensorVarB2:  ");
	Serial.println(incomingSensorVarB2);
	Serial.print("incomingSensorVarB3:  ");
	Serial.println(incomingSensorVarB3);
	Serial.print("incomingSensorVarB4 : ");
	Serial.println(incomingSensorVarB4);
	Serial.print("incomingSensorVarB5 : ");
	Serial.println(incomingSensorVarB5);
}

void updateDisplay3() { // create function  Display Readings in Serial Monitor
	String Mac = WiFi.macAddress();
	Serial.print("MAC OF THE MASTER CARD ===================== :  ");
	Serial.println(Mac);
	Serial.println("== THIS IS THE MASTER AND LED DISPLAY     >>333 THREE  333<< ");
	Serial.print("SAVED mac value");
	Serial.println(prefString.getString("slaveNum3", slaveThreeMac));
	Serial.print("CURRENT  mac value");
	Serial.println(macCapture);
	Serial.print("TANK 3  TANK 3      TANK 3");
	Serial.print("incomingSensorVarC1: ");
	Serial.println(incomingSensorVarC1);
	Serial.print("incomingSensorVarC2: ");
	Serial.println(incomingSensorVarC2);
	Serial.print("incomingSensorVarC3: ");
	Serial.println(incomingSensorVarC3);
	Serial.print("incomingSensorVarC4  ");
	Serial.println(incomingSensorVarC4);
	Serial.print("incomingSensorVarC5: ");
	Serial.println(incomingSensorVarC5);
	delay(5000);
}
Latest posts by Don Winchell (see all)
Please share it. Thanks!