Nrf24L01 Code works great between nanos -- not when receiver is a NodeMCU

Scenario: Nano with moisture sensor and DHT11 remotely sends data to another nano that controls a relay.
With both nanos - things work well.

I wanted to add wifi to the receiver so I’ve swapped it with a NodeMCU. Now the moisture content is in the millions and my temperature reads a steady zero. I hooked up the nano again and it works great…back to the NodeMCU and the received data is wrong. Same NRF24L01 modules for both.

I’ve commented out the relay in the receiver just to troubleshoot.

Receiver:

/
#include <SPI.h>  
#include "RF24.h"

//const int relay1 = 12; //D6
//const int red = 14; //D5
//const int green = 4; //D2

RF24 myRadio (4, 15); //CE D2 , CSN D8
struct package
{
  int X=0; //moisture content
  int Y=0; //temperature in Celcius
};

byte addresses[][6] = {"0"}; 

typedef struct package Package;
Package data;

void setup() 
{
  Serial.begin(9600);
 // delay(1000);
  //pinMode(relay1, OUTPUT);
  //pinMode(red, OUTPUT);
  //pinMode(green, OUTPUT);
  
  myRadio.begin(); 
  myRadio.setChannel(115); 
  myRadio.setPALevel(RF24_PA_MAX);
  myRadio.setDataRate( RF24_250KBPS ) ; 
  myRadio.openReadingPipe(1, addresses[0]);
  myRadio.startListening();
}
void loop()  {
  
  if ( myRadio.available()) 
  {
    while (myRadio.available())
    {
      myRadio.read( &data, sizeof(data) );
    }
    Serial.print("Moisture Content: ");
    Serial.print(data.X);
    Serial.print("%");
    Serial.print("   Temperature: ");
    Serial.print(data.Y);
    Serial.println(" C");

 

}
    /*if (data.X >=40){
      digitalWrite(relay1, LOW);
      digitalWrite(green, LOW);
      digitalWrite(red, HIGH);
      delay(1000);
    }
     else if (data.X >= 20 && data.X < 40){
      digitalWrite(relay1, HIGH);
      digitalWrite(green, HIGH);
      digitalWrite(red, LOW);
      delay(3000);
    }
    else if (data.X <20){
      digitalWrite(relay1, LOW);
      digitalWrite(green, LOW);
      digitalWrite(red, HIGH);
      delay(1000);
    }
    else {
    }*/
}

Transmitter:

#include <SPI.h>  
#include "RF24.h"
#include <DHT.h>

#define DHTPIN 7   //temperature data
#define DHTTYPE DHT11   //DHT11
DHT dht(DHTPIN, DHTTYPE);


RF24 myRadio (9, 10);
byte addresses[][6] = {"0"};

struct package
{
  int X=1;  //moisture content
  int Y=1;  //temperature
};


typedef struct package Package;
Package data;


void setup()
{
  Serial.begin(9600);
  delay(100);
  myRadio.begin();  
  myRadio.setChannel(115); 
  myRadio.setPALevel(RF24_PA_MAX);
  myRadio.setDataRate( RF24_250KBPS ) ; 
  myRadio.openWritingPipe( addresses[0]);
  delay(100);
}

void loop()
{
 
  myRadio.write(&data, sizeof(data)); 

  Serial.print("X:");
  Serial.print(data.X);
  Serial.print("    Y");
  Serial.println(data.Y);
  data.X = map(analogRead(A0),1023,450,0,100);
  data.Y = dht.readTemperature();
  delay(5000); //changed from 1000
}

What can be happening to skew the received data?

int is 16 bit on the Nano and 32 bit on the ESP8266.

So you would better use uint16_t and pack the structure on the ESP.

1 Like