rak811 LoRa tracker board

The board

rak811 LoRa board

Links

Steps for demo code

Steps for Arduino code

Issues

Arduino antenna switching

Arduino code sends the JOIN request and the request is receoived by the LoRa gateway but the JOIN is never completed. It could have something to do with the "external antenna receiving and transmitting state switching". That is implemented in LMIC but this board needs two bits to be flipped.

Receiving:

  pinMode(RADIO_RF_CRX_RX, OUTPUT);  
  digitalWrite(RADIO_RF_CRX_RX, HIGH);  //control LoRa work to receive
  pinMode(RADIO_RF_CTX_PA, OUTPUT);  
  digitalWrite(RADIO_RF_CTX_PA, LOW);

 

Transmitting:

  pinMode(RADIO_RF_CRX_RX, OUTPUT);  
  digitalWrite(RADIO_RF_CRX_RX, LOW);  
  pinMode(RADIO_RF_CTX_PA,OUTPUT);  
  digitalWrite(RADIO_RF_CTX_PA, HIGH);  //control LoRa send by PA_BOOST

 

See firmware source code in boards/RAK811BreakBoard/sx1276-board.c:

void SX1276SetAntSw( uint8_t opMode )
{
    switch( opMode )
    {
    case RFLR_OPMODE_TRANSMITTER:
        GpioWrite( &AntCtxPa, 1 );
        GpioWrite( &AntCbtHf, 0 );
        GpioWrite( &AntCrxRx, 0 );
        //printf("-Tx-rn");
        break;
    case RFLR_OPMODE_RECEIVER:
    case RFLR_OPMODE_RECEIVER_SINGLE:
    case RFLR_OPMODE_CAD:
    default:
        GpioWrite( &AntCtxPa, 0 );
        GpioWrite( &AntCbtHf, 0 );
        GpioWrite( &AntCrxRx, 1 );
        //printf("-Rx-rn");
        break;
    }
}

Arduino demo code

#include 
#include <hal/hal.h>
#include 

#define BUILTIN_LED LED2

// This EUI must be in little-endian format, so least-significant-byte
// first. When copying an EUI from ttnctl output, this means to reverse
// the bytes. For TTN issued EUIs the last bytes should be 0xD5, 0xB3, 0x70.
static const u1_t PROGMEM APPEUI[8] = { xxx };
void os_getArtEui (u1_t* buf) {
  memcpy_P(buf, APPEUI, 8);
}

// This should also be in little endian format, see above.
static const u1_t PROGMEM DEVEUI[8] = { xxx };
void os_getDevEui (u1_t* buf) {
  memcpy_P(buf, DEVEUI, 8);
}

// This key should be in big endian format (or, since it is not really a
// number but a block of memory, endianness does not really apply). In
// practice, a key taken from ttnctl can be copied as-is.
static const u1_t PROGMEM APPKEY[16] = { xxx };
void os_getDevKey (u1_t* buf) {
  memcpy_P(buf, APPKEY, 16);
}

static uint8_t lora_data[64];
uint32_t lora_count = 0;
static osjob_t sendjob;

typedef enum {
  LORA_UNCONFIRMED,
  LORA_CONFIRMED
};

// Schedule TX every this many seconds (might become longer due to duty cycle limitations).
const unsigned TX_INTERVAL = 10;

// Pin mapping
const lmic_pinmap lmic_pins = {
  .nss = RADIO_NSS,
  .rxtx = RADIO_RF_CRX_RX,
  .rst = RADIO_RESET,
  .dio = {RADIO_DIO_0, RADIO_DIO_1, RADIO_DIO_2},
};

void printHex2(unsigned v) {
  v &= 0xff;
  if (v < 16) Serial.print('0');
  Serial.print(v, HEX);
}

