NRF24L01 + Reed Sensor

Hello, I am using arduinos to make wireless door sensors using the NRF24l01. I have the code for both the transmitter and receiver, It seems that the receiver is either receiving blank data or the transmitter is not sending properly. Please Help!
Transmitter Code

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
const int REED_PIN = 2; // Pin connected to reed switch
const int LED_PIN = 3; // LED pin - active-high

RF24 radio(7, 8);
const byte rxAddr[6] = "00001";

void setup() 
{
  
  Serial.begin(9600);
  // Since the other end of the reed switch is connected to ground, we need
  // to pull-up the reed switch pin internally.
  pinMode(REED_PIN, INPUT_PULLUP);
  pinMode(LED_PIN, OUTPUT);
  radio.begin();
  radio.setRetries(15, 15);
  radio.setPALevel(RF24_PA_MIN);
  
  radio.openWritingPipe(rxAddr);
  
  radio.stopListening();
}

void loop() 
{
  const char closed[] = "MiddleFRWindowClosed";
  const char opened[] = "Middle FRWindow Open";
  int proximity = digitalRead(REED_PIN); // Read the state of the switch
  if (proximity == LOW) // If the pin reads low, the switch is closed.
  {
    Serial.println("Switch Closed");
    digitalWrite(LED_PIN, HIGH); // Turn the LED on
    radio.write(&closed, sizeof(closed));
  }
  else
  {
    digitalWrite(LED_PIN, LOW); // Turn the LED off
    radio.write(&opened, sizeof(opened));
    Serial.println("Switch Opened");
  }
    delay(500);
}

Receiver Code

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8); // CNS, CE
const byte rxAddr[6] = "00001";
void setup() {
  while (!Serial);
  Serial.begin(9600);
  radio.begin();
  radio.openReadingPipe(0, rxAddr);
  radio.setPALevel(RF24_PA_MIN);
  radio.startListening();
}
void loop() {
  if (radio.available()) {
    char text[32] = "";
    //char open[13]="Middle FRWindow Open";
  
    radio.read(&text, sizeof(text));
    Serial.println(text);
  }
  else
  {
    Serial.println("baddddd");
  }
  
}

Arduino_Reciever1_0.ino (728 Bytes)

Arduino_Transmitter1_01.ino (1.75 KB)

You need to read the same size of data as you sent.

I wonder why you are sending "Middle FRWindow Open" and "MiddleFRWindowClosed" when you could convey the same information with 'M' for middle window open and 'm' for middle window closed.

Have a look at this Simple nRF24L01+ Tutorial

...R

Hello, I tried it out and figured out that the code is hanging at radio.write( &dataToSend, sizeof(dataToSend) );

koenigb96:
Hello, I tried it out

I’m guessing that “it” means one of my examples - but which one?

Post the code you have actually uploaded to YOUR Arduino.

Are you using the TMRh20 version of the RF24 library?

…R

Here is the new transmitter code, seems to work as it is not hanging and serial works

// SimpleTx - the master or the transmitter

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>


#define CE_PIN   9
#define CSN_PIN 10
const int REED_PIN = 2; // Pin connected to reed switch
const int LED_PIN = 3; // LED pin - active-high


const byte slaveAddress[5] = {'R','x','A','A','A'};


RF24 radio(CE_PIN, CSN_PIN); // Create a Radio




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

char dataToSend[2] = "M";
char dataToSend1[2] = "m";
void setup() {

    Serial.begin(9600);

    Serial.println("SimpleTx Starting");
    pinMode(REED_PIN, INPUT_PULLUP);
    pinMode(LED_PIN, OUTPUT);
    radio.begin();
    radio.setDataRate( RF24_250KBPS );
    radio.setRetries(3,5); // delay, count
     radio.setPayloadSize(2);
    radio.openWritingPipe(slaveAddress);
    
}

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

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

void loop() {
  

 int p = digitalRead(REED_PIN);

    
    if (p == HIGH){
    hello();
    }
    if (p == LOW){
     hello2();
    }
        // Always use sizeof() as it gives the size as the number of bytes.
        // For example if dataToSend was an int sizeof() would correctly return 2

    //Serial.print("Data Sent ");
    //Serial.print(dataToSend);
    delay(500);
    }

void hello() {
  char dataReceived[2] = "M";
  radio.write( &dataReceived,2);
    Serial.print("M");
    delay(500);
    loop();
}
void hello2(){
  char dataReceived[2] = "m";
  radio.write(&dataReceived,2);
     Serial.print("m");
     delay(500);
     loop();
}

Here is the receiver code, I am not receiving any data on this side other than “Data Received”

// SimpleRx - the slave or the receiver

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

#define CE_PIN   9
#define CSN_PIN 10

const byte thisSlaveAddress[5] = {'R','x','A','A','A'};

RF24 radio(CE_PIN, CSN_PIN);

char dataReceived[2]; // this must match dataToSend in the TX
bool newData = false;

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

void setup() {

    Serial.begin(9600);

    Serial.println("SimpleRx Starting");
    radio.begin();
    radio.setDataRate( RF24_250KBPS );
    radio.openReadingPipe(1, thisSlaveAddress);
    radio.startListening();
}

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

void loop() {
    getData();
    showData();
}

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

void getData() {
    if ( radio.available() ) {
        radio.read( &dataReceived, 2);
        newData = true;
    }
}

void showData() {
    if (newData == true) {
        Serial.print("Data received ");
        Serial.println(dataReceived);
        newData = false;
    }
}

Wireless programs can be very hard to debug.

Start with my unchanged example and don't move from that until it works reliably.

You did not answer my question about the RF24 library you are using.

...R

I am using the RF24 library as many say it is the most straightforward, do you recommend another? Also the unedited code did the same thing just kept printing "Data Received" without any actual data being shown.
I have been researching bluetooth and 443mhz transmitters, do you think either of those works better?
Thanks

koenigb96:
I am using the RF24 library as many say it is the most straightforward, do you recommend another?

My code uses a particular version of the RF24 library. Is that the same as the one you have installed? Or have you the earlier ManiacBug version?

Unfortunately when TMRh20 produced his updated version he used the same name for the library - which is very confusing.

If in doubt I suggest you delete the library completely and then download and install the TMRh20 version.

...R

Okay thank you, I will try that now.