pzheng 20151201

Dependencies:   LoRaMacLib SX1276Lib mbed Chainable_RGB_LED DigitDisplay

Fork of LoRaWAN by LoRa All

Committer:
pzheng
Date:
Thu Dec 03 09:03:59 2015 +0000
Revision:
18:0ffbe03c5542
Parent:
17:60af095c8763
Child:
19:9ae3b9b57ae0
add the configurations of 470MHz and 915MHz

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 {
pzheng 18:0ffbe03c5542 176 uint32_t tempValue = (uint32_t)(LightValue*1000000.0);
Alliance 7:063ff5895cfe 177 debug( "[Tx] LightValue=%d 0x%x\n\r" , tempValue, tempValue);
Alliance 7:063ff5895cfe 178
Alliance 7:063ff5895cfe 179 AppData[0] = LightMode;
Alliance 7:063ff5895cfe 180 AppData[1] = ((tempValue&0xFF000000)>>24)&0xFF;
Alliance 7:063ff5895cfe 181 AppData[2] = ((tempValue&0x00FF0000)>>16)&0xFF;
Alliance 7:063ff5895cfe 182 AppData[3] = ((tempValue&0x0000FF00)>>8)&0xFF;
pzheng 18:0ffbe03c5542 183 AppData[4] = (tempValue&0x000000FF);
pzheng 12:e47ad0da6112 184 //commented in 20151130
pzheng 18:0ffbe03c5542 185 /*counter_preparetxframe_log++;
pzheng 18:0ffbe03c5542 186 display.write( counter_preparetxframe_log );*/
GregCr 0:fc538717c96e 187 }
GregCr 0:fc538717c96e 188
GregCr 0:fc538717c96e 189 static void ProcessRxFrame( LoRaMacEventFlags_t *flags, LoRaMacEventInfo_t *info )
GregCr 0:fc538717c96e 190 {
Alliance 7:063ff5895cfe 191 debug( "[Rx] Port=%d\n\r" , info->RxPort);
GregCr 0:fc538717c96e 192 switch( info->RxPort ) // Check Rx port number
GregCr 0:fc538717c96e 193 {
GregCr 0:fc538717c96e 194 case 10:
pzheng 12:e47ad0da6112 195 //commented in 20151130
pzheng 12:e47ad0da6112 196 /*display.write( 0, info->RxBuffer[0] );
Alliance 1:1ef4f6cd800c 197 display.write( 1, info->RxBuffer[1] );
Alliance 1:1ef4f6cd800c 198 display.write( 2, info->RxBuffer[2] );
Alliance 4:0c164d628006 199 display.write( 3, info->RxBuffer[3] );
pzheng 12:e47ad0da6112 200 break;*/
GregCr 0:fc538717c96e 201
Alliance 1:1ef4f6cd800c 202 case 20:
Alliance 7:063ff5895cfe 203 LightMode = info->RxBuffer[0];
Alliance 7:063ff5895cfe 204 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 205 if( LightMode )
Alliance 7:063ff5895cfe 206 {
Alliance 7:063ff5895cfe 207 color_led.setColorRGB(0, info->RxBuffer[1], info->RxBuffer[2], info->RxBuffer[3] );
Alliance 7:063ff5895cfe 208 }
Alliance 1:1ef4f6cd800c 209 break;
GregCr 0:fc538717c96e 210
Alliance 1:1ef4f6cd800c 211 case 30:
pzheng 12:e47ad0da6112 212 //commented in 20151130
pzheng 12:e47ad0da6112 213 /*BuzTimer.attach_us( &OnBuzTimerEvent, 500000 );
pzheng 12:e47ad0da6112 214 buzzer = 1;*/
GregCr 0:fc538717c96e 215 break;
GregCr 0:fc538717c96e 216
GregCr 0:fc538717c96e 217 default:
GregCr 0:fc538717c96e 218 break;
GregCr 0:fc538717c96e 219 }
GregCr 0:fc538717c96e 220 }
GregCr 0:fc538717c96e 221
GregCr 0:fc538717c96e 222 static bool SendFrame( void )
GregCr 0:fc538717c96e 223 {
GregCr 0:fc538717c96e 224 uint8_t sendFrameStatus = 0;
GregCr 0:fc538717c96e 225
pzheng 12:e47ad0da6112 226 if( LoRaMacGetUpLinkTestOn() == false)
pzheng 12:e47ad0da6112 227 {
pzheng 12:e47ad0da6112 228 sendFrameStatus = LoRaMacSendFrame( AppPort, AppData, AppDataSize );
pzheng 12:e47ad0da6112 229 }
pzheng 12:e47ad0da6112 230 else
pzheng 12:e47ad0da6112 231 {
pzheng 12:e47ad0da6112 232 sendFrameStatus = LoRaMacSendConfirmedFrame( AppPort, AppData, AppDataSize, 8 );
pzheng 12:e47ad0da6112 233 }
GregCr 0:fc538717c96e 234 switch( sendFrameStatus )
GregCr 0:fc538717c96e 235 {
GregCr 0:fc538717c96e 236 case 5: // NO_FREE_CHANNEL
GregCr 0:fc538717c96e 237 // Try again later
GregCr 0:fc538717c96e 238 return true;
GregCr 0:fc538717c96e 239 default:
GregCr 0:fc538717c96e 240 return false;
GregCr 0:fc538717c96e 241 }
GregCr 0:fc538717c96e 242 }
GregCr 0:fc538717c96e 243
GregCr 0:fc538717c96e 244
GregCr 0:fc538717c96e 245 #if( OVER_THE_AIR_ACTIVATION != 0 )
GregCr 0:fc538717c96e 246 /*!
GregCr 0:fc538717c96e 247 * \brief Function executed on JoinReq Timeout event
GregCr 0:fc538717c96e 248 */
GregCr 0:fc538717c96e 249 static void OnJoinReqTimerEvent( void )
GregCr 0:fc538717c96e 250 {
GregCr 0:fc538717c96e 251 TxNextPacket = true;
Alliance 1:1ef4f6cd800c 252 JoinReqTimer.detach( );
GregCr 0:fc538717c96e 253 }
GregCr 0:fc538717c96e 254 #endif
GregCr 0:fc538717c96e 255
GregCr 0:fc538717c96e 256
GregCr 0:fc538717c96e 257 /*!
GregCr 0:fc538717c96e 258 * \brief Function executed on TxNextPacket Timeout event
GregCr 0:fc538717c96e 259 */
GregCr 0:fc538717c96e 260 static void OnTxNextPacketTimerEvent( void )
Alliance 1:1ef4f6cd800c 261 {
GregCr 0:fc538717c96e 262 TxNextPacket = true;
Alliance 1:1ef4f6cd800c 263 TxNextPacketTimer.detach( );
GregCr 0:fc538717c96e 264 }
GregCr 0:fc538717c96e 265
GregCr 0:fc538717c96e 266
GregCr 0:fc538717c96e 267 /*!
GregCr 0:fc538717c96e 268 * \brief Function to be executed on MAC layer event
GregCr 0:fc538717c96e 269 */
GregCr 0:fc538717c96e 270 static void OnMacEvent( LoRaMacEventFlags_t *flags, LoRaMacEventInfo_t *info )
GregCr 0:fc538717c96e 271 {
GregCr 0:fc538717c96e 272 if( flags->Bits.JoinAccept == 1 )
GregCr 0:fc538717c96e 273 {
GregCr 0:fc538717c96e 274 #if( OVER_THE_AIR_ACTIVATION != 0 )
GregCr 0:fc538717c96e 275 JoinReqTimer.detach( );
GregCr 0:fc538717c96e 276 #endif
GregCr 0:fc538717c96e 277 IsNetworkJoined = true;
GregCr 0:fc538717c96e 278 }
GregCr 0:fc538717c96e 279
GregCr 0:fc538717c96e 280 if( flags->Bits.Tx == 1 )
GregCr 0:fc538717c96e 281 {
GregCr 0:fc538717c96e 282 }
GregCr 0:fc538717c96e 283
GregCr 0:fc538717c96e 284 if( flags->Bits.Rx == 1 )
GregCr 0:fc538717c96e 285 {
GregCr 0:fc538717c96e 286 if( flags->Bits.RxData == true )
GregCr 0:fc538717c96e 287 {
GregCr 0:fc538717c96e 288 ProcessRxFrame( flags, info );
GregCr 0:fc538717c96e 289 }
GregCr 0:fc538717c96e 290 }
GregCr 0:fc538717c96e 291
GregCr 0:fc538717c96e 292 // Schedule a new transmission
GregCr 0:fc538717c96e 293 TxDone = true;
GregCr 0:fc538717c96e 294 }
GregCr 0:fc538717c96e 295
GregCr 0:fc538717c96e 296 /**
GregCr 0:fc538717c96e 297 * Main application entry point.
GregCr 0:fc538717c96e 298 */
GregCr 0:fc538717c96e 299 int main( void )
GregCr 0:fc538717c96e 300 {
GregCr 0:fc538717c96e 301 #if( OVER_THE_AIR_ACTIVATION != 0 )
GregCr 0:fc538717c96e 302 uint8_t sendFrameStatus = 0;
GregCr 0:fc538717c96e 303 #endif
GregCr 0:fc538717c96e 304 bool trySendingFrameAgain = false;
Alliance 7:063ff5895cfe 305 float tempLightValue = 0;
pzheng 12:e47ad0da6112 306
Alliance 7:063ff5895cfe 307 LightMode = 0; // 0: manual, 1: automatic
pzheng 12:e47ad0da6112 308
pzheng 12:e47ad0da6112 309 //comment in 20151130
pzheng 12:e47ad0da6112 310 //buzzer = 0; // 0: OFF, 1: ON
pzheng 12:e47ad0da6112 311 ///*Added by pzheng 20151111*/
pzheng 12:e47ad0da6112 312 //display.write( counter_preparetxframe_log );
pzheng 12:e47ad0da6112 313
Alliance 1:1ef4f6cd800c 314 debug( "\n\n\r LoRaWAN Class A Demo code \n\n\r" );
Alliance 1:1ef4f6cd800c 315
GregCr 0:fc538717c96e 316 BoardInitMcu( );
GregCr 0:fc538717c96e 317 BoardInitPeriph( );
GregCr 0:fc538717c96e 318
GregCr 0:fc538717c96e 319 // Initialize LoRaMac device unique ID
pzheng 12:e47ad0da6112 320 BoardGetUniqueId( DevEui );
Alliance 3:db6ad4aa790d 321
GregCr 0:fc538717c96e 322 LoRaMacEvents.MacEvent = OnMacEvent;
GregCr 0:fc538717c96e 323 LoRaMacInit( &LoRaMacEvents );
GregCr 0:fc538717c96e 324
pzheng 12:e47ad0da6112 325 //Added by pzheng 20151111
pzheng 12:e47ad0da6112 326 float hue = 0.0;
pzheng 12:e47ad0da6112 327 bool up = true;
pzheng 12:e47ad0da6112 328 for(int i = 0; i<16; i++)
pzheng 12:e47ad0da6112 329 {
pzheng 12:e47ad0da6112 330 for (int i=0; i<NUM_LED; i++)
pzheng 12:e47ad0da6112 331 color_led.setColorHSB(i, hue, 1.0, 0.5);
pzheng 12:e47ad0da6112 332
pzheng 12:e47ad0da6112 333
pzheng 12:e47ad0da6112 334 if (up)
pzheng 12:e47ad0da6112 335 hue+= 0.025;
pzheng 12:e47ad0da6112 336 else
pzheng 12:e47ad0da6112 337 hue-= 0.025;
pzheng 12:e47ad0da6112 338
pzheng 12:e47ad0da6112 339 if (hue>=1.0 && up)
pzheng 12:e47ad0da6112 340 up = false;
pzheng 12:e47ad0da6112 341 else if (hue<=0.0 && !up)
pzheng 12:e47ad0da6112 342 up = true;
pzheng 12:e47ad0da6112 343 }
pzheng 12:e47ad0da6112 344
GregCr 0:fc538717c96e 345 IsNetworkJoined = false;
GregCr 0:fc538717c96e 346
pzheng 12:e47ad0da6112 347 #if( OVER_THE_AIR_ACTIVATION == 0 )
GregCr 0:fc538717c96e 348 // Random seed initialization
pzheng 12:e47ad0da6112 349 srand1( RAND_SEED );
GregCr 0:fc538717c96e 350 // Choose a random device address
GregCr 0:fc538717c96e 351 // NwkID = 0
GregCr 0:fc538717c96e 352 // NwkAddr rand [0, 33554431]
Alliance 8:d3a75f9efef8 353 if( ( DevAddr == 0 ) || ( DevAddr == 0xFFFFFFFF ) )
Alliance 8:d3a75f9efef8 354 {
Alliance 8:d3a75f9efef8 355 // Generate random DevAddr if it does not exist
Alliance 8:d3a75f9efef8 356 debug("Generate random DevAddr\n\r");
Alliance 8:d3a75f9efef8 357 DevAddr = randr( 0, 0x01FFFFFF );
Alliance 8:d3a75f9efef8 358 }
Alliance 7:063ff5895cfe 359 debug( "- DevAddr = 0x%x\n\r" , DevAddr);
pzheng 17:60af095c8763 360 LoRaMacInitNwkIds( 0x000000, DevAddr, NwkSKey, AppSKey );
pzheng 17:60af095c8763 361
pzheng 12:e47ad0da6112 362 for(int i = 0; i<16; i++)
pzheng 12:e47ad0da6112 363 {
pzheng 12:e47ad0da6112 364 debug( "- NwkSKey[%d] = 0x%x\n\r" , i, NwkSKey[i]);
pzheng 12:e47ad0da6112 365 }
pzheng 12:e47ad0da6112 366 for(int i = 0; i<16; i++)
pzheng 12:e47ad0da6112 367 {
pzheng 12:e47ad0da6112 368 debug( "- AppSKey[%d] = 0x%x\n\r" , i, AppSKey[i]);
pzheng 12:e47ad0da6112 369 }
GregCr 0:fc538717c96e 370 IsNetworkJoined = true;
GregCr 0:fc538717c96e 371 #endif
GregCr 0:fc538717c96e 372
GregCr 0:fc538717c96e 373 TxNextPacket = true;
GregCr 0:fc538717c96e 374
pzheng 12:e47ad0da6112 375 LoRaMacSetAdrOn( true );
Alliance 1:1ef4f6cd800c 376
Alliance 1:1ef4f6cd800c 377 LoRaMacSetDutyCycleOn( false );
pzheng 12:e47ad0da6112 378
pzheng 12:e47ad0da6112 379 //Added by pzheng 20151111
pzheng 12:e47ad0da6112 380 LoRaMacSetUpLinkTestOn( false );
GregCr 0:fc538717c96e 381
GregCr 0:fc538717c96e 382 while( 1 )
GregCr 0:fc538717c96e 383 {
GregCr 0:fc538717c96e 384 while( IsNetworkJoined == false )
GregCr 0:fc538717c96e 385 {
GregCr 0:fc538717c96e 386 #if( OVER_THE_AIR_ACTIVATION != 0 )
GregCr 0:fc538717c96e 387 if( TxNextPacket == true )
GregCr 0:fc538717c96e 388 {
GregCr 0:fc538717c96e 389 TxNextPacket = false;
GregCr 0:fc538717c96e 390
pzheng 12:e47ad0da6112 391 //Added by pzheng 20151111
pzheng 12:e47ad0da6112 392 for(int i = 0; i<8; i++)
pzheng 12:e47ad0da6112 393 {
pzheng 12:e47ad0da6112 394 //DevEui[i] = 0x10;
pzheng 12:e47ad0da6112 395 debug( "- DevEui[%d] = 0x%x\n\r" , i, DevEui[i]);
pzheng 12:e47ad0da6112 396 }
pzheng 12:e47ad0da6112 397 for(int i = 0; i<8; i++)
pzheng 12:e47ad0da6112 398 {
pzheng 12:e47ad0da6112 399 debug( "- AppEui[%d] = 0x%x\n\r" , i, AppEui[i]);
pzheng 12:e47ad0da6112 400 }
pzheng 12:e47ad0da6112 401 for(int i = 0; i<16; i++)
pzheng 12:e47ad0da6112 402 {
pzheng 12:e47ad0da6112 403 debug( "- AppKey[%d] = 0x%x\n\r" , i, AppKey[i]);
pzheng 12:e47ad0da6112 404 }
GregCr 0:fc538717c96e 405 sendFrameStatus = LoRaMacJoinReq( DevEui, AppEui, AppKey );
Alliance 3:db6ad4aa790d 406 debug("Req Sent\n\r");
GregCr 0:fc538717c96e 407 switch( sendFrameStatus )
GregCr 0:fc538717c96e 408 {
GregCr 0:fc538717c96e 409 case 1: // BUSY
GregCr 0:fc538717c96e 410 break;
GregCr 0:fc538717c96e 411 case 0: // OK
GregCr 0:fc538717c96e 412 case 2: // NO_NETWORK_JOINED
GregCr 0:fc538717c96e 413 case 3: // LENGTH_PORT_ERROR
GregCr 0:fc538717c96e 414 case 4: // MAC_CMD_ERROR
GregCr 0:fc538717c96e 415 case 6: // DEVICE_OFF
GregCr 0:fc538717c96e 416 default:
GregCr 0:fc538717c96e 417 // Relaunch timer for next trial
GregCr 0:fc538717c96e 418 JoinReqTimer.attach_us( &OnJoinReqTimerEvent, OVER_THE_AIR_ACTIVATION_DUTYCYCLE );
GregCr 0:fc538717c96e 419 break;
GregCr 0:fc538717c96e 420 }
GregCr 0:fc538717c96e 421 }
GregCr 0:fc538717c96e 422 // TimerLowPowerHandler( );
GregCr 0:fc538717c96e 423 #endif
GregCr 0:fc538717c96e 424 }
GregCr 0:fc538717c96e 425
GregCr 0:fc538717c96e 426 if( TxDone == true )
GregCr 0:fc538717c96e 427 {
pzheng 12:e47ad0da6112 428 for(int i = 0; i<8; i++)
pzheng 12:e47ad0da6112 429 {
pzheng 12:e47ad0da6112 430 debug( "- DevEui[%d] = 0x%x\n\r" , i, DevEui[i]);
pzheng 12:e47ad0da6112 431 }
GregCr 0:fc538717c96e 432 TxDone = false;
GregCr 0:fc538717c96e 433
GregCr 0:fc538717c96e 434 debug( "TxDone \n\n\r" );
GregCr 0:fc538717c96e 435 // Schedule next packet transmission
GregCr 0:fc538717c96e 436 TxDutyCycleTime = APP_TX_DUTYCYCLE + randr( -APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND );
GregCr 0:fc538717c96e 437 TxNextPacketTimer.attach_us( &OnTxNextPacketTimerEvent, TxDutyCycleTime );
GregCr 0:fc538717c96e 438 }
GregCr 0:fc538717c96e 439
GregCr 0:fc538717c96e 440 if( trySendingFrameAgain == true )
GregCr 0:fc538717c96e 441 {
GregCr 0:fc538717c96e 442 trySendingFrameAgain = SendFrame( );
GregCr 0:fc538717c96e 443 }
GregCr 0:fc538717c96e 444
GregCr 0:fc538717c96e 445 if( TxNextPacket == true )
GregCr 0:fc538717c96e 446 {
GregCr 0:fc538717c96e 447 TxNextPacketTimer.detach( );
GregCr 0:fc538717c96e 448
GregCr 0:fc538717c96e 449 TxNextPacket = false;
GregCr 0:fc538717c96e 450
GregCr 0:fc538717c96e 451 PrepareTxFrame( AppPort );
GregCr 0:fc538717c96e 452
GregCr 0:fc538717c96e 453 trySendingFrameAgain = SendFrame( );
GregCr 0:fc538717c96e 454 }
GregCr 0:fc538717c96e 455
Alliance 7:063ff5895cfe 456 // Read light sensor
Alliance 7:063ff5895cfe 457 tempLightValue = LightSens.read( ) * 1.65;
Alliance 7:063ff5895cfe 458
Alliance 7:063ff5895cfe 459 LightValue = ( 1 - tempLightValue );
Alliance 7:063ff5895cfe 460
Alliance 7:063ff5895cfe 461 // Set automatic RGB from light sensor
Alliance 7:063ff5895cfe 462 if( LightMode == 0 )
Alliance 7:063ff5895cfe 463 {
Alliance 7:063ff5895cfe 464 color_led.setColorRGB( 0, ( uint8_t )( 255 * LightValue ), ( uint8_t )( 255 * LightValue ), ( uint8_t )( 255 * LightValue ) );
Alliance 7:063ff5895cfe 465 }
GregCr 0:fc538717c96e 466 // TimerLowPowerHandler( );
GregCr 0:fc538717c96e 467 }
GregCr 0:fc538717c96e 468 }
GregCr 0:fc538717c96e 469