Skip to content
Permalink
Browse files

added downlink data handles example

  • Loading branch information
Heltec-Aaron-Lee committed Dec 26, 2019
1 parent 659c847 commit f69ba9020ed87875377b32ea2016474a66f4cfea
@@ -0,0 +1,137 @@
#include "LoRaWan_APP.h"
#include "Arduino.h"

/*
* set LoraWan_RGB to Active,the RGB active in loraWan
* RGB red means sending;
* RGB purple means joined done;
* RGB blue means RxWindow1;
* RGB yellow means RxWindow2;
* RGB green means received done;
*/

/*LoraWan Class*/
DeviceClass_t CLASS=LORAWAN_CLASS;
/*OTAA or ABP*/
bool OVER_THE_AIR_ACTIVATION = LORAWAN_NETMODE;
/*ADR enable*/
bool LORAWAN_ADR_ON = LORAWAN_ADR;
/* set LORAWAN_Net_Reserve ON, the node could save the network info to flash, when node reset not need to join again */
bool KeepNet = LORAWAN_Net_Reserve;
/*LoraWan REGION*/
LoRaMacRegion_t REGION = ACTIVE_REGION;

/* Indicates if the node is sending confirmed or unconfirmed messages */
bool IsTxConfirmed = true;
/*!
* Number of trials to transmit the frame, if the LoRaMAC layer did not
* receive an acknowledgment. The MAC performs a datarate adaptation,
* according to the LoRaWAN Specification V1.0.2, chapter 18.4, according
* to the following table:
*
* Transmission nb | Data Rate
* ----------------|-----------
* 1 (first) | DR
* 2 | DR
* 3 | max(DR-1,0)
* 4 | max(DR-1,0)
* 5 | max(DR-2,0)
* 6 | max(DR-2,0)
* 7 | max(DR-3,0)
* 8 | max(DR-3,0)
*
* Note, that if NbTrials is set to 1 or 2, the MAC will not decrease
* the datarate, in case the LoRaMAC layer did not receive an acknowledgment
*/
uint8_t ConfirmedNbTrials = 8;

/* Application port */
uint8_t AppPort = 2;

/*the application data transmission duty cycle. value in [ms].*/
uint32_t APP_TX_DUTYCYCLE = 15000;

/* Prepares the payload of the frame */
static void PrepareTxFrame( uint8_t port )
{
AppDataSize = 4;//AppDataSize max value is 64
AppData[0] = 0x00;
AppData[1] = 0x01;
AppData[2] = 0x02;
AppData[3] = 0x03;
}

//downlink data handle function example
void DownLinkDataHandle(McpsIndication_t *mcpsIndication)
{
Serial.printf("+REV DATA:%s,RXSIZE %d,PORT %d\r\n",mcpsIndication->RxSlot?"RXWIN2":"RXWIN1",mcpsIndication->BufferSize,mcpsIndication->Port);
Serial.print("+REV DATA:");
for(uint8_t i=0;i<mcpsIndication->BufferSize;i++)
{
Serial.printf("%02X",mcpsIndication->Buffer[i]);
}
Serial.println();
uint32_t color=mcpsIndication->Buffer[0]<<16|mcpsIndication->Buffer[1]<<8|mcpsIndication->Buffer[2];
#if(LoraWan_RGB==1)
RGB_ON(color,5000);
RGB_OFF();
#endif
}

void setup() {
BoardInitMcu();
Serial.begin(115200);
#if(AT_SUPPORT)
Enable_AT();
#endif
DeviceState = DEVICE_STATE_INIT;
LoRaWAN.Ifskipjoin();
}

void loop()
{
switch( DeviceState )
{
case DEVICE_STATE_INIT:
{
#if(AT_SUPPORT)
getDevParam();
#endif
printDevParam();
Serial.printf("LoRaWan Class%X start! \r\n",CLASS+10);
LoRaWAN.Init(CLASS,REGION);
DeviceState = DEVICE_STATE_JOIN;
break;
}
case DEVICE_STATE_JOIN:
{
LoRaWAN.Join();
break;
}
case DEVICE_STATE_SEND:
{
PrepareTxFrame( AppPort );
LoRaWAN.Send();
DeviceState = DEVICE_STATE_CYCLE;
break;
}
case DEVICE_STATE_CYCLE:
{
// Schedule next packet transmission
TxDutyCycleTime = APP_TX_DUTYCYCLE + randr( 0, APP_TX_DUTYCYCLE_RND );
LoRaWAN.Cycle(TxDutyCycleTime);
DeviceState = DEVICE_STATE_SLEEP;
break;
}
case DEVICE_STATE_SLEEP:
{
LoRaWAN.Sleep();
break;
}
default:
{
DeviceState = DEVICE_STATE_INIT;
break;
}
}
}
@@ -227,6 +227,18 @@ uint16_t GetBatteryVoltage(void)
return volt;
}


void __attribute__((weak)) DownLinkDataHandle(McpsIndication_t *mcpsIndication)
{
printf("+REV DATA:%s,RXSIZE %d,PORT %d\r\n",mcpsIndication->RxSlot?"RXWIN2":"RXWIN1",mcpsIndication->BufferSize,mcpsIndication->Port);
printf("+REV DATA:");
for(uint8_t i=0;i<mcpsIndication->BufferSize;i++)
{
printf("%02X",mcpsIndication->Buffer[i]);
}
printf("\r\n");
}

/*!
* \brief MCPS-Indication event function
*
@@ -287,19 +299,7 @@ static void McpsIndication( McpsIndication_t *mcpsIndication )
// Check RxSlot
if( mcpsIndication->RxData == true )
{
//memset(temp,0,200);
//memset(temp1,0,200);

//HexToString((const char *)(mcpsIndication->Buffer),mcpsIndication->BufferSize,(char *)(temp1));
//temp1[mcpsIndication->BufferSize * 2]='\0';

printf("+REV DATA:%s,RXSIZE %d,PORT %d\r\n",mcpsIndication->RxSlot?"RXWIN2":"RXWIN1",mcpsIndication->BufferSize,mcpsIndication->Port);
printf("+REV DATA:");
for(uint8_t i=0;i<mcpsIndication->BufferSize;i++)
{
printf("%02X",mcpsIndication->Buffer[i]);
}
printf("\r\n");
DownLinkDataHandle(mcpsIndication);
}
}

@@ -61,6 +61,7 @@ extern "C" void RGB_ON(uint32_t color,uint32_t time);
extern "C" void RGB_OFF(void);
extern "C" uint16_t GetBatteryVoltage(void);
extern "C" bool AT_user_check(char * cmd, char * content);
extern "C" void DownLinkDataHandle(McpsIndication_t *mcpsIndication);



0 comments on commit f69ba90

Please sign in to comment.
You can’t perform that action at this time.