Close

08/01/19 - Two-way Communication, first attempt failed

A project log for My attempt at an ESP-NOW Mesh

Designing a custom mesh for sensors, home automation and remote controlling using Espressif's ESP-NOW

david-mDavid M. 08/01/2019 at 19:490 Comments

To begin developing two-way communication, I will began with the example sketches. I have tried combining both Master and Slave example sketches into one but failed. To help me resolve this issue, I have reached out for help on Arduino and Espressif forums. While I wait for answers, I decided to explore more closely the advantages and disadvantages of using a pre-built mesh library, like Painless Mesh or ESP-MDF. Here's a snippet of code which I tried to use, but failed:

void setup()
{
  Serial.begin(115200);
  WiFi.mode(WIFI_MODE_APSTA);
  // configure device AP mode
  configDeviceAP();
  // This is the mac address of the Slave in AP Mode
  Serial.print("AP MAC: ");
  Serial.println(WiFi.softAPmacAddress());
  Serial.print("STA MAC: ");
  Serial.println(WiFi.macAddress());
  // Init ESPNow with a fallback logic
  InitESPNow();
  // Once ESPNow is successfully Init, we will register for recv CB to
  // get recv packer info.
  esp_now_register_recv_cb(OnDataRecv);
  esp_now_register_send_cb(OnDataSent);
}

unsigned long nextManage;

void loop() 
{
  // In the loop we scan for slave
  ScanForSlave();
  // If Slave is found, it would be populate in `slave` variable
  // We will check if `slave` is defined and then we proceed further
  if (slave.channel == CHANNEL2)
  { // check if slave channel is defined
    // `slave` is defined
    // Add slave as peer if it has not been added already
    bool isPaired = manageSlave();
    nextManage = millis() + 10000;
    while (isPaired)
    {
      if (packetRecieved)
      {
        packetRecieved = false;
        // Send data to device
        sendData();
      }

      unsigned long t = millis();
      if (t > nextManage)
      {
        nextManage = t + 10000;
        // pair success or already paired
        isPaired = manageSlave();
      }
    }
    Serial.println("Slave pair failed!");
  }
}
Fig. 23 - My first, failed attempt at a two-way ESP-NOW communication sketch. 

Discussions