Hello, I am having trouble getting two NRF24L01 wireless transceiver modules to communicate using the code I have written. I am using the RF24 library for the transceivers, and I am able to run the "Getting Started" example code included in the library with no issues.
I have tried various configurations of the code, using different power settings, channels, and pipe settings. I also have a 10 uF capacitor across VCC and GND of the tranceivers as reccomended by this website (hyperlinked).
Perhaps an experienced user of the NRF24L01 wireless transceiver can critique my code. I am using a Nano to transmit data from a moisture sensor ten times once a minute, and then sleeping the MCU using the "LowPower" library. I am having no problems sending out the data , according to the Serial Monitor. I am using an Uno to receive the data from the Nano. Please note the has incomplete code within it, since the project is not yet complete. Here is the code:
Nano (Transmitter):
/*---------------LIBRARIES---------------*/
#include "LowPower.h" //For sleep cycle
#include <SPI.h> //For RF comms
#include "nRF24L01.h" //For RF comms
#include "RF24.h" //For RF comms
#include "printf.h" //For RF comms
/*---------------PIN SETUP---------------*/
const int MoistureAnalog = A0;
const int MoisturePower = 2;
RF24 RFComms (9, 10);
/*------------VARIABLES & OBJECTS------------*/
int MoistureValue;
byte SleepCycle = 0;
byte RFCommsCycle = 0;
const uint64_t pipe = 0xE6E6E6E6E6E6;
/*------------------SETUP------------------*/
void setup() {
//RF Comms Initialization
RFComms.begin();
RFComms.setChannel(69);
RFComms.setPALevel(RF24_PA_LOW);
RFComms.openWritingPipe(pipe);
RFComms.stopListening();
//Moisture Sensor
pinMode(MoisturePower, OUTPUT);
digitalWrite(MoisturePower, LOW);
//Serial for testing
Serial.begin(115200);
}
/*------------------MAIN BODY------------------*/
void loop() {
while (SleepCycle < 5) {
SleepCycle++;
LowPower.powerDown(SLEEP_1S, ADC_OFF, BOD_OFF);
}
while (SleepCycle >= 5) {
Serial.println("Turning on Sensor...");
digitalWrite(MoisturePower, HIGH);
delay(250);
Serial.println("Reading Moisture...");
MoistureValue = analogRead(MoistureAnalog);
Serial.println(MoistureValue, DEC);
delay(250);
while (RFCommsCycle < 10) {
RFCommsCycle++;
Serial.println("BROADCASTING DATA");
RFComms.write( &MoistureValue, sizeof(MoistureValue) );
delay(500);
}
while (RFCommsCycle >= 10) {
RFCommsCycle = 0;
SleepCycle = 0;
}
}
}
Uno (Receiver):
/*---------------LIBRARIES---------------*/
#include <Wire.h> //For LCD, RTC
#include <LiquidCrystal_I2C.h> //For LCD
#include <NewPing.h> //For Ultrasonic Sensor
#include "RTClib.h" //For RTC
#include <SPI.h> //For RF comms
#include "nRF24L01.h" //For RF comms
#include "RF24.h" //For RF comms
#include "printf.h" //For RF comms
/*---------------PIN SETUP---------------*/
const int Relay = 2;
NewPing WaterLevel(3, 4, 80); //Water level ultrasonic sensor: 3 trig pin, 4 echo pin, 200 max distance in cm
const int Beeper = 5;
LiquidCrystal_I2C lcd(0x27, 20, 4); //LCD Display: A4 - SDA, A5 - SCL
RTC_DS3231 rtc; //RTC on corresponding SDA and SCL pins
RF24 RFComms (9, 10);
/*------------VARIABLES & OBJECTS------------*/
//Water Level Sensor
#define PingSpeed 35 //Frequency to ping the ultrasonic sensor (ms)
unsigned long PingWaterLevel;
int WaterLevelDistance;
//Moisture Sensor
int MoistureValue;
const char *MoistureString[] =
{
" Soil: Very Dry",
" Soil: Dry",
" Soil: Moist",
" Soil: Very Moist",
};
//RTC
unsigned long LCDPrintTime;
unsigned long currentLCDPrintTime;
//RF Comms
const uint64_t pipe = 0xE6E6E6E6E6E6;
/*------------------SETUP------------------*/
void setup() {
Serial.begin(115200);
//Pin Initialization
pinMode(Relay, OUTPUT);
pinMode(Beeper, OUTPUT);
//RTC Initialization
rtc.begin();
rtc.adjust(DateTime(__DATE__, __TIME__));
currentLCDPrintTime = millis();
//RF Comms Initialization
RFComms.begin();
RFComms.setChannel(69);
RFComms.setPALevel(RF24_PA_LOW);
RFComms.openReadingPipe(0, pipe);
RFComms.startListening();
RFComms.enableDynamicPayloads();
//LCD Initialization
lcd.init();
lcd.backlight();
lcd.setCursor ( 0, 0 );
lcd.print(" BEAR Automation ");
lcd.setCursor ( 0, 1 );
lcd.print(" AGWS v 1.0 ");
lcd.setCursor ( 0, 2 );
lcd.print(" ");
lcd.setCursor ( 0, 3 );
lcd.print(" Initializing... ");
delay(4000);
lcd.setCursor ( 0, 0 );
lcd.print(" ");
lcd.setCursor ( 0, 1 );
lcd.print(" ");
lcd.setCursor ( 0, 2 );
lcd.print(" ");
lcd.setCursor ( 0, 3 );
lcd.print(" ");
}
/*------------------MAIN BODY------------------*/
void loop() {
//LCD Display
DateTime now = rtc.now();
currentLCDPrintTime = millis();
if ((millis() - LCDPrintTime) > 1000){
//Time of day
lcd.setCursor ( 0, 0 );
lcd.print(" ");
lcd.setCursor ( 0, 0 );
lcd.print(" ");
char dateBuffer[12];
sprintf(dateBuffer,"%02u:%02u:%02u ",now.hour(),now.minute(),now.second());
lcd.print(dateBuffer);
//Moisture level
lcd.setCursor ( 0, 1 );
lcd.print(" ");
lcd.setCursor ( 0, 1 );
lcd.print(MoistureValue);
LCDPrintTime = currentLCDPrintTime;
}
//RF Comms (Listening)
if ( RFComms.available()) {
while (RFComms.available())
{
Serial.println("Reading data...");
RFComms.read( &MoistureValue, sizeof(MoistureValue) );
Serial.println(MoistureValue);
}
}
}