pzheng 20151201

Dependencies:   LoRaMacLib SX1276Lib mbed Chainable_RGB_LED DigitDisplay

Fork of LoRaWAN by LoRa All

Committer:
pzheng
Date:
Wed Dec 02 09:46:52 2015 +0000
Revision:
17:60af095c8763
Parent:
16:92dcf89f8c7a
Child:
18:0ffbe03c5542
without using the default NwkSKey and AppSKey

Who changed what in which revision?

UserRevisionLine numberNew contents of line
GregCr 0:fc538717c96e 1 /*
GregCr 0:fc538717c96e 2 / _____) _ | |
GregCr 0:fc538717c96e 3 ( (____ _____ ____ _| |_ _____ ____| |__
GregCr 0:fc538717c96e 4 \____ \| ___ | (_ _) ___ |/ ___) _ \
GregCr 0:fc538717c96e 5 _____) ) ____| | | || |_| ____( (___| | | |
GregCr 0:fc538717c96e 6 (______/|_____)_|_|_| \__)_____)\____)_| |_|
GregCr 0:fc538717c96e 7 (C)2015 Semtech
GregCr 0:fc538717c96e 8
GregCr 0:fc538717c96e 9 Description: LoRaMac classA device implementation
GregCr 0:fc538717c96e 10
GregCr 0:fc538717c96e 11 License: Revised BSD License, see LICENSE.TXT file include in the project
GregCr 0:fc538717c96e 12
GregCr 0:fc538717c96e 13 Maintainer: Miguel Luis and Gregory Cristian
GregCr 0:fc538717c96e 14 */
GregCr 0:fc538717c96e 15 #include "mbed.h"
GregCr 0:fc538717c96e 16 #include "board.h"
GregCr 0:fc538717c96e 17 #include "LoRaMac.h"
GregCr 0:fc538717c96e 18 #include "utilities.h"
GregCr 0:fc538717c96e 19 #include "DigitDisplay.h"
GregCr 0:fc538717c96e 20 #include "ChainableLED.h"
GregCr 0:fc538717c96e 21
pzheng 12:e47ad0da6112 22 //commented in 20151130
pzheng 12:e47ad0da6112 23 /*//Added by pzheng 20151111
pzheng 12:e47ad0da6112 24 static uint16_t counter_preparetxframe_log = 0;*/
GregCr 0:fc538717c96e 25 /*!
GregCr 0:fc538717c96e 26 * When set to 1 the application uses the Over-the-Air activation procedure
GregCr 0:fc538717c96e 27 * When set to 0 the application uses the Personalization activation procedure
GregCr 0:fc538717c96e 28 */
pzheng 14:097ea525b2c0 29 #define OVER_THE_AIR_ACTIVATION 0
GregCr 0:fc538717c96e 30 /*!
GregCr 0:fc538717c96e 31 * Mote device IEEE EUI
GregCr 0:fc538717c96e 32 */
GregCr 0:fc538717c96e 33 static uint8_t DevEui[] =
GregCr 0:fc538717c96e 34 {
pzheng 12:e47ad0da6112 35 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10 /// <<< Change here
pzheng 12:e47ad0da6112 36 };
pzheng 12:e47ad0da6112 37
pzheng 12:e47ad0da6112 38 #if( OVER_THE_AIR_ACTIVATION != 0 )
pzheng 12:e47ad0da6112 39
pzheng 12:e47ad0da6112 40 #define OVER_THE_AIR_ACTIVATION_DUTYCYCLE 10000000 // 10 [s] value in us
pzheng 12:e47ad0da6112 41
pzheng 12:e47ad0da6112 42 /*!
pzheng 12:e47ad0da6112 43 * Application IEEE EUI
pzheng 12:e47ad0da6112 44 */
pzheng 12:e47ad0da6112 45 static uint8_t AppEui[] =
pzheng 12:e47ad0da6112 46 {
pzheng 17:60af095c8763 47 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
GregCr 0:fc538717c96e 48 };
GregCr 0:fc538717c96e 49
GregCr 0:fc538717c96e 50 /*!
pzheng 12:e47ad0da6112 51 * AES encryption/decryption cipher application key
pzheng 12:e47ad0da6112 52 */
pzheng 12:e47ad0da6112 53 static uint8_t AppKey[] =
pzheng 12:e47ad0da6112 54 {
pzheng 12:e47ad0da6112 55 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6,
pzheng 12:e47ad0da6112 56 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C
pzheng 12:e47ad0da6112 57 };
pzheng 12:e47ad0da6112 58
pzheng 12:e47ad0da6112 59 #else
pzheng 12:e47ad0da6112 60 /*!
Alliance 11:b05c281295a3 61 * Device address
GregCr 0:fc538717c96e 62 */
pzheng 16:92dcf89f8c7a 63 static uint32_t DevAddr;// = 0x00001002; /// <<< Change here
GregCr 0:fc538717c96e 64
GregCr 0:fc538717c96e 65 /*!
GregCr 0:fc538717c96e 66 * AES encryption/decryption cipher network session key
GregCr 0:fc538717c96e 67 */
pzheng 17:60af095c8763 68 static uint8_t NwkSKey[] =
pzheng 12:e47ad0da6112 69 {
pzheng 12:e47ad0da6112 70 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6,
pzheng 12:e47ad0da6112 71 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C
GregCr 0:fc538717c96e 72 };
GregCr 0:fc538717c96e 73
GregCr 0:fc538717c96e 74 /*!
GregCr 0:fc538717c96e 75 * AES encryption/decryption cipher application session key
GregCr 0:fc538717c96e 76 */
GregCr 0:fc538717c96e 77 static uint8_t AppSKey[] =
GregCr 0:fc538717c96e 78 {
pzheng 17:60af095c8763 79 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6,
pzheng 17:60af095c8763 80 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C
GregCr 0:fc538717c96e 81 };
pzheng 17:60af095c8763 82
pzheng 17:60af095c8763 83 //static uint8_t NwkSKey[] =
pzheng 17:60af095c8763 84 //{
pzheng 17:60af095c8763 85 // 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
pzheng 17:60af095c8763 86 // 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x00
pzheng 17:60af095c8763 87 //};
pzheng 17:60af095c8763 88
pzheng 17:60af095c8763 89 ///*!
pzheng 17:60af095c8763 90 // * AES encryption/decryption cipher application session key
pzheng 17:60af095c8763 91 // */
pzheng 17:60af095c8763 92 //static uint8_t AppSKey[] =
pzheng 17:60af095c8763 93 //{
pzheng 17:60af095c8763 94 // 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
pzheng 17:60af095c8763 95 // 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x00
pzheng 17:60af095c8763 96 //};
pzheng 12:e47ad0da6112 97 #endif
GregCr 0:fc538717c96e 98
GregCr 0:fc538717c96e 99
GregCr 0:fc538717c96e 100 /*!
GregCr 0:fc538717c96e 101 * Indicates if the MAC layer has already joined a network.
GregCr 0:fc538717c96e 102 */
GregCr 0:fc538717c96e 103 static bool IsNetworkJoined = false;
GregCr 0:fc538717c96e 104
GregCr 0:fc538717c96e 105 /*!
GregCr 0:fc538717c96e 106 * Defines the application data transmission duty cycle
GregCr 0:fc538717c96e 107 */
pzheng 12:e47ad0da6112 108 #define APP_TX_DUTYCYCLE 5000000 // 5 [s] value in us (minimum 10s)
pzheng 12:e47ad0da6112 109 #define APP_TX_DUTYCYCLE_RND 1000000 // 1 [s] value in us (minimum 3s)
GregCr 0:fc538717c96e 110
GregCr 0:fc538717c96e 111 /*!
GregCr 0:fc538717c96e 112 * User application data buffer size
GregCr 0:fc538717c96e 113 */
Alliance 7:063ff5895cfe 114 #define APP_DATA_SIZE 5
GregCr 0:fc538717c96e 115
GregCr 0:fc538717c96e 116 /*!
GregCr 0:fc538717c96e 117 * User application data
GregCr 0:fc538717c96e 118 */
GregCr 0:fc538717c96e 119 static uint8_t AppData[APP_DATA_SIZE];
GregCr 0:fc538717c96e 120
GregCr 0:fc538717c96e 121 /*!
GregCr 0:fc538717c96e 122 * Defines the application data transmission duty cycle
GregCr 0:fc538717c96e 123 */
GregCr 0:fc538717c96e 124 static uint32_t TxDutyCycleTime;
GregCr 0:fc538717c96e 125
GregCr 0:fc538717c96e 126 Ticker TxNextPacketTimer;
GregCr 0:fc538717c96e 127
GregCr 0:fc538717c96e 128 #if( OVER_THE_AIR_ACTIVATION != 0 )
GregCr 0:fc538717c96e 129
GregCr 0:fc538717c96e 130 /*!
GregCr 0:fc538717c96e 131 * Defines the join request timer
GregCr 0:fc538717c96e 132 */
GregCr 0:fc538717c96e 133 Ticker JoinReqTimer;
GregCr 0:fc538717c96e 134
GregCr 0:fc538717c96e 135 #endif
GregCr 0:fc538717c96e 136
GregCr 0:fc538717c96e 137 /*!
GregCr 0:fc538717c96e 138 * Indicates if a new packet can be sent
GregCr 0:fc538717c96e 139 */
GregCr 0:fc538717c96e 140 static bool TxNextPacket = true;
GregCr 0:fc538717c96e 141 static bool TxDone = false;
GregCr 0:fc538717c96e 142
pzheng 12:e47ad0da6112 143 static uint8_t AppPort = 2;
GregCr 0:fc538717c96e 144 static uint8_t AppDataSize = APP_DATA_SIZE;
GregCr 0:fc538717c96e 145
GregCr 0:fc538717c96e 146 static LoRaMacEvent_t LoRaMacEvents;
GregCr 0:fc538717c96e 147
Alliance 7:063ff5895cfe 148 static float LightValue = 0.0;
Alliance 7:063ff5895cfe 149 static uint8_t LightMode = 0; // 0:automatic, 1:manual
Alliance 7:063ff5895cfe 150
GregCr 0:fc538717c96e 151 Ticker Led1Timer;
GregCr 0:fc538717c96e 152 Ticker Led2Timer;
Alliance 1:1ef4f6cd800c 153 Ticker BuzTimer;
GregCr 0:fc538717c96e 154
GregCr 0:fc538717c96e 155 #define NUM_LED 3
GregCr 0:fc538717c96e 156
Alliance 3:db6ad4aa790d 157
Alliance 7:063ff5895cfe 158 AnalogIn LightSens( A1 );
Alliance 6:ea71f564e0ce 159 ChainableLED color_led(D6, D7, NUM_LED);
pzheng 12:e47ad0da6112 160 //commented in 20151130
pzheng 12:e47ad0da6112 161 /*DigitDisplay display(D8, D9);
pzheng 12:e47ad0da6112 162 DigitalOut buzzer(A2);*/
GregCr 0:fc538717c96e 163
pzheng 12:e47ad0da6112 164 //commented in 20151130
pzheng 12:e47ad0da6112 165 /*static void OnBuzTimerEvent( void )
Alliance 1:1ef4f6cd800c 166 {
Alliance 1:1ef4f6cd800c 167 buzzer = 0;
Alliance 1:1ef4f6cd800c 168 BuzTimer.detach( );
pzheng 12:e47ad0da6112 169 }*/
GregCr 0:fc538717c96e 170
GregCr 0:fc538717c96e 171 /*!
GregCr 0:fc538717c96e 172 *
GregCr 0:fc538717c96e 173 */
GregCr 0:fc538717c96e 174 static void PrepareTxFrame( uint8_t port )
GregCr 0:fc538717c96e 175 {
Alliance 7:063ff5895cfe 176 uint32_t tempValue = (uint32_t)(LightValue*1000000.0);
Alliance 7:063ff5895cfe 177
Alliance 7:063ff5895cfe 178 debug( "[Tx] LightValue=%d 0x%x\n\r" , tempValue, tempValue);
Alliance 7:063ff5895cfe 179
Alliance 7:063ff5895cfe 180 AppData[0] = LightMode;
Alliance 7:063ff5895cfe 181 AppData[1] = ((tempValue&0xFF000000)>>24)&0xFF;
Alliance 7:063ff5895cfe 182 AppData[2] = ((tempValue&0x00FF0000)>>16)&0xFF;
Alliance 7:063ff5895cfe 183 AppData[3] = ((tempValue&0x0000FF00)>>8)&0xFF;
Alliance 7:063ff5895cfe 184 AppData[4] = (tempValue&0x000000FF);
pzheng 12:e47ad0da6112 185
pzheng 12:e47ad0da6112 186 //commented in 20151130
pzheng 12:e47ad0da6112 187 /*counter_preparetxframe_log++;
pzheng 12:e47ad0da6112 188
pzheng 12:e47ad0da6112 189 display.write( counter_preparetxframe_log );*/
pzheng 12:e47ad0da6112 190
GregCr 0:fc538717c96e 191 }
GregCr 0:fc538717c96e 192
GregCr 0:fc538717c96e 193 static void ProcessRxFrame( LoRaMacEventFlags_t *flags, LoRaMacEventInfo_t *info )
GregCr 0:fc538717c96e 194 {
Alliance 7:063ff5895cfe 195 debug( "[Rx] Port=%d\n\r" , info->RxPort);
GregCr 0:fc538717c96e 196 switch( info->RxPort ) // Check Rx port number
GregCr 0:fc538717c96e 197 {
GregCr 0:fc538717c96e 198 case 10:
pzheng 12:e47ad0da6112 199 //commented in 20151130
pzheng 12:e47ad0da6112 200 /*display.write( 0, info->RxBuffer[0] );
Alliance 1:1ef4f6cd800c 201 display.write( 1, info->RxBuffer[1] );
Alliance 1:1ef4f6cd800c 202 display.write( 2, info->RxBuffer[2] );
Alliance 4:0c164d628006 203 display.write( 3, info->RxBuffer[3] );
pzheng 12:e47ad0da6112 204 break;*/
GregCr 0:fc538717c96e 205
Alliance 1:1ef4f6cd800c 206 case 20:
Alliance 7:063ff5895cfe 207 LightMode = info->RxBuffer[0];
Alliance 7:063ff5895cfe 208 debug( "[Rx] LightMode=%x - R G B= 0x%x 0x%x 0x%x\n\r" , LightMode, info->RxBuffer[1], info->RxBuffer[2], info->RxBuffer[3]);
Alliance 7:063ff5895cfe 209 if( LightMode )
Alliance 7:063ff5895cfe 210 {
Alliance 7:063ff5895cfe 211 color_led.setColorRGB(0, info->RxBuffer[1], info->RxBuffer[2], info->RxBuffer[3] );
Alliance 7:063ff5895cfe 212 }
Alliance 1:1ef4f6cd800c 213 break;
GregCr 0:fc538717c96e 214
Alliance 1:1ef4f6cd800c 215 case 30:
pzheng 12:e47ad0da6112 216 //commented in 20151130
pzheng 12:e47ad0da6112 217 /*BuzTimer.attach_us( &OnBuzTimerEvent, 500000 );
pzheng 12:e47ad0da6112 218 buzzer = 1;*/
GregCr 0:fc538717c96e 219 break;
GregCr 0:fc538717c96e 220
GregCr 0:fc538717c96e 221 default:
GregCr 0:fc538717c96e 222 break;
GregCr 0:fc538717c96e 223 }
GregCr 0:fc538717c96e 224 }
GregCr 0:fc538717c96e 225
GregCr 0:fc538717c96e 226 static bool SendFrame( void )
GregCr 0:fc538717c96e 227 {
GregCr 0:fc538717c96e 228 uint8_t sendFrameStatus = 0;
GregCr 0:fc538717c96e 229
pzheng 12:e47ad0da6112 230 if( LoRaMacGetUpLinkTestOn() == false)
pzheng 12:e47ad0da6112 231 {
pzheng 12:e47ad0da6112 232 sendFrameStatus = LoRaMacSendFrame( AppPort, AppData, AppDataSize );
pzheng 12:e47ad0da6112 233 }
pzheng 12:e47ad0da6112 234 else
pzheng 12:e47ad0da6112 235 {
pzheng 12:e47ad0da6112 236 sendFrameStatus = LoRaMacSendConfirmedFrame( AppPort, AppData, AppDataSize, 8 );
pzheng 12:e47ad0da6112 237 }
GregCr 0:fc538717c96e 238 switch( sendFrameStatus )
GregCr 0:fc538717c96e 239 {
GregCr 0:fc538717c96e 240 case 5: // NO_FREE_CHANNEL
GregCr 0:fc538717c96e 241 // Try again later
GregCr 0:fc538717c96e 242 return true;
GregCr 0:fc538717c96e 243 default:
GregCr 0:fc538717c96e 244 return false;
GregCr 0:fc538717c96e 245 }
GregCr 0:fc538717c96e 246 }
GregCr 0:fc538717c96e 247
GregCr 0:fc538717c96e 248
GregCr 0:fc538717c96e 249 #if( OVER_THE_AIR_ACTIVATION != 0 )
GregCr 0:fc538717c96e 250 /*!
GregCr 0:fc538717c96e 251 * \brief Function executed on JoinReq Timeout event
GregCr 0:fc538717c96e 252 */
GregCr 0:fc538717c96e 253 static void OnJoinReqTimerEvent( void )
GregCr 0:fc538717c96e 254 {
GregCr 0:fc538717c96e 255 TxNextPacket = true;
Alliance 1:1ef4f6cd800c 256 JoinReqTimer.detach( );
GregCr 0:fc538717c96e 257 }
GregCr 0:fc538717c96e 258 #endif
GregCr 0:fc538717c96e 259
GregCr 0:fc538717c96e 260
GregCr 0:fc538717c96e 261 /*!
GregCr 0:fc538717c96e 262 * \brief Function executed on TxNextPacket Timeout event
GregCr 0:fc538717c96e 263 */
GregCr 0:fc538717c96e 264 static void OnTxNextPacketTimerEvent( void )
Alliance 1:1ef4f6cd800c 265 {
GregCr 0:fc538717c96e 266 TxNextPacket = true;
Alliance 1:1ef4f6cd800c 267 TxNextPacketTimer.detach( );
GregCr 0:fc538717c96e 268 }
GregCr 0:fc538717c96e 269
GregCr 0:fc538717c96e 270
GregCr 0:fc538717c96e 271 /*!
GregCr 0:fc538717c96e 272 * \brief Function to be executed on MAC layer event
GregCr 0:fc538717c96e 273 */
GregCr 0:fc538717c96e 274 static void OnMacEvent( LoRaMacEventFlags_t *flags, LoRaMacEventInfo_t *info )
GregCr 0:fc538717c96e 275 {
GregCr 0:fc538717c96e 276 if( flags->Bits.JoinAccept == 1 )
GregCr 0:fc538717c96e 277 {
GregCr 0:fc538717c96e 278 #if( OVER_THE_AIR_ACTIVATION != 0 )
GregCr 0:fc538717c96e 279 JoinReqTimer.detach( );
GregCr 0:fc538717c96e 280 #endif
GregCr 0:fc538717c96e 281 IsNetworkJoined = true;
GregCr 0:fc538717c96e 282 }
GregCr 0:fc538717c96e 283
GregCr 0:fc538717c96e 284 if( flags->Bits.Tx == 1 )
GregCr 0:fc538717c96e 285 {
GregCr 0:fc538717c96e 286 }
GregCr 0:fc538717c96e 287
GregCr 0:fc538717c96e 288 if( flags->Bits.Rx == 1 )
GregCr 0:fc538717c96e 289 {
GregCr 0:fc538717c96e 290 if( flags->Bits.RxData == true )
GregCr 0:fc538717c96e 291 {
GregCr 0:fc538717c96e 292 ProcessRxFrame( flags, info );
GregCr 0:fc538717c96e 293 }
GregCr 0:fc538717c96e 294 }
GregCr 0:fc538717c96e 295
GregCr 0:fc538717c96e 296 // Schedule a new transmission
GregCr 0:fc538717c96e 297 TxDone = true;
GregCr 0:fc538717c96e 298 }
GregCr 0:fc538717c96e 299
GregCr 0:fc538717c96e 300 /**
GregCr 0:fc538717c96e 301 * Main application entry point.
GregCr 0:fc538717c96e 302 */
GregCr 0:fc538717c96e 303 int main( void )
GregCr 0:fc538717c96e 304 {
GregCr 0:fc538717c96e 305 #if( OVER_THE_AIR_ACTIVATION != 0 )
GregCr 0:fc538717c96e 306 uint8_t sendFrameStatus = 0;
GregCr 0:fc538717c96e 307 #endif
GregCr 0:fc538717c96e 308 bool trySendingFrameAgain = false;
Alliance 7:063ff5895cfe 309 float tempLightValue = 0;
pzheng 12:e47ad0da6112 310
Alliance 7:063ff5895cfe 311 LightMode = 0; // 0: manual, 1: automatic
pzheng 12:e47ad0da6112 312
pzheng 12:e47ad0da6112 313 //comment in 20151130
pzheng 12:e47ad0da6112 314 //buzzer = 0; // 0: OFF, 1: ON
pzheng 12:e47ad0da6112 315 ///*Added by pzheng 20151111*/
pzheng 12:e47ad0da6112 316 //display.write( counter_preparetxframe_log );
pzheng 12:e47ad0da6112 317
Alliance 1:1ef4f6cd800c 318 debug( "\n\n\r LoRaWAN Class A Demo code \n\n\r" );
Alliance 1:1ef4f6cd800c 319
GregCr 0:fc538717c96e 320 BoardInitMcu( );
GregCr 0:fc538717c96e 321 BoardInitPeriph( );
GregCr 0:fc538717c96e 322
GregCr 0:fc538717c96e 323 // Initialize LoRaMac device unique ID
pzheng 12:e47ad0da6112 324 BoardGetUniqueId( DevEui );
Alliance 3:db6ad4aa790d 325
GregCr 0:fc538717c96e 326 LoRaMacEvents.MacEvent = OnMacEvent;
GregCr 0:fc538717c96e 327 LoRaMacInit( &LoRaMacEvents );
GregCr 0:fc538717c96e 328
pzheng 12:e47ad0da6112 329 //Added by pzheng 20151111
pzheng 12:e47ad0da6112 330 float hue = 0.0;
pzheng 12:e47ad0da6112 331 bool up = true;
pzheng 12:e47ad0da6112 332 for(int i = 0; i<16; i++)
pzheng 12:e47ad0da6112 333 {
pzheng 12:e47ad0da6112 334 for (int i=0; i<NUM_LED; i++)
pzheng 12:e47ad0da6112 335 color_led.setColorHSB(i, hue, 1.0, 0.5);
pzheng 12:e47ad0da6112 336
pzheng 12:e47ad0da6112 337
pzheng 12:e47ad0da6112 338 if (up)
pzheng 12:e47ad0da6112 339 hue+= 0.025;
pzheng 12:e47ad0da6112 340 else
pzheng 12:e47ad0da6112 341 hue-= 0.025;
pzheng 12:e47ad0da6112 342
pzheng 12:e47ad0da6112 343 if (hue>=1.0 && up)
pzheng 12:e47ad0da6112 344 up = false;
pzheng 12:e47ad0da6112 345 else if (hue<=0.0 && !up)
pzheng 12:e47ad0da6112 346 up = true;
pzheng 12:e47ad0da6112 347 }
pzheng 12:e47ad0da6112 348
GregCr 0:fc538717c96e 349 IsNetworkJoined = false;
GregCr 0:fc538717c96e 350
pzheng 12:e47ad0da6112 351 #if( OVER_THE_AIR_ACTIVATION == 0 )
GregCr 0:fc538717c96e 352 // Random seed initialization
pzheng 12:e47ad0da6112 353 srand1( RAND_SEED );
GregCr 0:fc538717c96e 354 // Choose a random device address
GregCr 0:fc538717c96e 355 // NwkID = 0
GregCr 0:fc538717c96e 356 // NwkAddr rand [0, 33554431]
Alliance 8:d3a75f9efef8 357 if( ( DevAddr == 0 ) || ( DevAddr == 0xFFFFFFFF ) )
Alliance 8:d3a75f9efef8 358 {
Alliance 8:d3a75f9efef8 359 // Generate random DevAddr if it does not exist
Alliance 8:d3a75f9efef8 360 debug("Generate random DevAddr\n\r");
Alliance 8:d3a75f9efef8 361 DevAddr = randr( 0, 0x01FFFFFF );
Alliance 8:d3a75f9efef8 362 }
Alliance 7:063ff5895cfe 363 debug( "- DevAddr = 0x%x\n\r" , DevAddr);
pzheng 17:60af095c8763 364 LoRaMacInitNwkIds( 0x000000, DevAddr, NwkSKey, AppSKey );
pzheng 17:60af095c8763 365
pzheng 12:e47ad0da6112 366 for(int i = 0; i<16; i++)
pzheng 12:e47ad0da6112 367 {
pzheng 12:e47ad0da6112 368 debug( "- NwkSKey[%d] = 0x%x\n\r" , i, NwkSKey[i]);
pzheng 12:e47ad0da6112 369 }
pzheng 12:e47ad0da6112 370 for(int i = 0; i<16; i++)
pzheng 12:e47ad0da6112 371 {
pzheng 12:e47ad0da6112 372 debug( "- AppSKey[%d] = 0x%x\n\r" , i, AppSKey[i]);
pzheng 12:e47ad0da6112 373 }
GregCr 0:fc538717c96e 374 IsNetworkJoined = true;
GregCr 0:fc538717c96e 375 #endif
GregCr 0:fc538717c96e 376
GregCr 0:fc538717c96e 377 TxNextPacket = true;
GregCr 0:fc538717c96e 378
pzheng 12:e47ad0da6112 379 LoRaMacSetAdrOn( true );
Alliance 1:1ef4f6cd800c 380
Alliance 1:1ef4f6cd800c 381 LoRaMacSetDutyCycleOn( false );
pzheng 12:e47ad0da6112 382
pzheng 12:e47ad0da6112 383 //Added by pzheng 20151111
pzheng 12:e47ad0da6112 384 LoRaMacSetUpLinkTestOn( false );
GregCr 0:fc538717c96e 385
GregCr 0:fc538717c96e 386 while( 1 )
GregCr 0:fc538717c96e 387 {
GregCr 0:fc538717c96e 388 while( IsNetworkJoined == false )
GregCr 0:fc538717c96e 389 {
GregCr 0:fc538717c96e 390 #if( OVER_THE_AIR_ACTIVATION != 0 )
GregCr 0:fc538717c96e 391 if( TxNextPacket == true )
GregCr 0:fc538717c96e 392 {
GregCr 0:fc538717c96e 393 TxNextPacket = false;
GregCr 0:fc538717c96e 394
pzheng 12:e47ad0da6112 395 //Added by pzheng 20151111
pzheng 12:e47ad0da6112 396 for(int i = 0; i<8; i++)
pzheng 12:e47ad0da6112 397 {
pzheng 12:e47ad0da6112 398 //DevEui[i] = 0x10;
pzheng 12:e47ad0da6112 399 debug( "- DevEui[%d] = 0x%x\n\r" , i, DevEui[i]);
pzheng 12:e47ad0da6112 400 }
pzheng 12:e47ad0da6112 401 for(int i = 0; i<8; i++)
pzheng 12:e47ad0da6112 402 {
pzheng 12:e47ad0da6112 403 debug( "- AppEui[%d] = 0x%x\n\r" , i, AppEui[i]);
pzheng 12:e47ad0da6112 404 }
pzheng 12:e47ad0da6112 405 for(int i = 0; i<16; i++)
pzheng 12:e47ad0da6112 406 {
pzheng 12:e47ad0da6112 407 debug( "- AppKey[%d] = 0x%x\n\r" , i, AppKey[i]);
pzheng 12:e47ad0da6112 408 }
GregCr 0:fc538717c96e 409 sendFrameStatus = LoRaMacJoinReq( DevEui, AppEui, AppKey );
Alliance 3:db6ad4aa790d 410 debug("Req Sent\n\r");
GregCr 0:fc538717c96e 411 switch( sendFrameStatus )
GregCr 0:fc538717c96e 412 {
GregCr 0:fc538717c96e 413 case 1: // BUSY
GregCr 0:fc538717c96e 414 break;
GregCr 0:fc538717c96e 415 case 0: // OK
GregCr 0:fc538717c96e 416 case 2: // NO_NETWORK_JOINED
GregCr 0:fc538717c96e 417 case 3: // LENGTH_PORT_ERROR
GregCr 0:fc538717c96e 418 case 4: // MAC_CMD_ERROR
GregCr 0:fc538717c96e 419 case 6: // DEVICE_OFF
GregCr 0:fc538717c96e 420 default:
GregCr 0:fc538717c96e 421 // Relaunch timer for next trial
GregCr 0:fc538717c96e 422 JoinReqTimer.attach_us( &OnJoinReqTimerEvent, OVER_THE_AIR_ACTIVATION_DUTYCYCLE );
GregCr 0:fc538717c96e 423 break;
GregCr 0:fc538717c96e 424 }
GregCr 0:fc538717c96e 425 }
GregCr 0:fc538717c96e 426 // TimerLowPowerHandler( );
GregCr 0:fc538717c96e 427 #endif
GregCr 0:fc538717c96e 428 }
GregCr 0:fc538717c96e 429
GregCr 0:fc538717c96e 430 if( TxDone == true )
GregCr 0:fc538717c96e 431 {
pzheng 12:e47ad0da6112 432 for(int i = 0; i<8; i++)
pzheng 12:e47ad0da6112 433 {
pzheng 12:e47ad0da6112 434 debug( "- DevEui[%d] = 0x%x\n\r" , i, DevEui[i]);
pzheng 12:e47ad0da6112 435 }
GregCr 0:fc538717c96e 436 TxDone = false;
GregCr 0:fc538717c96e 437
GregCr 0:fc538717c96e 438 debug( "TxDone \n\n\r" );
GregCr 0:fc538717c96e 439 // Schedule next packet transmission
GregCr 0:fc538717c96e 440 TxDutyCycleTime = APP_TX_DUTYCYCLE + randr( -APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND );
GregCr 0:fc538717c96e 441 TxNextPacketTimer.attach_us( &OnTxNextPacketTimerEvent, TxDutyCycleTime );
GregCr 0:fc538717c96e 442 }
GregCr 0:fc538717c96e 443
GregCr 0:fc538717c96e 444 if( trySendingFrameAgain == true )
GregCr 0:fc538717c96e 445 {
GregCr 0:fc538717c96e 446 trySendingFrameAgain = SendFrame( );
GregCr 0:fc538717c96e 447 }
GregCr 0:fc538717c96e 448
GregCr 0:fc538717c96e 449 if( TxNextPacket == true )
GregCr 0:fc538717c96e 450 {
GregCr 0:fc538717c96e 451 TxNextPacketTimer.detach( );
GregCr 0:fc538717c96e 452
GregCr 0:fc538717c96e 453 TxNextPacket = false;
GregCr 0:fc538717c96e 454
GregCr 0:fc538717c96e 455 PrepareTxFrame( AppPort );
GregCr 0:fc538717c96e 456
GregCr 0:fc538717c96e 457 trySendingFrameAgain = SendFrame( );
GregCr 0:fc538717c96e 458 }
GregCr 0:fc538717c96e 459
Alliance 7:063ff5895cfe 460 // Read light sensor
Alliance 7:063ff5895cfe 461 tempLightValue = LightSens.read( ) * 1.65;
Alliance 7:063ff5895cfe 462
Alliance 7:063ff5895cfe 463 LightValue = ( 1 - tempLightValue );
Alliance 7:063ff5895cfe 464
Alliance 7:063ff5895cfe 465 // Set automatic RGB from light sensor
Alliance 7:063ff5895cfe 466 if( LightMode == 0 )
Alliance 7:063ff5895cfe 467 {
Alliance 7:063ff5895cfe 468 color_led.setColorRGB( 0, ( uint8_t )( 255 * LightValue ), ( uint8_t )( 255 * LightValue ), ( uint8_t )( 255 * LightValue ) );
Alliance 7:063ff5895cfe 469 }
GregCr 0:fc538717c96e 470 // TimerLowPowerHandler( );
GregCr 0:fc538717c96e 471 }
GregCr 0:fc538717c96e 472 }
GregCr 0:fc538717c96e 473