Unstable connection

Hello,

I am using LoraWan as part of a study project. I am constantly facing problems with unstable connection. I don’t have my own Gateway, but the university has several available. Being close to the gateway I am getting “no JoinAccept”. There is constant confirmation in the console of ttn that the join-request is received and redirected. Sometimes (very rarely) the connection is established. Uplink works mostly fine, but downlink has systematic problems. Out of ~100 cases downlink was received only twice. Can you please tell me where the problem may lie?
My device: arduino Uno with Dragino Lora Bee v1.1 module. As code I use the standard example from the LMIC library with some modifications to decode the downlink.

image

/*******************************************************************************

#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.
//
#ifdef COMPILE_REGRESSION_TEST
# define FILLMEIN 0
#else
# warning "You must replace the values marked FILLMEIN with real values from the TTN control panel!"
# define FILLMEIN (#dont edit this, edit the lines that use FILLMEIN)
#endif

// 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]={ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
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]={ 0x24, 0xB8, 0x06, 0xD0, 0x7E, 0xD5, 0xB3, 0x70 };
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] = { 0x76, 0x06, 0x85, 0x09, 0x14, 0x41, 0x47, 0xAF, 0x60, 0xF0, 0x3A, 0x3A, 0xFE, 0x18, 0x9F, 0x33 };
void os_getDevKey (u1_t* buf) {  memcpy_P(buf, APPKEY, 16);}

static uint8_t mydata[] = "Hello, world!";
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 = 10,
.rxtx = LMIC_UNUSED_PIN,
.rst = 9,
.dio = {2, 6, 7}, // Specify pin numbers for DIO0, 1, 2
};

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"));
              // Convert received bytes to a string
              char receivedMessage[LMIC.dataLen + 1]; // +1 for null terminator
              for (int i = 0; i < LMIC.dataLen; i++) {
                  receivedMessage[i] = (char)LMIC.frame[LMIC.dataBeg + i];
              }
              receivedMessage[LMIC.dataLen] = '\0'; // Null-terminate the string

              // Print the received message as a string
              Serial.print(F("Message: "));
              Serial.println(receivedMessage);

              // 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(9600);
    Serial.println(F("Starting"));

    #ifdef VCC_ENABLE
    // For Pinoccio Scout boards
    pinMode(VCC_ENABLE, OUTPUT);
    digitalWrite(VCC_ENABLE, HIGH);
    delay(1000);
    #endif

    // LMIC init
    os_init();
    // Reset the MAC state. Session and pending data transfers will be discarded.
    LMIC_reset();

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

void loop() {
    os_runloop_once();
}

There is no concept of connection, it’s just shouting in to the void and hoping something hears it.

To that end, can you tell us what the RSSI & SNR is of the Join Accept and report back - that will inform us of how hard the LoRa Bee can shout - it it’s too high or too low it may then have issues hearing the response from the gateway due to it having a really small antenna.

You should be aware that the code you have posted breaks both the FUP and very probably the legal limits ( :policeman: :rotating_light: :police_car: ) - please review the FUP in the Learn section (linked above) and reassure us that you are on top of this aspect, particularly the number of downlinks you can do a day.

Thank you so much for your reply!

RSSI & SNR
I tried sending the signal from home (distance to the nearest gateway is about 200m). I got the following results:

  1. First attempt - the device successfully received join-accept:
    ‘rssi": -114,
    ‘channel_rssi": -114,
    ‘snr": 2.75
  2. Second attempt - no JoinAccept
    ‘rssi": -114,
    ‘channel_rssi": -114,
    ‘snr": 3.25,
  3. All further attempts were also unsuccessful
    At university the rssi was about -90, I didn’t pay attention to snr unfortunately. I can write more precisely next time when I test from university if necessary.

FUP
Thank you very much for the notice! I’ve corrected the code, specifically setting counters for the maximum number of downlinks (10) and joinAttempts (5)

Downlinks
I tried sending a downlink using mqtt-client, then compared the payload of downlink sent using console ttn. The difference is mostly just the additional correlation-id. Is this correlation-id mandatory?
PS. none of the downlink was received.
PPS. there have been previous successful attempts to receive a downlink from console of ttn. At least it can be concluded that getting downlink on the device side works.

Received payload on …/down/queued

From the own Mqtt-client

{
	"end_device_ids": {
		"device_id": "lorawan-project-htw",
		"application_ids": {
			"application_id": "project-seminar-lorawan"
		},
		"dev_eui": "70B3D57ED006B824",
		"join_eui": "0000000000000000"
	},
	"correlation_ids": [
		"as:downlink:01JF5TD80XSBJM1YEJQZTR338E"
	],
	"received_at": "2024-12-15T18:48:35.102559813Z",
	"downlink_queued": {
		"f_port": 1,
		"frm_payload": "V29ybGQgc2F5cyBoZWxsbw==",
		"priority": "NORMAL",
		"correlation_ids": [
			"as:downlink:01JF5TD80XSBJM1YEJQZTR338E"
		]
	}
}

From TTN-Console:

{
	"end_device_ids": {
		"device_id": "lorawan-project-htw",
		"application_ids": {
			"application_id": "project-seminar-lorawan"
		},
		"dev_eui": "70B3D57ED006B824",
		"join_eui": "0000000000000000"
	},
	"correlation_ids": [
		"as:downlink:01JF5THGKARWA3DXK734NKSC5J",
		"rpc:/ttn.lorawan.v3.AppAs/DownlinkQueueReplace:de1e2785-751e-46d0-b1c5-448945b1ed91"
	],
	"received_at": "2024-12-15T18:50:54.954649926Z",
	"downlink_queued": {
		"f_port": 1,
		"frm_payload": "V29ybGQgc2F5cyBoZWxsbw==",
		"correlation_ids": [
			"as:downlink:01JF5THGKARWA3DXK734NKSC5J",
			"rpc:/ttn.lorawan.v3.AppAs/DownlinkQueueReplace:de1e2785-751e-46d0-b1c5-448945b1ed91"
		]
	}
}

PPPS. I apologise for the long reply

At 200m I’d expect a far higher (or lower, as you brain works) RSSI. Somewhere around -90 which is sort of a guess value, but -114 is definitely in the km range so this isn’t ideal.

[Note to self, take Mappy for a walk]

As I suggested, the antenna on your device, which is relatively ancient technology, isn’t the greatest considering how close it is to a gateway. You need to optimise the signal path - line of sight, no big obstacles in the path.

Unless you are auditing the heck out of the messages for some legal submission or trying to catch Hannibal Lector, you can ignore the correlation ids. They do join up the whole sequence but are like drinking from the Matrix fire hydrant.

Can you tell us when you compile the sketch how much memory & flash is used / available. The memory available is essential to how well LMIC can process downlinks - too little and errors / drops will occur.

For LMIC, I’d go to an Adafruit Feather M0 with RFM95 - more program space, well built, minor hardware mod required for LW.

Or go for an ESP32 or Feather M0 using RadioLib which is more compliant / up to date.