Trouble with NRF24 stoplistening()

I am trying to use an NRF24 to send temperature and humidity data to a raspberry pi. It transmits fine with dummy data, but as soon as I try to take an actual reading from the sensor, the program does not get past the command radio.stoplistening(). Here is the code

if (role == role_ping_out)
  {
  int p = dht.readHumidity();
  Serial.print(p);
  int t = 22; // DUMMY
  int h = 50;// DUMMY
  thString = String(t) + String(h);
  thString.toCharArray(thChar, 12);
    // First, stop listening so we can talk.
  //Serial.print("Stopping");
  radio.stopListening();
  //Serial.print("Stopped");

    // Take the time, and send it.  This will block until complete
    //delay(4000);
    radio.write(&thChar,sizeof(thChar));

with the first 2 lines commented out, it transmits the dummy data fine. otherwise, it doesn't get past radio.stoplistening(). any suggestions?

Post all the code.

/*
 Copyright (C) 2011 J. Coliz <maniacbug@ymail.com>

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 version 2 as published by the Free Software Foundation.
 */

/**
 * Example using Dynamic Payloads 
 *
 * This is an example of how to use payloads of a varying (dynamic) size. 
 */
#include <LiquidCrystal.h>
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#include <DHT.h>
//
// Hardware configuration
//

// Set up nRF24L01 radio on SPI bus plus pins 7 & 8
const int rs = A5, en = 9, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
RF24 radio(7,8);
#define DHTPIN 10
#define DHTTYPE DHT11
String temp = "Temp: ";
String hum = "Humidity: ";
DHT dht(DHTPIN, DHTTYPE);
// sets the role of this unit in hardware.  Connect to GND to be the 'pong' receiver
// Leave open to be the 'ping' transmitter
const int role_pin = 6;

//
// Topology
//

// Radio pipe addresses for the 2 nodes to communicate.
const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };

//
// Role management
//
// Set up role.  This sketch uses the same software for all the nodes
// in this system.  Doing so greatly simplifies testing.  The hardware itself specifies
// which node it is.
//
// This is done through the role_pin
//

// The various roles supported by this sketch
typedef enum { role_ping_out = 1, role_pong_back } role_e;

// The debug-friendly names of those roles
const char* role_friendly_name[] = { "invalid", "Ping out", "Pong back"};

// The role of the current running sketch
role_e role;

//
// Payload
//

const int min_payload_size = 4;
const int max_payload_size = 32;
const int payload_size_increments_by = 1;
int next_payload_size = min_payload_size;

char receive_payload[max_payload_size+1]; // +1 to allow room for a terminating NULL char

