Go Down

Topic: Trouble adapting sketch to send Temp. sensor data (DS18B20) using nRF24L01 (Read 1 time) previous topic - next topic

EricMitjans

Hi guys,

I've found this tutorial(1) that basically simulates sensor data (generating a random number) and sends it over from one arduino to another using a couple nRF24L01 chips.

I've adapted the sketch to read real data from my DS18B20 sensor and it works but its kinda buggy.
First of all, it will kill the 2 decimals that come from the sensor readings (would be handy to actually have the whole reading, e.g. 18.04 instead of 18), and on the other hand, the receiver will only receive one single reading once I open the serial monitor on the transmitter.

So basically I'll be opening the Receiver serial monitor and this will appear:

Code: [Select]
**************V1 Receive Sensor Data***********
...Listening


After I open the transmitter serial monitor, the transmitter serial monitor will show:

Code: [Select]
**************V1 Send Sensor Data***********
...Sending
0 data[1]: 0  |  data[2]: 25  | Temperature sent:  25
1 data[1]: 0  |  data[2]: 25  | Temperature sent:  25
2 data[1]: 0  |  data[2]: 25  | Temperature sent:  25
3 data[1]: 0  |  data[2]: 25  | Temperature sent:  25


But the receiver will only read the first packet, showing the following:

Code: [Select]
**************V1 Receive Sensor Data***********
...Listening
0 | Temperature Received: 25


And it will stay like this, while the transmitter keeps sending data every second.

I've been playing around with both the transmitter and receiver sketches but so far no luck. I might have missed something in the loop of the receiver but I can't see what...

Here's my transmitter code:

Code: [Select]
#include <RF24.h>
#include <RF24_config.h>
#include <SPI.h>
#include <OneWire.h>
#include <DallasTemperature.h>

OneWire oneWire(4);
DallasTemperature sensors(&oneWire);

// ce,csn pins
RF24 radio(8,7);

// init data buffer to hold a sensor type byte and one uint16_t sensor data
unsigned char data[3] = {
  0};
unsigned long count=0;
void setup(void)
{
  sensors.begin(); 
  Serial.begin(57600);
  Serial.println("**************V1 Send Sensor Data***********");
  radio.begin();
  radio.setPALevel(RF24_PA_LOW);
  radio.setChannel(0x4c);

  // open pipe for writing
  radio.openWritingPipe(0xF0F0F0F0E1LL);

  radio.enableDynamicPayloads();
  radio.setAutoAck(true);
  radio.powerUp();
  Serial.println("...Sending");
}
void loop(void)
{
  //read sensor data
 
  sensors.requestTemperatures();
  float currentTemp;
  currentTemp = sensors.getTempCByIndex(0);
 
  uint16_t sensorValue = currentTemp;

//assign 'T' to represent a Temperature reading
  data[0] = 'T';
  //do the bit shift to put uint16 into uint8 array
  data[1] = sensorValue >> 8;
  data[2] = sensorValue;
  Serial.print(count);
  count++;
  Serial.print(" data[1]: ");
  Serial.print(data[1]);
  Serial.print("  |  data[2]: ");
  Serial.print(data[2]);
  // print and increment the counter
  radio.write(data, sizeof(uint16_t)+1);
  Serial.print("  | Temperature sent:  ");
  Serial.println(sensorValue);
  // pause a second
  delay(1000);
}


And the receiver sketch:

Code: [Select]
#include <RF24.h>
#include <RF24_config.h>
#include <SPI.h>

// ce,csn pins
RF24 radio(8,7);
// set up the data buffer to receive the sensor type byte and the uint16_t sensorValue
unsigned char data[3] = {
  0};
  unsigned long count=0;
void setup(void)
{
  // init serial monitor and radio
  Serial.begin(57600);
  Serial.println("**************V1 Receive Sensor Data***********");
  radio.begin();

  radio.setPALevel(RF24_PA_LOW);
  radio.setChannel(0x4c);

  // open pipe for reading
  radio.openReadingPipe(1,0xF0F0F0F0E1LL);

  radio.enableDynamicPayloads();
  radio.setAutoAck(true);
  radio.powerUp();
  radio.startListening();
  Serial.println("...Listening");
}

void loop(void)
{
  // if there is data ready
  if (radio.available())
  {
    // dump the payloads until we've got everything
    bool done = false;
    while (!done)
    {
      // fetch the payload, and see if this was the last one
      done = radio.read(data, sizeof(uint16_t)+1);
       bool done = true;
    }

    // print the payload
    if (data[0] == 'T') {
      //put the uint16_t back together
      uint16_t sensorValue = data[2] | data[1] << 8;
      Serial.print(count);
      count++;
      Serial.print(" | Temperature Received: ");
      Serial.println(sensorValue);
     
    }
    else
      Serial.println("Did not receive a value temperature reading");
  }
 
}


I'd appreciate any comments on what could be wrong!

Thanks,
Eric


1: http://bitknitting.wordpress.com/2013/09/29/home-sensor-network-part-2-transmitting-data-from-one-nrf24l01-to-the-other/

PaulS

Code: [Select]
      done = radio.read(data, sizeof(uint16_t)+1);
       bool done = true;

Global and local variables of the same name are not the brightest idea. Local variables that immediately go out of scope look pretty dumb, too. What were you thinking?
The art of getting good answers lies in asking good questions.

EricMitjans

Hi Paul,

I didn't write the code, I just modified it. Care to elaborate on what do you mean and how could this cause the problem I mention?

Thanks!
E

PaulS

Quote
Care to elaborate on what do you mean

No. I expect YOU to go do some research on the terms global variables and local variables, and learn what the terms mean.

Then, I expect you to go do some research on the term scope, and learn the lifetime of variables.

Then, you'll see that:
Code: [Select]
       bool done = true;
is useless.

Quote
and how could this cause the problem I mention?

Did I say that those issues were the cause of the problem? I said that those issues should be addressed. Whether or not addressing them resolves the issue, or not, remains to be seen. But, I don't look at stupid code, searching for problems, especially when the problems involve hardware I don't have.

Fix the code, and I'll be more inclined to read more of it.

Quote
I didn't write the code

That stupidity is NOT in the code you started with.
The art of getting good answers lies in asking good questions.

jidole

I have been struggling with the same issue. Only the first message was displayed.
The solution I have found is to add a "radio.stopListening();" command in the Sender code.

The part of code then looks like that:
Code: [Select]
radio.stopListening();
 bool ok = radio.write(data, sizeof(float)+1);

  if (ok)
  {
     Serial.println("ok...");
    digitalWrite(7, HIGH);
  }
   else
   {  Serial.println("failed.\n\r");
     digitalWrite(7, LOW);
    }
radio.startListening();
 // pause a second
 delay(1000);


The same is mentioned in the RF24 example Example RF Radio Ping Pair:

Code: [Select]
    // First, stop listening so we can talk.
    radio.stopListening();

Go Up