Finally, something that works
#include "LoRaWanMinimal_APP.h"
#include "Arduino.h"
#define LORA_BANDWIDTH 0
#define LORA_SPREADING_FACTOR 9
#define LORA_CODINGRATE 1
#define LORA_PREAMBLE_LENGTH 8
#define LORA_SYMBOL_TIMEOUT 0
#define LORA_FIX_LENGTH_PAYLOAD_ON false
#define LORA_IQ_INVERSION_ON false
//////////////////////////////////////////////////////////////////////////////////////////////////////
//Set these OTAA parameters to match your app/node in TTN
uint8_t devEui[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
uint8_t appEui[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
uint8_t appKey[] = { 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x66, 0x01 };
uint16_t userChannelsMask[6]={ 0x00FF,0x0000,0x0000,0x0000,0x0000,0x0000 };
//////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////
// INIT SLEEP MODE
TimerEvent_t sleepTimer;
bool sleepTimerExpired;
static void wakeUp()
{
sleepTimerExpired=true;
}
static void lowPowerSleep(uint32_t sleeptime)
{
sleepTimerExpired=false;
TimerInit( &sleepTimer, &wakeUp );
TimerSetValue( &sleepTimer, sleeptime );
TimerStart( &sleepTimer );
//Low power handler also gets interrupted by other timers
//So wait until our timer had expired
while (!sleepTimerExpired) lowPowerHandler();
TimerStop( &sleepTimer );
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////
// LOOP STUFF
typedef enum
{
LOWPOWER,
RX,
TX
} States_t;
States_t state;
uint8_t counter = 0;
//////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
Serial.begin(115200);
LoRaWAN.begin(LORAWAN_CLASS, ACTIVE_REGION);
Radio.SetRxConfig(
MODEM_LORA,LORA_BANDWIDTH, LORA_SPREADING_FACTOR, LORA_CODINGRATE, 0,
LORA_PREAMBLE_LENGTH, LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON,
0, true, 0, 0, LORA_IQ_INVERSION_ON, true);
LoRaWAN.setAdaptiveDR(true);
while (1) {
Serial.print("Joining... ");
LoRaWAN.joinOTAA(appEui, appKey, devEui);
if (!LoRaWAN.isJoined()) {
Serial.println("JOIN FAILED! Sleeping for 30 seconds");
lowPowerSleep(30000);
} else {
Serial.println("JOINED");
LoRaWAN.send(1, 0, 1, true);
break;
}
}
}
void loop()
{
switch(state)
{
case TX:
Serial.println("TX");
counter++;
LoRaWAN.send(1, &counter, 1, false);
state=RX;
break;
case RX:
Serial.println("RX");
lowPowerHandler();
break;
default:
Serial.printf("DEF");
state=RX;
break;
}
}
void downLinkDataHandle(McpsIndication_t *mcpsIndication)
{
Serial.printf("Received downlink: %s, RXSIZE %d, PORT %d, DATA: ",mcpsIndication->RxSlot?"RXWIN2":"RXWIN1",mcpsIndication->BufferSize,mcpsIndication->Port);
for(uint8_t i=0;i<mcpsIndication->BufferSize;i++) {
Serial.printf("%02X",mcpsIndication->Buffer[i]);
}
Serial.println();
state = TX;
}