Auto Ack completely fixed

Do check out the next post for a time comparison between Auto Ack mode and normal Tx – Rx switching mode.

In my previous post there were some serious issues about the Tx receiving Ack payload only twice before stopping altogether and the issue of ‘1’ being sent twice…. all that has been fixed and every thing is working perfect now

Ques: What does it actually mean to use Ack payloads rather than switching between Tx and Rx modes ??

Ans: The program dealing with Ack payloads will not use radio.startListening(); function to listen for payloads cause the Tx automatically switches to Rx to listen to Ack payloads. Similarly the Rx will not use radio.stopListening(); to write an Ack payload !. Everything is taken care by the way these transceivers are manufactured automatically.

I have written the code in such a way that the transceivers will stop functioning(basically the tx displays a msg ” status has become false so stop here….” and stops transmitting further data.) as soon as an Ack payload is not received.

To tackle the error i came across this page on the Arduino forum and after implementing it every thing was okay http://forum.arduino.cc/index.php?topic=272143.0

According to the forum the actual problem was that writeAckPayload can only buffer three packets, and if you do it four times in a row, the program will block. The only way to get packets out of the buffer is by successfully reading a packet from the transmitter (which causes one of the buffered ack packets to be used), or by flushing the transmit buffer manually. So ideally you should only do writeAckPayload once for each successful read.

As i have told you that Tx was receiving Ack twice before stopping (although what i have shown in the pictures is not that and you can see that the process does go on but the fact is that it was the best case scenario ….. a lot of problems still existed). i found out that there were some minute errors in my code and after writing the proper code things were just wonderful. In the first part of this video i show you how to write a code using Ack payloads and in the second part i compare the Ack payload mode with the normal switching between Rx and Tx mode.

So to achieve that we do this –


if ( radio.available() ) { // make sure the read will succeed
radio.writeAckPayload( 1, buf, len ); // prep the ack payload
radio.read( &got_time, sizeof(unsigned long) ); // also shunts out ack payload
}

The transmitter sends an integer message where consecutive transmissions have a difference of 3 and listens for an Ack payload from receiver node just after that and prints it on the screen.

The receiver node looks for packets and whenever it receives one it sends an Ack payload back to transmitter. Each consecutive Ack payload is  an integer with a difference of 2.

Thus the TX code is then –

#include<SPI.h>
#include<nRF24L01.h>
#include<RF24.h>
int msg[1] = {1};
int rec[1] = {5};
bool stat = true;
RF24 radio(9,10);
const uint64_t pipe[1] = {0xF0F0F0F0E1LL};
 
void setup()
{
  Serial.begin(57600);
  radio.begin();
  delay(100);
  radio.setAutoAck(true);
  radio.enableAckPayload();
  radio.enableDynamicPayloads();
  radio.stopListening();
  radio.openWritingPipe(pipe[0]);
  radio.setRetries(15,15);
 }
void loop()
{
if(stat)
{
    if(radio.write(msg,sizeof(msg)))
    {
      Serial.print( msg[0] );
      Serial.println("...tx success");
      if(radio.isAckPayloadAvailable())
      {
        radio.read(rec,sizeof(int));
        Serial.print("received ack payload is : ");
        Serial.println(rec[0]);
      }
      else
      {
        stat = false; //doing this completely shuts down the transmitter if an ack payload is not received !!
        Serial.println("status has become false so stop here....");
      }
      msg[0]+=3;;
    if(msg[0]>=100)
    {msg[0]=1;}
    }
 }
}

And the RX code becomes –

#include<SPI.h>
#include<nRF24L01.h>
#include<RF24.h>
const uint64_t pipe[1]= {0xF0F0F0F0E1LL};
RF24 radio(9,10);
int rec[1] = {2};
int red;
void setup()
{
  Serial.begin(57600);
  radio.begin();
  delay(100);
  radio.setAutoAck(true);
  radio.enableAckPayload();
  radio.enableDynamicPayloads();
  radio.openReadingPipe(1,pipe[0]);
  radio.startListening();
  radio.setRetries(15,15);
}
void loop()
{
  if ( radio.available() ) {
    radio.writeAckPayload( 1, rec, sizeof(int) );
    radio.read( &red,sizeof(red) );rec[0]+=2;
    Serial.print("integer got is : ");
    Serial.println(red);
}
}

And here are the results –

ack 1ack 2ack 3ack 4

 

It is also really stable…meaning if u reset the TX node while the RX node is running then you will get a different Ack payload when your transmission is 1 and so on without ever faltering.

ack 520141130T114314

Advertisements

About Shantam Raj

Currently I am a final year B.Tech undergraduate majoring in “Electronics and Communication Engineering” from IIT Guwahati. I am passionate about electronics and robotics. Apart from that I love writing, visit https://medium.com/@shantam_raj for some of my works. I am a die hard soccer fan. I also love to play badminton. I am a hardware hacking enthusiast and a tinkerer !!.
This entry was posted in Arduino, nRF24L01+, Wireless, wireless communication and tagged , , , , , , , . Bookmark the permalink.

11 Responses to Auto Ack completely fixed

  1. Rahul says:

    Hello Shantam,
    Nice blog, i am facing similar issue. While searching solution I came across your blog. Will try this today.
    Meanwhile I wanted to check which version of RF24 library are you using? is it maniacbug.github.io/RF24/ or https://github.com/gcopeland/RF24

    Appreciate any help from your end.

    • Shantam Raj says:

      thanks a lot !!
      i had started working on the transceivers quite early so i m using maniacbug’s library as i have mentioned in my first post . i m not sure though but i believe that both of them are same, even if they are not then thats not a big issue because almost all the bugs that were pointed in maniacbug’s library have been corrected and according to me its quite responsive.

    • Shantam Raj says:

      happy to help anytime !!

  2. Kien says:

    Thank you so much!

  3. Hey Shantam ,
    I am really impressed by your work . I have followed your blog thoroughly for nrf24l01 communication . Currently , I am facing problem with communication between 6 transmitters to 1 receiver type . I need Enhanced shockburst for reliable connection between the 6 PTX to 1 PRX .
    I am only able to make it to 1 PTX to 1PRX as explained in your blog . Can you give some inputs on how to create a reliable network for 6 PTX to 1 PRX connection with multiple pipes

    • Shantam Raj says:

      Hi Shubh, thanks!.
      First of all there is a networking library available (it wasn’t when i first wrote the blog) that allows you to communicate with a lot more devices by the same person. You should check that.
      Link to library https://github.com/maniacbug/RF24Network
      Details of the library http://maniacbug.github.io/RF24Network/
      But if you have read the datasheet then 6 to 1 communication is actually hardware enabled i.e using the old library you can achieve it. I have the code where i tried with 2 to 1 successfully. I will search for it and post it here itself later. Keep checking ur mail for that.

      • Greetings once again shantam,
        Well, I have tried a lot using RF24 network earlier before you posted this reply, it doesnt seem to work for me. You just said that you tried with 2 on 1, can you please find it ?

      • Shantam Raj says:

        Please check my github repo, the code for that has been available since a long time in a separate project itself. You will know automatically know which repo to clone once u visit my repo.

  4. Shivangi Shukla says:

    Hi,
    I am working on a project which uses two nrf24l01 modules. The sender sends the signal, the receiver receives it and sends an acknowledge that it has received it. I tried to make it with the code on your blog. But it does not respond well. Can you please help me with this?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s