WKGuy
WKGuy

Reputation: 109

Esp Now not sending or not receiving

I'm a beginner trying esp now for the first time. I have two Adafruit QT Py ESP32-C3 boards and I'm trying to make one board send a message, and the second board print on serial that it received the message.

Even though all my configuration seems to go through successfully (I never get any ESP error codes), all I get from my debug messages is that everytime the send callback runs, it gets called with a "failure" as a result.

Also, my receiver is never executing its receive callback, so its serial log is just constantly quiet.

So far I've tried setting the wifi channel of the peer in the sender, to make sure it matches the receiver, interchanging which board is sender and which one is receiver, changing from platformIO to arduino IDE, disabling my LED indicators and only keeping my serial port messages active.

I've also tried the code straight out of some tutorials I've seen on the topic and all messages still fail to send. Tutorials I've used:

dronebotworkshop

randomnerd

volosproject

Any ideas on why all my send messages are failing / nothing is being received? Thanks!!

This is what the serial port looks like on the sender (COM4):

34:85:18:17:DC:2C
ESP-NOW init OK
send callback registered OK
Peer added
Wifi channel:
1
Attempting to send 0
esp_now_send sent complete
Failed to send
Attempting to send 1
Callback invoked with status: 1
esp_now_send sent complete
Failed to send

and here is the serial port on the receiver (COM3):

34:85:18:17:EA:28
ESP-NOW init OK
receive calback registered OK
Wifi channel:
1

SENDER CODE:

#include <Arduino.h>
#include <esp_now.h>
#include <WiFi.h>
// #include <Adafruit_NeoPixel.h>
#include "controllerGlobals.h"

// Adafruit_NeoPixel strip(NUM_PIXELS, LED_PIN, NEO_GRB + NEO_KHZ800); // create led strip

// Helper function to set color
// void setLEDColor(uint8_t red, uint8_t green, uint8_t blue) {
//     strip.setPixelColor(0, strip.Color(red, green, blue)); // Set color for first pixel
//     strip.show();
// }

esp_now_peer_info_t peerInfo;
uint8_t data = 0;
uint8_t sentFailed= 1; // 0 = success, 1 = failed


void sendDataCb(const uint8_t* macAddress, esp_now_send_status_t sentStatus) {
  Serial.printf("Callback invoked with status: %d\n", sentStatus);
  if(sentStatus == ESP_NOW_SEND_SUCCESS)
  {
    sentFailed = 0;
  }
  else
  {
    //ERROR: FAILED TO SEND
    sentFailed = 1;
  }
}

void blinkSendResult()
{
  if(sentFailed)
  {
    //ERROR: FAILED TO SEND
    Serial.println("Failed to send");
    // for(short i = 0; i < 3; i++)
    // {
    //   setLEDColor(LED_RED);
    //   usleep(250*1000); //sleep 250ms
    //   setLEDColor(LED_OFF);
    //   usleep(250*1000); //sleep 250ms
    // }
  }
  else
  {
    // successful send!
    Serial.println("Successful send!");
    // for(short i = 0; i < 2; i++)
    // {
    //   setLEDColor(LED_GREEN);
    //   usleep(500*1000); //sleep 250ms
    //   setLEDColor(LED_OFF);
    //   usleep(500*1000); //sleep 250ms
    // }
    sentFailed = 1; //reset sentfailed flag for next loop
  }
}

void setup() {
  // start serial port
  Serial.begin(CONFIG_MONITOR_BAUD_OTHER_VAL); //115200

  //INITIALIZE LED
  // strip.begin();       // Initialize the Neopixel library
  // strip.setBrightness(10); // Set brightness (0 to 255; 50 is about 20% brightness)
  // strip.show();        // Turn off all LEDs initially

  // initialize wifi module in station mode
  WiFi.mode(WIFI_STA);
  // WiFi.disconnect();
  WiFi.channel(1);

  // print mac address to serial port
  // sleep(5);
  delay(5000);
  // Serial.printf("%s\n", String(WiFi.macAddress()).c_str());
  Serial.println(String(WiFi.macAddress()));

  // initialize esp-now protocol
  if(esp_now_init() == ESP_OK)
  {
    Serial.println("ESP-NOW init OK");
  }
  if (esp_now_register_send_cb(sendDataCb) == ESP_OK){
    Serial.println("send callback registered OK");
  }
  
  //register esp-now peer 
  memcpy(peerInfo.peer_addr, receiverAddr, 6);
  peerInfo.channel = 1;  
  peerInfo.encrypt = false;
  // Add peer        
  if (esp_now_add_peer(&peerInfo) != ESP_OK){
    Serial.println("Failed to add peer");
    return;
  }
  else
  {
    Serial.println("Peer added");
  }
  Serial.println("Wifi channel:");
  Serial.println(WiFi.channel());

}

