0

I use 2 sensor modules (DHT11 and TSL2561) with an Arduino Nano and want to send the meassured values via nRF24L01 Module to another nRF24L01 Module. I checked the other Topics but nothing worked so far.

Thats the Transmitter Code:

void loop() {

  String stringAll, stringTemp1, stringTemp2, stringHum1, stringHum2, stringLux1, stringLux2;    

  byte temperature = 0;
  byte humidity = 0;
  byte data[40] = {0};
  if (dht11.read(pinDHT11, &temperature, &humidity, data)) {
    Serial.print("Read DHT11 failed");
    return;
  }

  stringAll += (int)temperature;
  stringAll +=  ", ";           
  stringAll += (int)humidity;
  stringAll += ", ";              

  sensors_event_t event;
  tsl.getEvent(&event);

  if (event.light) {
    stringAll += event.light;
    stringAll += " lux";
  }

  radio.write(&stringAll, sizeof(stringAll));
  // Serial.println(stringAll);

  delay(1100);                                          
}

Thats the format (example) of the string if I test with "Serial.println(stringAll)": 25, 36, 123.00 lux

Thats the Receiver Code:

void loop() {
  //Serial.println("omfg");
  if (radio.available()) {

    // char stringAll[1000] = "";
    String stringAll;

    len = radio.getDynamicPayloadSize();
    radio.read( &stringAll, len );
    Serial.println(stringAll);

  }
}

In this case nothing appears in the serial monitor of the Receiver. If I use " char stringAll[1000] = ""; " instead of " String stringAll; " just two squares appear in each line of the Serial monitor.

I have no clue how to solve this problem and would be grateful if someone could help me.

Tim
  • 4,790
  • 4
  • 33
  • 41
Dura
  • 51
  • 2
  • 10
  • A String object behaves different than what you think, I guess. It's useless to transmit its address somewhere. sizeof (String) has nothing to do with its dynamic text content. – datafiddler Apr 27 '19 at 16:41
  • I tried a similar code with a char array, which worked fine, but I can't store float values in a char array. So I thought sending a string might be the easiest choice. I'm not sure why the same think doens't work with a string. – Dura Apr 28 '19 at 07:50
  • To store float in a char array use `dtostrf()` (or allow %f for sprintf, which is diasabled by default for memory reasons on small avr 8bit controllers like atmega328 = uno or nano) – datafiddler Apr 28 '19 at 12:19

0 Answers0