Author Topic: Code help with simple send receive and ACK  (Read 1741 times)

Hayato

  • NewMember
  • *
  • Posts: 2
Code help with simple send receive and ACK
« on: March 19, 2018, 11:16:24 PM »
Hello everyone,

I just started using Moteino recently. I have a Moteino and a Moteino MEGA, I want to set MEGA as a sender, Moteino as a receiver and use a push button on the sender to control the LED in the receiver.  But it always shows NO ACK received.

I really have no idea to fix that. This is my code:

Code: [Select]
#include <RFM69.h>
#include <SPI.h>   

#define NETWORKID     100
#define RECEIVER      1
#define SENDER        2



#define FREQUENCY     RF69_433MHZ
#define ENCRYPTKEY    "Hello"
#define SERIAL_BAUD   9600


//  indicater LED
#ifdef __AVR_ATmega1284P__
#define LED           15 // for MEGA
#define NODEID        SENDER
#else
#define LED           9 // for Moteino
#define NODEID        RECEIVER
#endif

//  PUSH BUTTON
# define BUTTON       3
#define BUTTON_INT    1 //user button on interrupt 1 (D3)


RFM69 radio;

void setup() {
  Serial.begin(SERIAL_BAUD);
  radio.initialize(FREQUENCY,NODEID,NETWORKID);
  // radio.encrypt(ENCRYPTKEY);
  char buff[50];
  sprintf(buff,"\nListening at %d Mhz 433");
  Serial.flush();

  //set up BUTTON pin and indicater LED pin
  pinMode(BUTTON,INPUT_PULLUP);
  pinMode(LED,OUTPUT);
  digitalWrite(LED,LOW);
}
boolean buttonPressed = false;
void loop() {
  // Sending
 
   if (digitalRead(BUTTON))
    {
      buttonPressed=true;
    }
 
  if (buttonPressed)
  {
    Serial.println("Button pressed!");
    buttonPressed=false;
   
   
    if (radio.sendWithRetry(RECEIVER, "Hi", 2))
    Serial.println("ACK received!");
    else
    Serial.println("no ACK received");
    delay(1000);
  }

  //Receiving
  if (radio.receiveDone())
  {
    Serial.print((char*)radio.DATA);
    if (radio.DATALEN==2 && radio.DATA[0]=='H' && radio.DATA[1]=='i')
    {
       digitalWrite(LED, HIGH);
       delay(1000);
       digitalWrite(LED, LOW);
    }
     if (radio.ACKRequested())
    {
      radio.sendACK();
      Serial.print(" - ACK sent");
    }
  }
}
« Last Edit: March 23, 2018, 05:52:01 PM by Felix »

TomWS

  • Hero Member
  • *****
  • Posts: 1930
Re: Please help me,I Really do not know how to fix that...
« Reply #1 on: March 20, 2018, 08:53:16 AM »
Sigh...

Do you know how long your sendWithRetry() waits to receive the ACK?   
Answer:  The default time is 40mS.  However it does valiantly retry 3 times for a total of 120mS.

Do you know how long it is taking your receiver to return the ACK?
Answer:  The time it takes to print the one and only message PLUS 1000mS Delay BEFORE it ACKs.

Guess what?  The sender has long since given up waiting for an ACK...


Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6866
  • Country: us
    • LowPowerLab
Re: Please help me,I Really do not know how to fix that...
« Reply #2 on: March 20, 2018, 09:21:21 AM »
Jianwen,
This looks like the TxRxBlinky example. However you added delays which as TomWS noted - will cripple everything.
Please spend a little time to look at the code and try to understand what it does. Then load it to your Motes. This is the same sketch for both the sender and receiver. Set the parameters of the RFM radio to match your hardware (frequency, HCW or not, different node IDs, same network ID).

The examples in the library are guaranteed to WORK.
If you have no experience with these modules, always start from a working example then make your changes gradually. If the example does not work, then you are doing something wrong, either some code setting, or hardware wiring, so circle back and verify everything is OK before trying again. Once the example works, then you are ready for changes.
Good luck.