ESP NOW ESP8266

I was using the code below for slave and master. Earlier the slave device was receiving the data but now the master is sending data but the slave is not receiving data.

I am trying to send a string which is received from the serial monitor on master and sending it to the slave via ESP NOW protocol and print it on serial monitor of the slave device

Can anyone tell me what is the problem?

Master

#include<ESP8266WiFi.h>
extern "C"
{
  #include<espnow.h>
}

  const byte dataLength = 7;
  byte cnt = 0;
  uint8_t rcvData[dataLength];
  uint8_t snd;
 // char serial[dataLength];


void setup() 
{
  Serial.begin(115200);
  Serial.println();
  Serial.println();
  WiFi.mode(WIFI_STA);
  WiFi.disconnect();

  if(esp_now_init()==0)
  {
    Serial.println("ESP NOW initialised. . .");
  }
  else
  {
    Serial.println("ESP NOW not initialised. . .");
    ESP.restart();
    delay(1);
  }

  Serial.print("Station MAC Address");
  Serial.println(WiFi.macAddress());

  esp_now_set_self_role(ESP_NOW_ROLE_CONTROLLER);

  uint8_t mac_addr[6] = {0x62, 0x01, 0x94, 0x42, 0x0B, 0x5B};
  uint8_t role = 2;
  uint8_t channel = 3;
  uint8_t key[0] = {};/////////no key
  //uint8_t key[16] = {1,3,3,4,j,9,8,6,t,v,l,2,5,d,b,1};
  uint8_t key_len = sizeof(key);
  
  Serial.print("keylength : - "); 
  Serial.println(key_len);
  int add = esp_now_add_peer(mac_addr, role, channel, NULL, 0);
  if(add == 0)
  {
    Serial.println("Pairing Success " + String(add));
  }
  else
  {
    Serial.println("Pairing Failed " + String(add));
  }

  esp_now_register_send_cb([](uint8_t *mac, uint8_t Status)
  {
    Serial.print("send status " + String(Status));
  });


  //byte mac[] = {0x62, 0x01, 0x94, 0x42, 0x0B, 0x5B};
  //uint8_t result = esp_now_send(mac, rcvData, sizeof(rcvData));


}

void loop() 
{
  if(Serial.available()!=0)
  {
    rcvData[dataLength] = Serial.read();
  }
  byte mac[] = {0x62, 0x01, 0x94, 0x42, 0x0B, 0x5B};
  uint8_t result = esp_now_send(mac, rcvData, sizeof(rcvData));
    if(result == 0)
  {
    Serial.println("Data Send Success ");
  }
  else
  {
    Serial.println("Data Send failed");
  }
}

slave

//#include<SoftwareSerial.h>
#include<ESP8266WiFi.h>
extern"C"
{
  #include<espnow.h>
}

void setup() 
{
  Serial.begin(115200);
  Serial.println();

  Serial.print("Access Point MAC address");
  Serial.println(WiFi.softAPmacAddress());
  //Serial.print("Station MAC Address");
  //Serial.println(WiFi.macAddress());

  WiFi.mode(WIFI_AP);
  
  if(esp_now_init()==0)
  {
    Serial.println("ESP NOW initialised. . .");
  }
  else
  {
    Serial.println("ESP NOW not initialised. . .");
    ESP.restart();
    delay(1);
  }

  esp_now_set_self_role(2);
  
  /*Serial.print("Access Point MAC address");
  Serial.println(WiFi.softAPmacAddress());
  Serial.print("Station MAC Address");
  Serial.println(WiFi.macAddress());*/
}

void loop() 
{
  esp_now_register_recv_cb([](uint8_t *mac, byte *data, uint8_t len)
  {
    char macMaster[6];
    sprintf(macMaster, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]);
    Serial.print("recevied data from master : -");

    for(int i = 0; i<len; i++)
    {
      //String tad = data[i];
      Serial.print(data[i]);
      Serial.print(" ");
    }
    Serial.println("");
  });
}

You may be interested in this short ESP NOW example

...R

EDIT - link fixed. Apologies for not testing it after I posted this Reply

I tried it. It is not working.

Master code

#include<ESP8266WiFi.h>
extern "C"
{
  #include<espnow.h>
}

#define channel 3

struct __attribute__((packed)) DataStruct
{
  char text[32];
}myData;

  const byte dataLength = 32;
  byte cnt = 0;
  uint8_t rcvData[dataLength];
  uint8_t snd;
//char serial[dataLength];
  char serialData[dataLength];
  boolean newdata = false;

  char str[32];

