How do I send a number from NRF24L01 TX to RX

I dont know how to send my temperature value through NRF24L01 RX to TX.

I hope someone can help me :smiley:

Here is my TX code:

//TEMP:
#include <OneWire.h>
#include <DallasTemperature.h>
#include <AccelStepper.h>
#include <SPI.h>
#include <RF24.h>


// Data wire er monteret på digital pin 2 på Arduino
#define ONE_WIRE_BUS 34

// PH-motor: Definere step konstant
#define HALFSTEP 8

// Temp: Progremmere en oneWire(1-leder) for at kommunikere med andre oneWire apparater
OneWire oneWire(ONE_WIRE_BUS);

// Temp: Led oneWire reference til DallasTemperature library
DallasTemperature sensors(&oneWire);

// PH-motor: Pins opsat i IN1-IN3-IN2-IN4 for korrekt step sequence
AccelStepper PH_stepper(HALFSTEP, 49, 46, 48, 44);

// Klor-motor: Pins opsat i IN1-IN3-IN2-IN4 for korrekt step sequence
AccelStepper Klor_stepper(HALFSTEP, 42, 38, 40, 36);

//create an RF24 object
RF24 radio(8, 9); // CE, CSN

//address through which two modules communicate.
const byte address[6] = "00001";

//PH:
unsigned long int PH_avgValue;  //Gem den gennemsnitlige værdi fra sensor feedback
int PH_buffer_array[10];


//KLOR:
unsigned long int Klor_avgValue;  //Gem den gennemsnitlige værdi fra sensor feedback
int Klor_buffer_array[10];


void setup()
{
  //TEMP:
  sensors.begin();  // Start library
  Serial.begin(9600);

  //PH-motor:
  // Set the maksimal steps per sekund:
  PH_stepper.setMaxSpeed(1000);
  // Set the maksimal acceleration i steps per sekund:
  PH_stepper.setAcceleration(100);

  //Klor-motor:
  // Set the maksimal steps per sekund:
  Klor_stepper.setMaxSpeed(1000);
  // Set the maksimal acceleration i steps per sekund:
  Klor_stepper.setAcceleration(100);  


  radio.begin();
  radio.openWritingPipe(address);
  //Hvis modulerne flyttes længere fra hinanden, skal "setPAlevel" slettes 
  //og der skal monteres en støjafkopling over forsyningen.
  radio.setPALevel(RF24_PA_MIN);
  //Set module as transmitter
  radio.stopListening();  

}
void loop()
{

  //TEMP:
  // Send kommandoen for at få temperaturen
  sensors.requestTemperatures();

  //skriv temperaturen i Celsius
  Serial.print("Temperatur: ");
  Serial.print(sensors.getTempCByIndex(0));
  Serial.println("°C");
 
  delay(1000);

  //PH:
  for (int a = 0; a < 10; a++) //PH: 10 prøver for at få en mere præcis værdi
  {
    PH_buffer_array[a] = analogRead(A0);
    delay(10);
  }
  for (int a = 0; a < 9; a++) //PH Sortere analog fra lille til stor

   PH_avgValue = 0;
  for (int a = 2; a < 8; a++)               //Gennemsnitlig værdi fra 6 center prøver
    PH_avgValue += PH_buffer_array[a];
  float phValue = (float)PH_avgValue * 4.15 / 1024 / 6; //Konventere analog til Volt og finde summen af de 6 center prøver
  phValue = 14 - 3.5 * phValue;                  //Konventere Volt til PH værdi

  if (phValue < 5)
  {
    Serial.print("PH for lav: ");
    Serial.println(phValue);
    
   const char text[] = "PH for lav";
    radio.write(&text, sizeof(text));
    
delay(2000);
  }

  else if (phValue > 8)
  {
    Serial.print("PH for høj: ");
    Serial.println(phValue);
    
   const char text[] = "PH for horj";
    radio.write(&text, sizeof(text));
    
  //PH-motor: 
  // Set slut position:
  PH_stepper.moveTo(2038); //2038 steps svarer til en halv omgang
  // Kør til den valgte position med den valgte hastighed og acceleration:
  PH_stepper.runToPosition();
  
  delay(1000);
  
  // Kør tilbage til start position:
  PH_stepper.moveTo(0);
  // Kør til start med den valgt hastighed og acceleration:
  PH_stepper.runToPosition();
  
 delay(2000);
  }

  else
  {
    Serial.print("PH OK :");
    Serial.println(phValue);

   const char text[] = "PH OK";
    radio.write(&text, sizeof(text));
   // radio.write(&phValue, sizeof(phValue));
    
   delay(2000);
  }

  {
    for (int b = 0; b < 10; b++) //10 prøver for at få en mere præcis værdi
    {
      Klor_buffer_array[b] = analogRead(A1);
      delay(10);
    }
    for (int b = 0; b < 9; b++) //Sortere analog fra lille til stor

   Klor_avgValue = 0;
    for (int b = 2; b < 8; b++)               //Gennemsnitlig værdi fra 6 center prøver
      Klor_avgValue += Klor_buffer_array[b];
    float klorValue = (float)Klor_avgValue * 5.09 / 1024 / 6; //Konventere analog til Volt og finde summen af de 6 center prøver

   if (klorValue <= 1.22)
    {
      Serial.print("Klor for lav: ");
      Serial.println(klorValue);

   const char text[] = "Klor for lav";
   radio.write(&text, sizeof(text));      
      
  //Klor-motor: 
  // Set slut position:
  Klor_stepper.moveTo(2038); //2038 steps svarer til en halv omgang
  // Kør til den valgte position med den valgte hastighed og acceleration:
  Klor_stepper.runToPosition();
  
  delay(1000);
  
  // Kør tilbage til start position:
  Klor_stepper.moveTo(0);
  // Kør til start med den valgt hastighed og acceleration:
  Klor_stepper.runToPosition();
  
   delay(2000);
    }

   else if (klorValue >= 1.47)
    {
      Serial.print("Klor for høj: ");
      Serial.println(klorValue);

   const char text[] = "Klor for hoej";
      radio.write(&text, sizeof(text));
      delay(2000);
    }

   else
    {
      Serial.print("Klor OK: ");
      Serial.println(klorValue);

   const char text[] = "Klor OK";
      radio.write(&text, sizeof(text));
      delay(2000);
    }
  }
}

