/** * Main application entry point. */ int main( void ) { #if( OVER_THE_AIR_ACTIVATION != 0 ) uint8_t sendFrameStatus = 0; #endif bool trySendingFrameAgain = false; // float tempLightValue = 0; // LightMode = 0; // 0: manual, 1: automatic buzzer = 0; // 0: OFF, 1: ON bar.setLevel(0); debug( "\n\n\r LoRaWAN Class A Demo code \n\n\r" ); BoardInitMcu( ); BoardInitPeriph( ); // Initialize LoRaMac device unique ID BoardGetUniqueId( DevEui ); LoRaMacEvents.MacEvent = OnMacEvent; LoRaMacInit( &LoRaMacEvents ); IsNetworkJoined = false; #if( OVER_THE_AIR_ACTIVATION == 0 ) // Random seed initialization srand( RAND_SEED ); // Choose a random device address // NwkID = 0 // NwkAddr rand [0, 33554431] if( ( DevAddr == 0 ) || ( DevAddr == 0xFFFFFFFF ) ) { // Generate random DevAddr if it does not exist debug("Generate random DevAddr\n\r"); DevAddr = randr( 0, 0x01FFFFFF ); } debug( "- DevAddr = 0x%x\n\r" , DevAddr); LoRaMacInitNwkIds( 0x000000, DevAddr, NwkSKey, AppSKey ); IsNetworkJoined = true; #endif TxNextPacket = true; LoRaMacSetAdrOn( false ); LoRaMacSetDutyCycleOn( false ); while( 1 ) { while( IsNetworkJoined == false ) { #if( OVER_THE_AIR_ACTIVATION != 0 ) if( TxNextPacket == true ) { TxNextPacket = false; sendFrameStatus = LoRaMacJoinReq( DevEui, AppEui, AppKey ); debug("Req Sent\n\r"); switch( sendFrameStatus ) { case 1: // BUSY break; case 0: // OK case 2: // NO_NETWORK_JOINED case 3: // LENGTH_PORT_ERROR case 4: // MAC_CMD_ERROR case 6: // DEVICE_OFF default: // Relaunch timer for next trial JoinReqTimer.attach_us( &OnJoinReqTimerEvent, OVER_THE_AIR_ACTIVATION_DUTYCYCLE ); break; } } // TimerLowPowerHandler( ); #endif } if( TxDone == true ) { TxDone = false; debug( "TxDone \n\n\r" ); // Schedule next packet transmission TxDutyCycleTime = APP_TX_DUTYCYCLE + randr( -APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND ); TxNextPacketTimer.attach_us( &OnTxNextPacketTimerEvent, TxDutyCycleTime ); } if( trySendingFrameAgain == true ) { trySendingFrameAgain = SendFrame( ); } if( TxNextPacket == true ) { TxNextPacketTimer.detach( ); TxNextPacket = false; PrepareTxFrame( AppPort ); trySendingFrameAgain = SendFrame( ); } /* Read light sensor tempLightValue = LightSens.read( ) * 1.65; LightValue = ( 1 - tempLightValue ); // Set automatic RGB from light sensor if( LightMode == 0 ) { color_led.setColorRGB( 0, ( uint8_t )( 255 * LightValue ), ( uint8_t )( 255 * LightValue ), ( uint8_t )( 255 * LightValue ) ); }*/ // TimerLowPowerHandler( ); } }
/** * Main application entry point. */ int main( void ) { uint8_t sendFrameStatus = 0; uint8_t batteryLevel = 0; BoardInitMcu( ); BoardInitPeriph( ); // Initialize LoRaMac device unique ID BoardGetUniqueId( DevEui ); LoRaMacEvents.MacEvent = OnMacEvent; LoRaMacInit( &LoRaMacEvents ); IsNetworkJoined = false; #if( OVER_THE_AIR_ACTIVATION == 0 ) // Random seed initialization srand( RAND_SEED ); // Choose a random device address // NwkID = 0 // NwkAddr rand [0, 33554431] DevAddr = randr( 0, 0x01FFFFFF ); LoRaMacInitNwkIds( 0x000000, DevAddr, NwkSKey, AppSKey ); IsNetworkJoined = true; #else // Sends a JoinReq Command every 5 seconds until the network is joined TimerInit( &JoinReqTimer, OnJoinReqTimerEvent ); TimerSetValue( &JoinReqTimer, OVER_THE_AIR_ACTIVATION_DUTYCYCLE ); TimerStart( &JoinReqTimer ); #endif TxNextPacket = true; TimerInit( &TxNextPacketTimer, OnTxNextPacketTimerEvent ); TimerInit( &Led1Timer, OnLed1TimerEvent ); TimerSetValue( &Led1Timer, 25000 ); TimerInit( &Led2Timer, OnLed2TimerEvent ); TimerSetValue( &Led2Timer, 25000 ); LoRaMacSetAdrOn( true ); while( 1 ) { while( IsNetworkJoined == false ) { #if( OVER_THE_AIR_ACTIVATION != 0 ) if( TxNextPacket == true ) { TxNextPacket = false; LoRaMacJoinReq( DevEui, AppEui, AppKey ); // Relaunch timer for next trial TimerStart( &JoinReqTimer ); } TimerLowPowerHandler( ); #endif } if( Led1TimerEvent == true ) { Led1TimerEvent = false; // Switch LED 1 OFF GpioWrite( &Led1, 1 ); } if( Led2TimerEvent == true ) { Led2TimerEvent = false; // Switch LED 2 OFF GpioWrite( &Led2, 1 ); } if( TxAckReceived == true ) { TxAckReceived = false; // Switch LED 2 ON GpioWrite( &Led2, 0 ); TimerStart( &Led2Timer ); } if( RxDone == true ) { RxDone = false; // Switch LED 2 ON GpioWrite( &Led2, 0 ); TimerStart( &Led2Timer ); if( AppLedStateOn == true ) { // Switch LED 3 ON GpioWrite( &Led2, 0 ); } else { // Switch LED 3 OFF GpioWrite( &Led2, 1 ); } } if( TxDone == true ) { TxDone = false; // Schedule next packet transmission TxDutyCycleTime = APP_TX_DUTYCYCLE + randr( -APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND ); TimerSetValue( &TxNextPacketTimer, TxDutyCycleTime ); TimerStart( &TxNextPacketTimer ); } if( TxNextPacket == true ) { TxNextPacket = false; batteryLevel = BoardMeasureBatterieLevel( ); // 1 (very low) to 254 (fully charged) // Switch LED 1 ON GpioWrite( &Led1, 0 ); TimerStart( &Led1Timer ); AppData[0] = AppLedStateOn; // // // AppData[7] = batteryLevel; sendFrameStatus = LoRaMacSendFrame( 2, AppData, APP_DATA_SIZE ); //sendFrameStatus = LoRaMacSendConfirmedFrame( 2, AppData, APP_DATA_SIZE, 8 ); switch( sendFrameStatus ) { case 3: // LENGTH_PORT_ERROR case 4: // MAC_CMD_ERROR case 5: // NO_FREE_CHANNEL // Schedule a new transmission TxDone = true; break; default: break; } } TimerLowPowerHandler( ); } }