void serial_rcv_data();
void send_data();

void setup() 
{
  Serial.begin(115200);
  Serial.println();
  Serial.println();
  WiFi.mode(WIFI_STA);
  //WiFi.disconnect();

  if(esp_now_init()==0)
  {
    Serial.println("ESP NOW initialised. . .");
  }
  else
  {
    Serial.println("ESP NOW not initialised. . .");
    ESP.restart();
    delay(1);
  }

  Serial.print("Station MAC Address");
  Serial.println(WiFi.macAddress());
  Serial.print("Access Point MAC address");
  Serial.println(WiFi.softAPmacAddress());

  esp_now_set_self_role(ESP_NOW_ROLE_CONTROLLER);

  uint8_t mac_addr[6] = {0x26, 0x01, 0x94, 0x42, 0x0B, 0x5B};
  uint8_t role = 2;
  //uint8_t channel = 3;
  uint8_t key[0] = {};/////////no key
  //uint8_t key[16] = {1,3,3,4,j,9,8,6,t,v,l,2,5,d,b,1};
  uint8_t key_len = sizeof(key);
  
  Serial.print("keylength : - "); 
  Serial.println(key_len);
  int add = esp_now_add_peer(mac_addr, role, channel, NULL, 0);
  if(add == 0)
  {
    Serial.println("Pairing Success " + String(add));
  }
  else
  {
    Serial.println("Pairing Failed " + String(add));
  }

  esp_now_register_send_cb([](uint8_t *mac, uint8_t Status)
  {
    Serial.println();
    Serial.print("send status " + String(Status));
  });


  //byte mac[] = {0x62, 0x01, 0x94, 0x42, 0x0B, 0x5B};
  //uint8_t result = esp_now_send(mac, rcvData, sizeof(rcvData));


}

void loop() 
{
  serial_rcv_data();
  send_data();
  
  /*if(Serial.available()!=0)
  {
    rcvData[dataLength] = Serial.read();
  }*/
}



void send_data()
{
  if(newdata == true)
  {
    uint8_t bs[sizeof(myData)];
    memcpy(bs, &myData, sizeof(myData));
    Serial.print("length of data sent");
    Serial.print(sizeof(myData));
    byte mac[] = {0x26, 0x01, 0x94, 0x42, 0x0B, 0x5B};
    uint8_t result = esp_now_send(mac, bs, sizeof(myData));
  
  if(result == 0)
  {
    Serial.println();
    Serial.print("Data Send Success ");
  }
  else
    {
    Serial.println();
    Serial.print("Data Send failed");
    }
  }

  newdata = false;
}

void serial_rcv_data()
{
  static byte ndx = 0;
  char marker ='\n';
  char rcv;

  while(Serial.available()>0 && newdata == false)
  {
    rcv = Serial.read();

    if(rcv!=marker)
    {
      serialData[ndx] = rcv;
      ndx++;

      if(ndx>=dataLength)
      {
        ndx = dataLength - 1;
      }

      else
      {
        serialData[ndx] = '\0';
        ndx = 0;
        //memcpy(&myData.text, serialData, sizeof(serialData));
        Serial.println();
        Serial.println("data to send is ");
        Serial.print(" : ");
        Serial.print(serialData);
        Serial.print(" : ");
        newdata = true;
      }
    }
  }
}

master’s output

ESP NOW initialised. . .
Station MAC Address5C:CF:7F:F0:56:12
Access Point MAC address5E:CF:7F:F0:56:12
keylength : - 0
Pairing Success 0

data to send is 
 : d : length of data sent32
Data Send Success 
send status 1

slave code

 #include<ESP8266WiFi.h>

extern"C"
{
  #include<espnow.h>
  #include<user_interface.h>
}


struct __attribute__((packed)) DataStruct
{
  char text[32];
};
DataStruct myData;


uint8_t mac_addr[] = {0x26, 0x01, 0x94, 0x42, 0x0B, 0x5B};
const byte len = 32;
char d[len];


void initVariant()
{
  WiFi.mode(WIFI_AP);
  wifi_set_macaddr(SOFTAP_IF, (uint8_t *)mac_addr);
}


void setup()
{
  Serial.begin(115200);
  Serial.println();
    
  if(esp_now_init()==0)
  {
    Serial.println("ESP NOW initialised. . .");
  }
  else
  {
    Serial.println("ESP NOW not initialised. . .");
    ESP.restart();
    delay(1);
  }

  esp_now_set_self_role(2);
  
  Serial.print("Access Point MAC address");
  Serial.println(WiFi.softAPmacAddress());
  Serial.print("Station MAC Address");
  Serial.println(WiFi.macAddress());

  esp_now_register_recv_cb(receiveCallBackFunction);

}

