Skip to content

Instantly share code, notes, and snippets.

@Neumi
Last active March 6, 2024 12:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Neumi/800213b38d660a6c46b079e79b0db2d7 to your computer and use it in GitHub Desktop.
Save Neumi/800213b38d660a6c46b079e79b0db2d7 to your computer and use it in GitHub Desktop.
// 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 Neumi
#include <lmic.h>
#include <hal/hal.h>
static const u1_t NWKSKEY[16] = { 0xF7, 0xA5, 0x02, 0x48, 0x9B, 0xD1, 0xF2, 0x92, 0x97, 0x62, 0x69, 0x4C, 0xDB, 0x2A, 0xE9, 0x7A };
static const u1_t APPSKEY[16] = { 0xB4, 0x0C, 0xEB, 0x93, 0x90, 0x3A, 0xF3, 0xED, 0x4D, 0xBE, 0x47, 0x16, 0x7F, 0x81, 0x81, 0x58 };
static const u4_t DEVADDR = 0x2601107A;
const unsigned TX_INTERVAL = 60;
void os_getArtEui (u1_t* buf) { }
void os_getDevEui (u1_t* buf) { }
void os_getDevKey (u1_t* buf) { }
static osjob_t sendjob;
const lmic_pinmap lmic_pins = {
.nss = 10,
.rxtx = LMIC_UNUSED_PIN,
.rst = 5,
.dio = {2, 3, LMIC_UNUSED_PIN},
};
void onEvent (ev_t ev) {
if (ev == EV_TXCOMPLETE) {
Serial.println(F("EV_TXCOMPLETE (includes waiting for RX windows)"));
os_setTimedCallback(&sendjob, os_getTime() + sec2osticks(TX_INTERVAL), do_send);
}
}
void do_send(osjob_t* j) {
// Payload to send (uplink)
static uint8_t message[] = "Hello, Medium!";
// Check if there is not a current TX/RX job running
if (LMIC.opmode & OP_TXRXPEND) {
Serial.println("OP_TXRXPEND, not sending");
} else {
// Prepare upstream data transmission at the next possible time.
LMIC_setTxData2(1, message, sizeof(message) - 1, 0);
Serial.println("Sending uplink packet...");
}
// Next TX is scheduled after TX_COMPLETE event.
}
void setup() {
Serial.begin(9600);
Serial.println("Starting...");
// LMIC init
Serial.println("os_init");
os_init();
// Reset the MAC state. Session and pending data transfers will be discarded.
Serial.println("LMIC_reset");
LMIC_reset();
// Set static session parameters.
Serial.println("LMIC_setSession");
LMIC_setSession (0x1, DEVADDR, NWKSKEY, APPSKEY);
// Disable link check validation
Serial.println("LMIC_setLinkCheckMode");
LMIC_setLinkCheckMode(0);
// TTN uses SF9 for its RX2 window.
Serial.println("DR_SF9");
LMIC.dn2Dr = DR_SF9;
// Set data rate and transmit power for uplink (note: txpow seems to be ignored by the library)
// LMIC_setDrTxpow(DR_SF12, 14);
Serial.println("LMIC_setDrTxpow");
LMIC_setDrTxpow(DR_SF7, 14);
// LMIC_setDrTxpow(DR_SF12, 14);
// Start job
do_send(&sendjob);
Serial.println("do send");
digitalWrite(7, LOW);
}
void loop() {
os_runloop_once();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment