Fork from LoRa Alliance program. available here: https://developer.mbed.org/users/Alliance/code/LoRaWAN/
Dependencies: LoRaMacLib SX1276Lib mbed Chainable_RGB_LED DigitDisplay
Fork of LoRaWAN by
main.cpp@8:d3a75f9efef8, 2015-10-27 (annotated)
- Committer:
- Alliance
- Date:
- Tue Oct 27 12:11:52 2015 +0000
- Revision:
- 8:d3a75f9efef8
- Parent:
- 7:063ff5895cfe
- Child:
- 9:36c624bb5d98
_-+
Who changed what in which revision?
User | Revision | Line number | New 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 | |
GregCr | 0:fc538717c96e | 22 | /*! |
GregCr | 0:fc538717c96e | 23 | * When set to 1 the application uses the Over-the-Air activation procedure |
GregCr | 0:fc538717c96e | 24 | * When set to 0 the application uses the Personalization activation procedure |
GregCr | 0:fc538717c96e | 25 | */ |
Alliance | 3:db6ad4aa790d | 26 | #define OVER_THE_AIR_ACTIVATION 1 |
GregCr | 0:fc538717c96e | 27 | |
GregCr | 0:fc538717c96e | 28 | /*! |
GregCr | 0:fc538717c96e | 29 | * Mote device IEEE EUI |
GregCr | 0:fc538717c96e | 30 | */ |
GregCr | 0:fc538717c96e | 31 | static uint8_t DevEui[] = |
GregCr | 0:fc538717c96e | 32 | { |
Alliance | 8:d3a75f9efef8 | 33 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 |
GregCr | 0:fc538717c96e | 34 | }; |
GregCr | 0:fc538717c96e | 35 | |
GregCr | 0:fc538717c96e | 36 | #if( OVER_THE_AIR_ACTIVATION != 0 ) |
GregCr | 0:fc538717c96e | 37 | |
Alliance | 6:ea71f564e0ce | 38 | #define OVER_THE_AIR_ACTIVATION_DUTYCYCLE 7000000 // 7 [s] value in us |
GregCr | 0:fc538717c96e | 39 | |
GregCr | 0:fc538717c96e | 40 | /*! |
GregCr | 0:fc538717c96e | 41 | * Application IEEE EUI |
GregCr | 0:fc538717c96e | 42 | */ |
GregCr | 0:fc538717c96e | 43 | static uint8_t AppEui[] = |
GregCr | 0:fc538717c96e | 44 | { |
GregCr | 0:fc538717c96e | 45 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 |
GregCr | 0:fc538717c96e | 46 | }; |
GregCr | 0:fc538717c96e | 47 | |
GregCr | 0:fc538717c96e | 48 | /*! |
GregCr | 0:fc538717c96e | 49 | * AES encryption/decryption cipher application key |
GregCr | 0:fc538717c96e | 50 | */ |
GregCr | 0:fc538717c96e | 51 | static uint8_t AppKey[] = |
GregCr | 0:fc538717c96e | 52 | { |
Alliance | 8:d3a75f9efef8 | 53 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
Alliance | 8:d3a75f9efef8 | 54 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 |
GregCr | 0:fc538717c96e | 55 | }; |
GregCr | 0:fc538717c96e | 56 | |
GregCr | 0:fc538717c96e | 57 | #else |
GregCr | 0:fc538717c96e | 58 | |
GregCr | 0:fc538717c96e | 59 | /*! |
GregCr | 0:fc538717c96e | 60 | * AES encryption/decryption cipher network session key |
GregCr | 0:fc538717c96e | 61 | */ |
GregCr | 0:fc538717c96e | 62 | static uint8_t NwkSKey[] = |
GregCr | 0:fc538717c96e | 63 | { |
Alliance | 3:db6ad4aa790d | 64 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
Alliance | 4:0c164d628006 | 65 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 |
GregCr | 0:fc538717c96e | 66 | }; |
GregCr | 0:fc538717c96e | 67 | |
GregCr | 0:fc538717c96e | 68 | /*! |
GregCr | 0:fc538717c96e | 69 | * AES encryption/decryption cipher application session key |
GregCr | 0:fc538717c96e | 70 | */ |
GregCr | 0:fc538717c96e | 71 | static uint8_t AppSKey[] = |
GregCr | 0:fc538717c96e | 72 | { |
Alliance | 3:db6ad4aa790d | 73 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
Alliance | 4:0c164d628006 | 74 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 |
GregCr | 0:fc538717c96e | 75 | }; |
GregCr | 0:fc538717c96e | 76 | |
GregCr | 0:fc538717c96e | 77 | /*! |
GregCr | 0:fc538717c96e | 78 | * Device address |
GregCr | 0:fc538717c96e | 79 | */ |
Alliance | 8:d3a75f9efef8 | 80 | static uint32_t DevAddr = 0x00001091; |
Alliance | 8:d3a75f9efef8 | 81 | |
GregCr | 0:fc538717c96e | 82 | |
GregCr | 0:fc538717c96e | 83 | #endif |
GregCr | 0:fc538717c96e | 84 | |
GregCr | 0:fc538717c96e | 85 | /*! |
GregCr | 0:fc538717c96e | 86 | * Indicates if the MAC layer has already joined a network. |
GregCr | 0:fc538717c96e | 87 | */ |
GregCr | 0:fc538717c96e | 88 | static bool IsNetworkJoined = false; |
GregCr | 0:fc538717c96e | 89 | |
GregCr | 0:fc538717c96e | 90 | /*! |
GregCr | 0:fc538717c96e | 91 | * Defines the application data transmission duty cycle |
GregCr | 0:fc538717c96e | 92 | */ |
Alliance | 6:ea71f564e0ce | 93 | #define APP_TX_DUTYCYCLE 5000000 // 6 [s] value in us |
Alliance | 3:db6ad4aa790d | 94 | #define APP_TX_DUTYCYCLE_RND 2000000 // 2 [s] value in us |
GregCr | 0:fc538717c96e | 95 | |
GregCr | 0:fc538717c96e | 96 | /*! |
GregCr | 0:fc538717c96e | 97 | * User application data buffer size |
GregCr | 0:fc538717c96e | 98 | */ |
Alliance | 7:063ff5895cfe | 99 | #define APP_DATA_SIZE 5 |
GregCr | 0:fc538717c96e | 100 | |
GregCr | 0:fc538717c96e | 101 | /*! |
GregCr | 0:fc538717c96e | 102 | * User application data |
GregCr | 0:fc538717c96e | 103 | */ |
GregCr | 0:fc538717c96e | 104 | static uint8_t AppData[APP_DATA_SIZE]; |
GregCr | 0:fc538717c96e | 105 | |
GregCr | 0:fc538717c96e | 106 | /*! |
GregCr | 0:fc538717c96e | 107 | * Defines the application data transmission duty cycle |
GregCr | 0:fc538717c96e | 108 | */ |
GregCr | 0:fc538717c96e | 109 | static uint32_t TxDutyCycleTime; |
GregCr | 0:fc538717c96e | 110 | |
GregCr | 0:fc538717c96e | 111 | Ticker TxNextPacketTimer; |
GregCr | 0:fc538717c96e | 112 | |
GregCr | 0:fc538717c96e | 113 | #if( OVER_THE_AIR_ACTIVATION != 0 ) |
GregCr | 0:fc538717c96e | 114 | |
GregCr | 0:fc538717c96e | 115 | /*! |
GregCr | 0:fc538717c96e | 116 | * Defines the join request timer |
GregCr | 0:fc538717c96e | 117 | */ |
GregCr | 0:fc538717c96e | 118 | Ticker JoinReqTimer; |
GregCr | 0:fc538717c96e | 119 | |
GregCr | 0:fc538717c96e | 120 | #endif |
GregCr | 0:fc538717c96e | 121 | |
GregCr | 0:fc538717c96e | 122 | /*! |
GregCr | 0:fc538717c96e | 123 | * Indicates if a new packet can be sent |
GregCr | 0:fc538717c96e | 124 | */ |
GregCr | 0:fc538717c96e | 125 | static bool TxNextPacket = true; |
GregCr | 0:fc538717c96e | 126 | static bool TxDone = false; |
GregCr | 0:fc538717c96e | 127 | |
Alliance | 3:db6ad4aa790d | 128 | static uint8_t AppPort = 3; |
GregCr | 0:fc538717c96e | 129 | static uint8_t AppDataSize = APP_DATA_SIZE; |
GregCr | 0:fc538717c96e | 130 | |
GregCr | 0:fc538717c96e | 131 | static LoRaMacEvent_t LoRaMacEvents; |
GregCr | 0:fc538717c96e | 132 | |
Alliance | 7:063ff5895cfe | 133 | static float LightValue = 0.0; |
Alliance | 7:063ff5895cfe | 134 | static uint8_t LightMode = 0; // 0:automatic, 1:manual |
Alliance | 7:063ff5895cfe | 135 | |
GregCr | 0:fc538717c96e | 136 | Ticker Led1Timer; |
GregCr | 0:fc538717c96e | 137 | Ticker Led2Timer; |
Alliance | 1:1ef4f6cd800c | 138 | Ticker BuzTimer; |
GregCr | 0:fc538717c96e | 139 | |
GregCr | 0:fc538717c96e | 140 | #define NUM_LED 3 |
GregCr | 0:fc538717c96e | 141 | |
Alliance | 3:db6ad4aa790d | 142 | |
Alliance | 7:063ff5895cfe | 143 | AnalogIn LightSens( A1 ); |
Alliance | 6:ea71f564e0ce | 144 | ChainableLED color_led(D6, D7, NUM_LED); |
Alliance | 6:ea71f564e0ce | 145 | DigitDisplay display(D8, D9); |
GregCr | 0:fc538717c96e | 146 | DigitalOut buzzer(A2); |
GregCr | 0:fc538717c96e | 147 | |
Alliance | 3:db6ad4aa790d | 148 | |
Alliance | 1:1ef4f6cd800c | 149 | static void OnBuzTimerEvent( void ) |
Alliance | 1:1ef4f6cd800c | 150 | { |
Alliance | 1:1ef4f6cd800c | 151 | buzzer = 0; |
Alliance | 1:1ef4f6cd800c | 152 | BuzTimer.detach( ); |
Alliance | 1:1ef4f6cd800c | 153 | } |
GregCr | 0:fc538717c96e | 154 | |
GregCr | 0:fc538717c96e | 155 | /*! |
GregCr | 0:fc538717c96e | 156 | * |
GregCr | 0:fc538717c96e | 157 | */ |
GregCr | 0:fc538717c96e | 158 | static void PrepareTxFrame( uint8_t port ) |
GregCr | 0:fc538717c96e | 159 | { |
Alliance | 7:063ff5895cfe | 160 | uint32_t tempValue = (uint32_t)(LightValue*1000000.0); |
Alliance | 7:063ff5895cfe | 161 | |
Alliance | 7:063ff5895cfe | 162 | debug( "[Tx] LightValue=%d 0x%x\n\r" , tempValue, tempValue); |
Alliance | 7:063ff5895cfe | 163 | |
Alliance | 7:063ff5895cfe | 164 | AppData[0] = LightMode; |
Alliance | 7:063ff5895cfe | 165 | AppData[1] = ((tempValue&0xFF000000)>>24)&0xFF; |
Alliance | 7:063ff5895cfe | 166 | AppData[2] = ((tempValue&0x00FF0000)>>16)&0xFF; |
Alliance | 7:063ff5895cfe | 167 | AppData[3] = ((tempValue&0x0000FF00)>>8)&0xFF; |
Alliance | 7:063ff5895cfe | 168 | AppData[4] = (tempValue&0x000000FF); |
GregCr | 0:fc538717c96e | 169 | } |
GregCr | 0:fc538717c96e | 170 | |
GregCr | 0:fc538717c96e | 171 | static void ProcessRxFrame( LoRaMacEventFlags_t *flags, LoRaMacEventInfo_t *info ) |
GregCr | 0:fc538717c96e | 172 | { |
Alliance | 7:063ff5895cfe | 173 | debug( "[Rx] Port=%d\n\r" , info->RxPort); |
GregCr | 0:fc538717c96e | 174 | switch( info->RxPort ) // Check Rx port number |
GregCr | 0:fc538717c96e | 175 | { |
GregCr | 0:fc538717c96e | 176 | case 10: |
Alliance | 4:0c164d628006 | 177 | display.write( 0, info->RxBuffer[0] ); |
Alliance | 1:1ef4f6cd800c | 178 | display.write( 1, info->RxBuffer[1] ); |
Alliance | 1:1ef4f6cd800c | 179 | display.write( 2, info->RxBuffer[2] ); |
Alliance | 4:0c164d628006 | 180 | display.write( 3, info->RxBuffer[3] ); |
GregCr | 0:fc538717c96e | 181 | break; |
GregCr | 0:fc538717c96e | 182 | |
Alliance | 1:1ef4f6cd800c | 183 | case 20: |
Alliance | 7:063ff5895cfe | 184 | LightMode = info->RxBuffer[0]; |
Alliance | 7:063ff5895cfe | 185 | 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 | 186 | if( LightMode ) |
Alliance | 7:063ff5895cfe | 187 | { |
Alliance | 7:063ff5895cfe | 188 | color_led.setColorRGB(0, info->RxBuffer[1], info->RxBuffer[2], info->RxBuffer[3] ); |
Alliance | 7:063ff5895cfe | 189 | } |
Alliance | 1:1ef4f6cd800c | 190 | break; |
GregCr | 0:fc538717c96e | 191 | |
Alliance | 1:1ef4f6cd800c | 192 | case 30: |
Alliance | 1:1ef4f6cd800c | 193 | BuzTimer.attach_us( &OnBuzTimerEvent, 500000 ); |
Alliance | 1:1ef4f6cd800c | 194 | buzzer = 1; |
GregCr | 0:fc538717c96e | 195 | break; |
GregCr | 0:fc538717c96e | 196 | |
GregCr | 0:fc538717c96e | 197 | default: |
GregCr | 0:fc538717c96e | 198 | break; |
GregCr | 0:fc538717c96e | 199 | } |
GregCr | 0:fc538717c96e | 200 | } |
GregCr | 0:fc538717c96e | 201 | |
GregCr | 0:fc538717c96e | 202 | static bool SendFrame( void ) |
GregCr | 0:fc538717c96e | 203 | { |
GregCr | 0:fc538717c96e | 204 | uint8_t sendFrameStatus = 0; |
GregCr | 0:fc538717c96e | 205 | |
GregCr | 0:fc538717c96e | 206 | sendFrameStatus = LoRaMacSendFrame( AppPort, AppData, AppDataSize ); |
GregCr | 0:fc538717c96e | 207 | // sendFrameStatus = LoRaMacSendConfirmedFrame( AppPort, AppData, AppDataSize, 8 ); |
GregCr | 0:fc538717c96e | 208 | switch( sendFrameStatus ) |
GregCr | 0:fc538717c96e | 209 | { |
GregCr | 0:fc538717c96e | 210 | case 5: // NO_FREE_CHANNEL |
GregCr | 0:fc538717c96e | 211 | // Try again later |
GregCr | 0:fc538717c96e | 212 | return true; |
GregCr | 0:fc538717c96e | 213 | default: |
GregCr | 0:fc538717c96e | 214 | return false; |
GregCr | 0:fc538717c96e | 215 | } |
GregCr | 0:fc538717c96e | 216 | } |
GregCr | 0:fc538717c96e | 217 | |
GregCr | 0:fc538717c96e | 218 | |
GregCr | 0:fc538717c96e | 219 | #if( OVER_THE_AIR_ACTIVATION != 0 ) |
GregCr | 0:fc538717c96e | 220 | /*! |
GregCr | 0:fc538717c96e | 221 | * \brief Function executed on JoinReq Timeout event |
GregCr | 0:fc538717c96e | 222 | */ |
GregCr | 0:fc538717c96e | 223 | static void OnJoinReqTimerEvent( void ) |
GregCr | 0:fc538717c96e | 224 | { |
GregCr | 0:fc538717c96e | 225 | TxNextPacket = true; |
Alliance | 1:1ef4f6cd800c | 226 | JoinReqTimer.detach( ); |
GregCr | 0:fc538717c96e | 227 | } |
GregCr | 0:fc538717c96e | 228 | #endif |
GregCr | 0:fc538717c96e | 229 | |
GregCr | 0:fc538717c96e | 230 | |
GregCr | 0:fc538717c96e | 231 | /*! |
GregCr | 0:fc538717c96e | 232 | * \brief Function executed on TxNextPacket Timeout event |
GregCr | 0:fc538717c96e | 233 | */ |
GregCr | 0:fc538717c96e | 234 | static void OnTxNextPacketTimerEvent( void ) |
Alliance | 1:1ef4f6cd800c | 235 | { |
GregCr | 0:fc538717c96e | 236 | TxNextPacket = true; |
Alliance | 1:1ef4f6cd800c | 237 | TxNextPacketTimer.detach( ); |
GregCr | 0:fc538717c96e | 238 | } |
GregCr | 0:fc538717c96e | 239 | |
GregCr | 0:fc538717c96e | 240 | |
GregCr | 0:fc538717c96e | 241 | /*! |
GregCr | 0:fc538717c96e | 242 | * \brief Function to be executed on MAC layer event |
GregCr | 0:fc538717c96e | 243 | */ |
GregCr | 0:fc538717c96e | 244 | static void OnMacEvent( LoRaMacEventFlags_t *flags, LoRaMacEventInfo_t *info ) |
GregCr | 0:fc538717c96e | 245 | { |
GregCr | 0:fc538717c96e | 246 | if( flags->Bits.JoinAccept == 1 ) |
GregCr | 0:fc538717c96e | 247 | { |
GregCr | 0:fc538717c96e | 248 | #if( OVER_THE_AIR_ACTIVATION != 0 ) |
GregCr | 0:fc538717c96e | 249 | JoinReqTimer.detach( ); |
GregCr | 0:fc538717c96e | 250 | #endif |
GregCr | 0:fc538717c96e | 251 | IsNetworkJoined = true; |
GregCr | 0:fc538717c96e | 252 | } |
GregCr | 0:fc538717c96e | 253 | |
GregCr | 0:fc538717c96e | 254 | if( flags->Bits.Tx == 1 ) |
GregCr | 0:fc538717c96e | 255 | { |
GregCr | 0:fc538717c96e | 256 | } |
GregCr | 0:fc538717c96e | 257 | |
GregCr | 0:fc538717c96e | 258 | if( flags->Bits.Rx == 1 ) |
GregCr | 0:fc538717c96e | 259 | { |
GregCr | 0:fc538717c96e | 260 | if( flags->Bits.RxData == true ) |
GregCr | 0:fc538717c96e | 261 | { |
GregCr | 0:fc538717c96e | 262 | ProcessRxFrame( flags, info ); |
GregCr | 0:fc538717c96e | 263 | } |
GregCr | 0:fc538717c96e | 264 | } |
GregCr | 0:fc538717c96e | 265 | |
GregCr | 0:fc538717c96e | 266 | // Schedule a new transmission |
GregCr | 0:fc538717c96e | 267 | TxDone = true; |
GregCr | 0:fc538717c96e | 268 | } |
GregCr | 0:fc538717c96e | 269 | |
GregCr | 0:fc538717c96e | 270 | /** |
GregCr | 0:fc538717c96e | 271 | * Main application entry point. |
GregCr | 0:fc538717c96e | 272 | */ |
GregCr | 0:fc538717c96e | 273 | int main( void ) |
GregCr | 0:fc538717c96e | 274 | { |
GregCr | 0:fc538717c96e | 275 | #if( OVER_THE_AIR_ACTIVATION != 0 ) |
GregCr | 0:fc538717c96e | 276 | uint8_t sendFrameStatus = 0; |
GregCr | 0:fc538717c96e | 277 | #endif |
GregCr | 0:fc538717c96e | 278 | bool trySendingFrameAgain = false; |
Alliance | 7:063ff5895cfe | 279 | float tempLightValue = 0; |
Alliance | 7:063ff5895cfe | 280 | |
Alliance | 7:063ff5895cfe | 281 | LightMode = 0; // 0: manual, 1: automatic |
Alliance | 7:063ff5895cfe | 282 | buzzer = 0; // 0: OFF, 1: ON |
Alliance | 1:1ef4f6cd800c | 283 | |
Alliance | 1:1ef4f6cd800c | 284 | debug( "\n\n\r LoRaWAN Class A Demo code \n\n\r" ); |
Alliance | 1:1ef4f6cd800c | 285 | |
GregCr | 0:fc538717c96e | 286 | BoardInitMcu( ); |
GregCr | 0:fc538717c96e | 287 | BoardInitPeriph( ); |
GregCr | 0:fc538717c96e | 288 | |
GregCr | 0:fc538717c96e | 289 | // Initialize LoRaMac device unique ID |
Alliance | 3:db6ad4aa790d | 290 | // BoardGetUniqueId( DevEui ); |
Alliance | 3:db6ad4aa790d | 291 | |
GregCr | 0:fc538717c96e | 292 | LoRaMacEvents.MacEvent = OnMacEvent; |
GregCr | 0:fc538717c96e | 293 | LoRaMacInit( &LoRaMacEvents ); |
GregCr | 0:fc538717c96e | 294 | |
GregCr | 0:fc538717c96e | 295 | IsNetworkJoined = false; |
GregCr | 0:fc538717c96e | 296 | |
GregCr | 0:fc538717c96e | 297 | #if( OVER_THE_AIR_ACTIVATION == 0 ) |
GregCr | 0:fc538717c96e | 298 | // Random seed initialization |
GregCr | 0:fc538717c96e | 299 | srand( RAND_SEED ); |
GregCr | 0:fc538717c96e | 300 | // Choose a random device address |
GregCr | 0:fc538717c96e | 301 | // NwkID = 0 |
GregCr | 0:fc538717c96e | 302 | // NwkAddr rand [0, 33554431] |
Alliance | 8:d3a75f9efef8 | 303 | if( ( DevAddr == 0 ) || ( DevAddr == 0xFFFFFFFF ) ) |
Alliance | 8:d3a75f9efef8 | 304 | { |
Alliance | 8:d3a75f9efef8 | 305 | // Generate random DevAddr if it does not exist |
Alliance | 8:d3a75f9efef8 | 306 | debug("Generate random DevAddr\n\r"); |
Alliance | 8:d3a75f9efef8 | 307 | DevAddr = randr( 0, 0x01FFFFFF ); |
Alliance | 8:d3a75f9efef8 | 308 | } |
Alliance | 7:063ff5895cfe | 309 | debug( "- DevAddr = 0x%x\n\r" , DevAddr); |
GregCr | 0:fc538717c96e | 310 | LoRaMacInitNwkIds( 0x000000, DevAddr, NwkSKey, AppSKey ); |
GregCr | 0:fc538717c96e | 311 | IsNetworkJoined = true; |
GregCr | 0:fc538717c96e | 312 | #endif |
GregCr | 0:fc538717c96e | 313 | |
GregCr | 0:fc538717c96e | 314 | TxNextPacket = true; |
GregCr | 0:fc538717c96e | 315 | |
Alliance | 1:1ef4f6cd800c | 316 | LoRaMacSetAdrOn( false ); |
Alliance | 1:1ef4f6cd800c | 317 | |
Alliance | 1:1ef4f6cd800c | 318 | LoRaMacSetDutyCycleOn( false ); |
GregCr | 0:fc538717c96e | 319 | |
GregCr | 0:fc538717c96e | 320 | while( 1 ) |
GregCr | 0:fc538717c96e | 321 | { |
GregCr | 0:fc538717c96e | 322 | while( IsNetworkJoined == false ) |
GregCr | 0:fc538717c96e | 323 | { |
GregCr | 0:fc538717c96e | 324 | #if( OVER_THE_AIR_ACTIVATION != 0 ) |
GregCr | 0:fc538717c96e | 325 | if( TxNextPacket == true ) |
GregCr | 0:fc538717c96e | 326 | { |
GregCr | 0:fc538717c96e | 327 | TxNextPacket = false; |
GregCr | 0:fc538717c96e | 328 | |
GregCr | 0:fc538717c96e | 329 | sendFrameStatus = LoRaMacJoinReq( DevEui, AppEui, AppKey ); |
Alliance | 3:db6ad4aa790d | 330 | debug("Req Sent\n\r"); |
GregCr | 0:fc538717c96e | 331 | switch( sendFrameStatus ) |
GregCr | 0:fc538717c96e | 332 | { |
GregCr | 0:fc538717c96e | 333 | case 1: // BUSY |
GregCr | 0:fc538717c96e | 334 | break; |
GregCr | 0:fc538717c96e | 335 | case 0: // OK |
GregCr | 0:fc538717c96e | 336 | case 2: // NO_NETWORK_JOINED |
GregCr | 0:fc538717c96e | 337 | case 3: // LENGTH_PORT_ERROR |
GregCr | 0:fc538717c96e | 338 | case 4: // MAC_CMD_ERROR |
GregCr | 0:fc538717c96e | 339 | case 6: // DEVICE_OFF |
GregCr | 0:fc538717c96e | 340 | default: |
GregCr | 0:fc538717c96e | 341 | // Relaunch timer for next trial |
GregCr | 0:fc538717c96e | 342 | JoinReqTimer.attach_us( &OnJoinReqTimerEvent, OVER_THE_AIR_ACTIVATION_DUTYCYCLE ); |
GregCr | 0:fc538717c96e | 343 | break; |
GregCr | 0:fc538717c96e | 344 | } |
GregCr | 0:fc538717c96e | 345 | } |
GregCr | 0:fc538717c96e | 346 | // TimerLowPowerHandler( ); |
GregCr | 0:fc538717c96e | 347 | #endif |
GregCr | 0:fc538717c96e | 348 | } |
GregCr | 0:fc538717c96e | 349 | |
GregCr | 0:fc538717c96e | 350 | if( TxDone == true ) |
GregCr | 0:fc538717c96e | 351 | { |
GregCr | 0:fc538717c96e | 352 | |
GregCr | 0:fc538717c96e | 353 | TxDone = false; |
GregCr | 0:fc538717c96e | 354 | |
GregCr | 0:fc538717c96e | 355 | debug( "TxDone \n\n\r" ); |
GregCr | 0:fc538717c96e | 356 | // Schedule next packet transmission |
GregCr | 0:fc538717c96e | 357 | TxDutyCycleTime = APP_TX_DUTYCYCLE + randr( -APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND ); |
GregCr | 0:fc538717c96e | 358 | TxNextPacketTimer.attach_us( &OnTxNextPacketTimerEvent, TxDutyCycleTime ); |
GregCr | 0:fc538717c96e | 359 | } |
GregCr | 0:fc538717c96e | 360 | |
GregCr | 0:fc538717c96e | 361 | if( trySendingFrameAgain == true ) |
GregCr | 0:fc538717c96e | 362 | { |
GregCr | 0:fc538717c96e | 363 | trySendingFrameAgain = SendFrame( ); |
GregCr | 0:fc538717c96e | 364 | } |
GregCr | 0:fc538717c96e | 365 | |
GregCr | 0:fc538717c96e | 366 | if( TxNextPacket == true ) |
GregCr | 0:fc538717c96e | 367 | { |
GregCr | 0:fc538717c96e | 368 | TxNextPacketTimer.detach( ); |
GregCr | 0:fc538717c96e | 369 | |
GregCr | 0:fc538717c96e | 370 | TxNextPacket = false; |
GregCr | 0:fc538717c96e | 371 | |
GregCr | 0:fc538717c96e | 372 | PrepareTxFrame( AppPort ); |
GregCr | 0:fc538717c96e | 373 | |
GregCr | 0:fc538717c96e | 374 | trySendingFrameAgain = SendFrame( ); |
GregCr | 0:fc538717c96e | 375 | } |
GregCr | 0:fc538717c96e | 376 | |
Alliance | 7:063ff5895cfe | 377 | // Read light sensor |
Alliance | 7:063ff5895cfe | 378 | tempLightValue = LightSens.read( ) * 1.65; |
Alliance | 7:063ff5895cfe | 379 | |
Alliance | 7:063ff5895cfe | 380 | LightValue = ( 1 - tempLightValue ); |
Alliance | 7:063ff5895cfe | 381 | |
Alliance | 7:063ff5895cfe | 382 | // Set automatic RGB from light sensor |
Alliance | 7:063ff5895cfe | 383 | if( LightMode == 0 ) |
Alliance | 7:063ff5895cfe | 384 | { |
Alliance | 7:063ff5895cfe | 385 | color_led.setColorRGB( 0, ( uint8_t )( 255 * LightValue ), ( uint8_t )( 255 * LightValue ), ( uint8_t )( 255 * LightValue ) ); |
Alliance | 7:063ff5895cfe | 386 | } |
GregCr | 0:fc538717c96e | 387 | // TimerLowPowerHandler( ); |
GregCr | 0:fc538717c96e | 388 | } |
GregCr | 0:fc538717c96e | 389 | } |
GregCr | 0:fc538717c96e | 390 |