Page 1 of 1

2 way communication using ESP-WROOM-32 not working

Posted: Tue Nov 28, 2023 6:23 am
by keshavshankar
I am trying to make a 2 way communication for my own IOT device. I have one esp32 acting as a hub, and one esp32 acting as an end device for a sensor. Ideally, my hub will always be on. Once my end device turns on, I want it to ping its mac address to be picked up by the hub, and once the hub receives it and stores it as a "paired device", it sends an acknowledgment to that end device with the hub's mac address. This enables a replicated pairing structure so that once paired, I can send signals to the specific devices connected.

My issue right now is that I am able to send my initial signal from the end device to the hub, but the hub does not send back the acknowledgement to the end device. When I debug the code, it looks like in the Hub.ino, it does get past line 36, and my flag updates. Additionally, I printed out the mac address of the end device received in the hub and it is correct. I suspect the problem is with the EndDevice.io in the loop(), since it is constantly sending data to the channel, thus it cant receive data from the hub on that same channel? Maybe I am misunderstanding how 2 way communication works with esp-now. Any help is appreciated!

Hub.ino
  1. #include <esp_now.h>
  2. #include <WiFi.h>
  3.  
  4. // Device data object
  5. typedef struct {
  6.   uint8_t device_mac_address[6]; // mac address of device
  7.   int device_type; // 0 = smart flusher, etc.
  8. } device_data;
  9. device_data device_info; // local storage of paired device
  10.  
  11. // Hub data object
  12. typedef struct {
  13.   uint8_t hub_mac_address[6]; // mac address of hub
  14. } hub_data;
  15. hub_data hub_info; // local storage of hub data
  16.  
  17. // Device pairing flags
  18. bool pairedFlag = false;
  19. bool sentAck = false;
  20.  
  21. // Handler for when data received using esp-now
  22. void on_data_received(const uint8_t * mac, const uint8_t *incomingData, int len) {
  23.   if(pairedFlag == false){
  24.     // copy received device data to local
  25.     memcpy(&device_info, incomingData, sizeof(device_info));
  26.  
  27.     // set as paired
  28.     pairedFlag = true;
  29.     Serial.println("Paired device " + String(device_info.device_type) + " with ");
  30.     for(int i = 0; i < 6; i++){
  31.       Serial.print(device_info.device_mac_address[i], HEX);
  32.     }
  33.     Serial.println();
  34.  
  35.     // get hub mac address
  36.     esp_read_mac(hub_info.hub_mac_address, ESP_MAC_WIFI_STA);
  37.    
  38.     // send paired acknowledgement back to device
  39.     if(pairedFlag == true  && sentAck == false){
  40.       esp_now_send(device_info.device_mac_address, (uint8_t *) &hub_info, sizeof(hub_info));
  41.       sentAck = true;
  42.     }
  43.   }
  44. }
  45.  
  46. // Handler for when data sent using esp-now
  47. void on_data_sent(const uint8_t *mac_addr, esp_now_send_status_t status) {
  48.   Serial.print("\r\nLast Packet Send Status:\t");
  49.   Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
  50. }
  51.  
  52. void setup() {
  53.   Serial.begin(115200);
  54.   WiFi.mode(WIFI_STA);
  55.  
  56.   if (esp_now_init() != ESP_OK) {
  57.     Serial.println("Error initializing ESP-NOW");
  58.     return;
  59.   }
  60.  
  61.   // call handler when data received
  62.   esp_now_register_recv_cb(on_data_received);
  63.  
  64.   // call handler when data sent
  65.   esp_now_register_send_cb(on_data_sent);
  66.  
  67.   Serial.println(WiFi.macAddress());
  68. }
  69.  
  70. void loop() {
  71. }
EndDevice.ino
  1. #include <esp_now.h>
  2. #include <WiFi.h>
  3.  
  4. // Device data object
  5. typedef struct {
  6.   uint8_t device_mac_address[6]; // mac address of device
  7.   int device_type; // 0 = smart flusher, etc.
  8. } device_data;
  9. device_data device_info; // local storage of paired device
  10.  
  11. // Hub data object
  12. typedef struct {
  13.   uint8_t hub_mac_address[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};; // mac address of hub
  14. } hub_data;
  15. hub_data hub_info; // local storage of hub data
  16.  
  17. // Device pairing flags
  18. bool pairedFlag = false;
  19.  
  20. esp_now_peer_info_t peerInfo;
  21.  
  22. // Handler for when data sent using esp-now
  23. void on_data_sent(const uint8_t *mac_addr, esp_now_send_status_t status) {
  24.   Serial.print("\r\nLast Packet Send Status:\t");
  25.   Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
  26. }
  27.  
  28. // Handler for when data received using esp-now
  29. void on_data_received(const uint8_t * mac, const uint8_t *incomingData, int len) {
  30.   if(pairedFlag == false){
  31.     // copy received device data to local
  32.     memcpy(&hub_info, incomingData, sizeof(hub_info));
  33.    
  34.     // set as paired
  35.     pairedFlag = true;
  36.     Serial.println("Paired to hub");
  37.   }
  38. }
  39.  
  40. void setup() {
  41.   Serial.begin(115200);
  42.   WiFi.mode(WIFI_STA);
  43.  
  44.   if (esp_now_init() != ESP_OK) {
  45.     Serial.println("Error initializing ESP-NOW");
  46.     return;
  47.   }
  48.  
  49.   // register peer
  50.   memcpy(peerInfo.peer_addr, hub_info.hub_mac_address, 6);
  51.   peerInfo.channel = 0;  
  52.   peerInfo.encrypt = false;
  53.  
  54.   // add peer        
  55.   if (esp_now_add_peer(&peerInfo) != ESP_OK){
  56.     Serial.println("Failed to add peer");
  57.     return;
  58.   }
  59.  
  60.   // set device mac address and type
  61.   esp_read_mac(device_info.device_mac_address, ESP_MAC_WIFI_STA);
  62.   device_info.device_type = 0;
  63.  
  64.   // call function on data send
  65.   esp_now_register_send_cb(on_data_sent);
  66.  
  67.   // call function on data receive
  68.   esp_now_register_recv_cb(on_data_received);
  69.  
  70.   Serial.println(WiFi.macAddress());
  71. }
  72.  
  73. void loop() {
  74.   // run pair sequence if not paired
  75.   if(pairedFlag == false){
  76.     // send data to pair to hub
  77.     esp_now_send(hub_info.hub_mac_address, (uint8_t *) &device_info, sizeof(device_info));
  78.   }
  79. }