5

I'm building a weather station with two UNOs, using NRF24l01+ radios. Communications are fine. I'm sending a struct from one to the other. The struct has three elements:

    struct weather {
      float tempData;
      float humData;
      float pressData;
    };
    weather wData = {0, 0, 0};

I then populate the struct with values from my DHT22 Temperature Sensor and my BMP085 pressure sensor.

    float c = dht.readTemperature();
    wData.tempData = (c * 9/5) + 32;
    wData.humData = dht.readHumidity();

    sensors_event_t event;
    bmp.getEvent(&event);
    wData.pressData = (0.0295 * event.pressure);

Now I send it via the NRF24L01+.

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

On the receiving Uno I have this.

    struct weather {
      float tempData;
      float humData;
      float pressData;
    };

    weather wData = {0, 0, 0};

    radio.read( &wData, sizeof(wData) );
    Serial.println(wData.tempData);
    Serial.println(wData.humData);
    Serial.println(wData.pressData);

I get results similar to this:

    Temp = 75.43
    Hum = 35.76
    Press = 0.00

The first two are correct. The final one is not. I can change the order and the first two will always be correct, but the last element is always 0.00. For example:

   Hum = 35.76
   Press = 30.14
   Temp = 0.00

I know I'm missing something here with my code but I can't find it. Anyone have some suggestions?

brorobw
  • 71
  • 2
  • `read()` and `write()` return a `bool`, did you try to get it and print it to `Serial`, just to check both functions consider everything's OK? That might help. – jfpoilpret Feb 25 '14 at 05:59
  • Also, did you check that the `weather` on the emiiter contained a non-0 pressure before `write()`? – jfpoilpret Feb 25 '14 at 06:05
  • The line with `radio.write(...` is missing a closing parenthesis. – jfpoilpret Feb 25 '14 at 06:25
  • `read()` and `write()` return 1. Missed the closing parenthesis while moving code here but it's there in my code. – brorobw Feb 25 '14 at 14:57
  • 1
    After taking a look at the RF24 library source code, I wonder if you have changed the payload size before read or write. To know about it, you should `Serial.println()` of `radio.getPayloadSize()` in both sketches; normally that should be `32` by default, but I wonder about it... – jfpoilpret Feb 25 '14 at 19:50
  • With the `struct' populated the size is `12`. – brorobw Feb 26 '14 at 03:32
  • The documentation mentions ["Be sure to call openWritingPipe() first"](http://maniacbug.github.io/RF24/classRF24.html#a4cd4c198a47704db20b6b5cf0731cd58) before calling write(). – David Cary Feb 28 '14 at 07:04
  • @brorobw, @jfpoilpret was asking about the payload size set on your `radio` variable, because the behavior you're seeing is consistent with writes being truncated at 8 bytes. The `RD24` class truncates/pads all writes to a fixed size, so you should check what that fixed size is set to. – microtherion Mar 02 '14 at 19:31

1 Answers1

2

Modified my struct by changing the first two elements from float to int.

struct weather {
  int tempData;
  int humData;
  float pressData;
}
weather wData;

Everything transmits fine now. I don't really need the precision of a float for temperature and humidity. However, I would still like to find the problem. Could there be some problem with the size of three floats versus two ints and a float? Serial.print(sizeof(wData)) is 12 when all elements are floats and 8 when using two int and a float. My understanding is the NRF24L01+ has a transmit and receive buffer of 32 bytes.

brorobw
  • 71
  • 2