void loop() {

  Serial.printf("Attempting to send %d\n", data);
  // sleep(3);
  // delay(500);
  // setLEDColor(LED_YELLOW);
  // sleep(3);
  // delay(1000);
  // setLEDColor(LED_OFF);
  // sleep(3);
  delay(1000);
  if(esp_now_send(peerInfo.peer_addr, (uint8_t*)&data, sizeof(data)) != ESP_OK)
  {
    Serial.println("esp_now_send failed");
  }
  else
  {
    Serial.println("esp_now_send sent complete");
  }
  blinkSendResult();
  data = (data) ? 0 : 1;
}

RECEIVER CODE:

#include <Arduino.h>
#include <esp_now.h>
#include <WiFi.h>
#include <Adafruit_NeoPixel.h>
#include "controllerGlobals.h"

Adafruit_NeoPixel strip(NUM_PIXELS, LED_PIN, NEO_GRB + NEO_KHZ800); // create led strip

// Helper function to set color
void setLEDColor(uint8_t red, uint8_t green, uint8_t blue) {
    strip.setPixelColor(0, strip.Color(red, green, blue)); // Set color for first pixel
    strip.show();
}

uint8_t data = 2;

void recvDataCb(const uint8_t * mac, const uint8_t *incomingData, int len) {
  Serial.println("Callback invoked");
  memcpy(&data, incomingData, sizeof(data));
  if(data == 0)
  {
    // reset data value
    data = 2;
    setLEDColor(LED_GREEN);
    sleep(1);
  }
  else if(data == 1)
  {
    // reset data value
    data = 2;
    setLEDColor(LED_BLUE);
    sleep(1);
  }
  else
  {
    // reset data value
    data = 2;
    setLEDColor(LED_RED);
    sleep(1);
  }
}


void setup() {
  // start serial port
  Serial.begin(CONFIG_MONITOR_BAUD_OTHER_VAL); //115200

  //INITIALIZE LED
  strip.begin();       // Initialize the Neopixel library
  strip.setBrightness(10); // Set brightness (0 to 255; 50 is about 20% brightness)
  strip.show();        // Turn off all LEDs initially

  // initialize wifi module in station mode
  WiFi.mode(WIFI_STA);
  // WiFi.disconnect();

  // print mac address to serial port
  sleep(5);
  Serial.println(String(WiFi.macAddress()));

  // initialize esp-now protocol
  if(esp_now_init() == ESP_OK)
  {
    Serial.println("ESP-NOW init OK");
  }
  // esp_err_t cbregistration = esp_now_register_recv_cb(recvDataCb);
  if(esp_now_register_recv_cb(recvDataCb) == ESP_OK)
  {
    Serial.println("receive calback registered OK");
  }
  Serial.println("Wifi channel:");
  Serial.println(WiFi.channel());
}

void loop() {
  // Serial.println("Waiting LED ON");
  // setLEDColor(LED_YELLOW);
  // usleep(250*1000); //sleep 250ms
  // Serial.println("Waiting LED OFF");
  // setLEDColor(LED_OFF);
  // delay(1000);

}

controllerGlobals.h

#include <Arduino.h>

#define LED_PIN 2       // Neopixel data pin (GPIO 2 for QT Py ESP32-C3)
#define NUM_PIXELS 1    // Number of LEDs (typically 1 for the built-in Neopixel)
// color definitions for LED
#define LED_RED 255,0,0
#define LED_GREEN 0,255,0
#define LED_BLUE 0,0,255
#define LED_YELLOW 255,255,0
#define LED_OFF 0,0,0

const uint8_t receiverAddr[] = {0x34, 0x85, 0x18, 0x17, 0xEA, 0x28}; //receiver -- com3
// const uint8_t receiverAddr[] = {0x34, 0x85, 0x18, 0x17, 0xDC, 0x2C}; //sender -- com4

Upvotes: 0

Views: 102

Answers (0)

Related Questions