NRF24l01 receiving problems

Hi. I am having problems with transmitting numbers between nrf24l01's. The labels like "Altitude = " transmit perfectly but the number do not and I am not sure why.

Transmitter code:

#include <Wire.h>
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>

#define BME_SCK 8
#define BME_MISO 7
#define BME_MOSI 6
#define BME_CS 5

#define SEALEVELPRESSURE_HPA (1013.25)

Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK);

RF24 radio(10, 9); // CE, CSN
const byte address[6] = "00001";
void setup() {
  Serial.begin(9600);
  radio.begin();
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_MIN);
  radio.stopListening();

  bme.begin();
  
}
void loop() {
  const char tempText[] = "Temperature = ";
  radio.write(&tempText, sizeof(tempText));
  delay(5);
  const char temp[] = { bme.readTemperature() };
  radio.write(&temp, sizeof(temp));

  delay(100);

  const char pressureText[] = "Pressure = ";
  radio.write(&pressureText, sizeof(pressureText));
  delay(5);
  const char pressure[] = { bme.readPressure() / 100.0F };
  radio.write(&pressure, sizeof(pressure));

  delay(100);

  const char altText[] = "Altitude = ";
  radio.write(&altText, sizeof(altText));
  delay(5);
  const char alt[] = { bme.readAltitude(SEALEVELPRESSURE_HPA) };
  radio.write(&alt, sizeof(alt));

  delay(100);

  const char humidityText[] = "Humidity = ";
  radio.write(&humidityText, sizeof(humidityText));
  delay(5);
  const char humidity[] = { bme.readHumidity() };
  radio.write(&humidity, sizeof(humidity));
  
  delay(10);

  Serial.print("Temperature = ");
  Serial.print(bme.readTemperature());
  Serial.println(" °C");

  Serial.print("Pressure = ");

  Serial.print(bme.readPressure() / 100.0F);
  Serial.println(" hPa");

  Serial.print("Approx. Altitude = ");
  Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
  Serial.println(" m");

  Serial.print("Humidity = ");
  Serial.print(bme.readHumidity());
  Serial.println(" %");

  Serial.println();
}

Reciever Code:

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8); // CE, CSN
const byte address[6] = "00001";
void setup() {
  Serial.begin(9600);
  radio.begin();
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_MIN);
  radio.startListening();
}
void loop() {
  if (radio.available()) {
    char text[32] = "";
    radio.read(&text, sizeof(text));
    Serial.println(text);
  }
}

Reciever Output:

10:32:26.943 -> Temperature =
10:32:27.012 -> \
10:32:27.152 -> Pressure =
10:32:27.221 -> ⸮
10:32:27.359 -> Approx. Altitude =
10:32:27.394 -> ;
10:32:27.567 -> Humidity =
10:32:27.601 -> 9

Please edit your post, select all code for transmitter and click the </> button.

Repeat for the receiver code.

Next save your post.

That will prevent the forum software from e.g. translating [] to [].

done

1 Like

These doesn't look right. What is the datatype output of bme.readTemperature?

a float

You can't directly "convert" it to a char array then. I normally would use sprintf for this, but I think the Arduino IDE doesn't support it directly.

Look for float to char array conversion in Arduino...

Don't write all the stuff in single packets,
create a struct (without the text labels) and send all data in one packet.

Maybe something like

struct Data {
float temperature;
float pressure;
float altitude;
float humidity;
} allData;

 radio.write(&allData, sizeof(allData));

That way, you don't have to figure out which packet contains what data.

And please don't use delay(), there is no room for that crap in a communication sketch.

Thanks for the suggestion. This is what I have in my loop now:

void loop() {

  struct allData {
    float temp = bme.readTemperature();
    float pres = { bme.readPressure() / 100.0F };
    float alt = { bme.readAltitude(SEALEVELPRESSURE_HPA) };
    float humid = { bme.readHumidity() };
  }

  radio.write(&allData, sizeof(allData));
}

It now comes with the error: "expected initializer before '.' token".

Does anyone know what this means?

Your understanding of a struct, its definition and usage are flawed.

void loop() {

  struct Data {
    float temp = bme.readTemperature();
    float pres = { bme.readPressure() / 100.0F };
    float alt = { bme.readAltitude(SEALEVELPRESSURE_HPA) };
    float humid = { bme.readHumidity() };
  } allData;

  radio.write(&allData, sizeof(allData));
}

I would put the definition of the structure into a .h file, to be used in both sketches.

Thanks. This code now runs without error on the transmitter without any problem. Any ideas what the receiver code would be with this new setup?

Maybe something like

struct Data {
  float temperature;
  float pressure;
  float altitude;
  float humidity;
} allData;

 radio.read(&allData, sizeof(allData));

Hi, I've put that into my reciever code. Here is that code:

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8); // CE, CSN
const byte address[6] = "00001";
void setup() {
  Serial.begin(9600);
  radio.begin();
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_MIN);
  radio.startListening();
}
void loop() {
  if (radio.available()) {
    struct Data {
    float temperature;
    float pressure;
    float altitude;
    float humidity;
    } allData;

    radio.read(&allData, sizeof(allData));
    Serial.write((byte*)&allData, sizeof(allData));
  }
}

And here is some of the output from it:

Floats are ugly when they are dumped as ASCII.

Serial.write((byte*)&allData, sizeof(allData));

try

Serial.print(F("temperature "));
Serial.print(allData.temperature);
Serial.print(F(", pressure "));
Serial.print(allData.pressure);
Serial.print(F(",  altitude "));
Serial.print(allData.altitude);
Serial.print(F(", humidity "));
Serial.println(allData.humidity);

You could also add a display function to the struct, which should be defined in a .h file,
or at the top of the sketch with the declarations.

    struct Data {
      float temperature;
      float pressure;
      float altitude;
      float humidity;
      void display() {
        Serial.print(F("temperature "));
        Serial.print(temperature);
        Serial.print(F(", pressure "));
        Serial.print(pressure);
        Serial.print(F(",  altitude "));
        Serial.print(altitude);
        Serial.print(F(", humidity "));
        Serial.println(humidity);
      }        
    };

  Data  allData;

  allData.display();

Thanks, I'm now getting proper data over the module. I am now dealing with unrelated issues but at least this part is working. Thanks for your help.

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