I am using 2 ESP32 DEV boards to communicate with each other as Master and Slave and have used the example sketches MASTER and SLAVE from the Arduino IDE (Examples>ESP32>ESPNOW>Multislave)
to enable communication between them
The communication works well and following some modification I am able to define GPIO 16 as an output on the master and GPIO 16 as an INPUT on the slave and on taking the pin high on the master operate an LED attached to GPIO 16 on the slave. as per the snippet of code below.
MASTER
#define CHANNEL 3
#define IR_PIN 16
#define NUM_SLAVES 20 // ESP-Now can handle a maximum of 20 slaves
#define PRINTSCANRESULTS 0
int slaveCount = 0; // Keeps count of no. of slaves with the defined prefix
esp_now_peer_info_t slaves[NUM_SLAVES]; // Stores the information of each of the slave that is added as a peer
void initESPNow();
void manageSlaves();
void scanForSlaves();
void onDataSent(const uint8_t *mac_addr, esp_now_send_status_t status);
void onDataRecv(const uint8_t *mac_add, const uint8_t *data, int data_len);
void sendData(uint8_t data);
void setup()
{
Serial.begin(115200);
pinMode(IR_PIN, INPUT);
//Set device in STA mode to begin with
WiFi.mode(WIFI_MODE_STA);
// This is the mac address of the Master in Station Mode
Serial.print("STA MAC: ");
Serial.println(WiFi.macAddress());
// Init ESPNow with a fallback logic
initESPNow();
// Once ESPNow is successfully Init, we will register for Send CB to
// get the status of Trasnmitted packet
esp_now_register_send_cb(onDataSent);
esp_now_register_recv_cb(onDataRecv);
scanForSlaves();
manageSlaves();
}
void loop()
{
Serial.println("Digital Read: " + String(digitalRead(IR_PIN)));
if(slaveCount > 0)
{
if(digitalRead(IR_PIN))
{
// Send 1 to turn on the LED
sendData(1);
}
else
{
// Send 0 to turn off the LED
sendData(0);
}
}
delay(100);
}
SLAVE
#define GPIO 16
#define CHANNEL 1
#define SENDCHANNEL 1
#define WIFI_DEFAULT_CHANNEL 3
uint8_t gpioStatus = 0;
esp_now_peer_info_t peer;
void initESPNow();
void configDeviceAP();
void addPeer(uint8_t *peerMacAddress);
void onDataSent(const uint8_t *mac_addr, esp_now_send_status_t status);
void onDataRecv(const uint8_t *mac_addr, const uint8_t *data, int data_len);
void setup()
{
Serial.begin(115200);
//Set device in AP mode to begin with
WiFi.mode(WIFI_AP);
// configure device AP mode
configDeviceAP();
// This is the mac address of the Slave in AP Mode
Serial.print("AP MAC: ");
Serial.println(WiFi.softAPmacAddress());
// Init ESPNow with a fallback logic
initESPNow();
// Once ESPNow is successfully Init, we will register for recv CB to
// get recv packer info.
esp_now_register_send_cb(onDataSent);
esp_now_register_recv_cb(onDataRecv);
pinMode(GPIO, OUTPUT);
}
void loop()
{
digitalWrite(GPIO, gpioStatus);
delay(10);
}
I now want to Define a second GPIO Pin (say 17) on the Master and Slave with the objective of allowing
each of the pins on the master to activate its counterpart on the slave independently.
It is easy to define an additional pin on the master and send data when its is switched from LOW to HIGH but I cannot figure out how to deal with the receipt of the data by the slave to route it to the correct GPIO pin. When I try any pin activation (16 or 17) on the master results in both pins on the slave being activated.
Are there any ESPNow tutorials dealing with this issue or does anyone have any ideas to help me?
Thanks