示例#1
0
/**
 * 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( );
	}
}
示例#2
0
/**
 * 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( );
    }
}
示例#3
0
/**
 * Main application entry point.
 */
int main( void )
{
    //                                   LC1        LC2        LC3        LC4        LC5        LC6        LC7        LC8
    const uint32_t channelsFreq[] = { 868100000, 868300000, 868500000, 868650000, 868800000, 869100000, 869250000, 869400000 };
    const uint8_t  channelsDatarate[] = { DR_SF7, DR_SF10, DR_SF12 };

    uint8_t channelNb = ( sizeof( channelsFreq ) / sizeof( uint32_t ) );

    uint8_t tstState = 0;
    int16_t pktCnt = 15;
    ChannelParams_t channel;
    LoRaMacHeader_t macHdr;
    LoRaMacFrameCtrl_t fCtrl;
    uint8_t channelsIndex = 0;
    uint8_t datarateIndex = 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 );
#endif

    TxNextPacket = true;
    TimerInit( &TxNextPacketTimer, OnTxNextPacketTimerEvent );
    
    TimerInit( &Led1Timer, OnLed1TimerEvent ); 
    TimerSetValue( &Led1Timer, 25000 );

    // Low power timer to be run when tests are finished.
    TimerInit( &StopTimer, OnStopTimerEvent ); 
    TimerSetValue( &StopTimer, 3.6e9 ); // wakes up the microcontroller every hour

    // Initialize MAC frame
    macHdr.Value = 0;
#if defined( LORAMAC_R3 )
    macHdr.Bits.MType = FRAME_TYPE_DATA_UNCONFIRMED_UP;
#else
    macHdr.Bits.MType = FRAME_TYPE_DATA_UNCONFIRMED;
#endif
    fCtrl.Value = 0;
    fCtrl.Bits.OptionsLength = 0;
    fCtrl.Bits.FPending      = 0;
    fCtrl.Bits.Ack           = false;
    fCtrl.Bits.AdrAckReq     = false;
    fCtrl.Bits.Adr           = false;

    // Initialize channel
    channel.DrRange.Fields.Min = DR_SF12;
    channel.DrRange.Fields.Min = DR_SF7;
    channel.DutyCycle = 0;
    LoRaMacSetChannelsTxPower( TX_POWER_14_DBM );
    
    // Disable reception windows opening
    LoRaMacTestRxWindowsOn( false );
    
    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
        }

        for( datarateIndex = 0; datarateIndex < 3; datarateIndex++ )
        {
            //for( channelsIndex = 0; channelsIndex < 3; channelsIndex++ )
            {
                pktCnt = 15 * channelNb;
                while( pktCnt > 0 )
                {
                    switch( tstState )
                    {
                        case 0: // Init
                            AppData[0] = SelectorGetValue( );
                            
                            channel.Frequency = channelsFreq[channelsIndex];
                            LoRaMacSetChannelsDatarate( channelsDatarate[datarateIndex] );
                            LoRaMacSendOnChannel( channel, &macHdr, &fCtrl, NULL, 15, AppData, 1 );

                            // Switch LED 1 ON
                            GpioWrite( &Led1, 0 );
                            TimerStart( &Led1Timer );

                            channelsIndex = ( channelsIndex + 1 ) % channelNb;
                            tstState = 1;
                            break;
                        case 1: // Wait for end of transmission
                            if( Led1TimerEvent == true )
                            {
                                Led1TimerEvent = false;
                                
                                // Switch LED 1 OFF
                                GpioWrite( &Led1, 1 );
                            }
                            if( TxDone == true )
                            {
                                TxDone = false;
                                pktCnt--;
                                // Schedule next packet transmission after 100 ms
                                TimerSetValue( &TxNextPacketTimer, 100000 );
                                TimerStart( &TxNextPacketTimer );
                                tstState = 2;
                            }
                            break;
                        case 2: // Wait for next packet timer to expire
                            if( TxNextPacket == true )
                            {
                                TxNextPacket = false;
                                tstState = 0;
                            }
                            break;
                    }
                    
                    TimerLowPowerHandler( );
                }
            }
        }
        
        TimerStart( &StopTimer );
        while( 1 ) // Reset device to restart
        {
            if( StopTimerEvent == true )
            {
                StopTimerEvent = false;
                TimerStart( &StopTimer );
            }
            TimerLowPowerHandler( );
        }
    }
}