2 way communication using ESP-WROOM-32 not working
Posted: Tue Nov 28, 2023 6:23 am
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
EndDevice.ino
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
- #include <esp_now.h>
- #include <WiFi.h>
- // Device data object
- typedef struct {
- uint8_t device_mac_address[6]; // mac address of device
- int device_type; // 0 = smart flusher, etc.
- } device_data;
- device_data device_info; // local storage of paired device
- // Hub data object
- typedef struct {
- uint8_t hub_mac_address[6]; // mac address of hub
- } hub_data;
- hub_data hub_info; // local storage of hub data
- // Device pairing flags
- bool pairedFlag = false;
- bool sentAck = false;
- // Handler for when data received using esp-now
- void on_data_received(const uint8_t * mac, const uint8_t *incomingData, int len) {
- if(pairedFlag == false){
- // copy received device data to local
- memcpy(&device_info, incomingData, sizeof(device_info));
- // set as paired
- pairedFlag = true;
- Serial.println("Paired device " + String(device_info.device_type) + " with ");
- for(int i = 0; i < 6; i++){
- Serial.print(device_info.device_mac_address[i], HEX);
- }
- Serial.println();
- // get hub mac address
- esp_read_mac(hub_info.hub_mac_address, ESP_MAC_WIFI_STA);
- // send paired acknowledgement back to device
- if(pairedFlag == true && sentAck == false){
- esp_now_send(device_info.device_mac_address, (uint8_t *) &hub_info, sizeof(hub_info));
- sentAck = true;
- }
- }
- }
- // Handler for when data sent using esp-now
- void on_data_sent(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);
- if (esp_now_init() != ESP_OK) {
- Serial.println("Error initializing ESP-NOW");
- return;
- }
- // call handler when data received
- esp_now_register_recv_cb(on_data_received);
- // call handler when data sent
- esp_now_register_send_cb(on_data_sent);
- Serial.println(WiFi.macAddress());
- }
- void loop() {
- }
- #include <esp_now.h>
- #include <WiFi.h>
- // Device data object
- typedef struct {
- uint8_t device_mac_address[6]; // mac address of device
- int device_type; // 0 = smart flusher, etc.
- } device_data;
- device_data device_info; // local storage of paired device
- // Hub data object
- typedef struct {
- uint8_t hub_mac_address[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};; // mac address of hub
- } hub_data;
- hub_data hub_info; // local storage of hub data
- // Device pairing flags
- bool pairedFlag = false;
- esp_now_peer_info_t peerInfo;
- // Handler for when data sent using esp-now
- void on_data_sent(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");
- }
- // Handler for when data received using esp-now
- void on_data_received(const uint8_t * mac, const uint8_t *incomingData, int len) {
- if(pairedFlag == false){
- // copy received device data to local
- memcpy(&hub_info, incomingData, sizeof(hub_info));
- // set as paired
- pairedFlag = true;
- Serial.println("Paired to hub");
- }
- }
- void setup() {
- Serial.begin(115200);
- WiFi.mode(WIFI_STA);
- if (esp_now_init() != ESP_OK) {
- Serial.println("Error initializing ESP-NOW");
- return;
- }
- // register peer
- memcpy(peerInfo.peer_addr, hub_info.hub_mac_address, 6);
- peerInfo.channel = 0;
- peerInfo.encrypt = false;
- // add peer
- if (esp_now_add_peer(&peerInfo) != ESP_OK){
- Serial.println("Failed to add peer");
- return;
- }
- // set device mac address and type
- esp_read_mac(device_info.device_mac_address, ESP_MAC_WIFI_STA);
- device_info.device_type = 0;
- // call function on data send
- esp_now_register_send_cb(on_data_sent);
- // call function on data receive
- esp_now_register_recv_cb(on_data_received);
- Serial.println(WiFi.macAddress());
- }
- void loop() {
- // run pair sequence if not paired
- if(pairedFlag == false){
- // send data to pair to hub
- esp_now_send(hub_info.hub_mac_address, (uint8_t *) &device_info, sizeof(device_info));
- }
- }