LORIOT firmware for the mbed Shield

Dependencies:   LMiC2 SX1276Lib mbed

Fork of LoRaWAN-lmic-app by Semtech

Revision:
6:e54c37d101f3
Parent:
5:1b2fcc2582e8
--- a/main.cpp	Thu Nov 26 17:20:53 2015 +0000
+++ b/main.cpp	Tue Apr 12 11:53:31 2016 +0000
@@ -17,6 +17,16 @@
 #include "lmic.h"
 #include "debug.h"
 
+
+/**********************
+ * LORIOT SETTINGS
+ **********************/
+static const uint32_t LORIOT_DEV_ADDR  = 0x010E0845;
+static const char LORIOT_NWK_S_KEY[]   = "88E5D1F343E83CE2ED96B2B6FD8B7893";
+static const char LORIOT_APP_S_KEY[]   = "7453B7BF1C43AFC4939D96188BD88601";
+
+
+
 /*!
  * When set to 1 the application uses the Over-the-Air activation procedure
  * When set to 0 the application uses the Personalization activation procedure
@@ -30,11 +40,6 @@
  */
 #define LORAWAN_NET_ID                              ( uint32_t )0x00000000
 
-/*!
- * Defines the device address when using personalization activation procedure
- */
-#define LORAWAN_DEV_ADDR                            ( uint32_t )0x12345678
-
 #endif
 
 /*!
@@ -62,10 +67,10 @@
  * User application data buffer size
  */
 #if ( LORAWAN_CONFIRMED_MSG_ON == 1 )
-#define LORAWAN_APP_DATA_SIZE                       6
+#define LORAWAN_APP_DATA_SIZE                       8
 
 #else
-#define LORAWAN_APP_DATA_SIZE                       1
+#define LORAWAN_APP_DATA_SIZE                       3
 
 #endif
 
@@ -93,21 +98,10 @@
 };
 
 #if( OVER_THE_AIR_ACTIVATION == 0 )
-// network session key
-static uint8_t NwkSKey[] = 
-{ 
-    0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6,
-    0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C
-};
+static uint8_t NwkSKey[16];
+static uint8_t ArtSKey[16];
+#endif
 
-// application session key
-static uint8_t ArtSKey[] = 
-{ 
-    0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6,
-    0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C
-};
-
-#endif
 
 // LEDs and Frame jobs
 osjob_t rxLedJob;
@@ -168,16 +162,26 @@
     debug_val("LED1 = ", 0 );
 }
 
+static InterruptIn btn3(P0_19);
+
+static int8_t clicks = 0; // overflows straight away when we register the fall() on the button
+static int8_t counter = 0;
 static void prepareTxFrame( void )
 {
-    LMIC.frame[0] = AppLedStateOn;
+    int16_t temp = (rand() % 500) + 2000;
+    
+    // if you change this also change LORAWAN_APP_DATA_SIZE !!
+    LMIC.frame[0] = clicks;
+    LMIC.frame[1] = (temp >> 8) & 0xff;
+    LMIC.frame[2] = temp & 0xff;
 #if ( LORAWAN_CONFIRMED_MSG_ON == 1 )
-    LMIC.frame[1] = LMIC.seqnoDn >> 8;
-    LMIC.frame[2] = LMIC.seqnoDn;
-    LMIC.frame[3] = LMIC.rssi >> 8;
-    LMIC.frame[4] = LMIC.rssi;
-    LMIC.frame[5] = LMIC.snr;
+    LMIC.frame[3] = LMIC.seqnoDn >> 8;
+    LMIC.frame[4] = LMIC.seqnoDn;
+    LMIC.frame[5] = LMIC.rssi >> 8;
+    LMIC.frame[6] = LMIC.rssi;
+    LMIC.frame[7] = LMIC.snr;
 #endif    
+    printf("Sent frame %d\r\n", ++counter);
 }
 
 void processRxFrame( void )
@@ -214,7 +218,7 @@
     LMIC_reset( );
     LMIC_setAdrMode( LORAWAN_ADR_ON );
 #if defined(CFG_eu868)
-    LMIC_setDrTxpow( DR_SF12, 14 );
+    LMIC_setDrTxpow( DR_SF7, 14 );
 #elif defined(CFG_us915)    
     LMIC_setDrTxpow( DR_SF10, 14 );
 #endif
@@ -223,14 +227,42 @@
 #if( OVER_THE_AIR_ACTIVATION != 0 )
     LMIC_startJoining( );
 #else
-    LMIC_setSession( LORAWAN_NET_ID, LORAWAN_DEV_ADDR, NwkSKey, ArtSKey );
+    // parse devkey
+    static int ni;
+    for (ni = 0; ni < 16; ni++)
+    {
+        char *non_numeric_ptr;
+        const char hex[] = { '0', 'x', LORIOT_NWK_S_KEY[ni * 2], LORIOT_NWK_S_KEY[ni * 2 + 1] };
+        NwkSKey[ni] = strtoul(hex, &non_numeric_ptr, 16);
+        free(non_numeric_ptr);
+    }
+
+    // parse appkey
+    static int ai;
+    for (ai = 0; ai < 16; ai++)
+    {
+        char *non_numeric_ptr;
+        const char hex[] = { '0', 'x', LORIOT_APP_S_KEY[ai * 2], LORIOT_APP_S_KEY[ai * 2 + 1] };
+        ArtSKey[ai] = strtoul(hex, &non_numeric_ptr, 16);
+        free(non_numeric_ptr);
+    }
+    
+    LMIC_setSession( LORAWAN_NET_ID, LORIOT_DEV_ADDR, NwkSKey, ArtSKey );
     onSendFrame( NULL );
 #endif
     // init done - onEvent( ) callback will be invoked...
 }
 
+void btn3_click() {
+    clicks++;
+    printf("btn3 click count is %d\r\n", clicks);
+    onSendFrame(NULL);
+}
+
 int main( void )
 {
+    btn3.fall(&btn3_click);
+    
     osjob_t initjob;
 
     // initialize runtime env
@@ -277,14 +309,4 @@
     default:
         break;
     }
-    if( txOn == true )
-    {
-        //Sends frame every APP_TX_DUTYCYCLE +/- APP_TX_DUTYCYCLE_RND random time (if not duty cycle limited)
-        os_setTimedCallback( &sendFrameJob,
-                             os_getTime( ) + ms2osticks( APP_TX_DUTYCYCLE + randr( -APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND ) ),
-                             onSendFrame );
-        
-        ////Sends frame as soon as possible (duty cylce limitations)
-        //onSendFrame( NULL );
-    }
 }