NRF24L01 data resolution issue

I have two NRF24L01 radio modules communicating to each other and have established a connection and can receive and transmit my data from a BMP-280 barometer, however the send data values only show an integer value, where I would like it to be able to show to 2d.p, which it can do in the serial monitor.

Transmitter code<#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

#include <Adafruit_BMP280.h>
Adafruit_BMP280 bmp;

float temperature;
char Convert[10];
char Temp;

struct MyData {
byte readTemperature;
//byte readPressure;
//byte readAltitude;
};

MyData data;

RF24 radio(7, 8); // CE, CSN
const byte address[6] = “00001”; //Byte of array representing the address. This is the address where we will send the data. This should be same on the receiving side.

void setup() {
Serial.begin(9600);
bmp.begin(0x76);

radio.begin(); //Starting the Wireless communication
radio.openWritingPipe(address); //Setting the address where we will send the data
radio.setPALevel(RF24_PA_MAX); //You can set it as minimum or maximum depending on the distance between the transmitter and receiver.
//myRadio.setChannel(115);
radio.stopListening(); //This sets the module as transmitter

}

void loop(){
//temperature = bmp.readTemperature();
data.readTemperature = bmp.readTemperature();

//Prints to serial port to see whats being output
Serial.println(“Temperature : “);
Serial.print(data.readTemperature);
Serial.println(””);

const char text = "Temperature : " ;
radio.write(&text, sizeof(text));
radio.write(&data, sizeof(MyData));

delay(1000);
}>

Receiver Code<#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

#include <Adafruit_BMP280.h>
Adafruit_BMP280 bmp;

float temperature;

struct MyData {
byte readTemperature;
//byte readPressure;
//byte readAltitude;
};

MyData data;

RF24 radio(7, 8); // CE, CSN
const byte address[6] = “00001”;

void setup() {
//pinMode(6, OUTPUT);
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(0, address); //Setting the address at which we will receive the data
radio.setPALevel(RF24_PA_MAX); //You can set this as minimum or maximum depending on the distance between the transmitter and receiver.
radio.startListening(); //This sets the module as receiver
}
void loop()
{
if (radio.available()) //Looking for the data.
{
char text[32] = “”;//Saving the incoming data

radio.read(&text, sizeof(text)); //Reading the data
radio.read(&data, sizeof(MyData));
Serial.println(“Receiving”);
Serial.println(text);
Serial.println(data.readTemperature);

delay(1000);
}
}>

It appears to be the data.readTemperature is the culprit as the serial monitor outputs 2 d.p but I have no idea how to improve/change this

any help is appreciated :slight_smile:

No worries, by changing the "byte data.readTemperature " to "float temperature" the code works with the desired resolution

If you put your data into a struct you can easily send any combination of data types. Here is example code that sends data in a struct. The data in the examples is int data but can be changed to float and work the same. These examples are modifications of the examples in Robin2’s simple rf24 tutorial.

Sender:

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


const byte CE_PIN = 9;
const byte CSN_PIN = 10;

const byte slaveAddress[5] = {'R', 'x', 'A', 'A', 'A'};


RF24 radio(CE_PIN, CSN_PIN); // Create a Radio

struct LdrValues  
{
  int ldr_1;  // change these to float to send float data
  int ldr_2; 
}ldrValues;

unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 1000; // send once per second

const byte LDR1 = A0;
const byte LDR2 = A1;

void setup()
{

   Serial.begin(9600);
   Serial.println("SimpleTx Starting");
   pinMode(LDR1, INPUT_PULLUP);
   pinMode(LDR2, INPUT_PULLUP);
   
   radio.begin();
   radio.setChannel(76);  //76 library default
   //RF24_PA_MIN, RF24_PA_LOW, RF24_PA_HIGH and RF24_PA_MAX
   radio.setPALevel(RF24_PA_HIGH);
   radio.setDataRate( RF24_250KBPS );
   radio.setRetries(3, 5); // delay, count
   radio.openWritingPipe(slaveAddress);
}

void loop()
{
   currentMillis = millis();
   if (currentMillis - prevMillis >= txIntervalMillis)
   {
      send();
      Serial.print("LDR 1 = ");
      Serial.print(ldrValues.ldr_1);
      Serial.print("    LDR 2 = ");
      Serial.println(ldrValues.ldr_2);
      prevMillis = millis();
   }
}

//====================

void send()
{
   ldrValues.ldr_1 = analogRead(LDR1);
   ldrValues.ldr_2 = analogRead(LDR2);
   radio.write( &ldrValues, sizeof(ldrValues) );
}

Receiver:

// SimpleRx - the slave or the receiver

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

const byte CE_PIN = 9;
const byte CSN_PIN = 10;

const byte thisSlaveAddress[5] = {'R', 'x', 'A', 'A', 'A'};

RF24 radio(CE_PIN, CSN_PIN);

struct LdrValues
{
   int ldr_1;  // change these to float to receive float data
   int ldr_2;
} ldrValues;

bool newData = false;

//===========

void setup()
{

   Serial.begin(9600);

   Serial.println("SimpleRx Starting");

   radio.begin();
   radio.setChannel(76);  //76 library default
   //RF24_PA_MIN, RF24_PA_LOW, RF24_PA_HIGH and RF24_PA_MAX
   radio.setPALevel(RF24_PA_HIGH);
   radio.setDataRate( RF24_250KBPS );
   radio.openReadingPipe(1, thisSlaveAddress);
   radio.startListening();
}

//=============

void loop()
{
   getData();
   showData();
}

//==============

void getData()
{
   if ( radio.available() )
   {
      radio.read( &ldrValues, sizeof(ldrValues) );
      newData = true;
   }
}

void showData()
{
   if (newData == true)
   {
      Serial.print("Data received >> ");
      Serial.print("LDR 1 = ");
      Serial.print(ldrValues.ldr_1);
      Serial.print("    LDR 2 = ");
      Serial.println(ldrValues.ldr_2);
      newData = false;
   }
}

Read the how get the most out of this forum sticky to see how to properly post code. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code in code tags.

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