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: mbed LoRaWAN-lib SX1276Lib
app/main.cpp
- Committer:
- uss1994
- Date:
- 2021-10-25
- Revision:
- 10:f3b1186fc0b0
- Parent:
- 9:ee9dcbb9708d
- Child:
- 11:7a7913d47ca6
File content as of revision 10:f3b1186fc0b0:
/* / _____) _ | | ( (____ _____ ____ _| |_ _____ ____| |__ \____ \| ___ | (_ _) ___ |/ ___) _ \ _____) ) ____| | | || |_| ____( (___| | | | (______/|_____)_|_|_| \__)_____)\____)_| |_| (C)2015 Semtech Description: LoRaMac classA device implementation License: Revised BSD License, see LICENSE.TXT file include in the project Maintainer: Miguel Luis and Gregory Cristian */ #include "mbed.h" #include "board.h" #include "radio.h" #include "LoRaMac.h" #include "Commissioning.h" #include "Si7021.h" #include "stm32l0xx_hal_iwdg.h" /*! * Defines the application data transmission duty cycle. 5s, value in [ms]. */ #define APP_TX_DUTYCYCLE 5000 /*! * Defines a random delay for application data transmission duty cycle. 1s, * value in [ms]. */ #define APP_TX_DUTYCYCLE_RND 1000 /*! * Default datarate */ #define LORAWAN_DEFAULT_DATARATE DR_0 /*! * LoRaWAN confirmed messages */ #define LORAWAN_CONFIRMED_MSG_ON true /*! * LoRaWAN Adaptive Data Rate * * \remark Please note that when ADR is enabled the end-device should be static */ #define LORAWAN_ADR_ON 1 #if defined( USE_BAND_868 ) #include "LoRaMacTest.h" /*! * LoRaWAN ETSI duty cycle control enable/disable * * \remark Please note that ETSI mandates duty cycled transmissions. Use only for test purposes */ #define LORAWAN_DUTYCYCLE_ON false #define USE_SEMTECH_DEFAULT_CHANNEL_LINEUP 1 #if( USE_SEMTECH_DEFAULT_CHANNEL_LINEUP == 1 ) #define LC4 { 867100000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 } #define LC5 { 867300000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 } #define LC6 { 867500000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 } #define LC7 { 867700000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 } #define LC8 { 867900000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 } #define LC9 { 868800000, { ( ( DR_7 << 4 ) | DR_7 ) }, 2 } #define LC10 { 868300000, { ( ( DR_6 << 4 ) | DR_6 ) }, 1 } #endif #endif /*! * LoRaWAN application port */ #define LORAWAN_APP_PORT 15 /*! * User application data buffer size */ #define LORAWAN_APP_DATA_SIZE 7 static uint8_t DevEui[] = LORAWAN_DEVICE_EUI; static uint8_t AppEui[] = LORAWAN_APPLICATION_EUI; static uint8_t AppKey[] = LORAWAN_APPLICATION_KEY; #if( OVER_THE_AIR_ACTIVATION == 0 ) static uint8_t NwkSKey[] = LORAWAN_NWKSKEY; static uint8_t AppSKey[] = LORAWAN_APPSKEY; /*! * Device address */ static uint32_t DevAddr = LORAWAN_DEVICE_ADDRESS; #endif /*! * Application port */ static uint8_t AppPort = LORAWAN_APP_PORT; /*! * User application data size */ static uint8_t AppDataSize = LORAWAN_APP_DATA_SIZE; /*! * User application data buffer size */ #define LORAWAN_APP_DATA_MAX_SIZE 64 /*! * User application data */ static uint8_t AppData[LORAWAN_APP_DATA_MAX_SIZE]; /*! * Indicates if the node is sending confirmed or unconfirmed messages */ static uint8_t IsTxConfirmed = LORAWAN_CONFIRMED_MSG_ON; /*! * Defines the application data transmission duty cycle */ static uint32_t TxDutyCycleTime; /*! * Timer to handle the application data transmission duty cycle */ static TimerEvent_t TxNextPacketTimer; /*! * Indicates if a new packet can be sent */ static bool NextTx = true; // Serial Serial pc(SERIAL_TX, SERIAL_RX,115200); // Humidty&Temp Sensor DigitalOut si7021_power_pin(PB_2); Si7021 *si7021; // I2C Definition I2C i2c(PB_9, PB_8); //sda, scl IWDG_HandleTypeDef wd; bool joining; time_t current_time; time_t last_rx; // Pressure // The amount of time between measurements (seconds) #define MEASUREMENT_GAP_SECONDS 30 #define RAW_MEASUREMENT_BUFFER_SIZE 10 // The moving average of the avg measurement data float average_pressure; // The raw pressure data of a single standard measurement float raw_pressure_measurements[RAW_MEASUREMENT_BUFFER_SIZE] = {0}; int raw_measurement_counter = 0; char single_measure_command[2] = {0x36, 0x2F}; char reset_command[1] = {0x06}; /*! * Device states */ static enum eDeviceState { DEVICE_STATE_INIT, DEVICE_STATE_JOIN, DEVICE_STATE_SEND, DEVICE_STATE_CYCLE, DEVICE_STATE_SLEEP }DeviceState; static enum eMessageType { MESSAGE_TYPE_ACK, MESSAGE_TYPE_READING }MessageType; /*! * LoRaWAN compliance tests support data */ struct ComplianceTest_s { bool Running; uint8_t State; bool IsTxConfirmed; uint8_t AppPort; uint8_t AppDataSize; uint8_t *AppDataBuffer; uint16_t DownLinkCounter; bool LinkCheck; uint8_t DemodMargin; uint8_t NbGateways; }ComplianceTest; /* * SerialDisplay managment variables */ /*! * Indicates if the MAC layer network join status has changed. */ static bool IsNetworkJoinedStatusUpdate = false; /*! * Strucure containing the Uplink status */ struct sLoRaMacUplinkStatus { uint8_t Acked; int8_t Datarate; uint16_t UplinkCounter; uint8_t Port; uint8_t *Buffer; uint8_t BufferSize; }LoRaMacUplinkStatus; volatile bool UplinkStatusUpdated = false; /*! * Strucure containing the Downlink status */ struct sLoRaMacDownlinkStatus { int16_t Rssi; int8_t Snr; uint16_t DownlinkCounter; bool RxData; uint8_t Port; uint8_t *Buffer; uint8_t BufferSize; }LoRaMacDownlinkStatus; volatile bool DownlinkStatusUpdated = false; /*! * \brief Prepares the payload of the frame */ static void PrepareTxFrame( uint8_t port ) { switch( port ) { case 15: { switch ( MessageType ) { case MESSAGE_TYPE_ACK: { AppDataSize = 0; IsTxConfirmed = false; break; } case MESSAGE_TYPE_READING: default: { AppDataSize = 7; IsTxConfirmed = true; // Maximum +- 300 Pa int16_t extended_pressure = (int16_t)(average_pressure * 100); int16_t extended_humidity = (int16_t)(si7021->get_humidity() / 10); int16_t extended_temp = (int16_t)(si7021->get_temperature() / 10); pc.printf("Sending Pressure: %i\r\n", extended_pressure); pc.printf("Sending Humidity: %i\r\n", extended_humidity); pc.printf("Sending Temp: %i\r\n", extended_temp); AppData[0] = 0xA0; AppData[1] = extended_pressure >> 8; AppData[2] = extended_pressure; AppData[3] = extended_humidity >> 8; AppData[4] = extended_humidity; AppData[5] = extended_temp >> 8; AppData[6] = extended_temp; break; } } } break; case 224: if( ComplianceTest.LinkCheck == true ) { ComplianceTest.LinkCheck = false; AppDataSize = 3; AppData[0] = 5; AppData[1] = ComplianceTest.DemodMargin; AppData[2] = ComplianceTest.NbGateways; ComplianceTest.State = 1; } else { switch( ComplianceTest.State ) { case 4: ComplianceTest.State = 1; break; case 1: AppDataSize = 2; AppData[0] = ComplianceTest.DownLinkCounter >> 8; AppData[1] = ComplianceTest.DownLinkCounter; break; } } break; default: break; } } /*! * \brief Prepares the payload of the frame * * \retval [0: frame could be send, 1: error] */ static bool SendFrame( void ) { McpsReq_t mcpsReq; LoRaMacTxInfo_t txInfo; if( LoRaMacQueryTxPossible( AppDataSize, &txInfo ) != LORAMAC_STATUS_OK ) { // Send empty frame in order to flush MAC commands mcpsReq.Type = MCPS_UNCONFIRMED; mcpsReq.Req.Unconfirmed.fBuffer = NULL; mcpsReq.Req.Unconfirmed.fBufferSize = 0; mcpsReq.Req.Unconfirmed.Datarate = LORAWAN_DEFAULT_DATARATE; LoRaMacUplinkStatus.Acked = false; LoRaMacUplinkStatus.Port = 0; LoRaMacUplinkStatus.Buffer = NULL; LoRaMacUplinkStatus.BufferSize = 0; } else { LoRaMacUplinkStatus.Acked = false; LoRaMacUplinkStatus.Port = AppPort; LoRaMacUplinkStatus.Buffer = AppData; LoRaMacUplinkStatus.BufferSize = AppDataSize; if( IsTxConfirmed == false ) { mcpsReq.Type = MCPS_UNCONFIRMED; mcpsReq.Req.Unconfirmed.fPort = AppPort; mcpsReq.Req.Unconfirmed.fBuffer = AppData; mcpsReq.Req.Unconfirmed.fBufferSize = AppDataSize; mcpsReq.Req.Unconfirmed.Datarate = LORAWAN_DEFAULT_DATARATE; } else { mcpsReq.Type = MCPS_CONFIRMED; mcpsReq.Req.Confirmed.fPort = AppPort; mcpsReq.Req.Confirmed.fBuffer = AppData; mcpsReq.Req.Confirmed.fBufferSize = AppDataSize; mcpsReq.Req.Confirmed.NbTrials = 8; mcpsReq.Req.Confirmed.Datarate = LORAWAN_DEFAULT_DATARATE; } } if( LoRaMacMcpsRequest( &mcpsReq ) == LORAMAC_STATUS_OK ) { return false; } return true; } /*! * \brief Function executed on TxNextPacket Timeout event */ static void OnTxNextPacketTimerEvent( void ) { MibRequestConfirm_t mibReq; LoRaMacStatus_t status; TimerStop( &TxNextPacketTimer ); mibReq.Type = MIB_NETWORK_JOINED; status = LoRaMacMibGetRequestConfirm( &mibReq ); if( status == LORAMAC_STATUS_OK ) { if( mibReq.Param.IsNetworkJoined == true ) { DeviceState = DEVICE_STATE_SEND; NextTx = true; } else { DeviceState = DEVICE_STATE_JOIN; } } } /*! * \brief MCPS-Confirm event function * * \param [IN] mcpsConfirm - Pointer to the confirm structure, * containing confirm attributes. */ static void McpsConfirm( McpsConfirm_t *mcpsConfirm ) { if( mcpsConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK ) { switch( mcpsConfirm->McpsRequest ) { case MCPS_UNCONFIRMED: { // Check Datarate // Check TxPower break; } case MCPS_CONFIRMED: { // Check Datarate // Check TxPower // Check AckReceived // Check NbTrials LoRaMacUplinkStatus.Acked = mcpsConfirm->AckReceived; break; } case MCPS_PROPRIETARY: { break; } default: break; } LoRaMacUplinkStatus.Datarate = mcpsConfirm->Datarate; LoRaMacUplinkStatus.UplinkCounter = mcpsConfirm->UpLinkCounter; UplinkStatusUpdated = true; } NextTx = true; } /*! * \brief MCPS-Indication event function * * \param [IN] mcpsIndication - Pointer to the indication structure, * containing indication attributes. */ static void McpsIndication( McpsIndication_t *mcpsIndication ) { if( mcpsIndication->Status != LORAMAC_EVENT_INFO_STATUS_OK ) { return; } switch( mcpsIndication->McpsIndication ) { case MCPS_UNCONFIRMED: { break; } case MCPS_CONFIRMED: { break; } case MCPS_PROPRIETARY: { break; } case MCPS_MULTICAST: { break; } default: break; } // Check Multicast // Check Port // Check Datarate // Check FramePending // Check Buffer // Check BufferSize // Check Rssi // Check Snr // Check RxSlot LoRaMacDownlinkStatus.Rssi = mcpsIndication->Rssi; if( mcpsIndication->Snr & 0x80 ) // The SNR sign bit is 1 { // Invert and divide by 4 LoRaMacDownlinkStatus.Snr = ( ( ~mcpsIndication->Snr + 1 ) & 0xFF ) >> 2; LoRaMacDownlinkStatus.Snr = -LoRaMacDownlinkStatus.Snr; } else { // Divide by 4 LoRaMacDownlinkStatus.Snr = ( mcpsIndication->Snr & 0xFF ) >> 2; } LoRaMacDownlinkStatus.DownlinkCounter++; LoRaMacDownlinkStatus.RxData = mcpsIndication->RxData; LoRaMacDownlinkStatus.Port = mcpsIndication->Port; LoRaMacDownlinkStatus.Buffer = mcpsIndication->Buffer; LoRaMacDownlinkStatus.BufferSize = mcpsIndication->BufferSize; if( ComplianceTest.Running == true ) { ComplianceTest.DownLinkCounter++; } // Update timestamp for last rx last_rx = current_time; if( mcpsIndication->RxData == true ) { switch( mcpsIndication->Port ) { case 15: break; case 224: if( ComplianceTest.Running == false ) { // Check compliance test enable command (i) if( ( mcpsIndication->BufferSize == 4 ) && ( mcpsIndication->Buffer[0] == 0x01 ) && ( mcpsIndication->Buffer[1] == 0x01 ) && ( mcpsIndication->Buffer[2] == 0x01 ) && ( mcpsIndication->Buffer[3] == 0x01 ) ) { IsTxConfirmed = false; AppPort = 224; AppDataSize = 2; ComplianceTest.DownLinkCounter = 0; ComplianceTest.LinkCheck = false; ComplianceTest.DemodMargin = 0; ComplianceTest.NbGateways = 0; ComplianceTest.Running = true; ComplianceTest.State = 1; MibRequestConfirm_t mibReq; mibReq.Type = MIB_ADR; mibReq.Param.AdrEnable = true; LoRaMacMibSetRequestConfirm( &mibReq ); #if defined( USE_BAND_868 ) LoRaMacTestSetDutyCycleOn( false ); #endif } } else { ComplianceTest.State = mcpsIndication->Buffer[0]; switch( ComplianceTest.State ) { case 0: // Check compliance test disable command (ii) IsTxConfirmed = LORAWAN_CONFIRMED_MSG_ON; AppPort = LORAWAN_APP_PORT; AppDataSize = LORAWAN_APP_DATA_SIZE; ComplianceTest.DownLinkCounter = 0; ComplianceTest.Running = false; MibRequestConfirm_t mibReq; mibReq.Type = MIB_ADR; mibReq.Param.AdrEnable = LORAWAN_ADR_ON; LoRaMacMibSetRequestConfirm( &mibReq ); #if defined( USE_BAND_868 ) LoRaMacTestSetDutyCycleOn( LORAWAN_DUTYCYCLE_ON ); #endif break; case 1: // (iii, iv) AppDataSize = 2; break; case 2: // Enable confirmed messages (v) IsTxConfirmed = true; ComplianceTest.State = 1; break; case 3: // Disable confirmed messages (vi) IsTxConfirmed = false; ComplianceTest.State = 1; break; case 4: // (vii) AppDataSize = mcpsIndication->BufferSize; AppData[0] = 4; for( uint8_t i = 1; i < AppDataSize; i++ ) { AppData[i] = mcpsIndication->Buffer[i] + 1; } break; case 5: // (viii) { MlmeReq_t mlmeReq; mlmeReq.Type = MLME_LINK_CHECK; LoRaMacMlmeRequest( &mlmeReq ); } break; case 6: // (ix) { MlmeReq_t mlmeReq; // Disable TestMode and revert back to normal operation IsTxConfirmed = LORAWAN_CONFIRMED_MSG_ON; AppPort = LORAWAN_APP_PORT; AppDataSize = LORAWAN_APP_DATA_SIZE; ComplianceTest.DownLinkCounter = 0; ComplianceTest.Running = false; MibRequestConfirm_t mibReq; mibReq.Type = MIB_ADR; mibReq.Param.AdrEnable = LORAWAN_ADR_ON; LoRaMacMibSetRequestConfirm( &mibReq ); #if defined( USE_BAND_868 ) LoRaMacTestSetDutyCycleOn( LORAWAN_DUTYCYCLE_ON ); #endif mlmeReq.Type = MLME_JOIN; mlmeReq.Req.Join.DevEui = DevEui; mlmeReq.Req.Join.AppEui = AppEui; mlmeReq.Req.Join.AppKey = AppKey; mlmeReq.Req.Join.NbTrials = 3; LoRaMacMlmeRequest( &mlmeReq ); DeviceState = DEVICE_STATE_SLEEP; } break; case 7: // (x) { if( mcpsIndication->BufferSize == 3 ) { MlmeReq_t mlmeReq; mlmeReq.Type = MLME_TXCW; mlmeReq.Req.TxCw.Timeout = ( uint16_t )( ( mcpsIndication->Buffer[1] << 8 ) | mcpsIndication->Buffer[2] ); LoRaMacMlmeRequest( &mlmeReq ); } else if( mcpsIndication->BufferSize == 7 ) { MlmeReq_t mlmeReq; mlmeReq.Type = MLME_TXCW_1; mlmeReq.Req.TxCw.Timeout = ( uint16_t )( ( mcpsIndication->Buffer[1] << 8 ) | mcpsIndication->Buffer[2] ); mlmeReq.Req.TxCw.Frequency = ( uint32_t )( ( mcpsIndication->Buffer[3] << 16 ) | ( mcpsIndication->Buffer[4] << 8 ) | mcpsIndication->Buffer[5] ) * 100; mlmeReq.Req.TxCw.Power = mcpsIndication->Buffer[6]; LoRaMacMlmeRequest( &mlmeReq ); } ComplianceTest.State = 1; } break; default: break; } } break; default: break; } } } /*! * \brief MLME-Confirm event function * * \param [IN] mlmeConfirm - Pointer to the confirm structure, * containing confirm attributes. */ static void MlmeConfirm( MlmeConfirm_t *mlmeConfirm ) { switch( mlmeConfirm->MlmeRequest ) { case MLME_JOIN: { if( mlmeConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK ) { // Status is OK, node has joined the network IsNetworkJoinedStatusUpdate = true; pc.printf("--- Joined ---\r\n"); DeviceState = DEVICE_STATE_SEND; MessageType = MESSAGE_TYPE_READING; joining = false; } else { // Join was not successful. Try to join again DeviceState = DEVICE_STATE_JOIN; } break; } case MLME_LINK_CHECK: { if( mlmeConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK ) { // Check DemodMargin // Check NbGateways if( ComplianceTest.Running == true ) { ComplianceTest.LinkCheck = true; ComplianceTest.DemodMargin = mlmeConfirm->DemodMargin; ComplianceTest.NbGateways = mlmeConfirm->NbGateways; } } break; } default: break; } NextTx = true; UplinkStatusUpdated = true; } void reset_pressure_sensor() { // Reset the sensor pc.printf("Resetting Sensor...\r\n"); i2c.write(0x00, reset_command, 1); } void measure_pressure() { // Take a few manual measurements pc.printf("Measuring...\r\n"); i2c.write((0x25 << 1), single_measure_command, 2); } float read_measurement() { char output[9]; // Read measurements i2c.read((0x25 << 1), output, 9); // read from register // Interpret int16_t diffPressureTicks = (output[0] << 8) | output[1]; // int16_t temperatureTicks = (output[3] << 8) | output[4]; uint16_t scaleFactorDiffPressure = (output[6] << 8) | output[7]; float diffPressure = (float)diffPressureTicks / (float)scaleFactorDiffPressure; return diffPressure; } /** * Main application entry point. */ int main( void ) { pc.printf("mbed-os-rev: %d.%d.%d lib-rev: %d\r\n", \ MBED_MAJOR_VERSION, MBED_MINOR_VERSION,MBED_PATCH_VERSION,MBED_VERSION); pc.printf("BUILD= %s, SysClock= %d\r\n", __TIME__, SystemCoreClock); pc.printf("CR= %X CSR= %X CFGR= %X PLL_HSE= %d\r\n", \ RCC->CR, RCC->CSR, RCC->CFGR, (RCC->CFGR>>16)&(1)); wait(3); wd.Instance = IWDG; wd.Init.Prescaler = IWDG_PRESCALER_256; //About 32s wd.Init.Reload = 0x0FFF; wd.Init.Window = 0x0FFF; HAL_IWDG_Init(&wd); time_t start_time = time(NULL); current_time = start_time; last_rx = start_time; time_t last_measure = start_time; time_t measure_start = start_time; joining = true; pc.printf("Starting Both Sensors..\r\n"); // set up SHT31 si7021_power_pin = 1; si7021 = new Si7021(PB_14, PB_13); wait(1); si7021->measure(); wait(1); pc.printf("Temp: %d\r\n", si7021->get_temperature()); pc.printf("Humidity: %d\r\n", si7021->get_humidity()); // set up pressure sensors reset_pressure_sensor(); wait(0.1); measure_pressure(); wait(0.1); average_pressure = read_measurement(); pc.printf("Pressure: %f\r\n", average_pressure); bool measuring = false; wait(5); LoRaMacPrimitives_t LoRaMacPrimitives; LoRaMacCallback_t LoRaMacCallbacks; MibRequestConfirm_t mibReq; BoardInit( ); DeviceState = DEVICE_STATE_INIT; while( 1 ) { //Call to reset watchdog HAL_IWDG_Refresh(&wd); current_time = time(NULL); // Update Time /* ---- Reset if joining for more than 3 hours ----- */ if ((current_time - start_time > 10800) && joining) { //Call to reset device NVIC_SystemReset(); } /* ---- Reset if no downlinks for more than 36 hours ----- */ if ((current_time - last_rx > 129600) && !joining) { //Call to reset device NVIC_SystemReset(); } switch( DeviceState ) { case DEVICE_STATE_INIT: { LoRaMacPrimitives.MacMcpsConfirm = McpsConfirm; LoRaMacPrimitives.MacMcpsIndication = McpsIndication; LoRaMacPrimitives.MacMlmeConfirm = MlmeConfirm; LoRaMacCallbacks.GetBatteryLevel = BoardGetBatteryLevel; LoRaMacInitialization( &LoRaMacPrimitives, &LoRaMacCallbacks ); TimerInit( &TxNextPacketTimer, OnTxNextPacketTimerEvent ); mibReq.Type = MIB_ADR; mibReq.Param.AdrEnable = LORAWAN_ADR_ON; LoRaMacMibSetRequestConfirm( &mibReq ); mibReq.Type = MIB_PUBLIC_NETWORK; mibReq.Param.EnablePublicNetwork = LORAWAN_PUBLIC_NETWORK; LoRaMacMibSetRequestConfirm( &mibReq ); // Limiting to just 2nd subband of 8 channels static uint16_t GatewayChannelsMask[] = {0x00FF, 0x0000, 0x0000, 0x0000, 0x0001, 0x0000}; mibReq.Type = MIB_CHANNELS_DEFAULT_MASK; mibReq.Param.ChannelsDefaultMask = GatewayChannelsMask; LoRaMacMibSetRequestConfirm( &mibReq ); mibReq.Type = MIB_CHANNELS_MASK; mibReq.Param.ChannelsMask = GatewayChannelsMask; LoRaMacMibSetRequestConfirm( &mibReq ); #if defined( USE_BAND_868 ) LoRaMacTestSetDutyCycleOn( LORAWAN_DUTYCYCLE_ON ); #if( USE_SEMTECH_DEFAULT_CHANNEL_LINEUP == 1 ) LoRaMacChannelAdd( 3, ( ChannelParams_t )LC4 ); LoRaMacChannelAdd( 4, ( ChannelParams_t )LC5 ); LoRaMacChannelAdd( 5, ( ChannelParams_t )LC6 ); LoRaMacChannelAdd( 6, ( ChannelParams_t )LC7 ); LoRaMacChannelAdd( 7, ( ChannelParams_t )LC8 ); LoRaMacChannelAdd( 8, ( ChannelParams_t )LC9 ); LoRaMacChannelAdd( 9, ( ChannelParams_t )LC10 ); mibReq.Type = MIB_RX2_DEFAULT_CHANNEL; mibReq.Param.Rx2DefaultChannel = ( Rx2ChannelParams_t ){ 869525000, DR_3 }; LoRaMacMibSetRequestConfirm( &mibReq ); mibReq.Type = MIB_RX2_CHANNEL; mibReq.Param.Rx2Channel = ( Rx2ChannelParams_t ){ 869525000, DR_3 }; LoRaMacMibSetRequestConfirm( &mibReq ); #endif #endif LoRaMacDownlinkStatus.DownlinkCounter = 0; DeviceState = DEVICE_STATE_JOIN; break; } case DEVICE_STATE_JOIN: { pc.printf("--- Joining ---\r\n"); #if( OVER_THE_AIR_ACTIVATION != 0 ) MlmeReq_t mlmeReq; mlmeReq.Type = MLME_JOIN; mlmeReq.Req.Join.DevEui = DevEui; mlmeReq.Req.Join.AppEui = AppEui; mlmeReq.Req.Join.AppKey = AppKey; if( NextTx == true ) { LoRaMacMlmeRequest( &mlmeReq ); } DeviceState = DEVICE_STATE_SLEEP; #else mibReq.Type = MIB_NET_ID; mibReq.Param.NetID = LORAWAN_NETWORK_ID; LoRaMacMibSetRequestConfirm( &mibReq ); mibReq.Type = MIB_DEV_ADDR; mibReq.Param.DevAddr = DevAddr; LoRaMacMibSetRequestConfirm( &mibReq ); mibReq.Type = MIB_NWK_SKEY; mibReq.Param.NwkSKey = NwkSKey; LoRaMacMibSetRequestConfirm( &mibReq ); mibReq.Type = MIB_APP_SKEY; mibReq.Param.AppSKey = AppSKey; LoRaMacMibSetRequestConfirm( &mibReq ); mibReq.Type = MIB_NETWORK_JOINED; mibReq.Param.IsNetworkJoined = true; LoRaMacMibSetRequestConfirm( &mibReq ); DeviceState = DEVICE_STATE_SEND; #endif IsNetworkJoinedStatusUpdate = true; break; } case DEVICE_STATE_SEND: { if( NextTx == true ) { PrepareTxFrame( AppPort ); NextTx = SendFrame( ); } DeviceState = DEVICE_STATE_CYCLE; break; } case DEVICE_STATE_CYCLE: { if (measuring) { /* check if done measuring & swap to send state */ if (current_time - measure_start > 0) { average_pressure = read_measurement(); pc.printf("Pressure: %f\r\n", average_pressure); pc.printf("Temperature: %d\r\n", si7021->get_temperature()); pc.printf("Humidity: %d\r\n", si7021->get_humidity()); measuring = false; DeviceState = DEVICE_STATE_SEND; MessageType = MESSAGE_TYPE_READING; } } else { // check if it's time to start measuring if (current_time - last_measure > MEASUREMENT_GAP_SECONDS) { // take a measurement measure_start = current_time; last_measure = current_time; measuring = true; measure_pressure(); si7021->measure(); } } break; } case DEVICE_STATE_SLEEP: { // Wake up through events break; } default: { DeviceState = DEVICE_STATE_INIT; break; } } } }