sparkfun / SparkFun_QwiicRF_Library

Arduino Library for the QwiicRF LoRa Radio Board
4 stars 2 forks source link

Strange behavior sending packets #3

Open dylandn opened 2 years ago

dylandn commented 2 years ago

I am getting strange behavior that is perplexing me. I have two Adafruit MacroPads, each with a Zio Qwiic Lora module. They are loaded with the following code, where I change the value for PAIR depending on which board I'm flashing:

#define PAIR 0 //0 or 1

#if PAIR
  #define ADDR 42
  #define OTHER 24
#else
  #define ADDR 24
  #define OTHER 42
#endif

unsigned char keys[13]={0}, prevKeys[13]={0};
char line[64];

#include <QwiicRF.h>
QwiicRF Radio;
byte i2c_addr = 0x35;
unsigned char SyncWord=42;

int i;
const char *msgs[] = {"null", "null", "null", "null", "Scenery", "null", "Mechanical Fault", "Hungry", "Stop", "Thirsty", "Turn Left", "GO", "Turn Right" };

inline void showStats() {
  Serial.print(F("Radio: "));
  Serial.print(F("I2C Address 0x"));
  Serial.print(i2c_addr, HEX);
  Serial.print(F(" | RF Address "));
  Serial.print(Radio.getRFAddress());
  Serial.print(F(" | Paired Address "));
  Serial.print(Radio.getPairedAddress());
  Serial.print(F(" | Sync Word "));
  Serial.println(Radio.getSyncWord());
}

inline void setup_serial() {
  Serial.begin(115200);
  delay(2500);

  Serial.println("Hello world");
}

inline void setup_keys() {
  // set all mechanical keys to inputs
  for (uint8_t i=0; i<=12; i++)
    pinMode(i, INPUT_PULLUP);

  pinMode(PIN_SWITCH, INPUT_PULLUP);
}

inline void setup_radio() {
  Wire.begin();

  Radio.begin(0x35);

  while(!Radio.isReady()) {}  //todo put a counter in here and see what's "normal"

  Radio.setRFAddress(ADDR);
  Radio.setPairedAddress(OTHER);
  Radio.setSyncWord(SyncWord);
  Radio.setReliableSendTimeout(3);

  while(!Radio.isReady()) {Serial.println("Waiting for radio...");} //todo put a counter in here and see what's "normal"

  showStats();
}

inline void sendRadio(String s) {
  Serial.print("Sending...");

  Radio.sendReliable(s);

  while (Radio.waitingForAck()) {
    Serial.print("Waiting... ");
    delay (25);
  }

  Serial.println("Sent!");
}

inline void checkRadio() {
  if(Radio.hasPayload()) {
    //we got data
    Serial.println("Received Packet!");
    sprintf(line, "OTHER: %i getPacketDestination: %i", OTHER, Radio.getPacketDestination());
    Serial.println(line);
    sprintf(line, "ADDR: %i getPacketOrigin: %i", ADDR, Radio.getPacketOrigin());
    Serial.println(line);

    if(Radio.getPacketDestination() == OTHER) {//why are we receiving msgs we sent?
      Serial.print("That's you, Coach: "); 
      Serial.println(Radio.read());//discard this message
    } else {

      Serial.print("ID: ");
      Serial.print(Radio.getPacketID());

      Serial.print(" SENT FROM: ");
      Serial.print(Radio.getPacketOrigin());

      Serial.print(" SENT TO: ");
      Serial.print(Radio.getPacketDestination());

      Serial.print(" RSSI: ");
      Serial.print(Radio.getPacketRSSI());

      Serial.print(" SNR: ");
      Serial.println(Radio.getPacketSNR());

      Serial.print("PAYLOAD: ");
      Serial.println(Radio.read());
    }
  }
  else {//we got no data, heartbeat?

  }
}

inline void copy(unsigned char* src, unsigned char* dst, int len) {
    for (i = 0; i < len; i++)
        *dst++ = *src++;
}

inline void scanKeys() {
  for(i=1; i<=12; i++)
    keys[i]=!digitalRead(i);
  keys[13]=digitalRead(PIN_SWITCH);

  for(i=1; i<=12; i++) {
    if(keys[i]  && !prevKeys[i]) {  //Key was pressed
      //Serial.print("Key press: "); Serial.println(i);
    }
    else if(!keys[i] && prevKeys[i]) { //key was released
      Serial.print("Key release: "); Serial.println(i);
      sendRadio(msgs[i]);
    }
    else if(keys[i] && prevKeys[i]) { //key still on
      //Serial.print("Key still on: "); Serial.println(i);
      }
    else if(!(keys[i] && prevKeys[i])) { //key still off
      //Serial.print("Key still off: "); Serial.println(i);
    }
    else {//whut?
      Serial.println("No man's land");
    }

    if(!keys[13]) //rotary pressed
      {Serial.println("Rotary Pressed");}
  }

  copy(keys, prevKeys, 13);

}

void setup() {

  setup_serial();

  setup_keys();

  setup_radio();
}

void loop() {
      scanKeys();
      checkRadio();
      delay (150);
}

Upon booting, each board prints to the serial port the expected output: MacroPad #0:

Hello world
Radio: I2C Address 0x35 | RF Address 24 | Paired Address 42 | Sync Word 42

and Macropad #1:

Hello world
Radio: I2C Address 0x35 | RF Address 42 | Paired Address 24 | Sync Word 42

Things go weird when I trigger a message send by depressing a key. On the sender Macropad:

Key release: 10
Sending...Sent!
Received Packet!
OTHER: 42 getPacketDestination: 24
ADDR: 24 getPacketOrigin: 42
ID: 88 SENT FROM: 42 SENT TO: 24 RSSI: 153 SNR: 9
PAYLOAD: 9155

On the receiving macropad:

Received Packet!
OTHER: 24 getPacketDestination: 42
ADDR: 42 getPacketOrigin: 24
ID: 0 SENT FROM: 24 SENT TO: 42 RSSI: 155 SNR: 9
PAYLOAD: Turn Left

Issues:

What am I doing wrong?