Hi,
I use TMRh20 - Optimized Fork of NRF24L01+ Driver. I have a setup with couple of wireless temperature sensors. I am building a new set of sensors and I wanted to rewrite my code to have acknowledgement of the transmitted messages with a possibility of re-sending failed ones.
I decided to transmit the payload on the transceiver, then receive it on receiver + send back an ack with certain value - so I can check again on the transceiver, whether the value is correct and in case it is missing or is incorrect, then repeat the sending.
This is my main loop code on the receiver:
void loop(void)
{
if ( radio.available() ) //if there is a data available
{
radio.read( &payload, sizeof(payload) ); //read the data into a buffer
sndAckBuf.counter = payload.A; //prepare the 2 values which will be sent back as ack
sndAckBuf.id = payload.E;
radio.writeAckPayload(1,&sndAckBuf, sizeof(sndAckBuf) ); //send ack
dtostrf(payload.B,1,2,temp_buff);
dtostrf(payload.C,1,2,humid_buff);
dtostrf(payload.D,1,4,volt_buff);
sprintf(pageAdd,"/add.php?temp=%s&humid=%s&voltage=%s&counter=%d&auth=password&mode=dht&s=%c",temp_buff,humid_buff,volt_buff,payload.A,payload.E);
// Delay just a little bit to let the other unit
// make the transition to receiver
delay(20);
//
// Ethernet send part
//
sent = 0;
retries = 0;
do
{
sent = sendData(pageAdd); //sendData function returns true if sending via ethernet was successful
if ( retries++ > MAX_RETRIES )
sent = 1;
}
while ( sent == 0 );
//
// END - ethernet send part
//
}
Basically this does the job. But not well. In case there is an outage on the ethernet line (cable disconnected, etc...) for longer time, it looks there is a buffer filled with an ack payloads and then the transmitter keeps re-send 3 times until it has correct value in read ack payload. This I was able to solve by adding radio.stopListening(); before the eth send part and again start listening after the eth sending part is done. I am not sure whether this is OK, but it looks it worked (it is not in the code above).
But I would like to know how exactly the writeAckPayload() function should be used. I do not understand the note in the class definition:
Ack payloads are handled automatically by the radio chip when a payload is received. Users should generally write an ack payload as soon as startListening() is called, so one is available when a regular payload is received.
So is my design completely wrong? Should I prepare some data in the buffer for ack payload and once the incoming payload is available (transceiver sent data), nrf24l01 chip will reply the transceiver with the ack payload? If so, then there is no possibility for including node id and counter in the payload, so I can't verify on the transceiver side whether that particular payload reached the receiver properly or not (I would like to have this to prevent reading of the ack payload sent from other node).
Can anybody help me understanding this?