application layer with: button, LED, pot, tempSense

Dependencies:   mbed SX1272Lib

Fork of LoRaWAN-demo-72-bootcamp by Semtech

Use with sx1272 shield with grove peripherals connected:

D8 D9: ButtonRX TXA3 A4: TempSense
D6 D7:SCL SDA : LEDA1 A2: Pot

Button

Sends to different payloads: short press (under 1 sec)
long press: held down > 1 sec.

serial console keys

115200bps, 8N1
Enter key not used
Keys '0' to '3': cayenne channel number
'0': pot (rotary sensor)
'1': temperature
' 2': digital out
'3': analog out

Revision:
5:fa113b25f612
Parent:
4:de084e23d884
Child:
7:ceb4063e6863
--- a/app/main.cpp	Mon Mar 14 09:19:11 2016 +0000
+++ b/app/main.cpp	Fri May 13 15:51:40 2016 +0000
@@ -24,7 +24,7 @@
 /*!
  * Join requests trials duty cycle.
  */
-#define OVER_THE_AIR_ACTIVATION_DUTYCYCLE           10000000  // 10 [s] value in us
+#define OVER_THE_AIR_ACTIVATION_DUTYCYCLE           10000000 // 10 [s] value in us
 
 /*!
  * Defines the application data transmission duty cycle. 5s, value in [us].
@@ -38,7 +38,7 @@
 #define APP_TX_DUTYCYCLE_RND                        1000000
 
 /*!
- * Default mote datarate
+ * Default datarate
  */
 #define LORAWAN_DEFAULT_DATARATE                    DR_5
 
@@ -65,6 +65,20 @@
  */
 #define LORAWAN_DUTYCYCLE_ON                        false
 
+#define USE_SEMTECH_DEFAULT_CHANNEL_LINEUP          1
+
+#if( USE_SEMTECH_DEFAULT_CHANNEL_LINEUP == 1 ) 
+
+#define LC4                { 867100000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
+#define LC5                { 867300000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
+#define LC6                { 867500000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
+#define LC7                { 867700000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
+#define LC8                { 867900000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
+#define LC9                { 868800000, { ( ( DR_7 << 4 ) | DR_7 ) }, 2 }
+#define LC10               { 868300000, { ( ( DR_6 << 4 ) | DR_6 ) }, 1 }
+
+#endif
+
 #endif
 
 /*!
@@ -169,24 +183,24 @@
  * 0: automatic (LED goes brigther as the light decrease, 
  * 1: manual (The LED is controlled by the user)
  */
-static uint8_t LightMode = 0;  // 0:automatic, 1:manual
+static uint8_t LightMode = 0;  // 0: automatic, 1: manual
 
 /*!
  * Ticker to create a PWM for the buzzer
  */
-Ticker BuzTimer;
+Ticker BuzzerTimer;
 
 
 /*!
  * Constructor for Buzzer
  */
-DigitalOut buzzer(A3);
+DigitalOut Buzzer( A3 );
 
 /*!
  * Constructor for the 3-color LED
  */
 #define NUM_LED 3
-ChainableLED color_led(D6, D7, NUM_LED);
+ChainableLED ColorLed( D6, D7, NUM_LED );
 
 /*!
  * Constructor for Light Sensor
@@ -196,7 +210,7 @@
 /*!
  * Constructor for 4 Digit 7 semgent display
  */
