1

Dependencies:   X_NUCLEO_IKS01A1 LoRaWAN-lib SX1276Lib mbed

Files at this revision

API Documentation at this revision

Comitter:
ubhat
Date:
Wed Dec 06 07:35:47 2017 +0000
Parent:
6:711a4e008afa
Child:
8:88e8a1c7b88a
Commit message:
Add delay to prep devices in field

Changed in this revision

app/Common.h Show annotated file Show diff for this revision Revisions of this file
app/Config.h Show annotated file Show diff for this revision Revisions of this file
app/LoRaDeviceStateProc.cpp Show annotated file Show diff for this revision Revisions of this file
app/LoRaEventProc.cpp Show annotated file Show diff for this revision Revisions of this file
app/LoRaEventProc.h Show annotated file Show diff for this revision Revisions of this file
app/main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/app/Common.h	Tue Nov 28 08:20:09 2017 +0000
+++ b/app/Common.h	Wed Dec 06 07:35:47 2017 +0000
@@ -77,4 +77,14 @@
 
 extern uint16_t DutyCycleIndx;
 
+/*!
+ * Indicates if the device is ready for application.
+ */
+extern bool IsDeviceReady;
+
+/*!
+ * Timer to handle the application start delay
+ */
+extern TimerEvent_t TxDelayTimer;
+
 #endif // __COMMON_H__
\ No newline at end of file
--- a/app/Config.h	Tue Nov 28 08:20:09 2017 +0000
+++ b/app/Config.h	Wed Dec 06 07:35:47 2017 +0000
@@ -35,45 +35,62 @@
 
 #endif
 
+#define USEC                                        1000000 // 1sec = 10^6 usec
+
 /*!
  * Join requests trials periodicity. It is the time between RX1/RX2 and next TX
  */
-#define OVER_THE_AIR_ACTIVATION_DUTYCYCLE           5000000  // value in us
+#define OVER_THE_AIR_ACTIVATION_DUTYCYCLE           (5 * USEC)  // value in us
 
 /*!
  * Defines the time in Hours after which dutycycle should change
  */
-#define APP_TX_DUTYCYCLE_HOUR                        12
+#define APP_DELAY_HOUR                              3       // value in hour
 
 /*!
  * Defines the application data transmission periodicity. It is the time between RX1/RX2 and next TX
  */
-#define APP_TX_DUTYCYCLE                            36000000  // value in us
+#define APP_TX_DUTYCYCLE                            (75 * USEC)   // value in us
 
 /*!
  * Defines the randomness in the dutycycle. 
  */
-#define APP_TX_DUTYCYCLE_RND                        30000000
+#define APP_TX_DUTYCYCLE_RND                        (15 * USEC)   // value in us
 
 /*!
- * Defines the application data transmission periodicity after APP_TX_DUTYCYCLE_HOUR hours. It is the time between RX1/RX2 and next TX
+ * Defines the time in Hours after which dutycycle should change
  */
-#define APP_TX_DUTYCYCLE_1                          18000000  // value in us
+#define APP_TX_DUTYCYCLE_HOUR                       12      // value in hour
+
+/*!
+ * Defines the application data transmission periodicity. It is the time between RX1/RX2 and next TX
+ */
+#define APP_TX_DUTYCYCLE_1                          36 * USEC  // value in us
 
 /*!
  * Defines the randomness in the dutycycle. 
  */
-#define APP_TX_DUTYCYCLE_RND_1                      12000000
+#define APP_TX_DUTYCYCLE_RND_1                      30 * USEC   // value in us
+
+/*!
+ * Defines the application data transmission periodicity after APP_TX_DUTYCYCLE_HOUR hours. It is the time between RX1/RX2 and next TX
+ */
+#define APP_TX_DUTYCYCLE_2                          18 * USEC   // value in us
+
+/*!
+ * Defines the randomness in the dutycycle. 
+ */
+#define APP_TX_DUTYCYCLE_RND_2                      12 * USEC   // value in us
 
 /*!
  * Defines the application data transmission periodicity after ( 2 x APP_TX_DUTYCYCLE_HOUR ) hours. It is the time between RX1/RX2 and next TX
  */
