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);
}
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.
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 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.