LoRa : invalidMIC with rfm95w + arduino pro mini

Hello
I try to connect a gateway with a rfm95w + arduino pro mini (3.3v, 8MHz). But I have an error on the gateway (here, gateway objenious : connection in OTAA) : "Erreur : InvalidMIC" like on the image

On the image, you can see also a test with an mkrwan1310, with the same APPEUI (70B3D57ED003F650), DEVEUI(A8610A3434387D10), APPKEY(91CAB8E692EE311F5FA271A0F9E03980).

I give you my code for the rfm95w version :

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

//
// For normal use, we require that you edit the sketch to replace FILLMEIN
// with values assigned by the TTN console. However, for regression tests,
// we want to be able to compile these scripts. The regression tests define
// COMPILE_REGRESSION_TEST, and in that case we define FILLMEIN to a non-
// working but innocuous value.
//


// 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]={0x50,0xF6,0x03,0xD0,0x7E,0xD5,0xB3,0x70};
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]={0x10,0x7D,0x38,0x34,0x34,0x0A,0x61,0xA8};
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] = { 0x91,0xCA,0xB8,0xE6,0x92,0xEE,0x31,0x1F,0x5F,0xA2,0x71,0xA0,0xF9,0xE0,0x39,0x80 };
void os_getDevKey (u1_t* buf) {  memcpy_P(buf, APPKEY, 16);}

static uint8_t mydata[10] = {0x00,0x01,0x01,0x49,0x75,0x39,0x03,0xb0,0x4e,0x21};
static osjob_t sendjob;

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

// Pin mapping
const lmic_pinmap lmic_pins = {
    .nss = 6,
    .rxtx = LMIC_UNUSED_PIN,
    .rst = 5,
    .dio = {3, 4, LMIC_UNUSED_PIN},
};

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

