Error using ESP8266 and NRF24L01 with the RadioHead library

Hi.
I´m using a ESP8266 Node MCU and the NRF24L01+PA+LNA and I´m trying to use the example sketch for client and server from the RadioHead library example folder to stablish a connection between two identical setups to pass some data from one point to another.
The problem is that in the serial monitor i´m not getting nothing but nonsense characters


I hope someone can help me, and thanks in advance
This is the code for the client

#include <SPI.h>
#include <RH_NRF24.h>

// Singleton instance of the radio driver
RH_NRF24 nrf24;
// RH_NRF24 nrf24(8, 7); // use this to be electrically compatible with Mirf
// RH_NRF24 nrf24(8, 10);// For Leonardo, need explicit SS pin
// RH_NRF24 nrf24(8, 7); // For RFM73 on Anarduino Mini

void setup() 
{
  Serial.begin(9600);
  while (!Serial) 
    ; // wait for serial port to connect. Needed for Leonardo only
  if (!nrf24.init())
    Serial.println("init failed");
  // Defaults after init are 2.402 GHz (channel 2), 2Mbps, 0dBm
  if (!nrf24.setChannel(1))
    Serial.println("setChannel failed");
  if (!nrf24.setRF(RH_NRF24::DataRate2Mbps, RH_NRF24::TransmitPower0dBm))
    Serial.println("setRF failed");    
}


void loop()
{
  Serial.println("Sending to nrf24_server");
  // Send a message to nrf24_server
  uint8_t data[] = "Hello World!";
  nrf24.send(data, sizeof(data));
  
  nrf24.waitPacketSent();
  // Now wait for a reply
  uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];
  uint8_t len = sizeof(buf);

  if (nrf24.waitAvailableTimeout(500))
  { 
    // Should be a reply message for us now   
    if (nrf24.recv(buf, &len))
    {
      Serial.print("got reply: ");
      Serial.println((char*)buf);
    }
    else
    {
      Serial.println("recv failed");
    }
  }
  else
  {
    Serial.println("No reply, is nrf24_server running?");
  }
  delay(400);
}

And this is the code for the server

#include <SPI.h>
#include <RH_NRF24.h>

// Singleton instance of the radio driver
RH_NRF24 nrf24;
// RH_NRF24 nrf24(8, 7); // use this to be electrically compatible with Mirf
// RH_NRF24 nrf24(8, 10);// For Leonardo, need explicit SS pin
// RH_NRF24 nrf24(8, 7); // For RFM73 on Anarduino Mini

void setup() 
{
  Serial.begin(9600);
  while (!Serial) 
    ; // wait for serial port to connect. Needed for Leonardo only
  if (!nrf24.init())
    Serial.println("init failed");
  // Defaults after init are 2.402 GHz (channel 2), 2Mbps, 0dBm
  if (!nrf24.setChannel(1))
    Serial.println("setChannel failed");
  if (!nrf24.setRF(RH_NRF24::DataRate2Mbps, RH_NRF24::TransmitPower0dBm))
    Serial.println("setRF failed");    
}

void loop()
{
  if (nrf24.available())
  {
    // Should be a message for us now   
    uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];
    uint8_t len = sizeof(buf);
    if (nrf24.recv(buf, &len))
    {
//      NRF24::printBuffer("request: ", buf, len);
      Serial.print("got request: ");
      Serial.println((char*)buf);
      
      // Send a reply
      uint8_t data[] = "And hello back to you";
      nrf24.send(data, sizeof(data));
      nrf24.waitPacketSent();
      Serial.println("Sent a reply");
    }
    else
    {
      Serial.println("recv failed");
    }
  }
}

Make sure the tool you use to monitor the data has the matching baud/stop bits/ parity etc.

I´m using the built-in Arduino serial monitor and everything is at 9600 baud rate and still nothing
I also search in other posts and forums about this error, and found about the ESP8266 its at 115200 from factory, does this can be the problem?

As long as they match, I generally use 115200, but 9600 is ok too.

how did you connect the NRF24L01 to the ESO8266?

with Radiohead library I used the following

// ESP8266 connections
// ESP8266 SCK pin GPIO14 goes to NRF24L10_pin SCK
// ESP8266 MISO pin GPIO12 goes to NRF24L10_pin MI
// ESP8266 MOSI pin GPIO13 goes to NRF24L10_pin MO
// NRF24L10 CE to ESP8266 pin GPIO4
// NRF24L10 CSN to ESP8266 pin GPIO5

RF24 radio(4, 5);  //CE and CSN

