Skip to content

Instantly share code, notes, and snippets.

@dniklaus
Last active August 31, 2017 06:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dniklaus/bac2febf8e905761ce133d4b3fabdde4 to your computer and use it in GitHub Desktop.
Save dniklaus/bac2febf8e905761ce133d4b3fabdde4 to your computer and use it in GitHub Desktop.
Sending Hello World from a test node based on a Dragino LoRa Shield on a Mega2560, based on https://github.com/SensorsIot/LoRa/blob/master/Nodes/Dragino/HelloWorld/HelloWorld.ino
// MIT License
// https://github.com/gonzalocasas/arduino-uno-dragino-lorawan/blob/master/LICENSE
// Based on examples from https://github.com/matthijskooijman/arduino-lmic
// Copyright (c) 2015 Thomas Telkamp and Matthijs Kooijman
// Adaptions: Andreas Spiess
#include <lmic.h>
#include <hal/hal.h>
//#include <credentials.h>
uint8_t NWKSKEY[] = { 0x0A, 0x50, 0x2A, 0x05, 0x2B, 0x05, 0x5D, 0x6E, 0x16, 0x76, 0x3B, 0xA8, 0x07, 0xC9, 0xEC, 0x5B };
uint8_t APPSKEY[] = { 0x14, 0x51, 0x6D, 0xA8, 0x09, 0x31, 0xC6, 0x2A, 0x44, 0x26, 0x28, 0x02, 0x1E, 0x4A, 0xDC, 0xC5 };
const long int DEVADDR = 0x2601103C;
// 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_getDevEui (u1_t* buf) { }
void os_getDevKey (u1_t* buf) { }
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 Dragino Shield
const lmic_pinmap lmic_pins = {
.nss = 10,
.rxtx = LMIC_UNUSED_PIN,
.rst = 9,
.dio = {2, 6, 7},
};
void onEvent (ev_t ev) {
if (ev == EV_TXCOMPLETE) {
Serial.println(F("EV_TXCOMPLETE (includes waiting for RX windows)"));
// Schedule next transmission
os_setTimedCallback(&sendjob, os_getTime()+sec2osticks(TX_INTERVAL), do_send);
}
}
void do_send(osjob_t* j){
// Payload to send (uplink)
static uint8_t message[] = "hi";
// 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, message, sizeof(message)-1, 0);
Serial.println(F("Sending uplink packet..."));
}
// Next TX is scheduled after TX_COMPLETE event.
}
void setup() {
Serial.begin(115200);
Serial.println(F("Starting..."));
// LMIC init
os_init();
// Reset the MAC state. Session and pending data transfers will be discarded.
LMIC_reset();
// Set static session parameters.
LMIC_setSession (0x1, DEVADDR, NWKSKEY, APPSKEY);
// 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);
for (int i=1; i<=8; i++)
{
LMIC_disableChannel(i);
}
// Start job
do_send(&sendjob);
}
void loop() {
os_runloop_once();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment