Probably best to post the code here. I’d already got rid of the double break and enabled the link checking when the problem was occurring. Only change to this code is you’ll see I reinitialise LMIC on a rejoin failed now.
When’s the best time to check the datarate? At the moment I’m just printing it butbthe aim is to adjust the delay between transmissions based on the data rate currently set by ADR. Is there perhaps a better variable to monitor?
Thanks for the help with this one!
/*******************************************************************************
Copyright (c) 2015 Thomas Telkamp and Matthijs Kooijman
Permission is hereby granted, free of charge, to anyone
obtaining a copy of this document and accompanying files,
to do whatever they want with them without any restriction,
including, but not limited to, copying, modification and redistribution.
NO WARRANTY OF ANY KIND IS PROVIDED.
This example sends a valid LoRaWAN packet with payload "Hello,
world!", using frequency and encryption settings matching those of
the The Things Network.
This uses OTAA (Over-the-air activation), where where a DevEUI and
application key is configured, which are used in an over-the-air
activation procedure where a DevAddr and session keys are
assigned/generated for use with all further communication.
Note: LoRaWAN per sub-band duty-cycle limitation is enforced (1% in
g1, 0.1% in g2), but not the TTN fair usage policy (which is probably
violated by this sketch when left running for longer)!
To use this sketch, first register your application and device with
the things network, to set or generate an AppEUI, DevEUI and AppKey.
Multiple devices can use the same AppEUI, but each device has its own
DevEUI and AppKey.
Do not forget to define the radio type correctly in config.h.
*******************************************************************************/
/*
****************************************************************************************
TheThingsNetwork Payload functions :
function Decoder(bytes, port)
{
var retValue = {
bytes: bytes
};
retValue.batt = bytes[0] / 10.0;
if (retValue.batt === 0)
delete retValue.batt;
if (bytes.length >= 2)
{
retValue.humidity = bytes[1];
if (retValue.humidity === 0)
delete retValue.humidity;
}
if (bytes.length >= 3)
{
retValue.temperature = (((bytes[2] << 8) | bytes[3]) / 10.0) - 40.0;
}
if (bytes.length >= 5)
{
retValue.pressure = ((bytes[4] << 8) | bytes[5]);
if (retValue.pressure === 0)
delete retValue.pressure;
}
return retValue;
}
*/
#include <lmic.h>
#include <hal/hal.h>
#include <SPI.h>
#include <Wire.h>
#include "adcvcc.h"
#include <BME280I2C.h>
#define debugSerial Serial
#define SHOW_DEBUGINFO
#define debugPrintLn(...) { if (debugSerial) debugSerial.println(__VA_ARGS__); }
#define debugPrint(...) { if (debugSerial) debugSerial.print(__VA_ARGS__); }
// Pin mapping for the RGBLED object:
#define redLed 9
#define greenLed 6
#define blueLed 5
// 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.
// The key shown here is the semtech default key.
static const u1_t PROGMEM APPKEY[16] = { };
void os_getDevKey (u1_t* buf) {
memcpy_P(buf, APPKEY, 16);
}
static osjob_t sendjob;
// global enviromental parameters
static float temp = 0.0;
static float pressure = 0.0;
static float humidity = 0.0;
BME280I2C bme; // Default : forced mode, standby time = 1000 ms
// Oversampling = pressure ×1, temperature ×1, humidity ×1, filter off,
/* ======================================================================
Function: ADC_vect
Purpose : IRQ Handler for ADC
Input : -
Output : -
Comments: used for measuring 8 samples low power mode, ADC is then in
free running mode for 8 samples
====================================================================== */
ISR(ADC_vect)
{
// Increment ADC counter
_adc_irq_cnt++;
}
// Schedule TX every this many seconds (might become longer due to duty
// cycle limitations).
const unsigned TX_INTERVAL = 10;
// Pin mapping
const lmic_pinmap lmic_pins = {
.nss = 10,
.rxtx = LMIC_UNUSED_PIN,
.rst = 14,
.dio = {2, 7, 8},
};
void printDataRate() {
switch (LMIC.datarate) {
case DR_SF12: debugPrintLn(F("Datarate: SF12")); break;
case DR_SF11: debugPrintLn(F("Datarate: SF11")); break;
case DR_SF10: debugPrintLn(F("Datarate: SF10")); break;
case DR_SF9: debugPrintLn(F("Datarate: SF9")); break;
case DR_SF8: debugPrintLn(F("Datarate: SF8")); break;
case DR_SF7: debugPrintLn(F("Datarate: SF7")); break;
case DR_SF7B: debugPrintLn(F("Datarate: SF7B")); break;
case DR_FSK: debugPrintLn(F("Datarate: FSK")); break;
default: debugPrint(F("Datarate Unknown Value: ")); debugPrintLn(LMIC.datarate); break;
}
}
void updateEnvParameters()
{
delay(1000);
BME280::TempUnit tempUnit(BME280::TempUnit_Celcius);
BME280::PresUnit presUnit(BME280::PresUnit_hPa);
bme.read(pressure, temp, humidity, tempUnit, presUnit);
}
void setColor(bool redValue, bool greenValue, bool blueValue) {
digitalWrite(redLed, !redValue);
digitalWrite(greenLed, !greenValue);
digitalWrite(blueLed, !blueValue);
}
void onEvent (ev_t ev) {
Serial.print(os_getTime());
Serial.print(": ");
switch (ev) {
case EV_SCAN_TIMEOUT:
//debugPrintLn(F("EV_SCAN_TIMEOUT"));
break;
case EV_BEACON_FOUND:
//debugPrintLn(F("EV_BEACON_FOUND"));
break;
case EV_BEACON_MISSED:
//debugPrintLn(F("EV_BEACON_MISSED"));
break;
case EV_BEACON_TRACKED:
//debugPrintLn(F("EV_BEACON_TRACKED"));
break;
case EV_JOINING:
debugPrintLn(F("EV_JOINING"));
printDataRate();
setColor(1, 0, 0);
break;
case EV_JOINED:
debugPrintLn(F("EV_JOINED"));
printDataRate();
setColor(1, 1, 0);
break;
case EV_RFU1:
debugPrintLn(F("EV_RFU1"));
break;
case EV_JOIN_FAILED:
debugPrintLn(F("EV_JOIN_FAILED"));
break;
case EV_REJOIN_FAILED:
debugPrintLn(F("EV_REJOIN_FAILED"));
lmicStartup(); //Added this as it seems LMIC just stops if it ends up here
break;
case EV_TXCOMPLETE:
debugPrintLn(F("EV_TXCOMPLETE (includes waiting for RX windows)"));
if (LMIC.txrxFlags & TXRX_ACK)
debugPrintLn(F("Received ack"));
if (LMIC.dataLen) {
debugPrintLn(F("Received "));
debugPrintLn(LMIC.dataLen);
debugPrintLn(F(" bytes of payload"));
}
setColor(0, 1, 0);
delay(1000);
setColor(1, 1, 0);
// Schedule next transmission
os_setTimedCallback(&sendjob, os_getTime() + sec2osticks(TX_INTERVAL), do_send);
break;
case EV_LOST_TSYNC:
debugPrintLn(F("EV_LOST_TSYNC"));
break;
case EV_RESET:
debugPrintLn(F("EV_RESET"));
break;
case EV_RXCOMPLETE:
// data received in ping slot
debugPrintLn(F("EV_RXCOMPLETE"));
break;
case EV_LINK_DEAD:
debugPrintLn(F("EV_LINK_DEAD"));
break;
case EV_LINK_ALIVE:
debugPrintLn(F("EV_LINK_ALIVE"));
break;
default:
debugPrintLn(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) {
debugPrintLn(F("OP_TXRXPEND, not sending"));
} else {
// Prepare upstream data transmission at the next possible time.
// Here the sensor information should be retrieved
// Pressure: 300...1100 hPa
// Temperature: -40…85°C
updateEnvParameters();
updateEnvParameters();
int batt = (int)(readVcc() / 100); // readVCC returns mVolt need just 100mVolt steps
byte batvalue = (byte)batt; // no problem putting it into a int.
#ifdef SHOW_DEBUGINFO
debugPrint(F("T="));
debugPrintLn(temp);
debugPrint(F("P="));
debugPrintLn(pressure);
debugPrint(F("H="));
debugPrintLn(humidity);
debugPrint(F("B="));
debugPrintLn(batt);
debugPrint(F("BV="));
debugPrintLn(batvalue);
#endif
int t = (int)((temp + 40.0) * 10.0);
// t = t + 40; => t [-40..+85] => [0..125] => t = t * 10; => t [0..125] => [0..1250]
int p = (int)(pressure); // p [300..1100]
int h = (int)(humidity);
unsigned char mydata[6];
mydata[0] = batvalue;
mydata[1] = h & 0xFF;
mydata[2] = t >> 8;
mydata[3] = t & 0xFF;
mydata[4] = p >> 8;
mydata[5] = p & 0xFF;
printDataRate();
LMIC_setTxData2(1, mydata, sizeof(mydata), 0);
debugPrintLn(F("PQ")); //Packet queued
}
// Next TX is scheduled after TX_COMPLETE event.
}
void lmicStartup() {
// Reset the MAC state. Session and pending data transfers will be discarded.
LMIC_reset();
#if defined(CFG_eu868)
// Set up the channels used by the Things Network, which corresponds
// to the defaults of most gateways. Without this, only three base
// channels from the LoRaWAN specification are used, which certainly
// works, so it is good for debugging, but can overload those
// frequencies, so be sure to configure the full frequency range of
// your network here (unless your network autoconfigures them).
// Setting up channels should happen after LMIC_setSession, as that
// configures the minimal channel set.
// NA-US channels 0-71 are configured automatically
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
// TTN defines an additional channel at 869.525Mhz using SF9 for class B
// devices' ping slots. LMIC does not have an easy way to define set this
// frequency and support for class B is spotty and untested, so this
// frequency is not configured here.
#elif defined(CFG_us915)
// NA-US channels 0-71 are configured automatically
// but only one group of 8 should (a subband) should be active
// TTN recommends the second sub band, 1 in a zero based count.
// https://github.com/TheThingsNetwork/gateway-conf/blob/master/US-global_conf.json
LMIC_selectSubBand(1);
#endif
LMIC_setClockError(MAX_CLOCK_ERROR * 1 / 100);
LMIC_setLinkCheckMode(1);
LMIC_setAdrMode(1);
// Start job (sending automatically starts OTAA too)
do_send(&sendjob);
}
void setup() {
Serial.begin(9600);
debugPrintLn(F("Starting"));
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(blueLed, OUTPUT);
setColor(1, 0, 1);
delay(1000);
Wire.begin();
if (!bme.begin())
{
debugPrintLn(F("No valid bme280 sensor!"));
}
// LMIC init
os_init();
lmicStartup();
}
void loop() {
os_runloop_once();
}