communication Problem Between Arduino pro Mini and Nodemcu using NRF24L01

I’m working on a project which uses arduino pro mini and Nodemcu. Actualy Arduino Pro Mini is sending two values to Nodemcu and in return Nodemcu is sending 1 value back to Arduino Pro Mini using NRF24L01. and then Nodemcu updating the status on server. A pressure sensor is also connected to the Nodemcu.

NodeMcu acts as receiver and Arduino Pro Mini acts as transmitter.

the communication continues only for 5 minutes and then it breaks until i disconnected arduino pro mini from power and plug it again. but I need to be connected until they are in range.
Here is the connection of these modules


Arduino Mini Arduino UNO

Tx Tx
Rx Rx
5v VCC
GND GND
Reset Reset NRF 24L01

9 CE
10 CSN
11 MOSI
12 MISO
13 SCK
VCC 3.3V
GND GND

Code For Arduino Mini

// MasterSwapRoles
#include "Arduino.h"
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define CE_PIN   9
#define CSN_PIN 10
  int button_pin = A0;
  Adafruit_SSD1306 display(-1);


const byte slaveAddress[5] = {'R','x','A','A','A'};
const byte masterAddress[5] = {'T','X','a','a','a'};


RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
 
struct dataStruct {
   short int seat; 

     short int Button_State;
  } myData;

int dataReceived;

bool newData = false;

unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 10; // send once per second

//============

void setup() {

    Serial.begin(115200);
 // initialize with the I2C addr 0x3C
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  
    //display.setCursor(0,0);
  // Clear the buffer.
  display.clearDisplay();


  // Scroll part of the screen
  display.setCursor(0,0);
  display.setTextSize(1);
    display.setTextColor(WHITE);
  display.println("AWALAN FOR DESIGN AND ");
   // display.setTextColor(WHITE);
  display.setCursor(28,14);
  display.setTextSize(1);
  display.println("PROGRAMMING");
  

    Serial.println("MasterSwapRoles Starting");
    
 pinMode(button_pin, INPUT);

    radio.begin();
      radio.setChannel(108);
    radio.setDataRate( RF24_2MBPS );
   radio.setPALevel(RF24_PA_MIN);
  
    

    radio.openWritingPipe(slaveAddress);
    radio.openReadingPipe(1, masterAddress);

    radio.setRetries(3,5); // delay, count
    send(); // to get things started
    prevMillis = millis(); // set clock
}

//=============

void loop() {

    currentMillis = millis();
    if (currentMillis - prevMillis >= txIntervalMillis) {
        send();
        prevMillis = millis();
    }
    getData();
    showData();
}

//====================

void send() {

        radio.stopListening();
           myData.Button_State = digitalRead(button_pin);
 //myData.seat = "999";
 myData.seat = 1; 
            bool rslt;
            rslt = radio.write( &myData, sizeof(myData) );
        radio.startListening();
        Serial.print("Data Sent ");
        Serial.print(myData.Button_State);
           Serial.print(", ");
        Serial.println(myData.seat);
        if (rslt) {
            Serial.println("  Acknowledge received");
            updateMessage();
         //  delay(2000);
        }
       
       else {
            Serial.println("  Tx failed");
                     delay(2000);
                       /*     digitalWrite(segA, HIGH);
                digitalWrite(segB, HIGH);
                digitalWrite(segC, HIGH);
                digitalWrite(segD, HIGH);
                digitalWrite(segE, HIGH);
                digitalWrite(segF, HIGH);
                digitalWrite(segG, LOW); */
                 //       Serial.println();
  //  Serial.println("Seat Searching ");
      //   Serial.println();
     display.setCursor(0,28);
  display.setTextSize(1);
   display.setTextColor(WHITE, BLACK);
//display.setTextColor(WHITE);
  display.println("Please.....");
      display.setCursor(0,42);
  display.setTextSize(2);
  display.println("..Search..");
  display.display();            
                       

        }
}

//================

void getData() {

    if ( radio.available() ) {
        radio.read( &dataReceived, sizeof(dataReceived) );
        newData = true;
    }
    else {
      Serial.println();
        newData = false;
    }
}

//================

void showData() {
    if (newData == true) {
        Serial.print("Data received ");
        Serial.print(dataReceived);
        //Serial.print(", ");
       // Serial.println(dataReceived);
        Serial.println();
        
if (dataReceived == 1)   {

    display.setCursor(0,28);
  display.setTextSize(1);
   display.setTextColor(WHITE, BLACK);
//display.setTextColor(WHITE);
  display.println("Reached at");
      display.setCursor(0,42);
  display.setTextSize(2);
  display.println("..Seat 1..");
  display.display();




}
else if (dataReceived == 2)
{

    display.setCursor(0,28);
  display.setTextSize(1);
   display.setTextColor(WHITE, BLACK);
 //  display.setTextColor(WHITE);
  display.println("Reached at");
      display.setCursor(0,42);
  display.setTextSize(2);
  display.println("..Seat 2..");
  display.display();
  
 
  }
 else if (dataReceived == 3)
{

    display.setCursor(0,28);
  display.setTextSize(1);
   display.setTextColor(WHITE, BLACK);
   //display.setTextColor(WHITE);
  display.println("Reached at");
      display.setCursor(0,42);
  display.setTextSize(2);
  display.println("..Seat 3..");
  display.display();


  }
   else if (dataReceived == 4)
{

 
  
    display.setCursor(0,28);
  display.setTextSize(1);
   display.setTextColor(WHITE, BLACK);
  display.println("Reached at");
      display.setCursor(0,42);
  display.setTextSize(2);
  display.println("..Seat 4..");
  display.display();


  }
   else if (dataReceived == 5)
{


    display.setCursor(0,28);
  display.setTextSize(1);
   display.setTextColor(WHITE, BLACK);
  display.println("Reached at");
      display.setCursor(0,42);
  display.setTextSize(2);
  display.println("..Seat 5..");
  display.display();


  }
   else if (dataReceived == 6)
{
  

  
    display.setCursor(0,28);
  display.setTextSize(1);
  display.println("Reached at");
      display.setCursor(0,42);
  display.setTextSize(2);
  display.println("..Seat 6..");
  display.display();


  }
   else if (dataReceived == 7)
{

    display.setCursor(0,28);
  display.setTextSize(1);
   display.setTextColor(WHITE, BLACK);
  display.println("Reached at");
      display.setCursor(0,42);
  display.setTextSize(2);
  display.println("..Seat 7..");
  display.display();


  }
   else if (dataReceived == 8)
{

    display.setCursor(0,28);
  display.setTextSize(1);
   display.setTextColor(WHITE, BLACK);
  display.println("Reached at");
      display.setCursor(0,42);
  display.setTextSize(2);
  display.println("..Seat 8..");
  display.display();

  }
   else if (dataReceived == 9)
{
  

    display.setCursor(0,28);
  display.setTextSize(1);
   display.setTextColor(WHITE, BLACK);
  display.println("Reached at");
      display.setCursor(0,42);
  display.setTextSize(2);
  display.println("..Seat 9..");
  display.display();

  }

else {

  
    display.setCursor(0,28);
  display.setTextSize(1);
   display.setTextColor(WHITE, BLACK);
  display.println("Wait.....");
      display.setCursor(0,42);
  display.setTextSize(2);
  display.println("Searching...");
  display.display();


}
       // newData = false;
    }
    delay(1000);
  
}

//================

void updateMessage() {

}

This line must not be commented out or you will only receive one time... if I understand your logic.

       // newData = false;

Nodemcu NRF 24L01

D4 CE
D2 CSN
D7 MOSI
D6 MISO
D5 SCK
3.3v 3.3V
GND GND

Code for Nodemcu

// SlaveSwapRoles
#include "Arduino.h"
#include <ESP8266WiFi.h>
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define WIFI_SSID "TANVEER"  
#define WIFI_PASSWORD "basit666"  
//#define WIFI_SSID "AndroidAP"  
//#define WIFI_PASSWORD "mpcq4768"  
const char* host = "awalanseat.com";

#define CE_PIN   D4
#define CSN_PIN D2
#define fsrpin A0
int fsrreading;
const byte slaveAddress[5] = {'R','x','A','A','A'};
const byte masterAddress[5] = {'T','X','a','a','a'};

RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
struct dataStruct {
   short int seat;
   // int Seat_Value_1;  
     // int button_pin = D0;
     short int Button_State;
  } myData;

//char dataReceived[100]; // must match dataToSend in master
//char replyData[100] = "8";
int replyData = {1};
//int replyData[2] = {"Data", -4000}; // the two values to be sent to the master
bool newData = false;

unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 10; // send once per second


void setup() {

    Serial.begin(115200);
      Serial.print("Connecting to ");
 // Serial.println(ssid);
 // WiFi.begin(ssid, password);
    WiFi.begin(WIFI_SSID, WIFI_PASSWORD);  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");  
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  Serial.print("Netmask: ");
  Serial.println(WiFi.subnetMask());
 Serial.print("Gateway: ");
  Serial.println(WiFi.gatewayIP());
fsrreading = analogRead(fsrpin);
    Serial.println("SlaveSwapRoles Starting");

    radio.begin();
       radio.setChannel(108);
        radio.setDataRate( RF24_2MBPS );
    radio.setPALevel(RF24_PA_MIN);
 
   // radio.setDataRate( RF24_250KBPS );
        // radio.setPALevel(RF24_PA_MIN);

    radio.openWritingPipe(masterAddress); // NB these are swapped compared to the master
    radio.openReadingPipe(1, slaveAddress);

    radio.setRetries(3,5); // delay, count
    radio.startListening();

}

//====================

void loop() {

    getData();
    showData();
    send();
}

//====================

void send() {
    if (newData == true) {
        radio.stopListening();
            bool rslt;
            rslt = radio.write( &replyData, sizeof(replyData) );
        radio.startListening();
       
  fsrreading = analogRead(fsrpin);
          Serial.print("FSR Reading ");
        Serial.print(fsrreading);
        Serial.print(", ");
        Serial.print("Reply Sent ");
        Serial.println(replyData);
       
       // Serial.print(", ");
        //Serial.println(replyData);

        if (rslt) {
            Serial.println("Acknowledge Received");
            updateReplyData();
        }
        else {
            Serial.println("Tx failed");
        }
       // Serial.println();
       // newData = false;
    }
}

//================

void getData() {

    if ( radio.available() ) {
        radio.read( &myData, sizeof(myData) );
        newData = true;
    }
    else {
              Serial.println();
        newData = false;
    }
}

//================

void showData() {
    if (newData == true) {
              Serial.print("Button State ");
        Serial.print(myData.Button_State);
           Serial.print(", ");
            Serial.print("Seat Number ");
        Serial.println(myData.seat);
  float p = analogRead(fsrpin);
  // Read temperature as Celsius (the default)
 // float b = myData.Button_State;
  //float s = myData.seat;
   Serial.print("connecting to ");
  Serial.println(host);
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }

  String url = "/awalan_seat_api/v1/api/seat_status/save?pressure=" + String(p) + "&seatid=" + String(myData.seat) + "&button_state="+ String(myData.Button_State);
     
 //String url = "/api/weather/insert.php?temp=" + String(p) + "&hum="+ String(b) + "&hum="+ String(s);
  Serial.print("Requesting URL: ");
  Serial.println(url);
 client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "Connection: close\r\n\r\n");
  //delay(500);
  while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
    break;
    }
  Serial.println();
  Serial.println("closing connection");
  //delay(3000);

    }
//    break;
    delay(1000);
    if (newData == false)   {
     
       float p = analogRead(fsrpin);
     
  // Read temperature as Celsius (the default)
 // float b = myData.Button_State;
  //float s = myData.seat;
   Serial.print("connecting to ");
  Serial.println(host);
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }

delay(2000);
 

  String url = "/awalan_seat_api/v1/api/seat_status/save?pressure=" + String(p) + "&seatid=" + String(0) + "&button_state="+ String(0);
     
 //String url = "/api/weather/insert.php?temp=" + String(p) + "&hum="+ String(b) + "&hum="+ String(s);
  Serial.print("Requesting URL: ");
  Serial.println(url);
 client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "Connection: close\r\n\r\n");
  //delay(500);
  while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
    break;
    }
  Serial.println();
  Serial.println("closing connection");
     
      //delay (1000);
     
      }
//      break;
}

//================

void updateReplyData() {
   // replyData[0] -= 1;
   // replyData[1] -= 1;
   // if (replyData[0] < 100) {
     //   replyData[0] = 109;
    //}
    //if (replyData[1] < -4009) {
      //  replyData[1] = -4000;
    //}
}

Dear Aarg, Thanks. After uncommenting the result is same.

The only problem is after 4-5 minutes both lost communication

unsigned long txIntervalMillis = 10; // send once per second

No, 100 times per second.

Nothing works. Here you can see the unsual behaviour like some times it starts showing different number and some time data received 0

You seem to be using programs adapted from my Simple nRF24L01+ Tutorial. But my program does not have the ELSE in this

void getData() {

    if ( radio.available() ) {
        radio.read( &dataReceived, sizeof(dataReceived) );
        newData = true;
    }
    else {
      Serial.println();
        newData = false;
    }
}

90% of the time there will be no data available and the print in the ELSE will just clog up the Serial Output Buffer. And there is no need for newData = false in the ELSE clause. It will already be false from some other part of the program.

I suggest you go back to basics. Get the original programs from my Tutorial working and then gradually add your code to them testing that everything still works after every few lines of code.

...R

1 Like

Thanks Robin. Yeah, I'm following your tutorials. I used ELSE because i want the notification when communication happening and when disconnected because based on this I was assuming these modules are far from eachother and when they come close to eachother and communication started I was assuming they are closed and reached to eachother spot with acceptable distance. I'm facing the problem of unstablility of communication Now. Even, when I tried to start from tutorial, unstable communication is there.

engr_khalil:
I used ELSE because i want the notification when communication happening and when disconnected

That ELSE clause is not the way to do that.

If you need to know that communication fails then your Tx needs to send a message at regular intervals (perhaps 5 per second) and then if the Rx receives nothing for (say) 1 second it can assume there is a problem.

...R

1 Like