void setup(void)
{
  //
  // Role
  //
  dht.begin();
  lcd.begin(16, 2);
  lcd.clear();
//  lcd.print("Hello WOrld");
  delay(400);
  role = role_ping_out;

  //
  // Print preamble
  //

  Serial.begin(9600);
  
  Serial.println(F("RF24/examples/pingpair_dyn/"));
  Serial.print(F("ROLE: "));
  Serial.println(role_friendly_name[role]);
  //
  // Setup and configure rf radio
  //
  delay(400);
  radio.begin();

  // enable dynamic payloads
  radio.enableDynamicPayloads();

  // optionally, increase the delay between retries & # of retries
  radio.setRetries(5,15);

  //
  // Open pipes to other nodes for communication
  //

  // This simple sketch opens two pipes for these two nodes to communicate
  // back and forth.
  // Open 'our' pipe for writing
  // Open the 'other' pipe for reading, in position #1 (we can have up to 5 pipes open for reading)

  if ( role == role_ping_out )
  {
    radio.openWritingPipe(pipes[0]);
    radio.openReadingPipe(1,pipes[1]);
  }
  else
  {
    radio.openWritingPipe(pipes[1]);
    radio.openReadingPipe(1,pipes[0]);
  }

  //
  // Start listening
  //

  //radio.startListening();

  //
  // Dump the configuration of the rf unit for debugging
  //
  Serial.print("End setup");
  //radio.printDetails();
}
char thChar[32] = "";
String thString = "";
void loop(void)
{
  //
  // Ping out role.  Repeatedly send the current time
  //

  if (role == role_ping_out)
  {
  int t = dht.readTemperature();
  Serial.print(t);
  //int t = 22; // DUMMY
  int h = 50;// DUMMY
  thString = String(t) + String(h);
  thString.toCharArray(thChar, 12);
    // First, stop listening so we can talk.
  Serial.print("Stopping");
  radio.stopListening();
  Serial.print("Stopped");

    // Take the time, and send it.  This will block until complete
    //delay(4000);
  radio.write(&thChar,sizeof(thChar));

    // Now, continue listening
  radio.startListening();
    // Wait here until we get a response, or timeout
  unsigned long started_waiting_at = millis();
  bool timeout = false;
  while ( ! radio.available() && ! timeout )
    if (millis() - started_waiting_at > 500 )
      timeout = true;

    // Describe the results
    if ( timeout )
    {
      Serial.println(F("Failed, response timed out."));
    }
    else
    {
      // Grab the response, compare, and send to debugging spew
      uint8_t len = radio.getDynamicPayloadSize();
      
      // If a corrupt dynamic payload is received, it will be flushed
      if(!len){
        return; 
      }
      
      radio.read( receive_payload, len );

      // Put a zero at the end for easy printing
      receive_payload[len] = 0;

      // Spew it
      Serial.print(F("Got response size="));
      Serial.print(len);
      Serial.print(F(" value="));
      Serial.println(receive_payload);
      lcd.clear();
      lcd.print(len);
    }
    
    // Update size for next time.
    next_payload_size += payload_size_increments_by;
    if ( next_payload_size > max_payload_size )
      next_payload_size = min_payload_size;

    // Try again 1s later
    delay(100);
  }

  
}

Thanks.

Now, please explain what you mean by "with the first 2 lines commented out". I doubt it would be these two:

if (role == role_ping_out)
  {

For helpful answers, put yourself in the shoes of someone who has no idea what the real problem is, and explain the situation clearly.

This is explained in the important post linked at the top of every forum topic page and titled "How to use this forum".

Note: on an Uno or similar Arduino, use of Strings is pretty much guaranteed to cause memory problems and program crashes. Avoid them.

Apologies, I meant if you comment the next 2 lines ie . The read humidity line. And serial print line.

You gave no evidence for the statement "the program does not get past the command radio.stoplistening()", but for the moment, let's assume that is true.

That suggests that the radio is not working, so narrow down why not, step by step.

Have you been able to get radio communications working perfectly, sending sample text data, but not including the sensor code?

Can you separately read and successfully print the sensor data, without using Strings?

If yes to both, go back and carefully add the sensor code to the radio code and verify that the combination still works as expected, sending the test data only.

Modify to send the sensor data. Hint: you can use the itoa() function to convert integers to C-strings (zero terminated character arrays).

Radios work perfectly, as does the sensor, just not in conjunction. I will try again with the itoa(). My point was that if I put serial print commands either side of the stop listening, then nothing Is printed afterwards.

Could be memory problems, but you forgot to state which Arduino you are using.

For such a simple task, you unfortunately chose to start with one of the most complicated of the radio example programs.

Ok, I’m using an Uno, but it has occured to me that it is complex, I might just start from scratch if I cant get this bit working with itoa()

Ok, I'm just plain stuck now, I've tried new tutorials and the Pi just can't receive anything. At least before I could get something.

People will be happy to help with specific Arduino problems, if in turn you will read and follow the directions in the "How to use this forum" post.

But the problem could be with the RPi.

@Rev_02, you have all of your code in loop() which makes it very difficult to isolate a problem.

Put the code of reading the sensor into its own function and have it save the value to a global variable. Put the code for the wireless in its own function and get it to send whatever value is in that global variable. That way you can test the two parts quite separately. Your code in loop() could then be as simple as

void loop() {
   readSensors();
   operateWireless();
}

Have a look at Planning and Implementing a Program

Also have a look at the examples in this Simple nRF24L01+ Tutorial Just be aware that it uses the newer TMRh20 version of the RF24 library.

...R

Ok thanks, I will try using separate functions, and I am aware that the code is very messy. I'm having all sorts of trouble with the RPis now which is really hindering progress, I might have to get another Arduino Uno and use that as a receiver, and transfer data over serial to the RPi.