RX code:

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

RF24 radio(8, 9); // CE, CSN

// Creates an LCD object. Parameters: (rs, enable, d4, d5, d6, d7)
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);


const byte address[6] = "00001";

void setup() {
  Serial.begin(9600);
  radio.begin();
  radio.openReadingPipe(0, address);
  //Hvis modulerne flyttes længere fra hinanden, skal "setPAlevel" slettes 
  //og der skal monteres en støjafkopling over forsyningen.
  radio.setPALevel(RF24_PA_MIN);
  //Set module som receiver
  radio.startListening();
  

  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);

  // Clears the LCD screen
  lcd.clear();
}

void loop() {
  if (radio.available()) 
  {
    char text[32] = "";
    radio.read(&text, sizeof(text));
    Serial.println(text);

  // Print a message to the LCD.
  lcd.print(text);

  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
 // lcd.setCursor(0, 1);
  // Print a message to the LCD.
  //lcd.print(" ");
  delay(3000);
  lcd.clear();
  //delay(100);    
  }
}

I also have a smaller exampel

TX:

#include <OneWire.h>
#include <DallasTemperature.h>
#include <SPI.h>
#include <RF24.h>


// Data wire er monteret på digital pin 2 på Arduino
#define ONE_WIRE_BUS 34

// Temp: Progremmere en oneWire(1-leder) for at kommunikere med andre oneWire apparater
OneWire oneWire(ONE_WIRE_BUS);

// Temp: Led oneWire reference til DallasTemperature library
DallasTemperature sensors(&oneWire);

 
//int pin = ONE_WIRE_BUS 34;
//DallasTemperature dallastemperature(pin);
//float temperature[2];
float temperature[1];
 


