Using the tutorials provided by Robin2 here: (Robin2's Simple Tutorials), I have built two devices with NRF24L01 transceivers.
One device uses a BME to read the temp and humidity at a distant point, and send it back to the main Mega chip for display.
I have managed to get the devices working with the sample sketches, and the "CheckConn" sketch returns non-zero values, so I am reasonably confident the radios themselves are not the issue.
The remote version is a simple device and it seems to be working as expected. Temp and humidity read correctly, and the NRF Serial monitor returns "Data Sent 50 Acknowledge Received" once per second as expected.
The receiver is a basic mega with nothing else atached. The code is designed to return the value sent from the remote to the Serial Monitor, and is almost unchanged from the "SimpleRX Sketch" provided by Robin2 in the much referenced forum post.
I suspect the issue lies in the use of "int" instead of "char" for the data sent between the units, but I am not sure how to best resolve this. I am experienced with electrical work but very new to programming. This issue has me tearing my hair out because I am certain there is only a tiny yet elusive problem here.
Thanks in advance for your patience. I am happy to provide more info or photos if needed.
Transmitter Code: Seems to work properly!
// REMOTE SENSOR - TX
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
Adafruit_BME280 bme;
#define CE_PIN 3
#define CSN_PIN 2
const byte slaveAddress[5] = {'R','x','A','A','A'};
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
char RArrow = (char)B01111110;
char LArrow = (char)B01111111;
char Degree = (char)223;
int Z2Hum;
int Z2Temp;
unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 1000; // send once per second
void setup() {
Serial.begin(9600);
Serial.println("SimpleTx Starting");
bool status;
status = bme.begin(0x76);
if (!status) {
Serial.println("Could not find a valid BME280 sensor, check wiring!");
while (1);
}
radio.begin();
radio.setPALevel(RF24_PA_MIN);
radio.setDataRate( RF24_250KBPS );
radio.setRetries(3,5); // delay, count
radio.openWritingPipe(slaveAddress);
lcd.begin(16,2);
}
//====================
void loop() {
currentMillis = millis();
MonitorTempHum();
Display();
if (currentMillis - prevMillis >= txIntervalMillis) {
send();
prevMillis = millis();
}
}
//====================
void Display(){
lcd.setCursor(0, 0);
lcd.print(Z2Hum);
lcd.print("%");
lcd.setCursor(8, 0);
lcd.print(Z2Temp);
lcd.print(Degree);
}
void TXOKDisplay(){
lcd.setCursor(0, 1);
lcd.print("Transmitting... ");
}
void TXERRDisplay(){
lcd.setCursor(0, 1);
lcd.print("ERROR SENDING...");
}
void send() {
bool rslt;
rslt = radio.write( &Z2Hum, sizeof(Z2Hum) );
Serial.print("Data Sent ");
Serial.print(Z2Hum);
if (rslt) {
Serial.println(" Acknowledge received");
TXOKDisplay();
}
else {
Serial.println(" Tx failed");
TXERRDisplay();
}
}
//================
void MonitorTempHum(){
int TempMeas = bme.readTemperature();
int HumMeas = bme.readHumidity();
Z2Hum = HumMeas;
Z2Temp = Celcius2Fahrenheit(TempMeas);
}
float Celcius2Fahrenheit(float celsius)
{
return celsius * 9 / 5 + 32;
}
Receiver Code - Stops at "Simple RX Started" and does not appear to do anything else
// SimpleRx - the slave or the receiver
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define CE_PIN 48
#define CSN_PIN 47
#define SS_PIN 53
const byte thisSlaveAddress[5] = {'R','x','A','A','A'};
RF24 radio(CE_PIN, CSN_PIN);
int dataReceived; // this must match dataToSend in the TX
bool newData = false;
char Z2Hum;
//===========
void setup() {
Serial.begin(9600);
pinMode(SS_PIN, OUTPUT);
Serial.println("SimpleRx Starting");
radio.begin();
radio.setPALevel(RF24_PA_MIN);
radio.setDataRate( RF24_250KBPS );
radio.openReadingPipe(1, thisSlaveAddress);
radio.startListening();
Serial.println("SimpleRx Started...");
}
//=============
void loop() {
getData();
showData();
}
//==============
void getData() {
if (radio.available()) {
radio.read( &dataReceived, sizeof(dataReceived));
Serial.println(dataReceived);
newData = true;
}
}
void showData() {
if (newData == true) {
Serial.print("Data received ");
Serial.println(dataReceived);
Z2Hum = dataReceived;
newData = false;
}
}