void onEvent (ev_t ev) {
  long now = os_getTime();
  Serial.printf("Time %lu: ", now);

  switch (ev) {
    case EV_SCAN_TIMEOUT:
      Serial.println("EV_SCAN_TIMEOUT");
      break;
    case EV_BEACON_FOUND:
      Serial.println("EV_BEACON_FOUND");
      break;
    case EV_BEACON_MISSED:
      Serial.println("EV_BEACON_MISSED");
      break;
    case EV_BEACON_TRACKED:
      Serial.println("EV_BEACON_TRACKED");
      break;
    case EV_JOINING:
      Serial.println("EV_JOINING");
      break;
    case EV_JOINED:
      Serial.println("EV_JOINED");
      {
        u4_t netid = 0;
        devaddr_t devaddr = 0;
        u1_t nwkKey[16];
        u1_t artKey[16];
        LMIC_getSessionKeys(&netid, &devaddr, nwkKey, artKey);
        Serial.print("netid: ");
        Serial.println(netid, DEC);
        Serial.print("devaddr: ");
        Serial.println(devaddr, HEX);
        Serial.print("AppSKey:");
        for (size_t i = 0; i < sizeof(artKey); ++i) {
          Serial.print(" ");
          printHex2(artKey[i]);
        }
        Serial.println("");
        Serial.print("NwkSKey:");
        for (size_t i = 0; i < sizeof(nwkKey); ++i) {
          Serial.print(" ");
          printHex2(nwkKey[i]);
        }
        Serial.println();
      }

      // Disable link check validation (automatically enabled
      // during join, but because slow data rates change max TX
      // size, we don't use it in this example.
      LMIC_setLinkCheckMode(0);
      break;
    case EV_RFU1:
      Serial.println("EV_RFU1");
      break;
    case EV_JOIN_FAILED:
      Serial.println("EV_JOIN_FAILED");
      break;
    case EV_REJOIN_FAILED:
      Serial.println("EV_REJOIN_FAILED");
      break;
    case EV_TXCOMPLETE:
      Serial.println("EV_TXCOMPLETE");
      digitalWrite(BUILTIN_LED, LOW);
      if (LMIC.txrxFlags & TXRX_ACK) {
        Serial.printf("rssi:%d, snr:%1dn", LMIC.rssi, LMIC.snr);
        Serial.println("Received ack");
      }
      if (LMIC.dataLen) {
        Serial.printf("rssi:%d, snr:%1dn", LMIC.rssi, LMIC.snr);
        Serial.printf("Received %d", LMIC.dataLen);
        Serial.print("Data:");
        for (size_t i = 0; i < LMIC.dataLen; i++) {
          Serial.print(" ");
          printHex2(LMIC.frame[i + LMIC.dataBeg]);
        }
        Serial.println();
      }
      // Schedule next transmission
      os_setTimedCallback(&sendjob, os_getTime() + sec2osticks(TX_INTERVAL), do_send);
      break;
    case EV_LOST_TSYNC:
      Serial.println("EV_LOST_TSYNC");
      break;
    case EV_RESET:
      Serial.println("EV_RESET");
      break;
    case EV_RXCOMPLETE:
      // data received in ping slot
      Serial.println("EV_RXCOMPLETE");
      break;
    case EV_LINK_DEAD:
      Serial.println("EV_LINK_DEAD");
      break;
    case EV_LINK_ALIVE:
      Serial.println("EV_LINK_ALIVE");
      break;
    case EV_SCAN_FOUND:
      Serial.println("EV_SCAN_FOUND");
      break;
    case EV_TXSTART:
      Serial.println("EV_TXSTART");
      break;
    case EV_TXCANCELED:
      Serial.println("EV_TXCANCELED");
      break;
    case EV_RXSTART:
      Serial.println("EV_RXSTART");
      break;
    case EV_JOIN_TXCOMPLETE:
      Serial.println("EV_JOIN_TXCOMPLETE");
      break;
    default:
      Serial.printf("Unknown event %udn", ev);
      break;
  }
}

void do_send(osjob_t* j) {
  // Check if there is not a current TX/RX job running
  if (LMIC.opmode & OP_TXRXPEND) {
    Serial.println("OP_TXRXPEND, not sending");
  } else {
    // Prepare upstream data transmission at the next possible time.
    sprintf((char *)lora_data, "Hello %d", lora_count++);
    LMIC_setTxData2(1, lora_data, strlen((char *)lora_data), LORA_UNCONFIRMED);
    Serial.println("Packet queued");
    digitalWrite(BUILTIN_LED, HIGH);
  }
  // Next TX is scheduled after TX_COMPLETE event.
}

void setup() {
  Serial.begin(115200);
  while ( !Serial ) delay(10);

  Serial.println("n*******************************");
  Serial.println("System Starting...");
  Serial.println("");

  Serial.println("Pins:");
  Serial.printf("  NSS: %d, RXTX: %d, RST: %d, DIO0: %d, DIO1: %d, DIO2: %dn", RADIO_NSS, RADIO_RF_CRX_RX, RADIO_RESET, RADIO_DIO_0, RADIO_DIO_1, RADIO_DIO_2);
  Serial.println("");
  
// Enable the LoRa module pinMode(RADIO_XTAL_EN, OUTPUT); digitalWrite(RADIO_XTAL_EN, HIGH); // LMIC init os_init(); LMIC_reset(); // Reset the MAC state. Session and pending data transfers will be discarded. //LMIC_setClockError(MAX_CLOCK_ERROR * 1 / 100); //LMIC_setLinkCheckMode(0); // Disable link check validation //LMIC_setAdrMode(true); //LMIC_setDrTxpow(DR_SF7, 14); // Set data rate and transmit power (note: txpow seems to be ignored by the library) // Start job (sending automatically starts OTAA too) do_send(&sendjob); pinMode(BUILTIN_LED, OUTPUT); digitalWrite(BUILTIN_LED, LOW); pinMode(LED1, OUTPUT); } void loop() { os_runloop_once(); digitalWrite(LED1, HIGH); delay(500); digitalWrite(LED1, LOW); delay(500); }