Relationship between ESPNOW range coverage and power source for ESP32
Posted: Mon Mar 11, 2024 10:01 am
Hi all,
I am currently doing a project using ESP32 which involving 1-to-many communication via ESPNOW. I am facing an issue that the chance of packet loss seems relate to the power source for ESP32. Please see details below and it would be grateful if anyone can share your ideas if there are better way to do it or something wrong or I missed on my project. Appreciate for your help!
My Project aims to remote control start and stop data collection of 10 devices (esp32 with several sensor connected).
Basically, the situation is when I change the power source for ESP32 from laptop USB port to powerbank, the control range decrease and become unstable.
Sender: Wemos D1 r32 x1
Receiver: NodeMCU 32S ESP-32 x10
send message via broadcast mode
Case A:
Power source to sender: 5V USB type A connected to laptop
Testing Environment: Indoor with wall and 2.4GHz and 5GHz wifi signal
Distance between sender and receiver with packet loss: approximately Greater than 15 meters
Case B:
Power source to sender: powerbank with 5V 2A output
Testing Environment: Indoor with wall and 2.4GHz and 5GHz wifi signal
Distance between sender and receiver with packet loss: approximately Greater than 1meter , No response with distance >3 meters
Sort out the setup code of sender and receiver for reference.
Sender code:
__________________________________________________________________________________________________
Receiver code:
I am currently doing a project using ESP32 which involving 1-to-many communication via ESPNOW. I am facing an issue that the chance of packet loss seems relate to the power source for ESP32. Please see details below and it would be grateful if anyone can share your ideas if there are better way to do it or something wrong or I missed on my project. Appreciate for your help!
My Project aims to remote control start and stop data collection of 10 devices (esp32 with several sensor connected).
Basically, the situation is when I change the power source for ESP32 from laptop USB port to powerbank, the control range decrease and become unstable.
Sender: Wemos D1 r32 x1
Receiver: NodeMCU 32S ESP-32 x10
send message via broadcast mode
Case A:
Power source to sender: 5V USB type A connected to laptop
Testing Environment: Indoor with wall and 2.4GHz and 5GHz wifi signal
Distance between sender and receiver with packet loss: approximately Greater than 15 meters
Case B:
Power source to sender: powerbank with 5V 2A output
Testing Environment: Indoor with wall and 2.4GHz and 5GHz wifi signal
Distance between sender and receiver with packet loss: approximately Greater than 1meter , No response with distance >3 meters
Sort out the setup code of sender and receiver for reference.
Sender code:
Code: Select all
void setup() {
//WiFiManager, Local intialization. Once its business is done, there is no need to keep it around
WiFiManager wm;
wm.setHttpPort(8080);
bool res;
res = wm.autoConnect("my_wifi", "my_pw"); // password protected ap
if (!res) {
Serial.println("Failed to connect");
ESP.restart();
}
Serial.println("WIFI mode : AP + STA");
WiFi.mode(WIFI_AP_STA);
WiFi.softAP(soft_ap_ssid, soft_ap_password);
delay(1000);
esp_wifi_set_ps(WIFI_PS_NONE);
int restart_count = 0;
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
digitalWrite(ON_Board_LED, HIGH);
delay(250);
digitalWrite(ON_Board_LED, LOW);
delay(250);
}
Serial.println("WiFi connected");
Serial.println("------------");
Serial.println("Start initializing ESP-NOW...");
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
Serial.println("Restart ESP32...");
Serial.println("-------------");
delay(1000);
ESP.restart();
}
Serial.println("Initializing ESP-NOW was successful.");
Serial.println("-------------");
// ----------------------------------------
// ---------------------------------------- Once ESPNow is successfully Init, we will register for Send CB to get the status of Trasnmitted packet
Serial.println();
Serial.println("get the status of Trasnmitted packet");
esp_now_register_send_cb(OnDataSent);
// ----------------------------------------
// ---------------------------------------- Register Peer
Serial.println();
Serial.println("-------------");
Serial.println("Register peer");
peerInfo.encrypt = false;
// Broadcast a message to every device in range
Serial.println("Register boardcast");
esp_now_peer_info_t peerInfo = {};
memcpy(&peerInfo.peer_addr, broadcastAddress, 6);
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
Serial.println("Failed to add boardcast peer");
return;
}
// ---------------------------------------- Handle Web Server
Serial.println();
Serial.println("Setting Up the Main Page on the Server.");
server.on("/", HTTP_GET, [](AsyncWebServerRequest * request) {
request->send_P(200, "text/html", MAIN_page);
});
// ----------------------------------------
// ---------------------------------------- Send a GET request to <ESP_IP>/set_LED?board=<inputMessage1>&gpio_output=<inputMessage2>&val=<inputMessage3>
server.on("/set_LED", HTTP_GET, [] (AsyncWebServerRequest * request) {
String A
String B
String C
String D
String E
String F
String G
String H
String I
int J
if (request->hasParam(PARAM_INPUT_1) && request->hasParam(PARAM_INPUT_2) && request->hasParam(PARAM_INPUT_3) && request->hasParam(PARAM_INPUT_4) && request->hasParam(PARAM_INPUT_5) && request->hasParam(PARAM_INPUT_6) && request->hasParam(PARAM_INPUT_7) && request->hasParam(PARAM_INPUT_8) && request->hasParam(PARAM_INPUT_9)) {
A= request->getParam(PARAM_INPUT_1)->value();
B= request->getParam(PARAM_INPUT_2)->value();
C= request->getParam(PARAM_INPUT_3)->value();
D= request->getParam(PARAM_INPUT_4)->value();
E= request->getParam(PARAM_INPUT_5)->value();
F= request->getParam(PARAM_INPUT_6)->value();
G= request->getParam(PARAM_INPUT_7)->value();
H= request->getParam(PARAM_INPUT_8)->value();
I= request->getParam(PARAM_INPUT_9)->value();
send_data_to_slave(A, B.toInt(), C.toInt(), D, E, F.toInt(), G.toInt(), H.toInt(), I.toInt(), my_wifi,my_pw);
}
else {
Serial.println("No message sent");
}
delay(1);
request->send(200, "text/plain", "OK");
delay(1);
});
server.begin();
}
Receiver code:
Code: Select all
void setup() {
WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0);
// initialize SD card
if (!SD.begin()) {
Serial.println("Card Mount Failed");
delay(5000);
ESP.restart();
}
uint8_t cardType = SD.cardType();
if (cardType == CARD_NONE) {
Serial.println("No SD card attached");
return;
}
// initialize SD card
Serial.println("Initializing SD card...");
if (!SD.begin(5)) {
Serial.println("ERROR - SD card initialization failed!");
return; // init failed
}
WiFi.mode(WIFI_STA);
esp_wifi_set_ps(WIFI_PS_NONE);
scan_and_set_Wifi_Channel();
Serial.println("Start initializing ESP-NOW...");
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
Serial.println("Restart ESP32...");
Serial.println("-------------");
delay(1000);
ESP.restart();
}
Serial.println("Initializing ESP-NOW was successful.");
Serial.println("-------------");
// ----------------------------------------
// ---------------------------------------- Register for a callback function that will be called when data is received
Serial.println();
Serial.println("Register for a callback function that will be called when data is received");
esp_now_register_recv_cb(OnDataRecv);
xTaskCreatePinnedToCore(codeForTask1, "Task1-Main", 10000, NULL, 2, &Task1, 0);
delay(500); // needed to start-up task1
xTaskCreatePinnedToCore(codeForTask2, "Task2-LED", 5000, NULL, 2, &Task2, 1);
}