Author Topic: Gateway and Node Example - Newline Required in Setup!?!  (Read 576 times)

mmilazzo31

  • NewMember
  • *
  • Posts: 3
Gateway and Node Example - Newline Required in Setup!?!
« on: December 29, 2019, 07:14:56 PM »
EDIT: I think I figured this out!!!

It turns out my encrypt key wasn't exactly 16 characters, it was 14. When I changed it back to the original in the example: "sampleEncryptKey", everything started working again. I'm not sure how the magical newline fixes the encryption length being short. My suggestion would be to have a check in the library when setting the encryption key and raising an error if it's not of the proper length.

END EDIT

I was trying to distill the basic Gateway/Node example from LowPowerLab down to a more basic form to troubleshoot an issue I was having. What I found is perplexing. The line in setup which prepends a newline character seems to be required for the radio to transmit correctly. Does anyone know what's going on here?

When I send without the newline I get this as output:

Code: [Select]
Sending[0]:  nothing...
Sending[1]: 1 nothing...
Sending[2]: 12 nothing...
Sending[3]: 123 nothing..

With the newline I get the expected:

Code: [Select]
Sending[0]:  ok!
Sending[1]: 1 ok!
Sending[2]: 12 ok!
[1] ACK TEST   [RX_RSSI:-23] - ACK sent


Original Code:
https://github.com/LowPowerLab/RFM69/blob/master/Examples/Node/Node.ino

Distilled Code:
Code: [Select]
#include <RFM69.h>         //get it here: https://www.github.com/lowpowerlab/rfm69
#include <RFM69_ATC.h>     //get it here: https://www.github.com/lowpowerlab/rfm69

#define NODEID        2    // keep UNIQUE for each node on same network
#define NETWORKID     100  // keep IDENTICAL on all nodes that talk to each other
#define GATEWAYID     1    // "central" node
#define FREQUENCY     RF69_915MHZ
#define ENCRYPTKEY    "sampleEncryptKey" //exactly the same 16 characters/bytes on all nodes!
#define ENABLE_ATC    //comment out this line to disable AUTO TRANSMISSION CONTROL
#define ATC_RSSI      -80
#define SERIAL_BAUD   115200

int TRANSMITPERIOD = 200; //transmit a packet to gateway so often (in ms)
char payload[]     = "123 ABCDEFGHIJKLMNOPQRSTUVWXYZ";
byte sendSize      = 0;

RFM69_ATC radio;

void setup() {
  Serial.begin(SERIAL_BAUD);
  radio.initialize(FREQUENCY,NODEID,NETWORKID);
  radio.encrypt(ENCRYPTKEY);
  radio.enableAutoPower(ATC_RSSI);
  Serial.println("Without the leading newline of this print statement, this sketch will not work! Why?");
}

void Blink(byte PIN, int DELAY_MS)
{
  pinMode(PIN, OUTPUT);
  digitalWrite(PIN,HIGH);
  delay(DELAY_MS);
  digitalWrite(PIN,LOW);
}

long lastPeriod = 0;
unsigned long m;
void loop() {

  //check for any received packets
  if (radio.receiveDone())
  {
    Serial.print('[');Serial.print(radio.SENDERID, DEC);Serial.print("] ");
    for (byte i = 0; i < radio.DATALEN; i++)
      Serial.print((char)radio.DATA[i]);
    Serial.print("   [RX_RSSI:");Serial.print(radio.RSSI);Serial.print("]");

    if (radio.ACKRequested())
    {
      radio.sendACK();
      Serial.print(" - ACK sent");
    }
    Blink(LED_BUILTIN,3);
    Serial.println();
  }

  m = millis();
  int currPeriod = m/TRANSMITPERIOD;
  if (currPeriod != lastPeriod)
  {
    lastPeriod=currPeriod;

    Serial.print("Sending[");
    Serial.print(sendSize);
    Serial.print("]: ");
    for(byte i = 0; i < sendSize; i++)
      Serial.print((char)payload[i]);

    if (radio.sendWithRetry(GATEWAYID, payload, sendSize))
     Serial.print(" ok!");
    else
      Serial.print(" nothing...");
    sendSize = (sendSize + 1) % 31;
    Serial.println();
    Blink(LED_BUILTIN,3);
  }
}
« Last Edit: December 29, 2019, 07:34:28 PM by mmilazzo31 »