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