Skip to content

Instantly share code, notes, and snippets.

@solars

solars/test.ino Secret

Created Feb 10, 2018
Embed
What would you like to do?
#include <lmic.h>
#include <hal/hal.h>
#include <SPI.h>
#include "LowPower.h"
bool next = false;
// LoRaWAN NwkSKey, network session key
static const PROGMEM u1_t NWKSKEY[16] = xxx;
// LoRaWAN AppSKey, application session key
static const u1_t PROGMEM APPSKEY[16] = xxx;
// LoRaWAN end-device address (DevAddr)
static const u4_t DEVADDR = 0x26011151;
// 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;
// LMIC pin mapping for RFM95
const lmic_pinmap lmic_pins = {
.nss = 6,
.rxtx = LMIC_UNUSED_PIN,
.rst = 5,
.dio = {2, 3, 4},
};
#define STEP_UP_PIN 7
const int trigPin = 8;
const int echoPin = 9;
long duration;
int distance;
void setup() {
Serial.begin(115200);
Serial.println("Starting");
pinMode(STEP_UP_PIN, OUTPUT);
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
digitalWrite(trigPin, HIGH);
digitalWrite(STEP_UP_PIN, LOW);
os_init(); // LMIC init
delay(100);
LMIC_reset(); // Reset the MAC state. Session and pending data transfers will be discarded.
// Set static session parameters. Instead of dynamically establishing a session
// by joining the network, precomputed session parameters are be provided.
// 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);
#if defined(CFG_eu868)
// Set up the channels used by the Things Network
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
#endif
LMIC_setLinkCheckMode(0); // Disable link check validation
LMIC.dn2Dr = DR_SF9; // TTN uses SF9 for its RX2 window.
LMIC_setDrTxpow(DR_SF7, 14); // Set data rate and transmit power for uplink (note: txpow seems to be ignored by the library)
Serial.println("Setup Complete, starting Job");
delay(100);
do_send(&sendjob); // Start job
}
void loop() {
if (next == false) {
os_runloop_once();
} else {
int sleepcycles = TX_INTERVAL / 8; // calculate the number of sleepcycles (8s) given the TX_INTERVAL
Serial.print(F("Enter sleeping for "));
Serial.print(sleepcycles);
Serial.println(F(" cycles of 8 seconds"));
delay(100);
for (int i = 0; i < sleepcycles; i++) {
LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
}
Serial.println(F("Sleep complete"));
delay(100);
next = false;
do_send(&sendjob); // Start job
}
}
void do_send(osjob_t* j) {
if (LMIC.opmode & OP_TXRXPEND) { // Check if there is not a current TX/RX job running
Serial.println(F("OP_TXRXPEND, not sending"));
} else {
digitalWrite(STEP_UP_PIN, HIGH);
delay(500);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
digitalWrite(trigPin, HIGH);
delay(500);
digitalWrite(STEP_UP_PIN, LOW);
distance = duration * 0.034 / 2;
byte payload[2];
payload[0] = highByte(distance);
payload[1] = lowByte(distance);
Serial.println(distance);
LMIC_setTxData2(1, payload, sizeof(payload), 0);
Serial.println(F("Packet queued"));
}
// Next TX is scheduled after TX_COMPLETE event.
}
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"));
break;
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.println(F("Received "));
Serial.println(LMIC.dataLen);
Serial.println(F(" bytes of payload"));
}
// Schedule next transmission
// os_setTimedCallback(&sendjob, os_getTime()+sec2osticks(TX_INTERVAL), do_send); // disabled for merge
delay(200);
next = true;
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;
default:
Serial.println(F("Unknown event"));
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment