Relay Board Web Server Control using ESP NOW

I’m working on a project that will use one ESP32 to control multiple relay boards using ESP NOW.
At the moment I’m struggling to get the web server working and am getting these errors:
‘relayGPIOs’ was not declared in this scope
‘RELAY_NO’ was not declared in this scope

I’m very new to all of this and am very inexperienced.

#include <Arduino.h>
#include <esp_now.h>
#include <WiFi.h>
#include <FS.h>
#include "ESPAsyncWebServer.h"

// Replace with your network credentials
const char* ssid = "CNE Creative Guest";
const char* password = "XXXXXXXXXXXXXXX";

uint8_t broadcastAddress[] = {0xCC, 0xDB, 0xA7, 0x5A, 0x2C, 0xF4};

// Structure example to send data
// Must match the receiver structure
typedef struct struct_message {
  char a[32];
  int b;
  float c;
  bool d;
} struct_message;

const char* PARAM_INPUT_1 = "relay";  
const char* PARAM_INPUT_2 = "state";

// Create AsyncWebServer object on port 80
AsyncWebServer server(80);

const char index_html[] PROGMEM = R"rawliteral(
  <meta name="viewport" content="width=device-width, initial-scale=1">
    html {font-family: Arial; display: inline-block; text-align: center;}
    h2 {font-size: 3.0rem;}
    p {font-size: 3.0rem;}
    body {max-width: 600px; margin:0px auto; padding-bottom: 25px;}
    .switch {position: relative; display: inline-block; width: 120px; height: 68px} 
    .switch input {display: none}
    .slider {position: absolute; top: 0; left: 0; right: 0; bottom: 0; background-color: #ccc; border-radius: 34px}
    .slider:before {position: absolute; content: ""; height: 52px; width: 52px; left: 8px; bottom: 8px; background-color: #fff; -webkit-transition: .4s; transition: .4s; border-radius: 68px}
    input:checked+.slider {background-color: #2196F3}
    input:checked+.slider:before {-webkit-transform: translateX(52px); -ms-transform: translateX(52px); transform: translateX(52px)}
  <h2>ESP Web Server</h2>
<script>function toggleCheckbox(element) {
  var xhr = new XMLHttpRequest();
  if(element.checked){"GET", "/update?relay=""&state=1", true); }
  else {"GET", "/update?relay=""&state=0", true); }

// Set number of relays
#define NUM_RELAYS  8

// Assign each GPIO to a relay
int relayGPIOs[NUM_RELAYS] = {19, 18, 5, 17, 16, 4, 2, 15};

// Replaces placeholder with button section in your web page
String processor(const String& var){
    String buttons ="";
    for(int i=1; i<8; i++){
      String relayState(int i);
      String relayStateValue = relayState(i);
      buttons+= "<h4>Relay #" + String(i) + " - GPIO " + relayGPIOs[i-1] + "</h4><label class=\"switch\"><input type=\"checkbox\" onchange=\"toggleCheckbox(this)\" id=\"" + String(i) + "\" "+ relayStateValue +"><span class=\"slider\"></span></label>";
    return buttons;
  return String();

String relayState(int numRelay){
      return "";
    else {
      return "checked";
  else {
      return "checked";
    else {
      return "";
  return "";

void setup(){
  // Serial port for debugging purposes

  // Set all relays to off when the program starts - if set to Normally Open (NO), the relay is off when you set the relay to HIGH
  for(int i=1; i<8; i++){
    pinMode(relayGPIOs[i-1], OUTPUT);
      digitalWrite(relayGPIOs[i-1], HIGH);
      digitalWrite(relayGPIOs[i-1], LOW);
  // Connect to Wi-Fi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.println("Connecting to WiFi..");

  // Print ESP32 Local IP Address

  // Route for root / web page
  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/html", index_html, processor);

  // Send a GET request to <ESP_IP>/update?relay=<inputMessage>&state=<inputMessage2>
  server.on("/update", HTTP_GET, [] (AsyncWebServerRequest *request) {
    String inputMessage;
    String inputParam;
    String inputMessage2;
    String inputParam2;
    // GET input1 value on <ESP_IP>/update?relay=<inputMessage>
    if (request->hasParam(PARAM_INPUT_1) & request->hasParam(PARAM_INPUT_2)) {
      inputMessage = request->getParam(PARAM_INPUT_1)->value();
      inputParam = PARAM_INPUT_1;
      inputMessage2 = request->getParam(PARAM_INPUT_2)->value();
      inputParam2 = PARAM_INPUT_2;
        Serial.print("NO ");
        digitalWrite(relayGPIOs[inputMessage.toInt()-1], !inputMessage2.toInt());
        Serial.print("NC ");
        digitalWrite(relayGPIOs[inputMessage.toInt()-1], inputMessage2.toInt());
    else {
      inputMessage = "No message sent";
      inputParam = "none";
    Serial.println(inputMessage + inputMessage2);
    request->send(200, "text/plain", "OK");
  // Start server
void loop() {


I don’t understand this error message because the array properly exsits at the top of the file.

This seems to indicuate “Relay Normally-Open”, and depending on whether it’s 0 or 1 it’ll either write HIGH or LOW to the relay control pins and for the state check a HIGH maps to unchecked and LOW maps to checked, else it’s the reversed.

So… I’d just add #define RELAY_NO 1 to the top of the file if you have normally-closed relays, else define it to 0.

Well to be pedantic you could try replacing [] with [&] so that automatic variable capture is turned on for the lambda.