-DigitDisplay display(D8, D9);
+DigitDisplay Display( D8, D9 );
 
 /*!
  * Device states
@@ -320,18 +334,16 @@
  */
 static void PrepareTxFrame( uint8_t port )
 {
-    uint32_t tempValue;
-    
     switch( port )
     {
     case 10:
         {  
-            tempValue = ( uint32_t )( LightValue * 1000000.0 );
+            uint32_t tempValue = ( uint32_t )( LightValue * 1000000.0 );
             AppData[0] = LightMode;
             AppData[1] = ( ( tempValue & 0xFF000000 ) >> 24 ) & 0xFF;
             AppData[2] = ( ( tempValue & 0x00FF0000 ) >> 16 ) & 0xFF;
             AppData[3] = ( ( tempValue & 0x0000FF00 ) >> 8 ) & 0xFF;
-            AppData[4] = ( tempValue & 0x000000FF );    
+            AppData[4] = ( tempValue & 0x000000FF );
         }
         break;
     case 15:
@@ -394,7 +406,7 @@
         mcpsReq.Req.Unconfirmed.fBuffer = NULL;
         mcpsReq.Req.Unconfirmed.fBufferSize = 0;
         mcpsReq.Req.Unconfirmed.Datarate = LORAWAN_DEFAULT_DATARATE;
-        
+
         LoRaMacUplinkStatus.Acked = false;
         LoRaMacUplinkStatus.Port = 0;
         LoRaMacUplinkStatus.Buffer = NULL;
@@ -487,23 +499,23 @@
 /*!
  * \brief Function executed on Buzzer Timeout event
  */
-static void OnBuzTimerEvent( void )
+static void OnBuzzerTimerEvent( void )
 {
-    buzzer = 0;
-    BuzTimer.detach( );
+    Buzzer = 0;
+    BuzzerTimer.detach( );
 }
 
 /*!
  * \brief   MCPS-Confirm event function
  *
- * \param   [IN] McpsConfirm - Pointer to the confirm structure,
+ * \param   [IN] mcpsConfirm - Pointer to the confirm structure,
  *               containing confirm attributes.
  */
-static void McpsConfirm( McpsConfirm_t *McpsConfirm )
+static void McpsConfirm( McpsConfirm_t *mcpsConfirm )
 {
-    if( McpsConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK )
+    if( mcpsConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK )
     {
-        switch( McpsConfirm->McpsRequest )
+        switch( mcpsConfirm->McpsRequest )
         {
             case MCPS_UNCONFIRMED:
             {
@@ -516,8 +528,8 @@
                 // Check Datarate
                 // Check TxPower
                 // Check AckReceived
-                // Check NbRetries
-                LoRaMacUplinkStatus.Acked = McpsConfirm->AckReceived;
+                // Check NbTrials
+                LoRaMacUplinkStatus.Acked = mcpsConfirm->AckReceived;
                 break;
             }
             case MCPS_PROPRIETARY:
@@ -527,9 +539,9 @@
             default:
                 break;
         }
-        LoRaMacUplinkStatus.Datarate = McpsConfirm->Datarate;
-        LoRaMacUplinkStatus.UplinkCounter = McpsConfirm->UpLinkCounter;
-    
+        LoRaMacUplinkStatus.Datarate = mcpsConfirm->Datarate;
+        LoRaMacUplinkStatus.UplinkCounter = mcpsConfirm->UpLinkCounter;
+
         UplinkStatusUpdated = true;
     }
     NextTx = true;
@@ -538,17 +550,17 @@
 /*!
  * \brief   MCPS-Indication event function
  *
- * \param   [IN] McpsIndication - Pointer to the indication structure,
+ * \param   [IN] mcpsIndication - Pointer to the indication structure,
  *               containing indication attributes.
  */
-static void McpsIndication( McpsIndication_t *McpsIndication )
+static void McpsIndication( McpsIndication_t *mcpsIndication )
 {
-    if( McpsIndication->Status != LORAMAC_EVENT_INFO_STATUS_OK )
+    if( mcpsIndication->Status != LORAMAC_EVENT_INFO_STATUS_OK )
     {
         return;
     }
 
-    switch( McpsIndication->McpsIndication )
+    switch( mcpsIndication->McpsIndication )
     {
         case MCPS_UNCONFIRMED:
         {
@@ -579,71 +591,67 @@
     // Check Rssi
     // Check Snr
     // Check RxSlot
-    LoRaMacDownlinkStatus.Rssi = McpsIndication->Rssi;
-    if( McpsIndication->Snr & 0x80 ) // The SNR sign bit is 1
+    LoRaMacDownlinkStatus.Rssi = mcpsIndication->Rssi;
+    if( mcpsIndication->Snr & 0x80 ) // The SNR sign bit is 1
     {
         // Invert and divide by 4
-        LoRaMacDownlinkStatus.Snr = ( ( ~McpsIndication->Snr + 1 ) & 0xFF ) >> 2;
+        LoRaMacDownlinkStatus.Snr = ( ( ~mcpsIndication->Snr + 1 ) & 0xFF ) >> 2;
         LoRaMacDownlinkStatus.Snr = -LoRaMacDownlinkStatus.Snr;
     }
     else
     {
         // Divide by 4
-        LoRaMacDownlinkStatus.Snr = ( McpsIndication->Snr & 0xFF ) >> 2;
+        LoRaMacDownlinkStatus.Snr = ( mcpsIndication->Snr & 0xFF ) >> 2;
     }
     LoRaMacDownlinkStatus.DownlinkCounter++;
-    LoRaMacDownlinkStatus.RxData = McpsIndication->RxData;
-    LoRaMacDownlinkStatus.Port = McpsIndication->Port;
-    LoRaMacDownlinkStatus.Buffer = McpsIndication->Buffer;
-    LoRaMacDownlinkStatus.BufferSize = McpsIndication->BufferSize;
+    LoRaMacDownlinkStatus.RxData = mcpsIndication->RxData;
+    LoRaMacDownlinkStatus.Port = mcpsIndication->Port;
+    LoRaMacDownlinkStatus.Buffer = mcpsIndication->Buffer;
+    LoRaMacDownlinkStatus.BufferSize = mcpsIndication->BufferSize;
 
     if( ComplianceTest.Running == true )
     {
         ComplianceTest.DownLinkCounter++;
     }
 
-    if( McpsIndication->RxData == true )
+    if( mcpsIndication->RxData == true )
     {
-        switch( McpsIndication->Port )
+        switch( mcpsIndication->Port )
         {
         case 1: // The application LED can be controlled on port 1 or 2
         case 2:
-            if( McpsIndication->BufferSize == 1 )
+            if( mcpsIndication->BufferSize == 1 )
             {
-                AppLedStateOn = McpsIndication->Buffer[0] & 0x01;
+                AppLedStateOn = mcpsIndication->Buffer[0] & 0x01;
                 Led3StateChanged = true;
             }
             break;
-            
         case 10: 
-            display.write( 0, McpsIndication->Buffer[0] );
-            display.write( 1, McpsIndication->Buffer[1] );
-            display.write( 2, McpsIndication->Buffer[2] );
-            display.write( 3, McpsIndication->Buffer[3] ); 
+            Display.write( 0, mcpsIndication->Buffer[0] );
+            Display.write( 1, mcpsIndication->Buffer[1] );
+            Display.write( 2, mcpsIndication->Buffer[2] );
+            Display.write( 3, mcpsIndication->Buffer[3] ); 
             break;
-            
         case 20:
-            LightMode = McpsIndication->Buffer[0];
+            LightMode = mcpsIndication->Buffer[0];
             if( LightMode )
             {
-                color_led.setColorRGB(0, McpsIndication->Buffer[1], McpsIndication->Buffer[2], McpsIndication->Buffer[3] );
+                ColorLed.setColorRGB( 0, mcpsIndication->Buffer[1], mcpsIndication->Buffer[2], mcpsIndication->Buffer[3] );
             }
             break;
-            
         case 30:
-            BuzTimer.attach_us( &OnBuzTimerEvent, 200000 );
-            buzzer = 1;
+            BuzzerTimer.attach_us( &OnBuzzerTimerEvent, 200000 );
+            Buzzer = 1;
             break;
-            
         case 224:
             if( ComplianceTest.Running == false )
             {
                 // Check compliance test enable command (i)
-                if( ( McpsIndication->BufferSize == 4 ) && 
-                    ( McpsIndication->Buffer[0] == 0x01 ) &&
-                    ( McpsIndication->Buffer[1] == 0x01 ) &&
-                    ( McpsIndication->Buffer[2] == 0x01 ) &&
-                    ( McpsIndication->Buffer[3] == 0x01 ) )
+                if( ( mcpsIndication->BufferSize == 4 ) &&
+                    ( mcpsIndication->Buffer[0] == 0x01 ) &&
+                    ( mcpsIndication->Buffer[1] == 0x01 ) &&
+                    ( mcpsIndication->Buffer[2] == 0x01 ) &&
+                    ( mcpsIndication->Buffer[3] == 0x01 ) )
                 {
                     IsTxConfirmed = false;
                     AppPort = 224;
@@ -667,7 +675,7 @@
             }
             else
             {
-                ComplianceTest.State = McpsIndication->Buffer[0];
+                ComplianceTest.State = mcpsIndication->Buffer[0];
                 switch( ComplianceTest.State )
                 {
                 case 0: // Check compliance test disable command (ii)
@@ -697,12 +705,12 @@
                     ComplianceTest.State = 1;
                     break;
                 case 4: // (vii)
-                    AppDataSize = McpsIndication->BufferSize;
+                    AppDataSize = mcpsIndication->BufferSize;
 
                     AppData[0] = 4;
                     for( uint8_t i = 1; i < AppDataSize; i++ )
                     {
-                        AppData[i] = McpsIndication->Buffer[i] + 1;
+                        AppData[i] = mcpsIndication->Buffer[i] + 1;
                     }
                     break;
                 case 5: // (viii)
@@ -732,14 +740,14 @@
 /*!
  * \brief   MLME-Confirm event function
  *
- * \param   [IN] MlmeConfirm - Pointer to the confirm structure,
+ * \param   [IN] mlmeConfirm - Pointer to the confirm structure,
  *               containing confirm attributes.
  */
-static void MlmeConfirm( MlmeConfirm_t *MlmeConfirm )
+static void MlmeConfirm( MlmeConfirm_t *mlmeConfirm )
 {
-    if( MlmeConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK )
+    if( mlmeConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK )
     {
-        switch( MlmeConfirm->MlmeRequest )
+        switch( mlmeConfirm->MlmeRequest )
         {
             case MLME_JOIN:
             {
@@ -754,8 +762,8 @@
                 if( ComplianceTest.Running == true )
                 {
                     ComplianceTest.LinkCheck = true;
-                    ComplianceTest.DemodMargin = MlmeConfirm->DemodMargin;
-                    ComplianceTest.NbGateways = MlmeConfirm->NbGateways;
+                    ComplianceTest.DemodMargin = mlmeConfirm->DemodMargin;
+                    ComplianceTest.NbGateways = mlmeConfirm->NbGateways;
                 }
                 break;
             }
@@ -772,11 +780,6 @@
  */
 int main( void )
 {
-    float tempLightValue = 0.0;   
-    
-    LightMode = 0;      // 0: manual,   1: automatic
-    buzzer = 0;         // 0: OFF,      1: ON
-    
     LoRaMacPrimitives_t LoRaMacPrimitives;
     LoRaMacCallback_t LoRaMacCallbacks;
     MibRequestConfirm_t mibReq;
@@ -784,6 +787,9 @@
     BoardInit( );
     SerialDisplayInit( );
 
+    LightMode = 0;      // 0: manual,   1: automatic
+    Buzzer = 0;         // 0: OFF,      1: ON
+
     DeviceState = DEVICE_STATE_INIT;
 
     while( 1 )
@@ -852,6 +858,17 @@
 #if defined( USE_BAND_868 )
                 LoRaMacTestSetDutyCycleOn( LORAWAN_DUTYCYCLE_ON );
                 SerialDisplayUpdateDutyCycle( LORAWAN_DUTYCYCLE_ON );
+
+#if( USE_SEMTECH_DEFAULT_CHANNEL_LINEUP == 1 ) 
+                LoRaMacChannelAdd( 3, ( ChannelParams_t )LC4 );
+                LoRaMacChannelAdd( 4, ( ChannelParams_t )LC5 );
+                LoRaMacChannelAdd( 5, ( ChannelParams_t )LC6 );
+                LoRaMacChannelAdd( 6, ( ChannelParams_t )LC7 );
+                LoRaMacChannelAdd( 7, ( ChannelParams_t )LC8 );
+                LoRaMacChannelAdd( 8, ( ChannelParams_t )LC9 );
+                LoRaMacChannelAdd( 9, ( ChannelParams_t )LC10 );
+#endif
+
 #endif
                 SerialDisplayUpdateActivationMode( OVER_THE_AIR_ACTIVATION );
                 SerialDisplayUpdateAdr( LORAWAN_ADR_ON );
@@ -934,8 +951,8 @@
                 }
                 if( ComplianceTest.Running == true )
                 {
-                    // Schedule next packet transmission as soon as possible
-                    TxDutyCycleTime = 1000; // 1 ms
+                    // Schedule next packet transmission
+                    TxDutyCycleTime = 5000000; // 5000000 us
                 }
                 else
                 {
@@ -947,11 +964,11 @@
             }
             case DEVICE_STATE_CYCLE:
             {
+                DeviceState = DEVICE_STATE_SLEEP;
+
                 // Schedule next packet transmission
                 TimerSetValue( &TxNextPacketTimer, TxDutyCycleTime );
                 TimerStart( &TxNextPacketTimer );
-
-                DeviceState = DEVICE_STATE_SLEEP;
                 break;
             }
             case DEVICE_STATE_SLEEP:
@@ -966,16 +983,14 @@
             }
             
         }
-        
+
         // Read light sensor
-        tempLightValue = LightSens.read( ) * 1.65;
-        
-        LightValue = ( 1 - tempLightValue );
-        
+        LightValue = ( 1 - ( LightSens.read( ) * 1.65 ) );
+
         // Set automatic RGB from light sensor
         if( LightMode == 0 )
         {
-            color_led.setColorRGB( 0, ( uint8_t )( 255 * LightValue ), ( uint8_t )( 255 * LightValue ), ( uint8_t )( 255 * LightValue ) );
+            ColorLed.setColorRGB( 0, ( uint8_t )( 255 * LightValue ), ( uint8_t )( 255 * LightValue ), ( uint8_t )( 255 * LightValue ) );
         }
     }
 }