Common operations

The LMiC library uses primarily the event handler (the function named onEvent in lora_job.c) and the global struct LMIC as its interface.

Sending LoRa frame

A LoRa frame is scheduled with the LMIC_setTxData2 function:

uint8_t len = 0;
LMIC.frame[len++] = 'L';
LMIC.frame[len++] = 'o';
LMIC.frame[len++] = 'R';
LMIC.frame[len++] = 'a';

u1_t port = 1;
u1_t confirmed = 1;
if (LMIC_setTxData2( port, LMIC.frame, len, confirmed ) != 0) {
    NRF_LOG("LoRa: *** Error sending frame!\n");
}

When the event EV_TXCOMPLETE is triggered the frame is sent. If the frame has the confirmation flag set the event won’t trigger until the backend server has acknowledged the message.

warning Port 0 is reserved for internal LoRa frames

The port number can be any number from 1 to 223, inclusive.

Receiving a LoRa frame

When the event EV_RXCOMPLETE is triggered the device have received a frame from the gateway/backend server. You will then be able to read the payload from the same buffer you used to send frames:

void onEvent (ev_t ev) {
    switch (ev) {
        // ... skipped events 
        case EV_RXCOMPLETE:     
            NRF_LOG_PRINTF("Got %d bytes from gateway! First byte is %02x\n", LMIC.dataLen, LMIC.frame[0]); 
            break;
        // ... skipped events
    }
}

LMiC diagnostic data

The LMIC struct contains a few interesting fields which you can use to retrieve diagnostic information from the library/sx1276 chip:

Field Description
freq The last frequency used to communicate with the gateway. Value is in Hz.
rssi RSSI for the last received data.
snr SNR for the last received data.
seqnoDn Downstream frame counter (increments for each frame received)
seqnoUp Upstream frame counter (increments for each frame sent)
frame Payload for up- and downstream frames.
dataLen Length of payload.
devaddr DevAddr assigned to device.