Created
October 14, 2020 15:32
-
-
Save Commifreak/a9b0195115e225b827159605d3a31450 to your computer and use it in GitHub Desktop.
Dragino LoRa Test on MKR 1010
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SPI.h> | |
#include <lmic.h> | |
#include <hal/hal.h> | |
int waitForSerial = 10000; //10s | |
unsigned long currentMillis = 0; | |
/** | |
################################################ LoRa stuff | |
*/ | |
// 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] = { }; | |
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] = { }; | |
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] = { }; | |
void os_getDevKey (u1_t* buf) { | |
memcpy_P(buf, APPKEY, 16); | |
} | |
static uint8_t mydata[] = "Hello, world!"; | |
static osjob_t sendjob; | |
// Pin mapping | |
const lmic_pinmap lmic_pins = { | |
.nss = 7, | |
.rxtx = LMIC_UNUSED_PIN, | |
.rst = LMIC_UNUSED_PIN, | |
.dio = {0, 1, 2}, | |
}; | |
/** | |
FUNCS | |
*/ | |
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. | |
} | |
/** | |
################################################ LoRa stuff end | |
*/ | |
void setup() { | |
// put your setup code here, to run once: | |
Serial.begin(9600); | |
while (!Serial) { | |
if (currentMillis + waitForSerial < millis()) { | |
// Waited enough! Go on!! | |
break; | |
} | |
} | |
Serial.println("Starting LMIC"); | |
os_init(); | |
LMIC_reset(); | |
do_send(&sendjob); | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
os_runloop_once(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment