Created
June 26, 2016 11:31
-
-
Save joscha/6c14414b464d5367f576531166636d08 to your computer and use it in GitHub Desktop.
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 <lmic.h> | |
#include <hal/hal.h> | |
#include <SPI.h> | |
// LoRaWAN NwkSKey, network session key | |
// This is the default Semtech key, which is used by the prototype TTN | |
// network initially. | |
static const PROGMEM u1_t NWKSKEY[16] = { 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C }; | |
// LoRaWAN AppSKey, application session key | |
// This is the default Semtech key, which is used by the prototype TTN | |
// network initially. | |
static const u1_t PROGMEM APPSKEY[16] = { 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C }; | |
// LoRaWAN end-device address (DevAddr) | |
// See http://thethingsnetwork.org/wiki/AddressSpace | |
static const u4_t DEVADDR = 0x09873406; // <-- Change this address for every node! | |
// 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 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 = 7, | |
.dio = {2, 5, 6}, | |
}; | |
void onEvent (ev_t ev) { | |
//Serial.print(os_getTime()); | |
Serial.print(": "); | |
switch(ev) { | |
case EV_TXCOMPLETE: | |
Serial.println(F("EV_TXCOMPLETE")); | |
if(LMIC.dataLen) { | |
// data received in rx slot after tx | |
Serial.print(F("Data Received: ")); | |
Serial.write(LMIC.frame+LMIC.dataBeg, LMIC.dataLen); | |
Serial.println(); | |
} | |
// Schedule next transmission | |
os_setTimedCallback(&sendjob, os_getTime()+sec2osticks(TX_INTERVAL), do_send); | |
break; | |
default: | |
Serial.println(F("Unknown event")); | |
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(115200); | |
Serial.println(F("Starting")); | |
digitalWrite(10, HIGH); | |
pinMode(53, OUTPUT); | |
digitalWrite(53, HIGH); | |
// LMIC init | |
os_init(); | |
// Reset the MAC state. Session and pending data transfers will be discarded. | |
LMIC_reset(); | |
// Set static session parameters. Instead of dynamically establishing a session | |
// by joining the network, precomputed session parameters are be provided. | |
#ifdef PROGMEM | |
// On AVR, these values are stored in flash and only copied to RAM | |
// once. Copy them to a temporary buffer here, LMIC_setSession will | |
// copy them into a buffer of its own again. | |
uint8_t appskey[sizeof(APPSKEY)]; | |
uint8_t nwkskey[sizeof(NWKSKEY)]; | |
memcpy_P(appskey, APPSKEY, sizeof(APPSKEY)); | |
memcpy_P(nwkskey, NWKSKEY, sizeof(NWKSKEY)); | |
LMIC_setSession (0x1, DEVADDR, nwkskey, appskey); | |
#else | |
// If not running an AVR with PROGMEM, just use the arrays directly | |
LMIC_setSession (0x1, DEVADDR, NWKSKEY, APPSKEY); | |
#endif | |
LMIC_setupChannel(0, 916800000, DR_RANGE_MAP(DR_SF12, DR_SF7), BAND_CENTI); | |
// Disable link check validation | |
LMIC_setLinkCheckMode(0); | |
// Set data rate and transmit power (note: txpow seems to be ignored by the library) | |
LMIC_setDrTxpow(DR_SF7,14); | |
// 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