nRF24L01+ Sending and receiving data

Hello everybody.

I'm needing a little help with the programming of my project.
The project's goal is to control and monitor two leds sensors using radio frequency. I chose the nRF24L01+ transceiver to be half-duplex.
An Arduino+nRF24(PC) will be connected to the computer, its role is to control the LEDs and receive the value of the sensors connected to the other Arduino+nRF24(HOME).

My first attempt was using the module RF433MHz, and arrived in this programming (working perfectly):
(Arduino+nRF24(HOME))

#include <VirtualWire.h>
#include <Thermistor.h>
Thermistor temp(5);

int LDR = A4;
const int pinQ = 10;
const int pinP[] = {5, 6, 7, 8, 4, 9, 10, 11, 12, 13};
bool pinS[] = {LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW};
char info[30]; //RF
long previousMillis = 0; //time
long interval = 1000; //time

void setup()
{
  Serial.begin(9600);
  for (int pinOUT=0;pinOUT<pinQ;pinOUT++){pinMode(pinP[pinOUT],OUTPUT);}
  pinMode(3,INPUT);
  vw_set_rx_pin(3); // Pino de Recepção
  vw_setup(2000);
  vw_rx_start();
}

void loop()
{
  // RF
  uint8_t buf[VW_MAX_MESSAGE_LEN];
  uint8_t buflen = VW_MAX_MESSAGE_LEN;
  if (vw_get_message(buf, &buflen))
  {
    int i;  for (i = 0; i < buflen; i++)  {  info[i]=buf[i];  }  // Guarda a Informação Recebida num Array
  
    // LEDs
    switch(info[0])
    {
      case 48:  digitalWrite(pinP[0],!digitalRead(pinP[0]));  break;
      case 49:  digitalWrite(pinP[1],!digitalRead(pinP[1]));  break;
      case 50:  digitalWrite(pinP[2],!digitalRead(pinP[2]));  break;
      case 51:  digitalWrite(pinP[3],!digitalRead(pinP[3]));  break;
      case 52:  digitalWrite(pinP[4],!digitalRead(pinP[4]));  break;
      case 53:  digitalWrite(pinP[5],!digitalRead(pinP[5]));  break;
      case 54:  digitalWrite(pinP[6],!digitalRead(pinP[6]));  break;
      case 55:  digitalWrite(pinP[7],!digitalRead(pinP[7]));  break;
      case 56:  digitalWrite(pinP[8],!digitalRead(pinP[8]));  break;
      case 57:  digitalWrite(pinP[9],!digitalRead(pinP[9]));  break;
      case 97:  for (int pinOUT = 0; pinOUT < pinQ; pinOUT++)  {  digitalWrite(pinP[pinOUT], LOW);  };  break;
      case 98:  for (int pinOUT = 0; pinOUT < pinQ; pinOUT++)  {  digitalWrite(pinP[pinOUT], HIGH);  };  break;
    }
    Serial.println(String(info)); // Escreve no SerialMonitor a Informação Recebida
    memset( &info, 0, sizeof(info) );
  }
  
  unsigned long currentMillis = millis();
  if(currentMillis - previousMillis > interval)
  {
    previousMillis = currentMillis;
    // Luminosidade
    int luminosidade = analogRead(LDR);
    Serial.print("Luminosidade: ");
    Serial.println(luminosidade);
    
    // Temperatura
    int temperatura = temp.getTemp();
    Serial.print("Temperatura: ");
    Serial.print(temperatura);
    Serial.println("*C");
  }
}

Arduino+nRF24(PC)

#include <VirtualWire.h>

char info[30];
int index = 0;
char inChar;

void setup()
{
  Serial.begin(9600);
  pinMode(4,OUTPUT);
  vw_set_tx_pin(4); // Pino para Transferir
  vw_set_ptt_inverted(true);
  vw_setup(2000); // Velociade de Transmissão
}

void loop()
{
  if (Serial.available() > 0)
  {
    while (1)
    {
      inChar = Serial.read(); // Guarda o Primeiro Caracter
      info[index] = inChar; // Coloca o Caracter numa Array
      index++;
      delay(2);
      if (Serial.available() <= 0)
      {
        index = 0;
        const char *msg = info;
        vw_send((uint8_t *)msg, strlen(msg)); // Envia a Informação
        vw_wait_tx();
        memset( &info, 0, sizeof(info) );
        break;
      }
    }
  }
}

And in the last days I have tried to adapt this code to use nRF24. But got no success.
I've success using this code. But it only shows the sensor value when I send a character.

(Arduino+nRF24(HOME))

#include <SPI.h>
#include "RF24.h"
RF24 radio(9, 10);
const uint64_t pipes[2] = {  0xF0F0F0F000LL, 0xF0F0F0F0FFLL  };

//int LDR = A5;
int sensor;
const int pinQ = 9;
const int pinP[] = {2, 3, 4, 5, 6, 7, 8, 14, 15};
bool pinS[] = {LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW};

void setup()
{
  Serial.begin(9600);
  radio.begin();
  radio.setDataRate(RF24_250KBPS);
  radio.setChannel(100);
  radio.setRetries(15,15);
  radio.openWritingPipe(pipes[0]);
  radio.openReadingPipe(1, pipes[1]);
  radio.startListening();
  for (int pinOUT=0;pinOUT<pinQ;pinOUT++){pinMode(pinP[pinOUT],OUTPUT);}
}

void loop()
{
  if(radio.available())
  {
    char led[32] = "";
    radio.read(&led, 32);
    
    switch(led[0])
    {
      case '0':  digitalWrite(pinP[0],!digitalRead(pinP[0]));  break;
      case '1':  digitalWrite(pinP[1],!digitalRead(pinP[1]));  break;
      case '2':  digitalWrite(pinP[2],!digitalRead(pinP[2]));  break;
      case '3':  digitalWrite(pinP[3],!digitalRead(pinP[3]));  break;
      case '4':  digitalWrite(pinP[4],!digitalRead(pinP[4]));  break;
      case '5':  digitalWrite(pinP[5],!digitalRead(pinP[5]));  break;
      case '6':  digitalWrite(pinP[6],!digitalRead(pinP[6]));  break;
      case '7':  digitalWrite(pinP[7],!digitalRead(pinP[7]));  break;
      case '8':  digitalWrite(pinP[8],!digitalRead(pinP[8]));  break;
      case '9':  digitalWrite(pinP[9],!digitalRead(pinP[9]));  break;
      case 'a':  for (int pinOUT = 0; pinOUT < pinQ; pinOUT++)  {  digitalWrite(pinP[pinOUT], LOW);  };  break;
      case 'b':  for (int pinOUT = 0; pinOUT < pinQ; pinOUT++)  {  digitalWrite(pinP[pinOUT], HIGH);  };  break;
    }
    radio.stopListening(); sensor = analogRead(A5); radio.write(&sensor, 32); radio.startListening();
  }
}

(Arduino+nRF24(PC))

#include <SPI.h>
#include "RF24.h"
RF24 radio(9, 10);
const uint64_t pipes[2] = {  0xF0F0F0F000LL, 0xF0F0F0F0FFLL  };

int sensor;

void setup()
{
  Serial.begin(9600);
  radio.begin();
  radio.setDataRate(RF24_250KBPS);
  radio.setChannel(100);
  radio.setRetries(15,15);
  radio.openWritingPipe(pipes[1]);
  radio.openReadingPipe(1, pipes[0]);
  radio.startListening();
}