void loop() 
{

}


void receiveCallBackFunction(uint8_t *mac, uint8_t *data, uint8_t len)
{
  memcpy(&myData.text, data, sizeof (myData));
  Serial.print("Mac addr");
  for(byte n = 0; n<6; n++)
  {
    Serial.print(mac[n], HEX);
  }
  Serial.print("msg len");
  Serial.print(len);
  Serial.print("text");
  Serial.print(myData.text);
  Serial.println();
}

slave output

ESP NOW initialised. . .
Access Point MAC address26:01:94:42:0B:5B
Station MAC Address60:01:94:42:0B:5B

The slave doest not receive any data. What could be the problem here?

gauravntpl:
I tried it. It is not working.

It's a good while since I wrote those programs and I don't have the time now to set up my hardware to test them again. I do know that they worked before I posted them because I am careful about that sort of thing.

Are the program in your Reply #2 identical in every respect with my programs, and if so, which pair?

...R

I want to send String using esp now

I am trying to understand how communication is taking place and developing my own code based on that but I guess they are pretty much similar in the way of sending and receiving data

In Slave's code in #2 reply, I have used

memcpy(&myData.text, data, sizeof(myData));

But I have also used

memcpy(&myData, data, sizeof(myData));

but the slave is not receiving any data. Everything else is the same in slave's code.

In master's code. I am trying to receive data from serial first with end Marker "'\n'".

In master, the data will be sent if data is received from the serial monitor.

In master's code in #2 reply, I have not used

strcpy(myData.text, serialData);

but I have tried it. It was not working.

I have added acknowledgment on pairing and sending data it shows success but the slave does not receive data.

Some things are another way around but I don't think that can be a reason.

ESP8266 pairing.

gauravntpl:
I want to send String using esp now

Based on that I must assume that the answer to my question
Are the program in your Reply #2 identical in every respect with my programs
is NO.

You are, of course, perfectly within your rights to ignore my advice.

...R

I set up two Wemos D1's and successfully ran Robin's ESP NOW code.

If you start there, and make slow controlled changes you will find out where your code is broken.

If Robins code does not work as written, you may need to change the channel.

cattledog:
I set up two Wemos D1's and successfully ran Robin's ESP NOW code.

Thank you. Very much appreciated on two levels - {A} just for testing and {B} for testing on the Wemos D1

...R

just for info..

I've also just run your (Robin2) sketches successfully on a D1 & a D1 mini.

Thanks for putting them together.

Its just a shame that you cant use WiFi & WiFiNow together on one ESP8266

Cheers
Mike

I'm looking at doing something similar using ESPNow on 8266's to transfer thermal array data between modules and am perplexed on where the espnow.h or user_interface.h headers referenced in the above coding are found to link to the sketch?

I've found for an ESP32 board I loaded under Arduino board manager with Expressif library, that there are IDE sample sketches that function. On saving these sketches there is an espnow.h file in the folder but I can't find any 8266 equivalents? I'm a newbie but would think there should be .cpp file as well?

Cheers
Mel

MelMak:
I'm looking at doing something similar using ESPNow on 8266's to transfer thermal array data between modules and am perplexed on where the espnow.h or user_interface.h headers referenced in the above coding are found to link to the sketch?

Have you added the ESP8266 programming system to the Arduino IDE?

I don't know anything about the ESP32

...R

Thanks R.

I have installed my Wemos D1's using Arduino boards manager and use preferences:
http://arduino.esp8266.com/stable/package_esp8266com_index.json” and successfully run many other sketches for them.

The ESP Now examples show up when connecting the ESP32 module, but not for the 8266's. Some other library needed or?

Found similar issue under:
https://www.esp8266.com/viewtopic.php?f=28&t=7713
but PlatformIO??

Thx
M

I have only dabbled briefly with the ESP8266 so I don’t have anything else to suggest. Have you seen Kolban’s book on the ESP8266?

…R

The ESP Now examples show up when connecting the ESP32 module, but not for the 8266's. Some other library needed or?

I do not think that there are any esp8266 espnow.h example programs packaged with the core installed through the board manager. The core code winds up in the Arduino15 folder.

Do you see errors when compiling Robin2's example code?

There are additional example code links with the Andreas Spiess espnow video at

