Author Topic: sending after receiving not working, but getting ACK  (Read 866 times)

jimanjim

  • NewMember
  • *
  • Posts: 1
sending after receiving not working, but getting ACK
« on: February 18, 2018, 10:33:41 AM »
Hello,
currently im wokring on a project where i want to have a sort of an rc rover, which is controlled by a radio, and sends back some data like temperature etc.
Now im just trying to send something from groundstation to the rover and the rover as an answer send a message back. But i can send a message from groundstation, even recieve an ACK from the rover, but nothing is sent back from the rover. I even tried to switch which hardware is doing what and the result is identical.
Heres the code for ground station:
Code: [Select]
#include <RFM69.h>
/*
#include <Wire.h>
#include <SPI.h>
*/

char payload[50];
RFM69 radio;
bool promiscuousMode = false; //set to 'true' to sniff all packets on the same network

//Radio Parameters
#define NODEID        1    //unique for each node on same network
#define NETWORKID     100  //the same on all nodes that talk to each other
#define GATEWAYID     2    //Receiving node
#define ENCRYPTKEY    "1234567887654321" //exactly the same 16 characters/bytes on all nodes!


void setup() {
  //Serial.begin(9600);
  delay(1000);
  radio.initialize(FREQUENCY,NODEID,NETWORKID);
  radio.setHighPower(); //To use the high power capabilities of the RFM69HW
  //radio.encrypt(ENCRYPTKEY);
  //radio.setFrequency(433900000);
}

unsigned int packetCount = 0;
char testMsg[50] = "data";
unsigned long curT = millis();
unsigned long prevT = 0;
char inMsg[65];


void loop()
{
  curT = millis();
  if(curT - prevT > 2000)
  {
    prevT = curT;
    radio.sendWithRetry(GATEWAYID, testMsg, 50, 2, 200);
    /*Serial.println(radio.sendWithRetry(GATEWAYID, testMsg, 50, 2, 200) ? "true" : "false");
    Serial.print("[");
    Serial.print(curT);
    Serial.print("]sending: ");
    Serial.println(testMsg);*/
  }
 
  if (radio.receiveDone())
  {
    //Serial.print("response recieved: ");
    if (radio.ACKRequested())
    {
      radio.sendACK();
      //Serial.print(" - ACK sent.");
    }
    //Serial.println((char*)radio.DATA);
    //Serial.println();
  }
  delay(500);
}

and here is the rover
Code: [Select]
#include <RFM69.h>
/*
#include <Wire.h>
#include <SPI.h>
*/

char payload[50];
RFM69 radio;
bool promiscuousMode = false; //set to 'true' to sniff all packets on the same network

//Radio Parameters
#define NODEID        2    //unique for each node on same network
#define NETWORKID     100  //the same on all nodes that talk to each other
#define GATEWAYID     1    //Receiving node
#define ENCRYPTKEY    "1234567887654321" //exactly the same 16 characters/bytes on all nodes!


void setup()
{
  //Serial.begin(9600);
  delay(1000);
  radio.initialize(FREQUENCY,NODEID,NETWORKID);
  radio.setHighPower(); //To use the high power capabilities of the RFM69HW
  //radio.encrypt(ENCRYPTKEY);
  //radio.setFrequency(433900000);

}

unsigned int packetCount = 0;
char inMsg[50];
char msg[65] = "response";

void loop()
{
  if (radio.receiveDone())
  {
    //Serial.print("recieved...\t");
    if (radio.ACKRequested())
    {
      radio.sendACK();
      //Serial.print(" - ACK sent.");
    }
    //Serial.println((char*)radio.DATA);
 /*   for (byte i = 0; i < radio.DATALEN; i++)
      inMsg[i] = (char)radio.DATA[i];
    sprintf(msg,"RES[%d]: %s", packetCount++, inMsg);*/

    //Serial.println(radio.canSend() ? "true" : "false");
    //Serial.println(" sending...\t");
    //Serial.println(radio.sendWithRetry(GATEWAYID, msg, 65, 2, 200) ? "true" : "false");
    radio.sendWithRetry(GATEWAYID, msg, 65, 2, 200);
    //Serial.println(msg);
   
  }
 
  delay(50);
}

when i tried writing out the radio.canSend(), it always said false, so i tried to look for examples from the library, and for me it looks theres no mistake. From the canSend() being false i tried to look if i had to change state to sending, but it is an internal function of the library, so im lost.

Also, this is my copletely first project on radio and im a highschool student, so i can be missing some basic stuff. Please, i would appreciate any help.

Thanks!

TomWS

  • Hero Member
  • *****
  • Posts: 1930
Re: sending after receiving not working, but getting ACK
« Reply #1 on: February 18, 2018, 05:09:22 PM »
Welcome to the forum!

Re your code, I suggest taking out the delay(500) at the end of the ground station loop.  Here is what you're doing:

You're sending a packet and getting the ack, but then immediately checking to see if you've received anything.  You probably haven't on the first time through simply because of timing, but then you spend 500mS doing nothing while your poor node is trying to send with retry...

There's no benefit to having that delay in the loop (we'll ignore power issues for the moment).  If your ground station can constantly check its receiver, that's an ideal situation.

Also, it's probably a good practice to explicitly declare that you have an RFM69HW in your radio constructor.  The default configuration is NO HW.  Now using setHighPower() will override that, but only after radio initialization.   Do you know if it matters during initialization???  Best to be sure and not trust that the author will make 'everything' right after initialization.

Tom