DHT11 over NRF24L01

Ok so I am just getting into the NRF24L01 module more, I've used it before to create a 2.4Ghz Frequency Analyzer to great success. Now I am using 2 to allow data transmission from one arduino to another. I have already utilized them successfully in conjunction with a PIR sensor to detect motion with one arduino and send the signal to another one that turns on an led.

I used pieces of that code to create a new code for the transmission of a DHT11 sensor I have that I want to but outside to my receiver inside. So far my receiver is getting the 2 text lines from the transmitter and printing them correctly, but the actual temperature and humidity line is not being sent or read properly as you will see. I am encloseing both codes and a picture of the read outs from the serial monitors.

The one is displaying the temperature and humidity from my DHT11 so I know it's working, the other is supposed to be the receiver.

TRANSMITTER

#include <dht.h>
dht DHT;
#define DHT11_pin 2

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(9, 10); 
const byte address[6] = "00001";

void setup(){
  Serial.begin(9600);
  radio.begin();                  
  radio.openWritingPipe(address); 
  radio.setPALevel(RF24_PA_MIN); 
  radio.stopListening();        
}

void loop(){
  int chk = DHT.read11(DHT11_pin);
  float temp = DHT.temperature;
  float hum = DHT.humidity;
  Serial.print("Temperature = ");
  Serial.println(DHT.temperature);
//  Serial.println(temp); //used to test value
  Serial.print("Humidity = ");
  Serial.println(DHT.humidity);
//  Serial.println(hum);//used to test value
  
    const char text1[] = "The outdoor Temperature is: ";
    radio.write(&text1, sizeof(text1));
    radio.write(&temp, sizeof(temp)); 
    const char text2[] = "The outdoor Humidity is: ";
    radio.write(&text2, sizeof(text2));
    radio.write(&hum, sizeof(hum));
  delay(5000);
}

RECEIVER

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(9, 10); // CE, CSN
const byte address[6] = "00001";
int temp;
int hum;

void setup() {
  Serial.begin(9600);
  radio.begin();
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_MIN);      
  radio.startListening();            
}

void loop()
{
  if (radio.available())             
  {
    char text1[32] = "";                 
    radio.read(&text1, sizeof(text1));  
    radio.read(&temp, sizeof(temp));   
    Serial.println(text1);
    Serial.println(temp);
    char text2[32] = "";
    radio.read(&text2, sizeof(text2));
    radio.read(&hum, sizeof(hum));
    Serial.println(text2);
    Serial.println(hum);
  }
}

float temp = DHT.temperature;
float hum = DHT.humidity;

Verses

int temp;
int hum;

float vs int :thinking:

If one packet is available, read four. Sound not so safe.

I floated the 2 in the transmitter so I get the decimal value.

And I just introduced the variables in the receiver.

I've tried both sets matching as float, int, and boolean with no luck same results just with or without the decimal.

I don't understand what you mean

I don't understand what can not be understood in

"if one packet is available, read four."

That's what your reception code does.

What happens if one packet gets lost?
Are you sure all packets make it?

Why don't you send temperature and humidity in their native format in one packet?

It is not guaranteed that packets make it,
nor is there a fixed duration for a send.

If collisions or dropouts occur,
packets may be repeated (up to 15 times automatically),
but to guarantee the delivery,
there has to be some entity that takes care of the 15-retry-and-failed packets.

Your code does not even care whether transmit failed.

Reading a packet from the packet buffer always works without any retries or delays,
so there is a very good chance that your reception code reads a phantom 4th packet,
while the real one is still on the way.
If that happens, it triggers the "if one packet.." sequence again reading three junk packets,
overwriting the part that was correct in the first round.

Any lost packet mixes the reception data up, interesting, but not so useful.

Put both values as floats in a struct, maybe add an id if you intend to sport more such nodes.
That is so much simpler and safer.

I mearly mimicked the code that worked from the PIR project, it was reading a total of 4 packets.

On what you said about sending it in the original packet before breaking the DHT11 data into the temp and humidity. That way I am only sending one packet...

int chk = DHT.read11(DHT11_pin)

To send one original packet I'd be using the 'chk' variable from that line in my code instead and then on the receiver take and break that into the temp and humidity to display.

Am I understanding that correctly?

So that code is probably unsafe too.

I don't think so.

chk is probably some return code, signaling that it succeeded or not,
but it is not my library, so I don't know.

int chk = DHT.read11(DHT11_pin)

All 'chk' is' is a created variable so that the DHT11 can be read. I could technically have a line of code following it like this...

Serial.println(chk);

And I would be able to read the raw data from the DHT11.

It's only after the sensor has been read that I can pull 'DHT.temperature' & 'DHT.humidity' out. At least that's how every project before has been. I've never tried forgoing the read line and seeing if I can pull the temp and humidity out.

So since 'chk' is the raw data as a whole I should be able to send it to the receiver... should be able to. I never thought of that. I'll try it and see what happens.

I don't think so.

Have fun with your project.

Ok, so I've tried using just the 'chk' packet and nothing. How in the world do I do this.

See #7

I tried both values as floats, I have even scoured through my books for something to try. I keep getting either no DHT data being written in the serial monitor on the receiving arduino or I get zeros where the temp should be.

Show the failed attempt that uses a struct with two floats, as suggested.

Why are you transmitting 50+ bytes of constant text when that text could be stored on the receiver and only changing, numerical data transmitted?

Because I don't know any other way.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.