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