ChuckMash / ESPythoNOW

A python library for sending, receiving, and monitoring ESP-NOW messages
MIT License
44 stars 6 forks source link

Sending ack to ESP32 #2

Closed weoiss1998 closed 3 weeks ago

weoiss1998 commented 1 month ago

Currently, the sending ESP32 doesn't get an ACK for the message. Is the ACK planned for the future?

I really like your work! Thanks for making it available. :)

ChuckMash commented 1 month ago

Hi @weoiss1998

Glad you like the project, can you provide the code you are using where ACK is not being sent?

Delivery confirmation ACK should be handled automatically whenever a message is received. I may be wrong, but I believe the chipset/driver is what handles it.

If you are overriding your local hardware MAC, that can also cause the ACK to not be sent as expected.

weoiss1998 commented 1 month ago

It's the example, but I've just added the OnDataSent Callback. It's with modified MAC, maybe this is the problem.

#include <esp_now.h>
#include "esp_private/wifi.h"
#include <WiFi.h>

typedef struct __attribute__ ((packed)) python_message {
  int a = 567;
  bool b = false;
} python_message;
python_message pm;
python_message pm_incomming;

esp_now_peer_info_t peer_info;
uint8_t python_peer[] = {0xE0,0x5A,0x1B,0x11,0x22,0x33};
uint8_t broadcastAddress[] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
uint8_t local_mac[] = {0xE0,0x5A,0x1B,0x33,0x22,0x11}; // Override local MAC

 void esp_now_rx_callback(esp_now_peer_info_t * info, const uint8_t *data, int len) {
  memcpy(&pm_incomming, data, sizeof(pm_incomming));
  Serial.println(pm_incomming.a);
  Serial.println(pm_incomming.b);
  Serial.println();
 }

 void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
  Serial.print("\r\nLast Packet Send Status:\t");
  Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
}

void setup() {
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  esp_wifi_set_channel(8, WIFI_SECOND_CHAN_NONE);
  esp_now_init();
  memcpy(peer_info.peer_addr, python_peer, 6);
  peer_info.channel = 0; // 0 defaults to existing channel setting
  peer_info.encrypt = false;
  esp_now_add_peer(&peer_info);
  esp_now_register_send_cb(OnDataSent);
  esp_now_register_recv_cb(esp_now_recv_cb_t(esp_now_rx_callback));
  pm.a = 0;
  pm.b = false;
}

void loop() {  
  esp_err_t result = esp_now_send(python_peer, (uint8_t *) &pm, sizeof(pm));
  if (result != ESP_OK) {
    Serial.println("Error sending the data");
  }
  pm.a++;
  delay(1000);
}
ChuckMash commented 1 month ago

It's with modified MAC, maybe this is the problem.

This may be the issue

Change the following to the HW MAC of your Linux WiFi device uint8_t python_peer[] = {0xE0,0x5A,0x1B,0x11,0x22,0x33};

On the python side of things, make sure to use something like this espnow = ESPythoNow(interface="wlp1s0", callback=callback) Where mac is not specified, it will default to the MAC of the specified interface.

If the delivery confirmation ACK is still missing at that point, please tell me your WiFi chipset and driver so I may investigate.

weoiss1998 commented 1 month ago

Yeah, you are right, after I changed it to the original MAC address, the issue is resolved.

I have another question. Is it possible to increase the speed of sniffing? I have to send a relatively large number of small packets very often, so that a packet is sent approximately every 4ms. I'm using a RasPi4.

ChuckMash commented 1 month ago

Yeah, you are right, after I changed it to the original MAC address, the issue is resolved.

Glad to hear it.

I have another question. Is it possible to increase the speed of sniffing? I have to send a relatively large number of small packets very often, so that a packet is sent approximately every 4ms. I'm using a RasPi4.

ESPythoNOW currently uses Scapy under the hood for sending and receiving packets. It may be possible there is some configuration or setting with Scapy that could improve performance, but I am unaware if this is the case.

If you are willing and able, collect some details about your current performance and create a new issue (tagged enhancement) Please include WiFi chipset and driver version, etc.

ChuckMash commented 3 weeks ago

Closing as the main issue has been resolved.