Thanks
The library i'm using is

which I found in my Arduino IDE library list.
The project is a real life application and I started working on it during covid, gave up and recently picked it up again.
For my project the "Buttons" are room presence sensors, which have a normally open relay output.
Walk in the room, sensor activates, relay closes and stays closed for as long as its activated and goes off around ten minutes after leaving the room. Like a single long press button which gets pressed once and doesn't matter what happens when button is released?
The added complication is that these PIR presence sensors are ten in number and are located in two parts of the building, 4 in one place and 6 in another.
So I'm using 2 ESP32s which communicate using ESPNOW , and the hardware is quite straight forward.
The Master has 4 inputs
GPIO PINS 32 33 18 19 each to GND when switched by room occupancy sensor
and the Slave has 6.
GPIO PINS 4 5 12 13 14 15 to GND when switched by room occupancy sensor
All activations are collected by the Slave which uses serial2.println to send the relevent string to my network using the UART 2 of the slave via a Waveshare UART TTL to ETHERNET converter.
GPIO PIN 16 (RX) to TX on Waveshare Uart to Ethernet module
GPIO PIN 17 (TX) to RX on Waveshare Uart to Ethernet module
2-Channel UART to Ethernet Converter (CH9121) | The Pi Hut
These TTL strings are read by NODERED which creates actions like "switch lamp on" etc
I have cobbled together the Master and Slave codes as below and it sort of works although the Serial output from the Uart is a bit slow and temperamental (if I watch from the inbuilt serial monitor it's a much faster response)
I think this may be to do with the order I have things happening in the code.
Ive tried to study the arrays used in the code to send button data from the master to the Slave but still need to improve my understanding of arrays and pointers.
Hope this is enough for you to look at!
Her is the full code for review
MASTER
```cpp
#include <esp_now.h>
#include <WiFi.h>
#define CHANNEL 1
esp_now_peer_info_t slave;
uint8_t gpios[] = { 32, 33, 18, 19 };
int gpioCount;
uint8_t macSlaves[][6] = {
{ 0x24, 0x62, 0xAB, 0xF2, 0x14, 0xE4 }
};
void setup() {
Serial.begin(9600);
gpioCount = sizeof(gpios) / sizeof(uint8_t);
WiFi.mode(WIFI_STA);
Serial.print("Mac Address in Station: ");
Serial.println(WiFi.macAddress());
InitESPNow();
int slavesCount = sizeof(macSlaves) / 6 / sizeof(uint8_t);
for (int i = 0; i < slavesCount; i++) {
slave.channel = CHANNEL;
slave.encrypt = 0;
memcpy(slave.peer_addr, macSlaves[i], sizeof(macSlaves[i]));
esp_now_add_peer(&slave);
}
esp_now_register_send_cb(OnDataSent);
for (int i = 0; i < gpioCount; i++) {
pinMode(gpios[i], INPUT_PULLUP);
}
send();
}
void InitESPNow() {
if (esp_now_init() == ESP_OK) {
Serial.println("ESPNow Init Success");
}
else {
Serial.println("ESPNow Init Failed");
ESP.restart();
}
}
void send() {
uint8_t values[gpioCount];
for (int i = 0; i < gpioCount; i++) {
values[i] = digitalRead(gpios[i]);
}
uint8_t broadcast[] = { 0x24, 0x62, 0xAB, 0xF2, 0x14, 0xE4 };
esp_err_t result = esp_now_send(broadcast, (uint8_t *)&values, sizeof(values));
Serial.print("Send Status: ");
if (result == ESP_OK) {
Serial.println("Success");
}
else {
Serial.println("Error");
}
delay(500);
}
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
char macStr[18];
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("Sent to: ");
Serial.println(macStr);
Serial.print("Status: ");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Success" : "Fail");
send();
}
void loop() {
}
SLAVE
//SLAVE Using ESP32 Button Library
// use UART2 pins 16RX 17TX
#include <esp_now.h>
#include <WiFi.h>
#include <Arduino.h>
#include "Button.h"
#include <HardwareSerial.h>
#define RXD2 16
#define TXD2 17
HardwareSerial SerialPort(2); //Use UART2
//On Slave Board
static void onButtonPressDownCb1(void *btn, void *usr_data) {
Serial.println("B4WARDON");
Serial2.println("B4WARDON");
}
static void onButtonPressDownCb2(void *btn, void *usr_data) {
Serial.println("B5BATHON");
Serial2.println("B5BATHON");
}
static void onButtonPressDownCb3(void *btn, void *usr_data) {
Serial.println("B1WARDON");
Serial2.println("B1WARDON");
}
static void onButtonPressDownCb4(void *btn, void *usr_data) {
Serial.println("B1BATHON");
Serial2.println("B1BATHON");
}
static void onButtonPressDownCb5(void *btn, void *usr_data) {
Serial.println("B1TOILON");
Serial2.println("B1TOILON");
}
static void onButtonPressDownCb6(void *btn, void *usr_data) {
Serial.println("B4BATHON");
Serial2.println("B4BATHON");
}
static void onButtonPressDownCb7(void *btn, void *usr_data) {
Serial.println("B3WARDON");
Serial2.println("B3WARDON");
}
static void onButtonPressDownCb8(void *btn, void *usr_data) {
Serial.println("B2WARDON");
Serial2.println("B2WARDON");
}
static void onButtonPressDownCb9(void *btn, void *usr_data) {
Serial.println("B2BATHON");
Serial2.println("B2BATHON");
}
static void onButtonPressDownCb10(void *btn, void *usr_data) {
Serial.println("B3BATHON");
Serial2.println("B3BATHON");
}
//ON MASTER BOARD
int switchPin7 = 32; // room 3 -Pin 16 sent to ESP slave - Pulled up - When pulled down to GND shows as B3WARDON
int switchPin10 = 33; // room 3A -Pin 17 sent to ESP slave - Pulled up - When pulled down to GND shows as B3BATHON
int switchPin8 = 18; // room 2 - Pin 18 sent to ESP slave - Pulled up - When pulled down to GND shows as B2WARDON
int switchPin9 = 19; // room 2A - Pin 19 sent to ESP slave - Pulled up - When pulled down to GND shows as B2BATHON
uint8_t gpios[] = { 32, 33, 18, 19 };
int gpioCount;
void setup() {
Serial.begin(9600);
while (!Serial)
;
//SerialPort.begin(19200, SERIAL_8N1, 16, 17);
Serial2.begin(115200, SERIAL_8N2, RXD2, TXD2);
while (!Serial2)
;
Button *btn1 = new Button(GPIO_NUM_4, false);
Button *btn2 = new Button(GPIO_NUM_5, false);
Button *btn3 = new Button(GPIO_NUM_12, false);
Button *btn4 = new Button(GPIO_NUM_13, false);
Button *btn5 = new Button(GPIO_NUM_14, false);
Button *btn6 = new Button(GPIO_NUM_15, false);
Button *btn7 = new Button(GPIO_NUM_32, false);
Button *btn8 = new Button(GPIO_NUM_18, false);
Button *btn9 = new Button(GPIO_NUM_19, false);
Button *btn10 = new Button(GPIO_NUM_33, false);
btn1->attachPressDownEventCb(&onButtonPressDownCb1, NULL);
btn2->attachPressDownEventCb(&onButtonPressDownCb2, NULL);
btn3->attachPressDownEventCb(&onButtonPressDownCb3, NULL);
btn4->attachPressDownEventCb(&onButtonPressDownCb4, NULL);
btn5->attachPressDownEventCb(&onButtonPressDownCb5, NULL);
btn6->attachPressDownEventCb(&onButtonPressDownCb6, NULL);
btn7->attachPressDownEventCb(&onButtonPressDownCb7, NULL);
btn8->attachPressDownEventCb(&onButtonPressDownCb8, NULL);
btn9->attachPressDownEventCb(&onButtonPressDownCb9, NULL);
btn10->attachPressDownEventCb(&onButtonPressDownCb10, NULL);
gpioCount = sizeof(gpios) / sizeof(uint8_t);
WiFi.mode(WIFI_STA);
//Serial.print("Mac Address in Station: ");
//Serial.println(WiFi.macAddress());
InitESPNow();
esp_now_register_recv_cb(OnDataRecv);
for (int i = 0; i < gpioCount; i++) {
pinMode(gpios[i], OUTPUT);
}
}
void InitESPNow() {
if (esp_now_init() == ESP_OK) {
Serial.println("ESPNow Init Success");
} else {
Serial.println("ESPNow Init Failed");
ESP.restart();
}
}
void OnDataRecv(const uint8_t *mac_addr, const uint8_t *data, int data_len) {
char macStr[18];
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("Received from: ");
//Serial.println(macStr);
//Serial.println("");
//Serial2.print("Received from: ");
//Serial2.println(macStr);
//Serial2.println("");
for (int i = 0; i < gpioCount; i++) {
digitalWrite(gpios[i], data[i]);
//delay(100);
}
}
void loop() {
delay(10);
}
Graham