ESP-NOW发送失败、接收无响应问题排查求助
ESP-NOW发送失败、接收无响应问题排查求助
我是ESP-NOW新手,用两块Adafruit QT Py ESP32-C3开发板做收发测试:一块发送消息,另一块通过串口打印接收内容。所有初始化步骤都返回成功(无ESP错误码),但发送回调始终返回失败状态,接收端的接收回调完全没触发,串口也没有相关输出。
已尝试的操作:
- 设置发送端Peer的WiFi通道与接收端一致
- 互换收发板角色
- 从PlatformIO切换到Arduino IDE
- 禁用LED指示灯,仅保留串口输出
- 直接使用dronebotworkshop、randomnerd、volosproject的教程代码
问题仍未解决,以下是串口日志及代码:
发送端(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
接收端(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
恳请各位帮忙分析发送失败、无接收响应的原因,谢谢!
内容的提问来源于stack exchange,提问作者WKGuy
相关产品推荐
相关产品推荐

