NRF24L01
Firstly, I'm not usually someone who likes to ask for help but having trawled through virtually every page that Google returns on NRF24L01 and having virtually not slept for the past 72 hours I can now admit defeat and ask for help.
I am trying to retrofit an NRF24L01 radio into an existing project of mine made a few years ago to monitor and log my electricity usage, which works flawlessly , however I had the bright idea that I could use these radios to display the collected data in real time on a 'remote monitor'. I have purchased 20 of these little devils off eBay as I'm sure I'll use them in other upcoming projects. The ones I have are the low power devices as I only have a small house so don't need to transmit vast distances.
Background:
I started with the simple ping/pong examples and soon realised that the 'optional' caps across Vcc and GND are not that optional at all, once these were on this worked a charm.
Next I created a simple transmit test sketch and a simple receive sketch to send 2 pieces of data continuously- this again worked.
Below if the transmit sketch:
/* nRF24L01 Transmit radioData values
- WHAT IT DOES: Reads Analog values on A0, A1 and transmits
them over a nRF24L01 Radio Link to another transceiver.
- CONNECTIONS: nRF24L01 Module:
1 - GND
2 - VCC 3.3V !!! NOT 5V
3 - CE to Arduino pin 6
4 - CSN to Arduino pin 7
5 - SCK to Arduino pin 13
6 - MOSI to Arduino pin 11
7 - MISO to Arduino pin 12
8 - UNUSED
-
Analog radioData:
GND to Arduino GND
VCC to Arduino +5V
X Pot to Arduino A0
Y Pot to Arduino A1
*/
// Import libraries
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
// declare constants
#define CE_PIN 6
#define CSN_PIN 7
#define radioData_X A0
#define radioData_Y A1
// define the transmit pipe
const uint64_t pipe = 0xE8E8F0F0E1LL;
// declare objects
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
//declare variables
int radioData[2]; // 2 element array holding radioData readings
void setup()
{
Serial.begin(57600);
radio.begin();
radio.openWritingPipe(pipe);
}
void loop()
{
Serial.println ("Sending data");
radioData[0] = analogRead(radioData_X); // read value for test trasmit
radioData[1] = analogRead(radioData_Y); // read value for test transmit
radio.write( radioData, sizeof(radioData) ); //send the values
}
Below is the receive sketch:
// Power Monitor Remote Display
#include <UTFT.h>
#include <Wire.h>
#include "Adafruit_MCP9808.h"
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
long previousMillis = 0;
long interval = 1000; // interval to look at the radio
// Declare which fonts we will be using
extern uint8_t SmallFont[];
extern uint8_t BigFont[];
extern uint8_t SevenSegNumFont[];
const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe
UTFT myGLCD(ILI9325C,15,14,17,16); //A1, A0, A3, A2
Adafruit_MCP9808 tempsensor = Adafruit_MCP9808();
RF24 radio(9, 10);
int kWData[2];
void setup()
{
myGLCD.InitLCD(LANDSCAPE);
myGLCD.clrScr();
myGLCD.setBackColor(0, 0, 0);
tempsensor.begin(0x18);
//if (!tempsensor.begin(0x18)) {
// myGLCD.setColor(255,0,0);
// myGLCD.print("Temp Sensor Error", CENTER, 32);
// while (1);
//}
drawMainOverviewScreen();
radio.begin();
radio.openReadingPipe(1,pipe);
radio.startListening();
}
void loop()
{
unsigned long currentMillis = millis();
// display the room temperature
myGLCD.setColor(255, 255, 255);
float c = tempsensor.readTempC();
//float f = c * 9.0 / 5.0 + 32;
myGLCD.setFont(BigFont);
myGLCD.printNumI(c,190,5);
// read the radio
if (currentMillis - previousMillis > interval) {
if (radio.available()) {
// display wireless connectivity
myGLCD.setColor(255, 255, 255);
myGLCD.fillRect(5, 15, 6, 12);
myGLCD.fillRect(10, 15, 11, 9);
myGLCD.fillRect(14, 15, 15, 5);
// Read the data payload until we've received everything
bool done = false;
while (!done) {
// Fetch the data payload
done = radio.read( kWData, sizeof(kWData) );
}
myGLCD.setColor(255, 255, 255);
char formattedkWvalue[5];
myGLCD.setFont(SevenSegNumFont);
// display current and total power consumption
sprintf(formattedkWvalue,"%04d",kWData[0]);
myGLCD.print(formattedkWvalue, 50, 50); // current usage
sprintf(formattedkWvalue,"%04d",kWData[1]);
myGLCD.print(formattedkWvalue, 50, 130); // total usage
previousMillis = currentMillis;
}
else {
//No radio available
// display wireless connectivity
myGLCD.setColor(16, 16, 16);
myGLCD.fillRect(5, 15, 6, 12);
myGLCD.fillRect(10, 15, 11, 9);
myGLCD.fillRect(14, 15, 15, 5);
// display current and total power consumption
//myGLCD.setFont(SevenSegNumFont);
//myGLCD.setColor(0,0,0);
//myGLCD.print("0000", 50, 50); // current usage
//myGLCD.print("0000", 50, 130); // total usage
}
}
}
void drawMainOverviewScreen()
{
// set up screen
myGLCD.fillScr(0,0,0); // fill screen with black
myGLCD.setColor(85,26,139); // purple
myGLCD.fillRect(250,0,320,240); // draw 'menu' box on right
myGLCD.setColor(0,0,0); // black
myGLCD.drawRect(250,0,319,80); // draw top menu box
myGLCD.drawRect(250,160,319,240); // draw bottom menu box
// set color white / font big for the rest of the display set up
myGLCD.setColor(255,255,255);
myGLCD.setFont(BigFont);
// display current power consumption
myGLCD.print("NOW",50,30);
myGLCD.print("W", 180, 85);
// display total power consumption
myGLCD.print("TOTAL",50,110);
myGLCD.print("W", 180, 165);
//display room temperature
myGLCD.print("C", 230, 5);
myGLCD.setFont(SmallFont);
myGLCD.print("o",223,3);
}
After this I transposed the sending code into my original energy monitor sketch, added in the hardware and smiled as I powered it up......only to then see nothing on my 'remote monitor' Note: I have changed the CSN and CE pins as my original project already used those pins,
I immediately thought that I must have some conflict in my energy monitor sketch and started trawling through this, and many Google searches before I drew a blank. Unfortunately "testing" is hard to near impossible as the project was made on stripboard as a bare bones Arduino so I don't have the ability to hook up the PC and serial monitor, so I went back and forth programming, changing, programming until I had no hair left.
I reverted back to my original sending sketch (running on a 'real' UNO) as the only thing I could think of was that my energy monitor sketch takes a while to "loop" due to the collecting of the data, formatting, displaying on an LCD and sending the data to my own web services for logging to a SQL database for future analysis.
Low and behold, if I put a delay in my original test sending sketch I get exactly the same results - nothing displaying on my 'remote monitor'
See Below for sketch including the delay:
/* nRF24L01 Transmit radioData values
- WHAT IT DOES: Reads Analog values on A0, A1 and transmits
them over a nRF24L01 Radio Link to another transceiver.
- CONNECTIONS: nRF24L01 Modules See:
1 - GND
2 - VCC 3.3V !!! NOT 5V
3 - CE to Arduino pin 6
4 - CSN to Arduino pin 7
5 - SCK to Arduino pin 13
6 - MOSI to Arduino pin 11
7 - MISO to Arduino pin 12
8 - UNUSED
-
Analog radioData or two 10K potentiometers:
GND to Arduino GND
VCC to Arduino +5V
X Pot to Arduino A0
Y Pot to Arduino A1
*/
// Import libraries
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
// declare constants
#define CE_PIN 6
#define CSN_PIN 7
#define radioData_X A0
#define radioData_Y A1
// define the transmit pipe
const uint64_t pipe = 0xE8E8F0F0E1LL;
// declare objects
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
//declare variables
int radioData[2]; // 2 element array holding radioData readings
void setup()
{
Serial.begin(57600);
radio.begin();
radio.openWritingPipe(pipe);
}
void loop()
{
Serial.println ("Sending data");
radioData[0] = analogRead(radioData_X); // read value for test trasmit
radioData[1] = analogRead(radioData_Y); // read value for test transmit
radio.write( radioData, sizeof(radioData) ); //send the values
//delay(1500);
}
Sorry if I've waffled on, but I wanted to give as much info as possible.
Can anyone please tell me what I'm doing wrong and, hopefully, what I can do to fix this issue?
Your help will be greatly received. Thanks