RF24 radio(8, 9);
const uint64_t pipe = 0xE8E8F0F0E1LL;


 
void setup(void) 
{
//TEMP:
sensors.begin();  // Start library
Serial.begin(9600);

radio.begin();
radio.openWritingPipe(pipe);
radio.setPALevel(RF24_PA_MIN);
//Set module as transmitter
radio.stopListening();  

}
 
void loop(void)
{
//TEMP:
// Send kommandoen for at få temperaturen
sensors.requestTemperatures(); 

  //skriv temperaturen i Celsius
  Serial.print("Temperatur: ");
  Serial.print(sensors.getTempCByIndex(0));
  Serial.println("°C");

float temp;
sensors.getTempCByIndex(temp);
temperature[0] = temp;
//temperature[1] = humi;
radio.write(temperature, sizeof(temperature));
delay(1000);
}

RX:

#include <SPI.h>
#include <RF24.h>
#include <LiquidCrystal.h>
#include <DallasTemperature.h>
#include <OneWire.h>

// Data wire er monteret på digital pin 2 på Arduino
//#define ONE_WIRE_BUS 34

// Temp: Progremmere en oneWire(1-leder) for at kommunikere med andre oneWire apparater
//OneWire oneWire(ONE_WIRE_BUS);

// Temp: Led oneWire reference til DallasTemperature library
//DallasTemperature sensors(&oneWire);
 
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
 
 
//float temperature[2];
float temperature[1];
 
RF24 radio(8, 9);
const uint64_t pipe = 0xE8E8F0F0E1LL;
 
void setup(void) 
{
  Serial.begin(9600);
  lcd.begin(16, 2);
 
  radio.begin();
  radio.openReadingPipe(1, pipe);
  radio.setPALevel(RF24_PA_MIN);
  radio.startListening();
 
 
  lcd.print("Temp");
  delay(2000);
  lcd.clear();
  lcd.print("Starting.....");
  delay(2000);
}
 
void loop(void)
{
  if ( radio.available() )

{
   //skriv temperaturen i Celsius
  Serial.print("Temperatur: ");
//  Serial.print(sensors.getTempCByIndex(0));
  Serial.println("°C");
  
  radio.read(temperature, sizeof(temperature));
  lcd.clear();
  delay(500);
 
  lcd.setCursor(0, 0);
  lcd.print("Temp");
  //lcd.setCursor(0, 1);
  //lcd.print("Humidity");
  lcd.setCursor(9, 0);
  lcd.print(temperature[0]);
  lcd.print(" C");
  //lcd.setCursor(9, 1);
  //lcd.print(temperature[1]);
  //lcd.print(" %");
  delay(1000);
}
}

please edit your posts, select the code part and press the </> icon in the tool bar to mark it as code. It's barely readable as it stands. (also make sure you indented the code in the IDE before copying, that's done by pressing ctrlT on a PC or cmdT on a Mac)

The easier you make it to read and copy the code the more likely it is that you will get help

Please follow the advice given in the link below when posting code , use code tags and post the code here

Is it better now?

I think i fixed it

You didn't

What about now? :- :sweat_smile:

still no luck... I'v done it for you on the first post

Thank you!

you still have #2 to modify :wink:

i got it now :joy:

great progress :wink:

to your question

I have an example of NRF24L01 communication (text is in French but code is code) that will transfer whatever is in the payload structure. here I just store 1 character, but this could be you data array.

(google translate could possibly help with the French)

@jesper1995 Whatever you are doing in your attempts to post code on code tags it is wrong

Once you have posted code the wrong way then the easiest way to correct it is to edit the post, select all of the code and click the </> icon

When posting new code it is probably easier to right click in the code in the IDE and select "Copy for forum" which adds the code tags for you, and paste directly into a post here complete with those code tags

I understand it now, I just find the 'Copy for Forum' :smiley:

Thank you I will try and see what I can learn/use from your code :smiley:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.