void loop()
{
  if(Serial.available())
  {
    char led[32] = "";
    byte i = 0;
    while(Serial.available())
    {
      led[i] = Serial.read();
      i++;
      delay(2);
    }
    led[i] = 0;
    radio.stopListening();
    radio.write(&led, 32);
    radio.startListening();
  }
  if(radio.available())
  {  radio.read(&sensor, 32);  Serial.print("luminosidade = ");  Serial.println(sensor); }
}

But I need to constantly monitor the value of the sensor, and only take a break when I turn on the led, and soon after returning to show value of sensor.
I saw several tutorials, but they all seem complex. My native language is Portuguese, and my english is not the best one. And since there are not many tutorials in Portuguese for this transceiver, I have to resort to tutorials in English. :~

If anyone can help, I will be very grateful.

It may not be relevant to your problem but it looks like that long switch/case could be reduced to two lines something like this

byte ledPin = led[0] - '0';
digitalWrite(ledPin, ! digitalRead(ledPin);

But I need to constantly monitor the value of the sensor, and only take a break when I turn on the led, and soon after returning to show value of sensor.

Is this a comment about the code on your HOME device or your PC device?

At the top of your Post you say the Arduino connected to the PC will be flashing LEDS but the flashing LED code seems to be in the block headed HOME ???

Perhaps you can give a simple description of what is supposed to happen - what sensor, how often it is read, what sort of data is produced by the sensor, what is to be transmitted - and in which direction, what is to be done with the received data.

My suspicion is that you need some control process that organizes the data transfer.

...R

Is this a comment about the code on your HOME device or your PC device?

At the top of your Post you say the Arduino connected to the PC will be flashing LEDS but the flashing LED code seems to be in the block headed HOME ???

Perhaps you can give a simple description of what is supposed to happen - what sensor, how often it is read, what sort of data is produced by the sensor, what is to be transmitted - and in which direction, what is to be done with the received data.

I have attached an image, it may be useful to explain. The image was taken when the RF433 was being used in the project.

The PC device will be responsible for sending a character, by serial monitor and transmitted by the transceiver, which will turn on the LED (connected to HOME device) associated with the character. It also will be responsible for, to receive the values ??of the LDR (which will be transmitted by the HOME device) and display in serial monitor with a small delay of 500ms.

HOME device receives the character and turned on the LED, long while it doesn't receive any characters, the value of the sensor is sent.

caioluan:
HOME device receives the character and turned on the LED, long while it doesn't receive any characters, the value of the sensor is sent.

I don't understand this sentence at all.

From the other explanation I think these are the steps you want to happen (correct me if I am wrong)

PC sends a single character that is typed in the Serial Monitor.
Arduino A receives the character via USB connection.
Arduino A turns on an LED - different LED for different characters ???
Arduino A listens for a transmission from Arduino B
Arduino B reads the value of a Light Dependent Resistor every 500 msecs
Arduino B transmits the value (in what format ?) to Arduino A
When Arduino A receives the value if sends it to the PC to be displayed in the Serial Monitor
The characters type in the Serial Monitor have nothing to do with Arduino B

If you look at this Thread you will see how I would turn these steps into a program (2 programs in this case).

If my list of steps is correct perhaps you can use it to explain what the problem is.

...R

It may not be relevant to your problem but it looks like that long switch/case could be reduced to two lines something like this

I followed your tip and it really reduced the program, never thought that way.

I'll read your topic and make some changes in my program to make it more understandable. Will soon post the new programming and better explaining my doubt.

Thank you R

my two updated codes:

HOME device

//==============# para o transceptor #=================================//
#include <SPI.h>
#include "RF24.h"
RF24 radio(9, 10);  //(CE, CSN)
const uint64_t pipes[2] = {  0xF0F0F0F000LL, 0xF0F0F0F0FFLL  };  //endereço

//==============# para os leds #=======================================//
const byte ledQ = 9;  //quantidade
const byte ledP[] = {2, 3, 4, 5, 6, 7, 8, 14, 15};  //pinos

//==============# para o sensor #======================================//
const byte LDR = A5;
byte sensor;

void setup(){
  Serial.begin(9600);
  
  radio.begin();
  radio.setDataRate(RF24_250KBPS);
  radio.setChannel(100);
  radio.setRetries(15,15);
  radio.openWritingPipe(pipes[0]);
  radio.openReadingPipe(1, pipes[1]);
  radio.startListening();
  
  for (byte ledOUT=0; ledOUT<ledQ; ledOUT++)  {pinMode(ledP[ledOUT],OUTPUT);} //configurando os 'ledPin' como Digital OUTPUT
}

void loop(){
  if(radio.available()){
    receberCaractere();
    enviarSensor();
  }
}

//==============# receber caractere do 'PC' #==========================//
void receberCaractere(){
  char ledC[32] = "";
  radio.read(&ledC, 32);
  if(ledC >= 0){
    byte ledN = ledC[0] - '0';
    digitalWrite(ledP[ledN], ! digitalRead(ledP[ledN]));
    if(ledN == byte(49)){  for (int pinOUT=0; pinOUT<ledQ; pinOUT++)  {  digitalWrite(ledP[pinOUT], LOW);  };  }
    if(ledN == byte(50)){  for (int pinOUT=0; pinOUT<ledQ; pinOUT++)  {  digitalWrite(ledP[pinOUT], HIGH);  }; }
  }
}

//==============# enviar valores dos sensores #========================//
void enviarSensor(){
    radio.stopListening();
    sensor = analogRead(LDR);
    radio.write(&sensor, 32);
    radio.startListening();
}

PC device

//==============# para o transceptor #=================================//
#include <SPI.h>
#include "RF24.h"
RF24 radio(9, 10);  //(CE, CSN)
const uint64_t pipes[2] = {  0xF0F0F0F000LL, 0xF0F0F0F0FFLL  };  //endereço

//==============# para o sensor #======================================//
byte sensor;

void setup(){
  Serial.begin(9600);
  
  radio.begin();
  radio.setDataRate(RF24_250KBPS);
  radio.setChannel(100);
  radio.setRetries(15,15);
  radio.openWritingPipe(pipes[1]);
  radio.openReadingPipe(1, pipes[0]);
  radio.startListening();
}

void loop(){
  receberSensor();
  enviarCaractere();
}

//==============# receber o valor do sensor #==========================//
void receberSensor(){
    radio.read(&sensor, 32);
    Serial.print("Luminosidade = ");
    Serial.println(sensor);
    delay (500);
}

//==============# enviar o caractere para controlar os leds #==========//
void enviarCaractere(){
  radio.stopListening();
  char ledC;
  ledC = Serial.read();
  radio.write(&ledC, 1);
  radio.startListening();
    
  if (ledC >= 0){
    Serial.print("Led = ");
    Serial.println(ledC);
  }
}

Both working. But, don't know for what reasons, the LDR starts to show the value 0 in the serial monitor after some time.

caioluan:
Both working. But, don't know for what reasons, the LDR starts to show the value 0 in the serial monitor after some time.

I'm not sure if you are looking for more advice. If so you will need to explain the remaining problem in a lot more detail.

...R

The project name is: Control and monitoring of lighting.

The user, through an Arduino + nrf24 connected to the computer, will control LEDs and monitor the luminosity (LDR) that will be connected to another Arduino + nrf24 device. For ease of explanation we will call the set "Arduino + nrf24" connected to the computer of "PC device", and the other "HOME device".

Steps:

  • The user types a character in the serial monitor of the PC device.
  • PC device sends the character for HOME device.
  • HOME device receives and turn on the LED associated with that character.
  • HOME device, while not receives, read and send the value of the LDR.
  • PC device, until the user sends a character, receives and shows the value of the LDR in the serial monitor.

caioluan:
The project name is: Control and monitoring of lighting.

I'm still not sure whether you are just telling us about your project or whether you have a question that you would like answered.

...R