Getting started - wireless packets & ACKs

Started by RaduV, March 17, 2015, 06:48:29 AM

RaduV

Hi,
I'm trying to establish a communication between a server and a client and I think i'm missing something.
The Server  is working ok. ( tested with the Send struc example, I was receiving packages )
The Client however is not. Here i'm trying to identify what I'm doing wrong.

Server
void setup() {
  // init radio.
  radio.initialize( WIFI_FREQUENCY, WIFI_NODEID_SERVER, WIFI_NETWORKID );
  radio.setHighPower();
  radio.encrypt( WIFI_ENC_KEY );
}
 
void loop(){
   wifiReceiveData();
}

void wifiReceiveData(){
  Serial.println("[DEBUG] - Waiting for message...");
  Blink(LED,3);
  if( radio.receiveDone() ){
    Serial.println("[DEBUG] - Receive Message from: ");Serial.print(radio.SENDERID, DEC);
  } else {
      //Serial.println("[DEBUG] - No message received...");
  }
}


Client
void setup() { 
  //radio init
 radio.initialize( WIFI_FREQUENCY, WIFI_NODEID_CLIENT, WIFI_NETWORKID );
  radio.setHighPower();
  radio.encrypt( WIFI_ENC_KEY );
} 

void loop(){
  // testing
  Serial.println ("[DEBUG] - Sending data");
  wifiSendData();
  delay(300);
}

int wifiSendData() {
  int ret = ERROR_NOK;
  ret = radio.sendWithRetry( WIFI_GATEWAYID, (const void*)(&wifiPackage), sizeof( wifiPackage ));
  if( ret ) {
    Serial.println( "[DEBUG] - Sent package ("); 
    Serial.print (sizeof( wifiPackage ));
    Serial.print (" bytes )\n");
  } else{
    Serial.print( "[DEBUG] - Unable to send package (");
    Serial.print (sizeof( wifiPackage ));
    Serial.print (" bytes )\n");
  }
  Blink(LED,3);
  return ret;
}

I'm always getting "[DEBUG] - Unable to send package" message.

Any ideas?

Felix

Hi Radu,
Please start with the Node and Gateway examples and ensure those work, then build around those.
Also include your complete code, we need to see how you declared your variables (edit in original post).
And note this is really not wifi ;)

TomWS

Quote from: RaduV on March 17, 2015, 06:48:29 AM
Hi,
I'm trying to establish a communication between a server and a client and I think i'm missing something.
The Server  is working ok. ( tested with the Send struc example, I was receiving packages )
The Client however is not. Here i'm trying to identify what I'm doing wrong.

Server
void setup() {
  // init radio.
  radio.initialize( WIFI_FREQUENCY, WIFI_NODEID_SERVER, WIFI_NETWORKID );
  radio.setHighPower();
  radio.encrypt( WIFI_ENC_KEY );
}
 
void loop(){
   wifiReceiveData();
}

void wifiReceiveData(){
  Serial.println("[DEBUG] - Waiting for message...");
  Blink(LED,3);
  if( radio.receiveDone() ){
    Serial.println("[DEBUG] - Receive Message from: ");Serial.print(radio.SENDERID, DEC);
  } else {
      //Serial.println("[DEBUG] - No message received...");
  }
}


Client
void setup() { 
  //radio init
 radio.initialize( WIFI_FREQUENCY, WIFI_NODEID_CLIENT, WIFI_NETWORKID );
  radio.setHighPower();
  radio.encrypt( WIFI_ENC_KEY );
} 

void loop(){
  // testing
  Serial.println ("[DEBUG] - Sending data");
  wifiSendData();
  delay(300);
}

int wifiSendData() {
  int ret = ERROR_NOK;
  ret = radio.sendWithRetry( WIFI_GATEWAYID, (const void*)(&wifiPackage), sizeof( wifiPackage ));
  if( ret ) {
    Serial.println( "[DEBUG] - Sent package ("); 
    Serial.print (sizeof( wifiPackage ));
    Serial.print (" bytes )\n");
  } else{
    Serial.print( "[DEBUG] - Unable to send package (");
    Serial.print (sizeof( wifiPackage ));
    Serial.print (" bytes )\n");
  }
  Blink(LED,3);
  return ret;
}

I'm always getting "[DEBUG] - Unable to send package" message.

Any ideas?
RaduV, the method sendWithRetry() MUST receive an ACK from the far end in order to know that it sent successfully.  Your Server code never returns an Ack, therefore it always 'fails'.

As Felix pointed out, see how Gateway returns its Ack after receiving a packet.

Tom

RaduV

Quote from: TomWS on March 17, 2015, 08:21:58 AM
RaduV, the method sendWithRetry() MUST receive an ACK from the far end in order to know that it sent successfully.  Your Server code never returns an Ack, therefore it always 'fails'.
As Felix pointed out, see how Gateway returns its Ack after receiving a packet.
Tom

Ok, now is see the problem.
I've overlooked the ACK part.
Thanks for pointing this out. I'll try this when i get home, and come back with the results.

@Felix: I know it's not really WiFi, but it's only the vars name :).


Felix

The first time I looked at the code I thought you're doing some wifi stuff. It's good to name vars relevant to their function and what they are. Or you'll get confused yourself a year or two when you look at your own code, happened to me.

bborncr

My nodes that go to sleep do send their packets successfully (data is being logged) but always report "Nothing..." which I believe means that they aren't receiving the ACK from the Gateway. Do I need to put a delay after sending to receive the ACK? Or increase ACK_TIME?

RaduV

I've added ack on the server side.
Now on every message received, i check if ack is requested and i send it.
This seems to do the trick.

@Felix, @TomWS
Thanks for the hint.

Quote from: bborncr on March 17, 2015, 01:42:24 PM
My nodes that go to sleep do send their packets successfully (data is being logged) but always report "Nothing..." which I believe means that they aren't receiving the ACK from the Gateway. Do I need to put a delay after sending to receive the ACK? Or increase ACK_TIME?

Adding a delay is not needed as the ack is checked inside the sendWithRetry function.
It seems that the server is not responding fast enough.

bborncr

Adding the 2, 100 to the SendWithRetry() function fixes the problem by manually upping the time to wait for an ACK:  (radio.sendWithRetry(GATEWAYID, (const void*)(&theData), sizeof(theData)),2,100)

It is strange, however, that the ACK_TIME variable defined at the start of the code doesn't have any effect.

Felix

Quote from: bborncr on March 17, 2015, 03:01:35 PM
It is strange, however, that the ACK_TIME variable defined at the start of the code doesn't have any effect.
They were left overs from older code, now removed.

TomWS

Quote from: bborncr on March 17, 2015, 03:01:35 PM
Adding the 2, 100 to the SendWithRetry() function fixes the problem by manually upping the time to wait for an ACK:  (radio.sendWithRetry(GATEWAYID, (const void*)(&theData), sizeof(theData)),2,100)

It is strange, however, that the ACK_TIME variable defined at the start of the code doesn't have any effect.
What are you doing in the gateway code after you call receiveDone()?  Many times people have debug code (eg printing packets received, etc) BEFORE they Ack. This kills the responsiveness of the system and keeps the node awake longer (waiting for the Ack).  Note, however, if you do want to Ack right away, be sure to save the receive data (plus senderid, etc) BEFORE sending the Ack.  DAMHIKT!   ::)

Tom