void onEvent (ev_t ev) {
    Serial.print(os_getTime());
    Serial.print(": ");
    switch(ev) {
        case EV_SCAN_TIMEOUT:
            Serial.println(F("EV_SCAN_TIMEOUT"));
            break;
        case EV_BEACON_FOUND:
            Serial.println(F("EV_BEACON_FOUND"));
            break;
        case EV_BEACON_MISSED:
            Serial.println(F("EV_BEACON_MISSED"));
            break;
        case EV_BEACON_TRACKED:
            Serial.println(F("EV_BEACON_TRACKED"));
            break;
        case EV_JOINING:
            Serial.println(F("EV_JOINING"));
            break;
        case EV_JOINED:
            Serial.println(F("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) {
                if (i != 0)
                  Serial.print("-");
                printHex2(artKey[i]);
              }
              Serial.println("");
              Serial.print("NwkSKey: ");
              for (size_t i=0; i<sizeof(nwkKey); ++i) {
                      if (i != 0)
                              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;
        /*
        || This event is defined but not used in the code. No
        || point in wasting codespace on it.
        ||
        || case EV_RFU1:
        ||     Serial.println(F("EV_RFU1"));
        ||     break;
        */
        case EV_JOIN_FAILED:
            Serial.println(F("EV_JOIN_FAILED"));
            break;
        case EV_REJOIN_FAILED:
            Serial.println(F("EV_REJOIN_FAILED"));
            break;
        case EV_TXCOMPLETE:
            Serial.println(F("EV_TXCOMPLETE (includes waiting for RX windows)"));
            if (LMIC.txrxFlags & TXRX_ACK)
              Serial.println(F("Received ack"));
            if (LMIC.dataLen) {
              Serial.print(F("Received "));
              Serial.print(LMIC.dataLen);
              Serial.println(F(" bytes of payload"));
            }
            // Schedule next transmission
            os_setTimedCallback(&sendjob, os_getTime()+sec2osticks(TX_INTERVAL), do_send);
            break;
        case EV_LOST_TSYNC:
            Serial.println(F("EV_LOST_TSYNC"));
            break;
        case EV_RESET:
            Serial.println(F("EV_RESET"));
            break;
        case EV_RXCOMPLETE:
            // data received in ping slot
            Serial.println(F("EV_RXCOMPLETE"));
            break;
        case EV_LINK_DEAD:
            Serial.println(F("EV_LINK_DEAD"));
            break;
        case EV_LINK_ALIVE:
            Serial.println(F("EV_LINK_ALIVE"));
            break;
        /*
        || This event is defined but not used in the code. No
        || point in wasting codespace on it.
        ||
        || case EV_SCAN_FOUND:
        ||    Serial.println(F("EV_SCAN_FOUND"));
        ||    break;
        */
        case EV_TXSTART:
            Serial.println(F("EV_TXSTART"));
            break;
        case EV_TXCANCELED:
            Serial.println(F("EV_TXCANCELED"));
            break;
        case EV_RXSTART:
            /* do not print anything -- it wrecks timing */
            break;
        case EV_JOIN_TXCOMPLETE:
            Serial.println(F("EV_JOIN_TXCOMPLETE: no JoinAccept"));
            break;

        default:
            Serial.print(F("Unknown event: "));
            Serial.println((unsigned) 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(F("OP_TXRXPEND, not sending"));
    } else {
        // Prepare upstream data transmission at the next possible time.
        LMIC_setTxData2(1, mydata, sizeof(mydata)-1, 0);
        Serial.println(F("Packet queued"));
    }
    // Next TX is scheduled after TX_COMPLETE event.
}

void setup() {
  Serial.begin(115200);
  delay(2000);
     Serial.println(F("Starting"));
     // LMIC init
     os_init();
     // Reset the MAC state. Session and pending data transfers will be discarded.
     LMIC_reset();    
     LMIC_setClockError(MAX_CLOCK_ERROR * 2 / 100);
     LMIC_setupChannel(0, 868100000, DR_RANGE_MAP(DR_SF12, DR_SF7),  BAND_CENTI);      // g-band 
     LMIC_setupChannel(1, 868300000, DR_RANGE_MAP(DR_SF12, DR_SF7B), BAND_CENTI);      // g-band 
     LMIC_setupChannel(2, 868500000, DR_RANGE_MAP(DR_SF12, DR_SF7),  BAND_CENTI);      // g-band 
     LMIC_setupChannel(3, 867100000, DR_RANGE_MAP(DR_SF12, DR_SF7),  BAND_CENTI);      // g-band 
     LMIC_setupChannel(4, 867300000, DR_RANGE_MAP(DR_SF12, DR_SF7),  BAND_CENTI);      // g-band 
     LMIC_setupChannel(5, 867500000, DR_RANGE_MAP(DR_SF12, DR_SF7),  BAND_CENTI);      // g-band 
     LMIC_setupChannel(6, 867700000, DR_RANGE_MAP(DR_SF12, DR_SF7),  BAND_CENTI);      // g-band 
     LMIC_setupChannel(7, 867900000, DR_RANGE_MAP(DR_SF12, DR_SF7),  BAND_CENTI);      // g-band 
     LMIC_setupChannel(8, 868800000, DR_RANGE_MAP(DR_FSK,  DR_FSK),  BAND_MILLI);      // g2-band 

     LMIC_setLinkCheckMode(0); 
     LMIC.dn2Dr = SF9; 
     LMIC_setDrTxpow(DR_SF7,14); 

   // Start job (sending automatically starts OTAA too)      
   do_send(&sendjob); 

}

void loop() {
    os_runloop_once();
}

On the monitor, I have :

12:05:52.511 -> Starting
12:05:52.544 -> Packet queued
12:05:52.577 -> 127632: EV_JOINING
12:05:53.903 -> 211858: EV_TXSTART
12:06:00.232 -> 605673: EV_JOIN_TXCOMPLETE: no JoinAccept

Do you have an idea of the problem ?
Thanks !!!

I forgot the connections :

HopeRF RFM95 LoRa transceiver module Arduino Pin HopeRF RFM95 LoRa transceiver module Arduino Pin
ANT GND
GND GND DIO5
DIO3 RESET 5
DIO4 NSS 6
3.3V VCC SCK 13
DIO0 3 MOSI 11
DIO1 4 MISO 12
DIO2 GND

for bad_mic errors have a look at LoraWAN Troubleshooting
it is some time since I had this problem when I upgraded from The Things V2 to V3
initially I set the device Network Layer LoRaWAN version to LoRwAN Specification 1.1
every time I restarted the target device the DevNonce was reset and caused problem when connecting as the server which was expecting continuation from the previous value to calculate MIC - see lorawan-1-1-devnonce-must-be-stored-in-a-non-volatile-memory-on-end-device
I solved the problem by changing the Networ Layer LoraWAN version to LoRwAN Specification 1.0.2 which does not use DevNonce in calculating MIC

Hi

Invalid MIC ? it's more a LoRaWAN problem than a LoRa one :wink:

Whe using a slow microcontroller like 8MHz Pro Mini one, clock inacurracy can become a problem for OTAA's short downlink windows.

Look in your library's doc for a parameter or a function (I don't remember... :slightly_frowning_face: ) able to relax timing issues.
I had to set it when using Pro Mini 8MHZ board, not ESP32 board.

Got it here : https://github.com/mcci-catena/arduino-lmic#controlling-protocol-timing

I try to put in lmic_project_config.h :

#define LMIC_LORAWAN_SPEC_VERSION   LMIC_LORAWAN_SPEC_VERSION_1_0_2

and I increase clockerror:

LMIC_setClockError(MAX_CLOCK_ERROR * 20.0 / 100);

It's ok ! It works!
Thanks!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.