Hello,
I am trying to connect a Adafruit feather M0 LoraWan to a Sentrius RG1xx via OTAA without success, the the ABP version works instead.
I already tried different suggestions found online and inside this forum with no luck.
Here is the code I am using.
#include <lmic.h>
#include <hal/hal.h>
#include <SPI.h>
#include <SRSPacket.h>
#include <ArduinoUniqueID.h>
bool connected = true;
static uint8_t mydata[] = "Hello, world!";
static const u1_t PROGMEM APPEUI[8] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA };
void os_getArtEui (u1_t* buf) {
memcpy_P(buf, APPEUI, 8);
}
static const u1_t PROGMEM DEVEUI[8] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA };
void os_getDevEui (u1_t* buf) {
memcpy_P(buf, DEVEUI, 8);
}
static const u1_t PROGMEM APPKEY[16] = {0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA};
void os_getDevKey (u1_t* buf) {
memcpy_P(buf, APPKEY, 16);
}
static osjob_t sendjob;
const unsigned TX_INTERVAL = 60;
const lmic_pinmap lmic_pins = {
.nss = 8, // PA06
.rxtx = LMIC_UNUSED_PIN,
.rst = 4,
.dio = {3, 6, LMIC_UNUSED_PIN},
.rxtx_rx_active = 0,
.rssi_cal = 8, // LBT cal for the Adafruit Feather M0 LoRa, in dB
.spi_freq = 8000000,
};
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"));
{
connected = true;
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("artKey: ");
for (size_t i = 0; i < sizeof(artKey); ++i) {
if (i != 0)
Serial.print("-");
Serial.print(artKey[i], HEX);
}
Serial.println("");
Serial.print("nwkKey: ");
for (size_t i = 0; i < sizeof(nwkKey); ++i) {
if (i != 0)
Serial.print("-");
Serial.print(nwkKey[i], HEX);
}
Serial.println("");
}
LMIC_setLinkCheckMode(0);
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)"));
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
if (LMIC.txrxFlags & TXRX_ACK)
Serial.println(F("Received ack"));
if (LMIC.dataLen) {
Serial.println(F("Received "));
Serial.println(LMIC.dataLen);
Serial.println(F(" bytes of payload"));
}
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;
case EV_TXSTART:
if (firstTime) {
connected = false;
firstTime = false;
}
Serial.println(F("EV_TXSTART"));
break;
default:
Serial.print(F("Unknown event: "));
Serial.println((unsigned) ev);
break;
}
}
/**
convert ADC value to microvolt
ADV range 0 1.65
ADC resolution = 4096
each unit correspond to 403 uV 1650000/4096
*/
int adc2Microvolt(int adcValue)
{
return adcValue * 403;
}
void
printADCValue(char *label, int value)
{
int uV = adc2Microvolt(value);
Serial.print(label);
Serial.print(value, DEC);
Serial.print("(Dec) ");
Serial.print(value, HEX);
Serial.print("(Hex) ");
Serial.print(value, BIN);
Serial.print("(Bin) ");
Serial.print(uV, DEC);
Serial.println("(uV) ");
}
/**
*/
void do_send(osjob_t* j) {
if (LMIC.opmode & OP_TXRXPEND) {
Serial.println(F("OP_TXRXPEND, not sending"));
} else {
LMIC_setTxData2(1, mydata, sizeof(mydata) - 1, 0);
Serial.println(F("Packet queued"));
}
}
void(* RebootSW)(void) = 0;
void setup() {
osjob_t initjob;
delay(1000);
while (! Serial);
Serial.begin(115200);
Serial.println(F("Starting"));
firstTime = true;
os_init();
LMIC_reset();
LMIC_setClockError(MAX_CLOCK_ERROR * 10 / 100);
LMIC_setLinkCheckMode(0);
LMIC.dn2Dr = SF9;
LMIC_setDrTxpow(DR_SF12,14);
do_send(&sendjob);
delay(5000);
//LMIC_startJoining();
}
void loop() {
os_runloop_once();
}
The serial output looks like this
12:31:54.718 → Starting
12:31:54.752 → Packet queued
12:31:59.749 → 398917: EV_JOINING
12:32:06.899 → 846772: EV_TXSTART
12:33:12.271 → 4935503: EV_TXSTART
12:34:22.099 → 9302670: EV_TXSTART
12:35:35.236 → 13875239: EV_TXSTART
12:37:44.651 → 21970289: EV_TXSTART
12:39:50.922 → 29867053: EV_TXSTART
12:41:51.919 → 37433584: EV_TXSTART
12:45:44.605 → 51987602: EV_TXSTART
12:49:29.507 → 66052911: EV_TXSTART
I have no real idea why every minute this code sends a new message to the gateway that is seen as a new join reequest.
Do you have any suggestions?
Thanks in advance.