Understanding Stream* and float temp(NAN)

I'm using a library to read my Bosch BME280 sensor.

Part of the code uses code (see below) I've not been able to find or understand. In another part I don't understand the benefit of the Stream* approach.

  1. float temp(NAN) I understand declaring a float variable. And I find NAN is "not a number" but I could not find what is meant by the standard brackets i.e. () nor what the return might be if its not a number (ASCII code?)

  2. I don't understand the benefit of using "Stream* client" vs the standard print statements.

In addition to just trying to understand the code, I want to do other things with the data from "bme.read(pres, temp, hum, tempUnit, presUnit);" partly for a local display and partly to process it with other code (yet to be written).

Any help would be appreciated :confused:

void printBME280Data
(
   Stream* client
)
{
   float temp(NAN), hum(NAN), pres(NAN);

   BME280::TempUnit tempUnit(BME280::TempUnit_Fahrenheit);
   BME280::PresUnit presUnit(BME280::PresUnit_hPa);

   bme.read(pres, temp, hum, tempUnit, presUnit);

   client->print("Temp: ");
   client->print(temp);
   client->print("°"+ String(tempUnit == BME280::TempUnit_Celsius ? 'C' :'F'));
   client->print("\t\tHumidity: ");
   client->print(hum);
   client->print("% RH");
   client->print("\t\tPressure: ");
   client->print(pres);
   client->println(" Pa");

   delay(1000);
}

Any help would be appreciated

Hard to give any help if you don’t post ALL your code.

The notation:

float x(3.14159);

is an initialization equivalent to:

float x = 3.14159;

Google will find you abundant information about NaN.

The function you posted takes as its argument a pointer to a Stream class object (Stream *). This allows you to pass in a pointer to ANY object whose class inherits from Stream and the function will print the results there.

However, since only the .print() and .println() methods are used, it would have been better to have the function take a pointer to a Print class object (Print *) instead. That way it could also print to output-only objects like an LCD.

Thank you @gfvalvo

Google will find you abundant information about NaN.

I kind of see what a NaN is from mt other searches, however its not clear the benefit or meaning of initializing a float to NaN. Could it be so the returned float is NaN if the function does not redefine it?

John

Could it be so the returned float is NaN if the function does not redefine it?

Yes spot on.