Skip to content

Commit

Permalink
Arduino: Update examples for BasicMAC
Browse files Browse the repository at this point in the history
  • Loading branch information
matthijskooijman committed Apr 15, 2020
1 parent 3ccfb5b commit 1505722
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 32 deletions.
73 changes: 47 additions & 26 deletions target/arduino/examples/ttn-abp/ttn-abp.ino
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ static const u4_t DEVADDR = 0x03FF0001 ; // <-- Change this address for every no
// These callbacks are only used in over-the-air activation, so they are
// left empty here (we cannot leave them out completely unless
// DISABLE_JOIN is set in config.h, otherwise the linker will complain).
void os_getArtEui (u1_t* buf) { }
void os_getJoinEui (u1_t* buf) { }
void os_getDevEui (u1_t* buf) { }
void os_getDevKey (u1_t* buf) { }
void os_getNwkKey (u1_t* buf) { }
u1_t os_getRegion (void) { return REGCODE_EU868; }

// Schedule TX every this many milliseconds (might become longer due to duty
// cycle limitations).
Expand All @@ -65,7 +66,7 @@ const lmic_pinmap lmic_pins = {
.dio = {2, 3, 4},
};

void onEvent (ev_t ev) {
void onLmicEvent (ev_t ev) {
Serial.print(os_getTime());
Serial.print(": ");
switch(ev) {
Expand Down Expand Up @@ -122,8 +123,27 @@ void onEvent (ev_t ev) {
case EV_LINK_ALIVE:
Serial.println(F("EV_LINK_ALIVE"));
break;
case EV_SCAN_FOUND:
Serial.println(F("EV_SCAN_FOUND"));
break;
case EV_TXSTART:
Serial.println(F("EV_TXSTART,"));
break;
case EV_TXDONE:
Serial.println(F("EV_TXDONE"));
break;
case EV_DATARATE:
Serial.println(F("EV_DATARATE"));
break;
case EV_START_SCAN:
Serial.println(F("EV_START_SCAN"));
break;
case EV_ADR_BACKOFF:
Serial.println(F("EV_ADR_BACKOFF"));
break;
default:
Serial.println(F("Unknown event"));
Serial.print(F("Unknown event: "));
Serial.println(ev);
break;
}
}
Expand All @@ -133,7 +153,7 @@ void setup() {
Serial.println(F("Starting"));

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

Expand All @@ -154,6 +174,9 @@ void setup() {
#endif

#if defined(CFG_eu868)
// These are defined by the LoRaWAN specification
const uint8_t EU_DR_SF12 = 0, EU_DR_SF9 = 3, EU_DR_SF7 = 5, EU_DR_SF7_BW250 = 6;

// Set up the channels used by the Things Network, which corresponds
// to the defaults of most gateways. Without this, only three base
// channels from the LoRaWAN specification are used, which certainly
Expand All @@ -162,20 +185,24 @@ void setup() {
// your network here (unless your network autoconfigures them).
// Setting up channels should happen after LMIC_setSession, as that
// configures the minimal channel set.
// NA-US channels 0-71 are configured automatically
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
// TTN defines an additional channel at 869.525Mhz using SF9 for class B
// devices' ping slots. LMIC does not have an easy way to define set this
// frequency and support for class B is spotty and untested, so this
// frequency is not configured here.
LMIC_setupChannel(0, 868100000, DR_RANGE_MAP(EU_DR_SF12, EU_DR_SF7)); // g-band
LMIC_setupChannel(1, 868300000, DR_RANGE_MAP(EU_DR_SF12, EU_DR_SF7_BW250)); // g-band
LMIC_setupChannel(2, 868500000, DR_RANGE_MAP(EU_DR_SF12, EU_DR_SF7)); // g-band
LMIC_setupChannel(3, 867100000, DR_RANGE_MAP(EU_DR_SF12, EU_DR_SF7)); // g-band
LMIC_setupChannel(4, 867300000, DR_RANGE_MAP(EU_DR_SF12, EU_DR_SF7)); // g-band
LMIC_setupChannel(5, 867500000, DR_RANGE_MAP(EU_DR_SF12, EU_DR_SF7)); // g-band
LMIC_setupChannel(6, 867700000, DR_RANGE_MAP(EU_DR_SF12, EU_DR_SF7)); // g-band
LMIC_setupChannel(7, 867900000, DR_RANGE_MAP(EU_DR_SF12, EU_DR_SF7)); // g-band
LMIC_setupChannel(8, 868800000, DR_RANGE_MAP(FSK, FSK)); // g2-band

// TTN uses SF9 at 869.525Mhz for its RX2 window (frequency is
// default LoRaWAN, SF is different, but set them both to be
// explicit).
LMIC.dn2Freq = 869525000;
LMIC.dn2Dr = EU_DR_SF9;

// Set data rate for uplink
LMIC_setDrTxpow(EU_DR_SF7, KEEP_TXPOWADJ);
#elif defined(CFG_us915)
// NA-US channels 0-71 are configured automatically
// but only one group of 8 should (a subband) should be active
Expand All @@ -187,12 +214,6 @@ void setup() {
// Disable link check validation
LMIC_setLinkCheckMode(0);

// TTN uses SF9 for its RX2 window.
LMIC.dn2Dr = DR_SF9;

// Set data rate and transmit power for uplink (note: txpow seems to be ignored by the library)
LMIC_setDrTxpow(DR_SF7,14);

// Enable this to increase the receive window size, to compensate
// for an inaccurate clock. // This compensate for +/- 10% clock
// error, a lower value will likely be more appropriate.
Expand All @@ -206,7 +227,7 @@ uint32_t last_packet = 0;

void loop() {
// Let LMIC handle background tasks
os_runloop_once();
os_runstep();

// If TX_INTERVAL passed, *and* our previous packet is not still
// pending (which can happen due to duty cycle limitations), send
Expand Down
35 changes: 29 additions & 6 deletions target/arduino/examples/ttn-otaa/ttn-otaa.ino
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
// 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);}
void os_getJoinEui (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]={ 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
Expand All @@ -49,7 +49,10 @@ void os_getDevEui (u1_t* buf) { memcpy_P(buf, DEVEUI, 8);}
// practice, a key taken from ttnctl can be copied as-is.
// The key shown here is the semtech default key.
static const u1_t PROGMEM APPKEY[16] = { 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C };
void os_getDevKey (u1_t* buf) { memcpy_P(buf, APPKEY, 16);}
void os_getNwkKey (u1_t* buf) { memcpy_P(buf, APPKEY, 16);}

// The region to use
u1_t os_getRegion (void) { return REGCODE_EU868; }

// Schedule TX every this many milliseconds (might become longer due to duty
// cycle limitations).
Expand All @@ -63,7 +66,7 @@ const lmic_pinmap lmic_pins = {
.dio = {2, 3, 4},
};

void onEvent (ev_t ev) {
void onLmicEvent (ev_t ev) {
Serial.print(os_getTime());
Serial.print(": ");
switch(ev) {
Expand Down Expand Up @@ -125,8 +128,28 @@ void onEvent (ev_t ev) {
case EV_LINK_ALIVE:
Serial.println(F("EV_LINK_ALIVE"));
break;
case EV_SCAN_FOUND:
Serial.println(F("EV_SCAN_FOUND"));
break;
case EV_TXSTART:
Serial.println(F("EV_TXSTART,"));
break;
case EV_TXDONE:
Serial.println(F("EV_TXDONE"));
break;
case EV_DATARATE:
Serial.println(F("EV_DATARATE"));
break;
case EV_START_SCAN:
Serial.println(F("EV_START_SCAN"));
break;
case EV_ADR_BACKOFF:
Serial.println(F("EV_ADR_BACKOFF"));
break;

default:
Serial.println(F("Unknown event"));
Serial.print(F("Unknown event: "));
Serial.println(ev);
break;
}
}
Expand All @@ -136,7 +159,7 @@ void setup() {
Serial.println(F("Starting"));

// LMIC init
os_init();
os_init(nullptr);
LMIC_reset();

// Enable this to increase the receive window size, to compensate
Expand All @@ -152,7 +175,7 @@ uint32_t last_packet = 0;

void loop() {
// Let LMIC handle background tasks
os_runloop_once();
os_runstep();

// If TX_INTERVAL passed, *and* our previous packet is not still
// pending (which can happen due to duty cycle limitations), send
Expand Down

0 comments on commit 1505722

Please sign in to comment.