ESP-NOW Web Server Error

I’m currently working on an ESP-Now project where I’m creating a web server to control several relays.

I have the following error in the receiver code:

collect2.exe: error: ld returned 1 exit status
*** [.pio\build\upesy_wroom\firmware.elf] Error 1

This is what my code looks like. I was looking into it and I’m not sure if its a driver issue or something in my code. I’m very new to all of this and don’t really know what I’m doing.

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

// Set to true to define Relay as Normally Open (NO)
#define RELAY_NO    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};

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

// Create a struct_message called myData
struct_message myData;

// callback function that will be executed when data is received
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
  memcpy(&myData, incomingData, sizeof(myData));
  Serial.print("Bytes received: ");
  Serial.println(len);
  Serial.print("Char: ");
  Serial.println(myData.a);
  Serial.print("Int: ");
  Serial.println(myData.b);
  Serial.print("Float: ");
  Serial.println(myData.c);
  Serial.print("Bool: ");
  Serial.println(myData.d);
  Serial.println();
}
 
void setup() {
  // Initialize Serial Monitor
  Serial.begin(115200);
  
  // Set device as a Wi-Fi Station
  WiFi.mode(WIFI_STA);

  // Init ESP-NOW
  if (esp_now_init() != ESP_OK) {
    Serial.println("Error initializing ESP-NOW");
    return;
  }
  
  // Once ESPNow is successfully Init, we will register for recv CB to
  // get recv packer info
  esp_now_register_recv_cb(OnDataRecv);
  
}

That’s not the real error message. That would be the one coming one line before it, which you didn’t show.

Plugging your code in a new project with the following platformio.ini (which you also didn’t show the version you’re using):

[env:upesy_wroom]
platform = espressif32
board = upesy_wroom
framework = arduino
lib_deps =
   ottowinter/ESPAsyncWebServer-esphome@^3.0.0

you indeed get the error

c:/users/max/.platformio/packages/toolchain-xtensa-esp32@8.4.0+2021r2-patch5/bin/…/lib/gcc/xtensa-esp32-elf/8.4.0/…/…/…/…/xtensa-esp32-elf/bin/ld.exe: .pio\build\upesy_wroom\libFrameworkArduino.a(main.cpp.o):(.literal._Z8loopTaskPv+0x8): undefined reference to loop()' c:/users/max/.platformio/packages/toolchain-xtensa-esp32@8.4.0+2021r2-patch5/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld.exe: .pio\build\upesy_wroom\libFrameworkArduino.a(main.cpp.o): in function loopTask(void*)‘:
C:/Users/Max/.platformio/packages/framework-arduinoespressif32/cores/esp32/main.cpp:48: undefined reference to `loop()’
collect2.exe: error: ld returned 1 exit status

In which it says

undefined reference to `loop()’

Arduino sketches must define both setup() and loop() functions. Even if you don’t plan on doing anything in the loop() functions since you have that callback based ESP-NOW architecture, it must at least be defined as an empty function. Hence, adding

void loop() {}

to the bottom makes it compile succesfully.

When you look at the most prominent tutorial available online for ESP-NOW, you will see the same thing: Getting Started with ESP-NOW (ESP32 with Arduino IDE) | Random Nerd Tutorials

Main takeaway: Post error messages in full and read them carefully, post problems as minimal, self-contained projects with code and configuration file (platformio.ini).