I guess I don’t understand how the sketch like Robin’s would compile if there is not the espnow.h file somewhere that the compiler can locate which is referenced by Arduino ESP8266. Similarly the user.h file I would think needs some user definitions? One must have installed them somewhere to make these sketches execute, or linked them?:

extern"C"
{
#include<espnow.h>
#include<user_interface.h>

I’ve looked at many other 8266 Now examples like Harringgay… on Github etc. and they all have the same reference? ESP32 Arduino NOW examples execute as the .h files presumably come in the Espressif installation package for board loading?

My errors on compiling Robin’s’ Controller sketch:

Arduino: 1.8.5 (Windows 7), TD: 1.43, Board: “WeMos D1 R2 & mini, 80 MHz, 921600, 4M (3M SPIFFS)”

Archiving built core (caching) in: C:\Users\Mel\AppData\Local\Temp\arduino_cache_986895\core\core_esp8266_esp8266_d1_mini_CpuFrequency_80,UploadSpeed_921600,FlashSize_4M3M_287082a9acd7f5a3b8972086ace7a11b.a
sketch\EspnowControllerM1.ino.cpp.o:(.text.setup+0x38): undefined reference to `esp_now_init’

sketch\EspnowControllerM1.ino.cpp.o:(.text.setup+0x3c): undefined reference to `esp_now_set_self_role’

sketch\EspnowControllerM1.ino.cpp.o:(.text.setup+0x40): undefined reference to `esp_now_add_peer’

sketch\EspnowControllerM1.ino.cpp.o:(.text.setup+0x44): undefined reference to `esp_now_register_send_cb’

Thanks

MelMak:
and they all have the same reference? ESP32 Arduino NOW examples execute as the .h files presumably come in the Espressif installation package for board loading?

My errors on compiling Robin's' Controller sketch:

I am confused.

I don't have an ESP32 and I have never tried my code with an ESP32.

I don't have a WemosD1 either but in Reply #6 @cattledog said that my code worked for him with a WemosD1

...R

I have installed my Wemos D1's using Arduino boards manager and use preferences:
"http://arduino.esp8266.com/stable/package_esp8266com_index.json" and successfully run many other sketches for them.

I don't know what to tell you. I installed the esp8266 board package with the same http: entered into the additional boards manager url and added the library using the board manager and it is was located in the Arduino15 folder. I have version 2.4.2 which I loaded in August 2018.

I can see espnow.h at
C:\Users\xxx\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.2\tools\sdk\include

I compiled Robin2's sketches without problem. If you do not have a recent version of the esp8266 core, you might want to reinstall.

It is now compiling and uploading OK. Many thanks. Maybe useful to other newbies:

I did find the espnow.h file buried deep in Arduino 15 as cattledog noted; still not sure why AppData subfolder does not show up under Users\xxx\ but found it via search. I also noted that this compiler error message, or very similar was addressed a few years back under:

https://www.esp8266.com/viewtopic.php?p=41667

probably fixed with later versions?

Following cattledog help I updated the ESP8266 under boards manager from 2.3.0 to current 2.4.2. This still resulted in a new compiling error message, this time... ESP8266 package is unknown.
Following this link How to resolve “Board generic (platform esp8266, package esp8266) is unknown” error? — ESP8266 Arduino Core 3.0.2-2-g5f04fbbf documentation
I found the package files they identify and deleted the 2.3.0 one (which was empty),
it then compiled and uploaded OK.

Following cattledog I guess I would not have the compile error if I removed the 2.3.0 version first rather than updating via boards manager.

Now all I have to do is figure out how to transfer big floating point array data for my application:)

My apologies for confusing you Robin; I had figured your ESP8266 sketch for ESP-7 would apply to the Wemos ESP-12, which it does, and the problematic espnow.h include file and its location was something known to other users.

I have got an esp8266 working with both espnow a d WiFi. It essentially sits there recently espnow data from another device and then having connected to my router responds to http requests from my phone to obtain the data. I believe the trick is that your router WiFi needs to be on the same channel number as your espnow

Having said all this, it has been working for some time and has suddenly stopped working exhibiting the same behaviour as the OP. The device sends the data ok with no error messages but the CB function on the server never gets called.

As far as I know whilst I have made some changes to the code, I have not knowingly updated this aspect of it (this comms is done by a separate class in separate files). Also I have not been working on the server side. The only other thing I can think of is that I updated the SDK version and reflash both client and server.

Quick update. I loaded Robin's simple examples onto the 2 devices. With the simple sender on my sender device and receiver on my receiving device it didn't work. Swapping then around and it worked. Now the very odd but... Swapped them back again and it worked ....

Flashing my code back has made them fail again