-#define APP_TX_DUTYCYCLE_2                          12000000  // value in us
+#define APP_TX_DUTYCYCLE_3                          12 * USEC   // value in us
 
 /*!
  * Defines the randomness in the dutycycle. 
  */
-#define APP_TX_DUTYCYCLE_RND_2                       6000000
+#define APP_TX_DUTYCYCLE_RND_3                      6 * USEC    // value in us
 
 
 /*!
--- a/app/LoRaDeviceStateProc.cpp	Tue Nov 28 08:20:09 2017 +0000
+++ b/app/LoRaDeviceStateProc.cpp	Wed Dec 06 07:35:47 2017 +0000
@@ -32,7 +32,7 @@
 
 uint16_t ChannelMaskTemp[6] = {0};
 
-static bool DutyCycleFlag = false;
+static bool DutyCycleFlag = true;
 
 /*!
  * \brief Function executed on TxNextPacket Timeout event
@@ -59,6 +59,10 @@
                 IsDutyCycleUpdate = true;
 
                 DutyCycleFlag = true;
+    
+                mibReq.Type = MIB_ADR;  
+                mibReq.Param.AdrEnable = LORAWAN_ADR_ON;
+                LoRaMacMibSetRequestConfirm( &mibReq );
             }
         }
         else
@@ -88,6 +92,26 @@
     }
 }
 
+static void OnDelayEvent( void )
+{
+    TimerStop( &TxDelayTimer );
+
+    DutyCycleCntr++;
+
+    if( DutyCycleCntr >= (2 * APP_DELAY_HOUR ) )
+    {
+        IsDeviceReady = true;
+
+        DutyCycleFlag = false;
+
+        DutyCycleCntr = 0;
+    }
+    else
+    {
+        InitDelayTimer( );
+    }
+}
+
 void DeviceInit( void )
 {
     LoRaPrimitives.MacMcpsConfirm = McpsConfirm;
@@ -100,8 +124,17 @@
 
     TimerInit( &TxDutyCycleTimer, OnDutyCycleEvent );
 
+    TimerInit( &TxDelayTimer, OnDelayEvent );
+
     LoRaMibReq.Type = MIB_ADR;
-    LoRaMibReq.Param.AdrEnable = LORAWAN_ADR_ON;
+    if( IsDeviceReady == true )
+    {
+        LoRaMibReq.Param.AdrEnable = LORAWAN_ADR_ON;
+    }
+    else
+    {
+        LoRaMibReq.Param.AdrEnable = 0;
+    }
     LoRaMacMibSetRequestConfirm( &LoRaMibReq );
 
     LoRaMibReq.Type = MIB_PUBLIC_NETWORK;
@@ -253,20 +286,6 @@
  */
 void PrepareTxFrame( uint8_t port )
 {
-    MibRequestConfirm_t mibReq;
-
-    if( BoardGetBatteryLevel( ) < LOW_BAT_THRESHOLD )
-    {
-        mibReq.Type = MIB_CHANNELS_TX_POWER;
-        LoRaMacMibGetRequestConfirm( &mibReq );
-        // TX_POWER_30_DBM = 0, TX_POWER_28_DBM = 1, ..., TX_POWER_20_DBM = 5, ..., TX_POWER_10_DBM = 10
-        // The if condition is then "less than" to check if the power is greater than 20 dBm
-        if( mibReq.Param.ChannelsTxPower < TX_POWER_20_DBM )
-        {
-            mibReq.Param.ChannelsTxPower = TX_POWER_20_DBM;
-            LoRaMacMibSetRequestConfirm( &mibReq );
-        }
-    }
     
     if( port == 224 )
     {
--- a/app/LoRaEventProc.cpp	Tue Nov 28 08:20:09 2017 +0000
+++ b/app/LoRaEventProc.cpp	Wed Dec 06 07:35:47 2017 +0000
@@ -46,17 +46,24 @@
             // Point the pointer to position index of Tx Buffer
             LoRaApp.ApplicationPtrPos( ptrIndex );
 
-            if( DutyCycleIndx == 0 )
-            {
-                tmp = 0xAA;     
-            }
-            else if( DutyCycleIndx == 1 )
+            if( IsDeviceReady == true )
             {
-                tmp = 0xBB;
+                if( DutyCycleIndx == 0 )
+                {
+                    tmp = 0xAA;     
+                }
+                else if( DutyCycleIndx == 1 )
+                {
+                    tmp = 0xBB;
+                }
+                else if( DutyCycleIndx > 1 )
+                {
+                    tmp = 0xCC;
+                }
             }
-            else if( DutyCycleIndx > 1 )
+            else
             {
-                tmp = 0xCC;
+                tmp = 0xFF;
             }
             tmpLength = 1;
 
@@ -335,32 +342,37 @@
 }
 
 void InitDutyCycleTimer( void )
-{
+{       
     if( DutyCycleIndx == 0 )
     {
-        printf("DutyCyle = A\r\n");
-        AppTxDutyCycle = APP_TX_DUTYCYCLE;
-        AppTxDutyCycleRnd = APP_TX_DUTYCYCLE_RND;       
+        AppTxDutyCycle = APP_TX_DUTYCYCLE_1;
+        AppTxDutyCycleRnd = APP_TX_DUTYCYCLE_RND_1;       
     }
     else if( DutyCycleIndx == 1 )
     {
-        printf("DutyCyle = B\r\n");
-        AppTxDutyCycle = APP_TX_DUTYCYCLE_1;
-        AppTxDutyCycleRnd = APP_TX_DUTYCYCLE_RND_1;     
+        AppTxDutyCycle = APP_TX_DUTYCYCLE_2;
+        AppTxDutyCycleRnd = APP_TX_DUTYCYCLE_RND_2;     
     }
     else if( DutyCycleIndx > 1 )
     {
-        printf("DutyCyle = C\r\n");
-        AppTxDutyCycle = APP_TX_DUTYCYCLE_2;
-        AppTxDutyCycleRnd = APP_TX_DUTYCYCLE_RND_2;     
+        AppTxDutyCycle = APP_TX_DUTYCYCLE_3;
+        AppTxDutyCycleRnd = APP_TX_DUTYCYCLE_RND_3;     
     }
     
-    // Schedule next packet transmission
-    TxDutyCycleTime =  1800000000;
+    // 30 min timer
+    TxDutyCycleTime =  1800 * USEC;
     TimerSetValue( &TxDutyCycleTimer, TxDutyCycleTime );
     TimerStart( &TxDutyCycleTimer );
 }
 
+void InitDelayTimer( void )
+{   
+    // 30 min timer
+    TxDutyCycleTime =  1800 * USEC;
+    TimerSetValue( &TxDelayTimer, TxDutyCycleTime );
+    TimerStart( &TxDelayTimer );
+}
+
 /*!
  * \brief   What to do during JOIN process ? blink/toggle LED etc.
  */
--- a/app/LoRaEventProc.h	Tue Nov 28 08:20:09 2017 +0000
+++ b/app/LoRaEventProc.h	Wed Dec 06 07:35:47 2017 +0000
@@ -50,4 +50,6 @@
 
 void InitDutyCycleTimer( void );
 
+void InitDelayTimer( void );
+
 #endif // __LORA_EVENT_PROC_H__
--- a/app/main.cpp	Tue Nov 28 08:20:09 2017 +0000
+++ b/app/main.cpp	Wed Dec 06 07:35:47 2017 +0000
@@ -128,6 +128,16 @@
  */
 bool IsRxUpdate = false;
 
+/*!
+ * Indicates if the device is ready for application.
+ */
+bool IsDeviceReady = false;
+
+/*!
+ * Timer to handle the application start delay
+ */
+TimerEvent_t TxDelayTimer;
+
 
 /**
  * Main application entry point.
@@ -190,12 +200,18 @@
                 // Initialize MAC, MAC services, Primitives
                 DeviceInit( );
 
+                if( IsDeviceReady == false )
+                {
+                    // Initialize delay until device ready
+                    InitDelayTimer( );
+                }
+
                 // Change Device state
                 DeviceState = DEVICE_STATE_JOIN;
                 break;
             }
             case DEVICE_STATE_JOIN:
-            {
+            {               
 #if( OVER_THE_AIR_ACTIVATION != 0 )                // OTA 
 
                 // Generate DevEUI if not defined by User