void setup() {
  Serial.begin(115200);
  radio.begin();
  if (radio.isChipConnected())
    Serial.println("\n\nTransmitter NF24 connected to SPI");
  else Serial.println("\n\nNF24 is NOT connected to SPI");

I was connecting them like this:
CE - GPIO2
CSN - GPIO4
SCK - GPIO14
MOSI - GPIO13
MISO - GPIO12

I was missing the part that states which CE and CSN pin where in the code and I managed to get working the server part but the client does not show anything in the serial monitor, i using the exact same connection you are using and the same code stated in the beginning of the post, am I missing something?
Edit: When I upload the server code to the other ESP8266 that I'm using the serial monitor does not shows anything, but when I unplug it from the computer and connect again, it shows a almost infinite long string of nonsense characters

on power up or reset the ESP8266 outputs boot information at 74880baud which you can ignore - it should then output any startup serial monitor output at your specified baudrate, e.g.
does your server and client output "Transmitter NF24 connected to SPI" which indicates the host microcontroller is communicating with the NRF24L01 OK

a few thoughts
1 have a look at esp8266-pinout-reference-gpios - GPIO2 is used at BOOT - generally recommend avoiding using such pins - suggest moving to GPIO5
2 the NRF24L01 can overload the power supply of a host - connect a 10uF capacitor between GND and VCC
3 don't have the server and client too close - a least a meter apart

I got confused with the names of the pins, I really sorry, my connections are the follow:

CE - D2(GPIO4)
CSN - D1(GPIO5)
SCK - D5(GPIO14)
MOSI - D7(GPIO13)
MISO - D6(GPIO12)

And I was aware of the power problems a direct connection could cause so I'm using a NRF24L01 Adapter that I understand can solve the power issue

In the server part this is the characters that appear after I reset the connection


This is the code for the server part

#include <SPI.h>
#include <RH_NRF24.h>
 
// Singleton instance of the radio driver
RH_NRF24 nrf24(4, 5);

void setup() 
{
  Serial.begin(9600);
  while (!Serial) 
    ; // wait for serial port to connect. Needed for Leonardo only
  if (!nrf24.init())
    Serial.println("init failed");
  // Defaults after init are 2.402 GHz (channel 2), 2Mbps, 0dBm
  if (!nrf24.setChannel(1))
    Serial.println("setChannel failed");
  if (!nrf24.setRF(RH_NRF24::DataRate2Mbps, RH_NRF24::TransmitPower0dBm))
    Serial.println("setRF failed");    
}
 
void loop()
{
  if (nrf24.available())
  {
    // Should be a message for us now   
    uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];
    uint8_t len = sizeof(buf);
    if (nrf24.recv(buf, &len))
    {
//      NRF24::printBuffer("request: ", buf, len);
      Serial.print("got request: ");
      Serial.println((char*)buf);
      
      // Send a reply
      uint8_t data[] = "And hello back to you";
      nrf24.send(data, sizeof(data));
      nrf24.waitPacketSent();
      Serial.println("Sent a reply");
    }
    else
    {
      Serial.println("recv failed");
    }
  }
}

just realized that the example code I gave in post 5 is NOT using the Radiohead library but the Optimized High Speed Driver for nRF24L01(+) 2.4GHz Wireless Transceiver

here is an example using the Radiohead library RH_NRF24 Server and client example code with a few modifications to add a packet number to the transmitted text

ESP8266 client

// ESP8266 <> NRF24L01 radiohead client

// nrf24_client.pde
// -*- mode: C++ -*-
// Example sketch showing how to create a simple messageing client
// with the RH_NRF24 class. RH_NRF24 class does not provide for addressing or
// reliability, so you should only use RH_NRF24 if you do not need the higher
// level messaging abilities.
// It is designed to work with the other example nrf24_server.
// Tested on Uno with Sparkfun NRF25L01 module
// Tested on Anarduino Mini (http://www.anarduino.com/mini/) with RFM73 module
// Tested on Arduino Mega with Sparkfun WRL-00691 NRF25L01 module

// ESP8266 connections
// ESP8266 SCK pin GPIO14 goes to NRF24L10_pin SCK
// ESP8266 MISO pin GPIO12 goes to NRF24L10_pin MI
// ESP8266 MOSI pin GPIO13 goes to NRF24L10_pin MO
// NRF24L10 CE to ESP8266 pin GPIO4
// NRF24L10 CSN to ESP8266 pin GPIO5

#include <SPI.h>
#include <RH_NRF24.h>

// Singleton instance of the radio driver
//RH_NRF24 nrf24;
// RH_NRF24 nrf24(8, 7); // use this to be electrically compatible with Mirf
// RH_NRF24 nrf24(8, 10);// For Leonardo, need explicit SS pin
// RH_NRF24 nrf24(8, 7); // For RFM73 on Anarduino Mini
RH_NRF24 nrf24(4, 5);  // For ESP8266


void setup() {
  Serial.begin(115200);
  while (!Serial)
    ;  // wait for serial port to connect. Needed for Leonardo only
  delay(2000);
  Serial.println("\n\nESP8266 <> NRF24L01 radiohead client");
  if (!nrf24.init())
    Serial.println("init failed");
  // Defaults after init are 2.402 GHz (channel 2), 2Mbps, 0dBm
  if (!nrf24.setChannel(1))
    Serial.println("setChannel failed");
  if (!nrf24.setRF(RH_NRF24::DataRate2Mbps, RH_NRF24::TransmitPower0dBm))
    Serial.println("setRF failed");
}


void loop() {
  Serial.print("Sending to nrf24_server ");
  // Send a message to nrf24_server
  static uint8_t data[] = "Hello World! 0";
  Serial.println((char*) data);
  nrf24.send(data, sizeof(data));

  nrf24.waitPacketSent();
  // Now wait for a reply
  uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];
  uint8_t len = sizeof(buf);

  if (nrf24.waitAvailableTimeout(2000)) {
    // Should be a reply message for us now
    if (nrf24.recv(buf, &len)) {
      Serial.print("got reply: ");
      Serial.println((char*)buf);
    } else {
      Serial.println("recv failed");
    }
  } else {
    Serial.println("No reply, is nrf24_server running?");
  }
  delay(5000);
  data[13]++;
}

Pro Micro server

// Pro Micro > NRF24L01 radiohead server

// nrf24_server.pde
// -*- mode: C++ -*-
// Example sketch showing how to create a simple messageing server
// with the RH_NRF24 class. RH_NRF24 class does not provide for addressing or
// reliability, so you should only use RH_NRF24  if you do not need the higher
// level messaging abilities.
// It is designed to work with the other example nrf24_client
// Tested on Uno with Sparkfun NRF25L01 module
// Tested on Anarduino Mini (http://www.anarduino.com/mini/) with RFM73 module
// Tested on Arduino Mega with Sparkfun WRL-00691 NRF25L01 module

// Pro Micro connections
// Pro Micro SCK pin 15 to NRF24L10_pin SCK
// Pro Micro MISO pin 14 to NRF24L10_pin MISO
// Pro Micro MOSI pin 16 to NRF24L10_pin MOSI
// Pro Micro pin 9 to NRF24L10 CE
// Pro Micro pin 10 to NRF24L10 CSN

#include <SPI.h>
#include <RH_NRF24.h>

// Singleton instance of the radio driver
//RH_NRF24 nrf24;
// RH_NRF24 nrf24(8, 7); // use this to be electrically compatible with Mirf
// RH_NRF24 nrf24(8, 10);// For Leonardo, need explicit SS pin
// RH_NRF24 nrf24(8, 7); // For RFM73 on Anarduino Mini
RH_NRF24 nrf24(9, 10);  // For Pro Micro

void setup() {
  Serial.begin(115200);
  while (!Serial)
    delay(2000);
  Serial.println("Pro Micro > NRF24L01 radiohead server");
  ;  // wait for serial port to connect. Needed for Leonardo only
  if (!nrf24.init())
    Serial.println("init failed");
  // Defaults after init are 2.402 GHz (channel 2), 2Mbps, 0dBm
  if (!nrf24.setChannel(1))
    Serial.println("setChannel failed");
  if (!nrf24.setRF(RH_NRF24::DataRate2Mbps, RH_NRF24::TransmitPower0dBm))
    Serial.println("setRF failed");
}

void loop() {
  if (nrf24.available()) {
    // Should be a message for us now
    uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];
    uint8_t len = sizeof(buf);
    if (nrf24.recv(buf, &len)) {
      //      NRF24::printBuffer("request: ", buf, len);
      Serial.print("got request: ");
      Serial.println((char*)buf);

      // Send a reply
      static uint8_t data[] = "And hello back to you 0";
      nrf24.send(data, sizeof(data));
      nrf24.waitPacketSent();
      Serial.print("Sent a reply ");
      Serial.println((char*) data);
      data[22]++;
    } else {
      Serial.println("recv failed");
    }
  }
}

ESP8266 client serial monitor output

ESP8266 <> NRF24L01 radiohead client
Sending to nrf24_server Hello World! 0
got reply: And hello back to you 0
Sending to nrf24_server Hello World! 1
got reply: And hello back to you 1
Sending to nrf24_server Hello World! 2
got reply: And hello back to you 2
Sending to nrf24_server Hello World! 3
got reply: And hello back to you 3
Sending to nrf24_server Hello World! 4
got reply: And hello back to you 4
Sending to nrf24_server Hello World! 5
got reply: And hello back to you 5
Sending to nrf24_server Hello World! 6
got reply: And hello back to you 6
Sending to nrf24_server Hello World! 7
got reply: And hello back to you 7

Pro Micro server serial monitor output



Pro Micro > NRF24L01 radiohead server
got request: Hello World! 0
Sent a reply And hello back to you 0
got request: Hello World! 1
Sent a reply And hello back to you 1
got request: Hello World! 2
Sent a reply And hello back to you 2
got request: Hello World! 3
Sent a reply And hello back to you 3
got request: Hello World! 4
Sent a reply And hello back to you 4
got request: Hello World! 5
Sent a reply And hello back to you 5
got request: Hello World! 6
Sent a reply And hello back to you 6

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.