Node code for sx1272 LoRa transciever
Dependencies: mbed BMP085 BufferedSerial DHT Sds021 Chainable_RGB_LED DigitDisplay LoRaWAN-lib SX1272Lib
Fork of LoRaWAN-demo-72-bootcamp by
app/main.cpp@9:16106008960b, 2017-01-30 (annotated)
- Committer:
- abouillot
- Date:
- Mon Jan 30 21:49:58 2017 +0000
- Revision:
- 9:16106008960b
- Parent:
- 7:ceb4063e6863
Added support for BMP085, DHT22 and SDS021 sensors; Added support of update upon User Button push
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mluis | 0:45496a70a8a5 | 1 | /* |
mluis | 0:45496a70a8a5 | 2 | / _____) _ | | |
mluis | 0:45496a70a8a5 | 3 | ( (____ _____ ____ _| |_ _____ ____| |__ |
mluis | 0:45496a70a8a5 | 4 | \____ \| ___ | (_ _) ___ |/ ___) _ \ |
mluis | 0:45496a70a8a5 | 5 | _____) ) ____| | | || |_| ____( (___| | | | |
mluis | 0:45496a70a8a5 | 6 | (______/|_____)_|_|_| \__)_____)\____)_| |_| |
mluis | 0:45496a70a8a5 | 7 | (C)2015 Semtech |
mluis | 0:45496a70a8a5 | 8 | |
mluis | 0:45496a70a8a5 | 9 | Description: LoRaMac classA device implementation |
mluis | 0:45496a70a8a5 | 10 | |
mluis | 0:45496a70a8a5 | 11 | License: Revised BSD License, see LICENSE.TXT file include in the project |
mluis | 0:45496a70a8a5 | 12 | |
mluis | 0:45496a70a8a5 | 13 | Maintainer: Miguel Luis and Gregory Cristian |
mluis | 0:45496a70a8a5 | 14 | */ |
mluis | 0:45496a70a8a5 | 15 | #include "mbed.h" |
mluis | 0:45496a70a8a5 | 16 | #include "board.h" |
mluis | 0:45496a70a8a5 | 17 | #include "radio.h" |
mluis | 0:45496a70a8a5 | 18 | |
mluis | 0:45496a70a8a5 | 19 | #include "LoRaMac.h" |
mluis | 0:45496a70a8a5 | 20 | #include "Comissioning.h" |
mluis | 0:45496a70a8a5 | 21 | #include "SerialDisplay.h" |
GregCr | 2:48d8d4806d48 | 22 | #include "DigitDisplay.h" |
GregCr | 2:48d8d4806d48 | 23 | #include "ChainableLED.h" |
mluis | 0:45496a70a8a5 | 24 | |
abouillot | 9:16106008960b | 25 | Serial pc(USBTX, USBRX, 115200); |
abouillot | 9:16106008960b | 26 | |
mluis | 0:45496a70a8a5 | 27 | /*! |
mluis | 0:45496a70a8a5 | 28 | * Defines the application data transmission duty cycle. 5s, value in [us]. |
mluis | 0:45496a70a8a5 | 29 | */ |
abouillot | 9:16106008960b | 30 | //#define APP_TX_DUTYCYCLE 5000000 |
abouillot | 9:16106008960b | 31 | #define APP_TX_DUTYCYCLE 60000000 |
mluis | 0:45496a70a8a5 | 32 | |
mluis | 0:45496a70a8a5 | 33 | /*! |
mluis | 0:45496a70a8a5 | 34 | * Defines a random delay for application data transmission duty cycle. 1s, |
mluis | 0:45496a70a8a5 | 35 | * value in [us]. |
mluis | 0:45496a70a8a5 | 36 | */ |
abouillot | 9:16106008960b | 37 | //#define APP_TX_DUTYCYCLE_RND 1000000 |
abouillot | 9:16106008960b | 38 | #define APP_TX_DUTYCYCLE_RND 10000000 |
mluis | 0:45496a70a8a5 | 39 | |
mluis | 0:45496a70a8a5 | 40 | /*! |
mluis | 5:fa113b25f612 | 41 | * Default datarate |
mluis | 0:45496a70a8a5 | 42 | */ |
GregCr | 2:48d8d4806d48 | 43 | #define LORAWAN_DEFAULT_DATARATE DR_5 |
mluis | 0:45496a70a8a5 | 44 | |
mluis | 0:45496a70a8a5 | 45 | /*! |
mluis | 0:45496a70a8a5 | 46 | * LoRaWAN confirmed messages |
mluis | 0:45496a70a8a5 | 47 | */ |
GregCr | 2:48d8d4806d48 | 48 | #define LORAWAN_CONFIRMED_MSG_ON false |
mluis | 0:45496a70a8a5 | 49 | |
mluis | 0:45496a70a8a5 | 50 | /*! |
mluis | 0:45496a70a8a5 | 51 | * LoRaWAN Adaptive Data Rate |
mluis | 0:45496a70a8a5 | 52 | * |
mluis | 0:45496a70a8a5 | 53 | * \remark Please note that when ADR is enabled the end-device should be static |
mluis | 0:45496a70a8a5 | 54 | */ |
mluis | 0:45496a70a8a5 | 55 | #define LORAWAN_ADR_ON 1 |
mluis | 0:45496a70a8a5 | 56 | |
mluis | 0:45496a70a8a5 | 57 | #if defined( USE_BAND_868 ) |
mluis | 0:45496a70a8a5 | 58 | |
mluis | 0:45496a70a8a5 | 59 | #include "LoRaMacTest.h" |
mluis | 0:45496a70a8a5 | 60 | |
mluis | 0:45496a70a8a5 | 61 | /*! |
mluis | 0:45496a70a8a5 | 62 | * LoRaWAN ETSI duty cycle control enable/disable |
mluis | 0:45496a70a8a5 | 63 | * |
mluis | 0:45496a70a8a5 | 64 | * \remark Please note that ETSI mandates duty cycled transmissions. Use only for test purposes |
mluis | 0:45496a70a8a5 | 65 | */ |
GregCr | 2:48d8d4806d48 | 66 | #define LORAWAN_DUTYCYCLE_ON false |
mluis | 0:45496a70a8a5 | 67 | |
mluis | 5:fa113b25f612 | 68 | #define USE_SEMTECH_DEFAULT_CHANNEL_LINEUP 1 |
mluis | 5:fa113b25f612 | 69 | |
abouillot | 9:16106008960b | 70 | #if( USE_SEMTECH_DEFAULT_CHANNEL_LINEUP == 1 ) |
mluis | 5:fa113b25f612 | 71 | |
mluis | 5:fa113b25f612 | 72 | #define LC4 { 867100000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 } |
mluis | 5:fa113b25f612 | 73 | #define LC5 { 867300000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 } |
mluis | 5:fa113b25f612 | 74 | #define LC6 { 867500000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 } |
mluis | 5:fa113b25f612 | 75 | #define LC7 { 867700000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 } |
mluis | 5:fa113b25f612 | 76 | #define LC8 { 867900000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 } |
mluis | 5:fa113b25f612 | 77 | #define LC9 { 868800000, { ( ( DR_7 << 4 ) | DR_7 ) }, 2 } |
mluis | 5:fa113b25f612 | 78 | #define LC10 { 868300000, { ( ( DR_6 << 4 ) | DR_6 ) }, 1 } |
mluis | 5:fa113b25f612 | 79 | |
mluis | 5:fa113b25f612 | 80 | #endif |
mluis | 5:fa113b25f612 | 81 | |
mluis | 0:45496a70a8a5 | 82 | #endif |
mluis | 0:45496a70a8a5 | 83 | |
mluis | 0:45496a70a8a5 | 84 | /*! |
mluis | 0:45496a70a8a5 | 85 | * LoRaWAN application port |
mluis | 0:45496a70a8a5 | 86 | */ |
GregCr | 3:d4142832d5af | 87 | #define LORAWAN_APP_PORT 10 |
mluis | 0:45496a70a8a5 | 88 | |
mluis | 0:45496a70a8a5 | 89 | /*! |
mluis | 0:45496a70a8a5 | 90 | * User application data buffer size |
mluis | 0:45496a70a8a5 | 91 | */ |
mluis | 0:45496a70a8a5 | 92 | #if ( LORAWAN_CONFIRMED_MSG_ON == 1 ) |
mluis | 0:45496a70a8a5 | 93 | #define LORAWAN_APP_DATA_SIZE 6 |
mluis | 0:45496a70a8a5 | 94 | |
mluis | 0:45496a70a8a5 | 95 | #else |
GregCr | 3:d4142832d5af | 96 | #define LORAWAN_APP_DATA_SIZE 5 |
mluis | 0:45496a70a8a5 | 97 | |
mluis | 0:45496a70a8a5 | 98 | #endif |
mluis | 0:45496a70a8a5 | 99 | |
mluis | 0:45496a70a8a5 | 100 | static uint8_t DevEui[] = LORAWAN_DEVICE_EUI; |
mluis | 0:45496a70a8a5 | 101 | static uint8_t AppEui[] = LORAWAN_APPLICATION_EUI; |
mluis | 0:45496a70a8a5 | 102 | static uint8_t AppKey[] = LORAWAN_APPLICATION_KEY; |
mluis | 0:45496a70a8a5 | 103 | |
mluis | 7:ceb4063e6863 | 104 | #if( OVER_THE_AIR_ACTIVATION == 0 ) |
mluis | 0:45496a70a8a5 | 105 | |
mluis | 0:45496a70a8a5 | 106 | static uint8_t NwkSKey[] = LORAWAN_NWKSKEY; |
mluis | 0:45496a70a8a5 | 107 | static uint8_t AppSKey[] = LORAWAN_APPSKEY; |
mluis | 0:45496a70a8a5 | 108 | |
mluis | 0:45496a70a8a5 | 109 | /*! |
mluis | 0:45496a70a8a5 | 110 | * Device address |
mluis | 0:45496a70a8a5 | 111 | */ |
mluis | 0:45496a70a8a5 | 112 | static uint32_t DevAddr = LORAWAN_DEVICE_ADDRESS; |
mluis | 0:45496a70a8a5 | 113 | |
mluis | 0:45496a70a8a5 | 114 | #endif |
mluis | 0:45496a70a8a5 | 115 | |
mluis | 0:45496a70a8a5 | 116 | /*! |
mluis | 0:45496a70a8a5 | 117 | * Application port |
mluis | 0:45496a70a8a5 | 118 | */ |
mluis | 0:45496a70a8a5 | 119 | static uint8_t AppPort = LORAWAN_APP_PORT; |
mluis | 0:45496a70a8a5 | 120 | |
mluis | 0:45496a70a8a5 | 121 | /*! |
mluis | 0:45496a70a8a5 | 122 | * User application data size |
mluis | 0:45496a70a8a5 | 123 | */ |
mluis | 0:45496a70a8a5 | 124 | static uint8_t AppDataSize = LORAWAN_APP_DATA_SIZE; |
mluis | 0:45496a70a8a5 | 125 | |
mluis | 0:45496a70a8a5 | 126 | /*! |
mluis | 0:45496a70a8a5 | 127 | * User application data buffer size |
mluis | 0:45496a70a8a5 | 128 | */ |
mluis | 0:45496a70a8a5 | 129 | #define LORAWAN_APP_DATA_MAX_SIZE 64 |
mluis | 0:45496a70a8a5 | 130 | |
mluis | 0:45496a70a8a5 | 131 | /*! |
mluis | 0:45496a70a8a5 | 132 | * User application data |
mluis | 0:45496a70a8a5 | 133 | */ |
mluis | 0:45496a70a8a5 | 134 | static uint8_t AppData[LORAWAN_APP_DATA_MAX_SIZE]; |
mluis | 0:45496a70a8a5 | 135 | |
mluis | 0:45496a70a8a5 | 136 | /*! |
mluis | 0:45496a70a8a5 | 137 | * Indicates if the node is sending confirmed or unconfirmed messages |
mluis | 0:45496a70a8a5 | 138 | */ |
mluis | 0:45496a70a8a5 | 139 | static uint8_t IsTxConfirmed = LORAWAN_CONFIRMED_MSG_ON; |
mluis | 0:45496a70a8a5 | 140 | |
mluis | 0:45496a70a8a5 | 141 | /*! |
mluis | 0:45496a70a8a5 | 142 | * Defines the application data transmission duty cycle |
mluis | 0:45496a70a8a5 | 143 | */ |
mluis | 0:45496a70a8a5 | 144 | static uint32_t TxDutyCycleTime; |
mluis | 0:45496a70a8a5 | 145 | |
mluis | 0:45496a70a8a5 | 146 | /*! |
mluis | 0:45496a70a8a5 | 147 | * Timer to handle the application data transmission duty cycle |
mluis | 0:45496a70a8a5 | 148 | */ |
mluis | 0:45496a70a8a5 | 149 | static TimerEvent_t TxNextPacketTimer; |
mluis | 0:45496a70a8a5 | 150 | |
mluis | 0:45496a70a8a5 | 151 | /*! |
mluis | 0:45496a70a8a5 | 152 | * Specifies the state of the application LED |
mluis | 0:45496a70a8a5 | 153 | */ |
mluis | 0:45496a70a8a5 | 154 | static bool AppLedStateOn = false; |
mluis | 0:45496a70a8a5 | 155 | volatile bool Led3StateChanged = false; |
mluis | 0:45496a70a8a5 | 156 | /*! |
mluis | 0:45496a70a8a5 | 157 | * Timer to handle the state of LED1 |
mluis | 0:45496a70a8a5 | 158 | */ |
mluis | 0:45496a70a8a5 | 159 | static TimerEvent_t Led1Timer; |
mluis | 0:45496a70a8a5 | 160 | volatile bool Led1State = false; |
mluis | 0:45496a70a8a5 | 161 | volatile bool Led1StateChanged = false; |
mluis | 0:45496a70a8a5 | 162 | /*! |
mluis | 0:45496a70a8a5 | 163 | * Timer to handle the state of LED2 |
mluis | 0:45496a70a8a5 | 164 | */ |
mluis | 0:45496a70a8a5 | 165 | static TimerEvent_t Led2Timer; |
mluis | 0:45496a70a8a5 | 166 | volatile bool Led2State = false; |
mluis | 0:45496a70a8a5 | 167 | volatile bool Led2StateChanged = false; |
mluis | 0:45496a70a8a5 | 168 | |
mluis | 0:45496a70a8a5 | 169 | /*! |
mluis | 0:45496a70a8a5 | 170 | * Indicates if a new packet can be sent |
mluis | 0:45496a70a8a5 | 171 | */ |
mluis | 0:45496a70a8a5 | 172 | static bool NextTx = true; |
mluis | 0:45496a70a8a5 | 173 | |
GregCr | 3:d4142832d5af | 174 | /*! |
GregCr | 3:d4142832d5af | 175 | * Hold the value returned from the Light Sensor |
GregCr | 3:d4142832d5af | 176 | */ |
GregCr | 3:d4142832d5af | 177 | static float LightValue = 0.0; |
GregCr | 2:48d8d4806d48 | 178 | |
GregCr | 3:d4142832d5af | 179 | /*! |
abouillot | 9:16106008960b | 180 | * Control the 3-color LED |
abouillot | 9:16106008960b | 181 | * 0: automatic (LED goes brigther as the light decrease, |
GregCr | 3:d4142832d5af | 182 | * 1: manual (The LED is controlled by the user) |
GregCr | 3:d4142832d5af | 183 | */ |
mluis | 5:fa113b25f612 | 184 | static uint8_t LightMode = 0; // 0: automatic, 1: manual |
GregCr | 3:d4142832d5af | 185 | |
GregCr | 3:d4142832d5af | 186 | /*! |
GregCr | 3:d4142832d5af | 187 | * Ticker to create a PWM for the buzzer |
GregCr | 3:d4142832d5af | 188 | */ |
mluis | 5:fa113b25f612 | 189 | Ticker BuzzerTimer; |
GregCr | 2:48d8d4806d48 | 190 | |
GregCr | 2:48d8d4806d48 | 191 | |
GregCr | 3:d4142832d5af | 192 | /*! |
GregCr | 3:d4142832d5af | 193 | * Constructor for Buzzer |
GregCr | 3:d4142832d5af | 194 | */ |
mluis | 5:fa113b25f612 | 195 | DigitalOut Buzzer( A3 ); |
GregCr | 3:d4142832d5af | 196 | |
GregCr | 3:d4142832d5af | 197 | /*! |
GregCr | 3:d4142832d5af | 198 | * Constructor for the 3-color LED |
GregCr | 3:d4142832d5af | 199 | */ |
GregCr | 3:d4142832d5af | 200 | #define NUM_LED 3 |
mluis | 5:fa113b25f612 | 201 | ChainableLED ColorLed( D6, D7, NUM_LED ); |
GregCr | 2:48d8d4806d48 | 202 | |
GregCr | 3:d4142832d5af | 203 | /*! |
GregCr | 3:d4142832d5af | 204 | * Constructor for Light Sensor |
GregCr | 3:d4142832d5af | 205 | */ |
abouillot | 9:16106008960b | 206 | AnalogIn LightSens( A1 ); |
GregCr | 3:d4142832d5af | 207 | |
GregCr | 3:d4142832d5af | 208 | /*! |
GregCr | 3:d4142832d5af | 209 | * Constructor for 4 Digit 7 semgent display |
GregCr | 3:d4142832d5af | 210 | */ |
mluis | 5:fa113b25f612 | 211 | DigitDisplay Display( D8, D9 ); |
GregCr | 3:d4142832d5af | 212 | |
mluis | 0:45496a70a8a5 | 213 | /*! |
mluis | 0:45496a70a8a5 | 214 | * Device states |
mluis | 0:45496a70a8a5 | 215 | */ |
abouillot | 9:16106008960b | 216 | static enum eDevicState { |
mluis | 0:45496a70a8a5 | 217 | DEVICE_STATE_INIT, |
mluis | 0:45496a70a8a5 | 218 | DEVICE_STATE_JOIN, |
mluis | 0:45496a70a8a5 | 219 | DEVICE_STATE_SEND, |
mluis | 0:45496a70a8a5 | 220 | DEVICE_STATE_CYCLE, |
mluis | 0:45496a70a8a5 | 221 | DEVICE_STATE_SLEEP |
abouillot | 9:16106008960b | 222 | } DeviceState; |
mluis | 0:45496a70a8a5 | 223 | |
mluis | 0:45496a70a8a5 | 224 | /*! |
mluis | 0:45496a70a8a5 | 225 | * LoRaWAN compliance tests support data |
mluis | 0:45496a70a8a5 | 226 | */ |
abouillot | 9:16106008960b | 227 | struct ComplianceTest_s { |
mluis | 0:45496a70a8a5 | 228 | bool Running; |
mluis | 0:45496a70a8a5 | 229 | uint8_t State; |
mluis | 0:45496a70a8a5 | 230 | bool IsTxConfirmed; |
mluis | 0:45496a70a8a5 | 231 | uint8_t AppPort; |
mluis | 0:45496a70a8a5 | 232 | uint8_t AppDataSize; |
mluis | 0:45496a70a8a5 | 233 | uint8_t *AppDataBuffer; |
mluis | 0:45496a70a8a5 | 234 | uint16_t DownLinkCounter; |
mluis | 0:45496a70a8a5 | 235 | bool LinkCheck; |
mluis | 0:45496a70a8a5 | 236 | uint8_t DemodMargin; |
mluis | 0:45496a70a8a5 | 237 | uint8_t NbGateways; |
abouillot | 9:16106008960b | 238 | } ComplianceTest; |
mluis | 0:45496a70a8a5 | 239 | |
mluis | 0:45496a70a8a5 | 240 | /* |
mluis | 0:45496a70a8a5 | 241 | * SerialDisplay managment variables |
mluis | 0:45496a70a8a5 | 242 | */ |
mluis | 0:45496a70a8a5 | 243 | |
mluis | 0:45496a70a8a5 | 244 | /*! |
mluis | 0:45496a70a8a5 | 245 | * Indicates if the MAC layer network join status has changed. |
mluis | 0:45496a70a8a5 | 246 | */ |
mluis | 0:45496a70a8a5 | 247 | static bool IsNetworkJoinedStatusUpdate = false; |
mluis | 0:45496a70a8a5 | 248 | |
mluis | 0:45496a70a8a5 | 249 | /*! |
mluis | 0:45496a70a8a5 | 250 | * Strucure containing the Uplink status |
mluis | 0:45496a70a8a5 | 251 | */ |
abouillot | 9:16106008960b | 252 | struct sLoRaMacUplinkStatus { |
mluis | 0:45496a70a8a5 | 253 | uint8_t Acked; |
mluis | 0:45496a70a8a5 | 254 | int8_t Datarate; |
mluis | 0:45496a70a8a5 | 255 | uint16_t UplinkCounter; |
mluis | 0:45496a70a8a5 | 256 | uint8_t Port; |
mluis | 0:45496a70a8a5 | 257 | uint8_t *Buffer; |
mluis | 0:45496a70a8a5 | 258 | uint8_t BufferSize; |
abouillot | 9:16106008960b | 259 | } LoRaMacUplinkStatus; |
mluis | 0:45496a70a8a5 | 260 | volatile bool UplinkStatusUpdated = false; |
mluis | 0:45496a70a8a5 | 261 | |
mluis | 0:45496a70a8a5 | 262 | /*! |
mluis | 0:45496a70a8a5 | 263 | * Strucure containing the Downlink status |
mluis | 0:45496a70a8a5 | 264 | */ |
abouillot | 9:16106008960b | 265 | struct sLoRaMacDownlinkStatus { |
mluis | 0:45496a70a8a5 | 266 | int16_t Rssi; |
mluis | 0:45496a70a8a5 | 267 | int8_t Snr; |
mluis | 0:45496a70a8a5 | 268 | uint16_t DownlinkCounter; |
mluis | 0:45496a70a8a5 | 269 | bool RxData; |
mluis | 0:45496a70a8a5 | 270 | uint8_t Port; |
mluis | 0:45496a70a8a5 | 271 | uint8_t *Buffer; |
mluis | 0:45496a70a8a5 | 272 | uint8_t BufferSize; |
abouillot | 9:16106008960b | 273 | } LoRaMacDownlinkStatus; |
mluis | 0:45496a70a8a5 | 274 | volatile bool DownlinkStatusUpdated = false; |
mluis | 0:45496a70a8a5 | 275 | |
abouillot | 9:16106008960b | 276 | /*! |
abouillot | 9:16106008960b | 277 | * User Button to generate interupt and send messages |
abouillot | 9:16106008960b | 278 | */ |
abouillot | 9:16106008960b | 279 | InterruptIn userButton(USER_BUTTON); |
abouillot | 9:16106008960b | 280 | |
mluis | 0:45496a70a8a5 | 281 | void SerialDisplayRefresh( void ) |
mluis | 0:45496a70a8a5 | 282 | { |
mluis | 0:45496a70a8a5 | 283 | MibRequestConfirm_t mibReq; |
mluis | 0:45496a70a8a5 | 284 | |
mluis | 0:45496a70a8a5 | 285 | SerialDisplayInit( ); |
mluis | 0:45496a70a8a5 | 286 | SerialDisplayUpdateActivationMode( OVER_THE_AIR_ACTIVATION ); |
mluis | 0:45496a70a8a5 | 287 | |
mluis | 0:45496a70a8a5 | 288 | #if( OVER_THE_AIR_ACTIVATION == 0 ) |
mluis | 0:45496a70a8a5 | 289 | SerialDisplayUpdateNwkId( LORAWAN_NETWORK_ID ); |
mluis | 0:45496a70a8a5 | 290 | SerialDisplayUpdateDevAddr( DevAddr ); |
mluis | 0:45496a70a8a5 | 291 | SerialDisplayUpdateKey( 12, NwkSKey ); |
mluis | 0:45496a70a8a5 | 292 | SerialDisplayUpdateKey( 13, AppSKey ); |
mluis | 7:ceb4063e6863 | 293 | #endif |
mluis | 0:45496a70a8a5 | 294 | SerialDisplayUpdateEui( 5, DevEui ); |
mluis | 0:45496a70a8a5 | 295 | SerialDisplayUpdateEui( 6, AppEui ); |
mluis | 0:45496a70a8a5 | 296 | SerialDisplayUpdateKey( 7, AppKey ); |
mluis | 0:45496a70a8a5 | 297 | |
mluis | 0:45496a70a8a5 | 298 | mibReq.Type = MIB_NETWORK_JOINED; |
mluis | 0:45496a70a8a5 | 299 | LoRaMacMibGetRequestConfirm( &mibReq ); |
mluis | 0:45496a70a8a5 | 300 | SerialDisplayUpdateNetworkIsJoined( mibReq.Param.IsNetworkJoined ); |
mluis | 0:45496a70a8a5 | 301 | |
mluis | 0:45496a70a8a5 | 302 | SerialDisplayUpdateAdr( LORAWAN_ADR_ON ); |
mluis | 0:45496a70a8a5 | 303 | #if defined( USE_BAND_868 ) |
mluis | 0:45496a70a8a5 | 304 | SerialDisplayUpdateDutyCycle( LORAWAN_DUTYCYCLE_ON ); |
mluis | 0:45496a70a8a5 | 305 | #else |
mluis | 0:45496a70a8a5 | 306 | SerialDisplayUpdateDutyCycle( false ); |
mluis | 0:45496a70a8a5 | 307 | #endif |
mluis | 0:45496a70a8a5 | 308 | SerialDisplayUpdatePublicNetwork( LORAWAN_PUBLIC_NETWORK ); |
abouillot | 9:16106008960b | 309 | |
mluis | 0:45496a70a8a5 | 310 | SerialDisplayUpdateLedState( 3, AppLedStateOn ); |
mluis | 0:45496a70a8a5 | 311 | } |
mluis | 0:45496a70a8a5 | 312 | |
mluis | 0:45496a70a8a5 | 313 | void SerialRxProcess( void ) |
mluis | 0:45496a70a8a5 | 314 | { |
abouillot | 9:16106008960b | 315 | if( SerialDisplayReadable( ) == true ) { |
abouillot | 9:16106008960b | 316 | switch( SerialDisplayGetChar( ) ) { |
mluis | 0:45496a70a8a5 | 317 | case 'R': |
mluis | 0:45496a70a8a5 | 318 | case 'r': |
mluis | 0:45496a70a8a5 | 319 | // Refresh Serial screen |
mluis | 0:45496a70a8a5 | 320 | SerialDisplayRefresh( ); |
mluis | 0:45496a70a8a5 | 321 | break; |
mluis | 0:45496a70a8a5 | 322 | default: |
mluis | 0:45496a70a8a5 | 323 | break; |
mluis | 0:45496a70a8a5 | 324 | } |
mluis | 0:45496a70a8a5 | 325 | } |
mluis | 0:45496a70a8a5 | 326 | } |
mluis | 0:45496a70a8a5 | 327 | |
mluis | 0:45496a70a8a5 | 328 | /*! |
mluis | 0:45496a70a8a5 | 329 | * \brief Prepares the payload of the frame |
mluis | 0:45496a70a8a5 | 330 | */ |
mluis | 0:45496a70a8a5 | 331 | static void PrepareTxFrame( uint8_t port ) |
mluis | 0:45496a70a8a5 | 332 | { |
abouillot | 9:16106008960b | 333 | switch( port ) { |
abouillot | 9:16106008960b | 334 | case 10: { |
abouillot | 9:16106008960b | 335 | int pos = 0; |
abouillot | 9:16106008960b | 336 | |
abouillot | 9:16106008960b | 337 | pc.printf("Prepare message\n"); |
abouillot | 9:16106008960b | 338 | #if 0 |
mluis | 5:fa113b25f612 | 339 | uint32_t tempValue = ( uint32_t )( LightValue * 1000000.0 ); |
GregCr | 3:d4142832d5af | 340 | AppData[0] = LightMode; |
GregCr | 3:d4142832d5af | 341 | AppData[1] = ( ( tempValue & 0xFF000000 ) >> 24 ) & 0xFF; |
GregCr | 3:d4142832d5af | 342 | AppData[2] = ( ( tempValue & 0x00FF0000 ) >> 16 ) & 0xFF; |
GregCr | 3:d4142832d5af | 343 | AppData[3] = ( ( tempValue & 0x0000FF00 ) >> 8 ) & 0xFF; |
mluis | 5:fa113b25f612 | 344 | AppData[4] = ( tempValue & 0x000000FF ); |
abouillot | 9:16106008960b | 345 | #else |
abouillot | 9:16106008960b | 346 | int err = 1; |
abouillot | 9:16106008960b | 347 | |
abouillot | 9:16106008960b | 348 | pc.printf("read dht "); |
abouillot | 9:16106008960b | 349 | for (int t = 0; t<5; t ++) { |
abouillot | 9:16106008960b | 350 | err = dhtSensor.readData(); |
abouillot | 9:16106008960b | 351 | if (err == 0) |
abouillot | 9:16106008960b | 352 | break; |
abouillot | 9:16106008960b | 353 | } |
abouillot | 9:16106008960b | 354 | if (err) { |
abouillot | 9:16106008960b | 355 | pc.printf("error: %d ", err); |
abouillot | 9:16106008960b | 356 | for (int i=0; i<4; i++) |
abouillot | 9:16106008960b | 357 | AppData[pos++] = 0xFF; |
abouillot | 9:16106008960b | 358 | } else { |
abouillot | 9:16106008960b | 359 | |
abouillot | 9:16106008960b | 360 | int16_t humidity = dhtSensor.ReadHumidity()*100; |
abouillot | 9:16106008960b | 361 | int16_t temp = dhtSensor.ReadTemperature(CELCIUS) * 100; |
abouillot | 9:16106008960b | 362 | AppData[pos++] = humidity >> 8; |
abouillot | 9:16106008960b | 363 | AppData[pos++] = humidity; |
abouillot | 9:16106008960b | 364 | AppData[pos++] = temp >> 8; |
abouillot | 9:16106008960b | 365 | AppData[pos++] = temp ; |
mluis | 0:45496a70a8a5 | 366 | } |
abouillot | 9:16106008960b | 367 | |
abouillot | 9:16106008960b | 368 | |
abouillot | 9:16106008960b | 369 | pc.printf("read bmp "); |
abouillot | 9:16106008960b | 370 | bmpSensor.measure(); |
abouillot | 9:16106008960b | 371 | int16_t pressure = bmpSensor.get_pressure(); |
abouillot | 9:16106008960b | 372 | AppData[pos++] = pressure >> 8; |
abouillot | 9:16106008960b | 373 | AppData[pos++] = pressure; |
abouillot | 9:16106008960b | 374 | int16_t temp = bmpSensor.get_temperature() * 100; |
abouillot | 9:16106008960b | 375 | AppData[pos++] = temp >> 8; |
abouillot | 9:16106008960b | 376 | AppData[pos++] = temp ; |
abouillot | 9:16106008960b | 377 | pc.printf("bmp: %d %d", pressure, temp); |
abouillot | 9:16106008960b | 378 | |
abouillot | 9:16106008960b | 379 | pc.printf("read sds "); |
abouillot | 9:16106008960b | 380 | sdsSensor.readData(); |
abouillot | 9:16106008960b | 381 | int pm = sdsSensor.PM2_5() * 10; |
abouillot | 9:16106008960b | 382 | AppData[pos++] = pm >> 8; |
abouillot | 9:16106008960b | 383 | AppData[pos++] = pm; |
abouillot | 9:16106008960b | 384 | pm = sdsSensor.PM10() * 10; |
abouillot | 9:16106008960b | 385 | AppData[pos++] = pm >> 8; |
abouillot | 9:16106008960b | 386 | AppData[pos++] = pm; |
abouillot | 9:16106008960b | 387 | |
abouillot | 9:16106008960b | 388 | AppDataSize = pos; |
abouillot | 9:16106008960b | 389 | for (int i=0; i<pos; i++) |
abouillot | 9:16106008960b | 390 | { pc.printf("%02x ", AppData[i]); |
abouillot | 9:16106008960b | 391 | } |
abouillot | 9:16106008960b | 392 | pc.printf("\n"); |
abouillot | 9:16106008960b | 393 | #endif |
abouillot | 9:16106008960b | 394 | pc.printf("Message Ready\n"); |
mluis | 0:45496a70a8a5 | 395 | } |
mluis | 0:45496a70a8a5 | 396 | break; |
abouillot | 9:16106008960b | 397 | case 15: { |
abouillot | 9:16106008960b | 398 | int pos = 0; |
abouillot | 9:16106008960b | 399 | AppData[pos++] = AppLedStateOn; |
abouillot | 9:16106008960b | 400 | #if 0 |
abouillot | 9:16106008960b | 401 | int err = 1; |
abouillot | 9:16106008960b | 402 | |
abouillot | 9:16106008960b | 403 | while(err != 0) { |
abouillot | 9:16106008960b | 404 | wait(2.0f); |
abouillot | 9:16106008960b | 405 | // err = dhtSensor.readData(); |
mluis | 0:45496a70a8a5 | 406 | } |
abouillot | 9:16106008960b | 407 | |
abouillot | 9:16106008960b | 408 | int16_t humidity = dhtSensor.ReadHumidity()*100; |
abouillot | 9:16106008960b | 409 | AppData[pos++] = humidity >> 8; |
abouillot | 9:16106008960b | 410 | AppData[pos++] = humidity; |
abouillot | 9:16106008960b | 411 | int16_t temp = dhtSensor.ReadTemperature(CELCIUS) * 100; |
abouillot | 9:16106008960b | 412 | AppData[pos++] = temp >> 8; |
abouillot | 9:16106008960b | 413 | AppData[pos++] = temp ; |
abouillot | 9:16106008960b | 414 | |
abouillot | 9:16106008960b | 415 | // bmpSensor.measure(); |
abouillot | 9:16106008960b | 416 | int16_t pressure = bmpSensor.get_pressure(); |
abouillot | 9:16106008960b | 417 | AppData[pos++] = pressure >> 8; |
abouillot | 9:16106008960b | 418 | AppData[pos++] = pressure; |
abouillot | 9:16106008960b | 419 | temp = bmpSensor.get_temperature() * 100; |
abouillot | 9:16106008960b | 420 | AppData[pos++] = temp >> 8; |
abouillot | 9:16106008960b | 421 | AppData[pos++] = temp ; |
abouillot | 9:16106008960b | 422 | |
abouillot | 9:16106008960b | 423 | // sdsSensor.readData(); |
abouillot | 9:16106008960b | 424 | int pm = sdsSensor.PM2_5() * 10; |
abouillot | 9:16106008960b | 425 | AppData[pos++] = pm >> 8; |
abouillot | 9:16106008960b | 426 | AppData[pos++] = pm; |
abouillot | 9:16106008960b | 427 | pm = sdsSensor.PM10() * 10; |
abouillot | 9:16106008960b | 428 | AppData[pos++] = pm >> 8; |
abouillot | 9:16106008960b | 429 | AppData[pos++] = pm; |
abouillot | 9:16106008960b | 430 | |
abouillot | 9:16106008960b | 431 | if( IsTxConfirmed == true ) { |
abouillot | 9:16106008960b | 432 | AppData[pos++] = LoRaMacDownlinkStatus.DownlinkCounter >> 8; |
abouillot | 9:16106008960b | 433 | AppData[pos++] = LoRaMacDownlinkStatus.DownlinkCounter; |
abouillot | 9:16106008960b | 434 | AppData[pos++] = LoRaMacDownlinkStatus.Rssi >> 8; |
abouillot | 9:16106008960b | 435 | AppData[pos++] = LoRaMacDownlinkStatus.Rssi; |
abouillot | 9:16106008960b | 436 | AppData[pos++] = LoRaMacDownlinkStatus.Snr; |
abouillot | 9:16106008960b | 437 | } |
abouillot | 9:16106008960b | 438 | #endif |
abouillot | 9:16106008960b | 439 | AppDataSize = pos; |
mluis | 0:45496a70a8a5 | 440 | } |
mluis | 0:45496a70a8a5 | 441 | break; |
abouillot | 9:16106008960b | 442 | case 224: |
abouillot | 9:16106008960b | 443 | if( ComplianceTest.LinkCheck == true ) { |
abouillot | 9:16106008960b | 444 | ComplianceTest.LinkCheck = false; |
abouillot | 9:16106008960b | 445 | AppDataSize = 3; |
abouillot | 9:16106008960b | 446 | AppData[0] = 5; |
abouillot | 9:16106008960b | 447 | AppData[1] = ComplianceTest.DemodMargin; |
abouillot | 9:16106008960b | 448 | AppData[2] = ComplianceTest.NbGateways; |
abouillot | 9:16106008960b | 449 | ComplianceTest.State = 1; |
abouillot | 9:16106008960b | 450 | } else { |
abouillot | 9:16106008960b | 451 | switch( ComplianceTest.State ) { |
abouillot | 9:16106008960b | 452 | case 4: |
abouillot | 9:16106008960b | 453 | ComplianceTest.State = 1; |
abouillot | 9:16106008960b | 454 | break; |
abouillot | 9:16106008960b | 455 | case 1: |
abouillot | 9:16106008960b | 456 | AppDataSize = 2; |
abouillot | 9:16106008960b | 457 | AppData[0] = ComplianceTest.DownLinkCounter >> 8; |
abouillot | 9:16106008960b | 458 | AppData[1] = ComplianceTest.DownLinkCounter; |
abouillot | 9:16106008960b | 459 | break; |
abouillot | 9:16106008960b | 460 | } |
abouillot | 9:16106008960b | 461 | } |
abouillot | 9:16106008960b | 462 | break; |
abouillot | 9:16106008960b | 463 | default: |
abouillot | 9:16106008960b | 464 | break; |
mluis | 0:45496a70a8a5 | 465 | } |
mluis | 0:45496a70a8a5 | 466 | } |
mluis | 0:45496a70a8a5 | 467 | |
mluis | 0:45496a70a8a5 | 468 | /*! |
mluis | 0:45496a70a8a5 | 469 | * \brief Prepares the payload of the frame |
mluis | 0:45496a70a8a5 | 470 | * |
mluis | 0:45496a70a8a5 | 471 | * \retval [0: frame could be send, 1: error] |
mluis | 0:45496a70a8a5 | 472 | */ |
mluis | 0:45496a70a8a5 | 473 | static bool SendFrame( void ) |
mluis | 0:45496a70a8a5 | 474 | { |
mluis | 0:45496a70a8a5 | 475 | McpsReq_t mcpsReq; |
mluis | 0:45496a70a8a5 | 476 | LoRaMacTxInfo_t txInfo; |
abouillot | 9:16106008960b | 477 | |
abouillot | 9:16106008960b | 478 | if( LoRaMacQueryTxPossible( AppDataSize, &txInfo ) != LORAMAC_STATUS_OK ) { |
mluis | 0:45496a70a8a5 | 479 | // Send empty frame in order to flush MAC commands |
mluis | 0:45496a70a8a5 | 480 | mcpsReq.Type = MCPS_UNCONFIRMED; |
mluis | 0:45496a70a8a5 | 481 | mcpsReq.Req.Unconfirmed.fBuffer = NULL; |
mluis | 0:45496a70a8a5 | 482 | mcpsReq.Req.Unconfirmed.fBufferSize = 0; |
mluis | 0:45496a70a8a5 | 483 | mcpsReq.Req.Unconfirmed.Datarate = LORAWAN_DEFAULT_DATARATE; |
mluis | 5:fa113b25f612 | 484 | |
mluis | 0:45496a70a8a5 | 485 | LoRaMacUplinkStatus.Acked = false; |
mluis | 0:45496a70a8a5 | 486 | LoRaMacUplinkStatus.Port = 0; |
mluis | 0:45496a70a8a5 | 487 | LoRaMacUplinkStatus.Buffer = NULL; |
mluis | 0:45496a70a8a5 | 488 | LoRaMacUplinkStatus.BufferSize = 0; |
mluis | 0:45496a70a8a5 | 489 | SerialDisplayUpdateFrameType( false ); |
abouillot | 9:16106008960b | 490 | } else { |
mluis | 0:45496a70a8a5 | 491 | LoRaMacUplinkStatus.Acked = false; |
mluis | 0:45496a70a8a5 | 492 | LoRaMacUplinkStatus.Port = AppPort; |
mluis | 0:45496a70a8a5 | 493 | LoRaMacUplinkStatus.Buffer = AppData; |
mluis | 0:45496a70a8a5 | 494 | LoRaMacUplinkStatus.BufferSize = AppDataSize; |
mluis | 0:45496a70a8a5 | 495 | SerialDisplayUpdateFrameType( IsTxConfirmed ); |
mluis | 0:45496a70a8a5 | 496 | |
abouillot | 9:16106008960b | 497 | if( IsTxConfirmed == false ) { |
mluis | 0:45496a70a8a5 | 498 | mcpsReq.Type = MCPS_UNCONFIRMED; |
mluis | 0:45496a70a8a5 | 499 | mcpsReq.Req.Unconfirmed.fPort = AppPort; |
mluis | 0:45496a70a8a5 | 500 | mcpsReq.Req.Unconfirmed.fBuffer = AppData; |
mluis | 0:45496a70a8a5 | 501 | mcpsReq.Req.Unconfirmed.fBufferSize = AppDataSize; |
mluis | 0:45496a70a8a5 | 502 | mcpsReq.Req.Unconfirmed.Datarate = LORAWAN_DEFAULT_DATARATE; |
abouillot | 9:16106008960b | 503 | } else { |
mluis | 0:45496a70a8a5 | 504 | mcpsReq.Type = MCPS_CONFIRMED; |
mluis | 0:45496a70a8a5 | 505 | mcpsReq.Req.Confirmed.fPort = AppPort; |
mluis | 0:45496a70a8a5 | 506 | mcpsReq.Req.Confirmed.fBuffer = AppData; |
mluis | 0:45496a70a8a5 | 507 | mcpsReq.Req.Confirmed.fBufferSize = AppDataSize; |
mluis | 4:de084e23d884 | 508 | mcpsReq.Req.Confirmed.NbTrials = 8; |
mluis | 0:45496a70a8a5 | 509 | mcpsReq.Req.Confirmed.Datarate = LORAWAN_DEFAULT_DATARATE; |
mluis | 0:45496a70a8a5 | 510 | } |
mluis | 0:45496a70a8a5 | 511 | } |
mluis | 0:45496a70a8a5 | 512 | |
abouillot | 9:16106008960b | 513 | if( LoRaMacMcpsRequest( &mcpsReq ) == LORAMAC_STATUS_OK ) { |
mluis | 0:45496a70a8a5 | 514 | return false; |
mluis | 0:45496a70a8a5 | 515 | } |
mluis | 0:45496a70a8a5 | 516 | return true; |
mluis | 0:45496a70a8a5 | 517 | } |
mluis | 0:45496a70a8a5 | 518 | |
mluis | 0:45496a70a8a5 | 519 | /*! |
mluis | 0:45496a70a8a5 | 520 | * \brief Function executed on TxNextPacket Timeout event |
mluis | 0:45496a70a8a5 | 521 | */ |
mluis | 0:45496a70a8a5 | 522 | static void OnTxNextPacketTimerEvent( void ) |
mluis | 0:45496a70a8a5 | 523 | { |
mluis | 0:45496a70a8a5 | 524 | MibRequestConfirm_t mibReq; |
mluis | 0:45496a70a8a5 | 525 | LoRaMacStatus_t status; |
mluis | 0:45496a70a8a5 | 526 | |
mluis | 0:45496a70a8a5 | 527 | TimerStop( &TxNextPacketTimer ); |
mluis | 0:45496a70a8a5 | 528 | |
mluis | 0:45496a70a8a5 | 529 | mibReq.Type = MIB_NETWORK_JOINED; |
mluis | 0:45496a70a8a5 | 530 | status = LoRaMacMibGetRequestConfirm( &mibReq ); |
mluis | 0:45496a70a8a5 | 531 | |
abouillot | 9:16106008960b | 532 | if( status == LORAMAC_STATUS_OK ) { |
abouillot | 9:16106008960b | 533 | if( mibReq.Param.IsNetworkJoined == true ) { |
mluis | 0:45496a70a8a5 | 534 | DeviceState = DEVICE_STATE_SEND; |
mluis | 0:45496a70a8a5 | 535 | NextTx = true; |
abouillot | 9:16106008960b | 536 | } else { |
mluis | 0:45496a70a8a5 | 537 | DeviceState = DEVICE_STATE_JOIN; |
mluis | 0:45496a70a8a5 | 538 | } |
mluis | 0:45496a70a8a5 | 539 | } |
mluis | 0:45496a70a8a5 | 540 | } |
mluis | 0:45496a70a8a5 | 541 | |
mluis | 0:45496a70a8a5 | 542 | /*! |
mluis | 0:45496a70a8a5 | 543 | * \brief Function executed on Led 1 Timeout event |
mluis | 0:45496a70a8a5 | 544 | */ |
mluis | 0:45496a70a8a5 | 545 | static void OnLed1TimerEvent( void ) |
mluis | 0:45496a70a8a5 | 546 | { |
mluis | 0:45496a70a8a5 | 547 | TimerStop( &Led1Timer ); |
mluis | 0:45496a70a8a5 | 548 | // Switch LED 1 OFF |
mluis | 0:45496a70a8a5 | 549 | Led1State = false; |
mluis | 0:45496a70a8a5 | 550 | Led1StateChanged = true; |
mluis | 0:45496a70a8a5 | 551 | } |
mluis | 0:45496a70a8a5 | 552 | |
mluis | 0:45496a70a8a5 | 553 | /*! |
mluis | 0:45496a70a8a5 | 554 | * \brief Function executed on Led 2 Timeout event |
mluis | 0:45496a70a8a5 | 555 | */ |
mluis | 0:45496a70a8a5 | 556 | static void OnLed2TimerEvent( void ) |
mluis | 0:45496a70a8a5 | 557 | { |
mluis | 0:45496a70a8a5 | 558 | TimerStop( &Led2Timer ); |
mluis | 0:45496a70a8a5 | 559 | // Switch LED 2 OFF |
mluis | 0:45496a70a8a5 | 560 | Led2State = false; |
mluis | 0:45496a70a8a5 | 561 | Led2StateChanged = true; |
mluis | 0:45496a70a8a5 | 562 | } |
mluis | 0:45496a70a8a5 | 563 | |
mluis | 0:45496a70a8a5 | 564 | /*! |
GregCr | 3:d4142832d5af | 565 | * \brief Function executed on Buzzer Timeout event |
GregCr | 3:d4142832d5af | 566 | */ |
mluis | 5:fa113b25f612 | 567 | static void OnBuzzerTimerEvent( void ) |
GregCr | 3:d4142832d5af | 568 | { |
mluis | 5:fa113b25f612 | 569 | Buzzer = 0; |
mluis | 5:fa113b25f612 | 570 | BuzzerTimer.detach( ); |
GregCr | 3:d4142832d5af | 571 | } |
GregCr | 3:d4142832d5af | 572 | |
GregCr | 3:d4142832d5af | 573 | /*! |
mluis | 0:45496a70a8a5 | 574 | * \brief MCPS-Confirm event function |
mluis | 0:45496a70a8a5 | 575 | * |
mluis | 5:fa113b25f612 | 576 | * \param [IN] mcpsConfirm - Pointer to the confirm structure, |
mluis | 0:45496a70a8a5 | 577 | * containing confirm attributes. |
mluis | 0:45496a70a8a5 | 578 | */ |
mluis | 5:fa113b25f612 | 579 | static void McpsConfirm( McpsConfirm_t *mcpsConfirm ) |
mluis | 0:45496a70a8a5 | 580 | { |
abouillot | 9:16106008960b | 581 | if( mcpsConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK ) { |
abouillot | 9:16106008960b | 582 | switch( mcpsConfirm->McpsRequest ) { |
abouillot | 9:16106008960b | 583 | case MCPS_UNCONFIRMED: { |
mluis | 0:45496a70a8a5 | 584 | // Check Datarate |
mluis | 0:45496a70a8a5 | 585 | // Check TxPower |
mluis | 0:45496a70a8a5 | 586 | break; |
mluis | 0:45496a70a8a5 | 587 | } |
abouillot | 9:16106008960b | 588 | case MCPS_CONFIRMED: { |
mluis | 0:45496a70a8a5 | 589 | // Check Datarate |
mluis | 0:45496a70a8a5 | 590 | // Check TxPower |
mluis | 0:45496a70a8a5 | 591 | // Check AckReceived |
mluis | 5:fa113b25f612 | 592 | // Check NbTrials |
mluis | 5:fa113b25f612 | 593 | LoRaMacUplinkStatus.Acked = mcpsConfirm->AckReceived; |
mluis | 0:45496a70a8a5 | 594 | break; |
mluis | 0:45496a70a8a5 | 595 | } |
abouillot | 9:16106008960b | 596 | case MCPS_PROPRIETARY: { |
mluis | 0:45496a70a8a5 | 597 | break; |
mluis | 0:45496a70a8a5 | 598 | } |
mluis | 0:45496a70a8a5 | 599 | default: |
mluis | 0:45496a70a8a5 | 600 | break; |
mluis | 0:45496a70a8a5 | 601 | } |
mluis | 5:fa113b25f612 | 602 | LoRaMacUplinkStatus.Datarate = mcpsConfirm->Datarate; |
mluis | 5:fa113b25f612 | 603 | LoRaMacUplinkStatus.UplinkCounter = mcpsConfirm->UpLinkCounter; |
mluis | 5:fa113b25f612 | 604 | |
mluis | 7:ceb4063e6863 | 605 | // Switch LED 1 ON |
mluis | 7:ceb4063e6863 | 606 | Led1State = true; |
mluis | 7:ceb4063e6863 | 607 | Led1StateChanged = true; |
mluis | 7:ceb4063e6863 | 608 | TimerStart( &Led1Timer ); |
mluis | 7:ceb4063e6863 | 609 | |
mluis | 0:45496a70a8a5 | 610 | UplinkStatusUpdated = true; |
mluis | 0:45496a70a8a5 | 611 | } |
mluis | 0:45496a70a8a5 | 612 | NextTx = true; |
mluis | 0:45496a70a8a5 | 613 | } |
mluis | 0:45496a70a8a5 | 614 | |
mluis | 0:45496a70a8a5 | 615 | /*! |
mluis | 0:45496a70a8a5 | 616 | * \brief MCPS-Indication event function |
mluis | 0:45496a70a8a5 | 617 | * |
mluis | 5:fa113b25f612 | 618 | * \param [IN] mcpsIndication - Pointer to the indication structure, |
mluis | 0:45496a70a8a5 | 619 | * containing indication attributes. |
mluis | 0:45496a70a8a5 | 620 | */ |
mluis | 5:fa113b25f612 | 621 | static void McpsIndication( McpsIndication_t *mcpsIndication ) |
mluis | 0:45496a70a8a5 | 622 | { |
abouillot | 9:16106008960b | 623 | if( mcpsIndication->Status != LORAMAC_EVENT_INFO_STATUS_OK ) { |
mluis | 0:45496a70a8a5 | 624 | return; |
mluis | 0:45496a70a8a5 | 625 | } |
mluis | 0:45496a70a8a5 | 626 | |
abouillot | 9:16106008960b | 627 | switch( mcpsIndication->McpsIndication ) { |
abouillot | 9:16106008960b | 628 | case MCPS_UNCONFIRMED: { |
mluis | 0:45496a70a8a5 | 629 | break; |
mluis | 0:45496a70a8a5 | 630 | } |
abouillot | 9:16106008960b | 631 | case MCPS_CONFIRMED: { |
mluis | 0:45496a70a8a5 | 632 | break; |
mluis | 0:45496a70a8a5 | 633 | } |
abouillot | 9:16106008960b | 634 | case MCPS_PROPRIETARY: { |
mluis | 0:45496a70a8a5 | 635 | break; |
mluis | 0:45496a70a8a5 | 636 | } |
abouillot | 9:16106008960b | 637 | case MCPS_MULTICAST: { |
mluis | 0:45496a70a8a5 | 638 | break; |
mluis | 0:45496a70a8a5 | 639 | } |
mluis | 0:45496a70a8a5 | 640 | default: |
mluis | 0:45496a70a8a5 | 641 | break; |
mluis | 0:45496a70a8a5 | 642 | } |
mluis | 0:45496a70a8a5 | 643 | |
mluis | 0:45496a70a8a5 | 644 | // Check Multicast |
mluis | 0:45496a70a8a5 | 645 | // Check Port |
mluis | 0:45496a70a8a5 | 646 | // Check Datarate |
mluis | 0:45496a70a8a5 | 647 | // Check FramePending |
mluis | 0:45496a70a8a5 | 648 | // Check Buffer |
mluis | 0:45496a70a8a5 | 649 | // Check BufferSize |
mluis | 0:45496a70a8a5 | 650 | // Check Rssi |
mluis | 0:45496a70a8a5 | 651 | // Check Snr |
mluis | 0:45496a70a8a5 | 652 | // Check RxSlot |
mluis | 5:fa113b25f612 | 653 | LoRaMacDownlinkStatus.Rssi = mcpsIndication->Rssi; |
abouillot | 9:16106008960b | 654 | if( mcpsIndication->Snr & 0x80 ) { // The SNR sign bit is 1 |
mluis | 0:45496a70a8a5 | 655 | // Invert and divide by 4 |
mluis | 5:fa113b25f612 | 656 | LoRaMacDownlinkStatus.Snr = ( ( ~mcpsIndication->Snr + 1 ) & 0xFF ) >> 2; |
mluis | 0:45496a70a8a5 | 657 | LoRaMacDownlinkStatus.Snr = -LoRaMacDownlinkStatus.Snr; |
abouillot | 9:16106008960b | 658 | } else { |
mluis | 0:45496a70a8a5 | 659 | // Divide by 4 |
mluis | 5:fa113b25f612 | 660 | LoRaMacDownlinkStatus.Snr = ( mcpsIndication->Snr & 0xFF ) >> 2; |
mluis | 0:45496a70a8a5 | 661 | } |
mluis | 0:45496a70a8a5 | 662 | LoRaMacDownlinkStatus.DownlinkCounter++; |
mluis | 5:fa113b25f612 | 663 | LoRaMacDownlinkStatus.RxData = mcpsIndication->RxData; |
mluis | 5:fa113b25f612 | 664 | LoRaMacDownlinkStatus.Port = mcpsIndication->Port; |
mluis | 5:fa113b25f612 | 665 | LoRaMacDownlinkStatus.Buffer = mcpsIndication->Buffer; |
mluis | 5:fa113b25f612 | 666 | LoRaMacDownlinkStatus.BufferSize = mcpsIndication->BufferSize; |
mluis | 0:45496a70a8a5 | 667 | |
abouillot | 9:16106008960b | 668 | if( ComplianceTest.Running == true ) { |
mluis | 0:45496a70a8a5 | 669 | ComplianceTest.DownLinkCounter++; |
mluis | 0:45496a70a8a5 | 670 | } |
mluis | 0:45496a70a8a5 | 671 | |
abouillot | 9:16106008960b | 672 | if( mcpsIndication->RxData == true ) { |
abouillot | 9:16106008960b | 673 | switch( mcpsIndication->Port ) { |
abouillot | 9:16106008960b | 674 | case 1: // The application LED can be controlled on port 1 or 2 |
abouillot | 9:16106008960b | 675 | case 2: |
abouillot | 9:16106008960b | 676 | if( mcpsIndication->BufferSize == 1 ) { |
abouillot | 9:16106008960b | 677 | AppLedStateOn = mcpsIndication->Buffer[0] & 0x01; |
abouillot | 9:16106008960b | 678 | Led3StateChanged = true; |
abouillot | 9:16106008960b | 679 | } |
abouillot | 9:16106008960b | 680 | break; |
abouillot | 9:16106008960b | 681 | case 10: |
abouillot | 9:16106008960b | 682 | Display.write( 0, mcpsIndication->Buffer[0] ); |
abouillot | 9:16106008960b | 683 | Display.write( 1, mcpsIndication->Buffer[1] ); |
abouillot | 9:16106008960b | 684 | Display.write( 2, mcpsIndication->Buffer[2] ); |
abouillot | 9:16106008960b | 685 | Display.write( 3, mcpsIndication->Buffer[3] ); |
abouillot | 9:16106008960b | 686 | break; |
abouillot | 9:16106008960b | 687 | case 20: |
abouillot | 9:16106008960b | 688 | LightMode = mcpsIndication->Buffer[0]; |
abouillot | 9:16106008960b | 689 | if( LightMode ) { |
abouillot | 9:16106008960b | 690 | ColorLed.setColorRGB( 0, mcpsIndication->Buffer[1], mcpsIndication->Buffer[2], mcpsIndication->Buffer[3] ); |
abouillot | 9:16106008960b | 691 | } |
abouillot | 9:16106008960b | 692 | break; |
abouillot | 9:16106008960b | 693 | case 30: |
abouillot | 9:16106008960b | 694 | BuzzerTimer.attach_us( &OnBuzzerTimerEvent, 200000 ); |
abouillot | 9:16106008960b | 695 | Buzzer = 1; |
abouillot | 9:16106008960b | 696 | break; |
abouillot | 9:16106008960b | 697 | case 224: |
abouillot | 9:16106008960b | 698 | if( ComplianceTest.Running == false ) { |
abouillot | 9:16106008960b | 699 | // Check compliance test enable command (i) |
abouillot | 9:16106008960b | 700 | if( ( mcpsIndication->BufferSize == 4 ) && |
abouillot | 9:16106008960b | 701 | ( mcpsIndication->Buffer[0] == 0x01 ) && |
abouillot | 9:16106008960b | 702 | ( mcpsIndication->Buffer[1] == 0x01 ) && |
abouillot | 9:16106008960b | 703 | ( mcpsIndication->Buffer[2] == 0x01 ) && |
abouillot | 9:16106008960b | 704 | ( mcpsIndication->Buffer[3] == 0x01 ) ) { |
abouillot | 9:16106008960b | 705 | IsTxConfirmed = false; |
abouillot | 9:16106008960b | 706 | AppPort = 224; |
abouillot | 9:16106008960b | 707 | AppDataSize = 2; |
abouillot | 9:16106008960b | 708 | ComplianceTest.DownLinkCounter = 0; |
abouillot | 9:16106008960b | 709 | ComplianceTest.LinkCheck = false; |
abouillot | 9:16106008960b | 710 | ComplianceTest.DemodMargin = 0; |
abouillot | 9:16106008960b | 711 | ComplianceTest.NbGateways = 0; |
abouillot | 9:16106008960b | 712 | ComplianceTest.Running = true; |
abouillot | 9:16106008960b | 713 | ComplianceTest.State = 1; |
abouillot | 9:16106008960b | 714 | |
abouillot | 9:16106008960b | 715 | MibRequestConfirm_t mibReq; |
abouillot | 9:16106008960b | 716 | mibReq.Type = MIB_ADR; |
abouillot | 9:16106008960b | 717 | mibReq.Param.AdrEnable = true; |
abouillot | 9:16106008960b | 718 | LoRaMacMibSetRequestConfirm( &mibReq ); |
mluis | 0:45496a70a8a5 | 719 | |
mluis | 0:45496a70a8a5 | 720 | #if defined( USE_BAND_868 ) |
abouillot | 9:16106008960b | 721 | LoRaMacTestSetDutyCycleOn( false ); |
mluis | 0:45496a70a8a5 | 722 | #endif |
abouillot | 9:16106008960b | 723 | } |
abouillot | 9:16106008960b | 724 | } else { |
abouillot | 9:16106008960b | 725 | ComplianceTest.State = mcpsIndication->Buffer[0]; |
abouillot | 9:16106008960b | 726 | switch( ComplianceTest.State ) { |
abouillot | 9:16106008960b | 727 | case 0: // Check compliance test disable command (ii) |
abouillot | 9:16106008960b | 728 | IsTxConfirmed = LORAWAN_CONFIRMED_MSG_ON; |
abouillot | 9:16106008960b | 729 | AppPort = LORAWAN_APP_PORT; |
abouillot | 9:16106008960b | 730 | AppDataSize = LORAWAN_APP_DATA_SIZE; |
abouillot | 9:16106008960b | 731 | ComplianceTest.DownLinkCounter = 0; |
abouillot | 9:16106008960b | 732 | ComplianceTest.Running = false; |
abouillot | 9:16106008960b | 733 | |
abouillot | 9:16106008960b | 734 | MibRequestConfirm_t mibReq; |
abouillot | 9:16106008960b | 735 | mibReq.Type = MIB_ADR; |
abouillot | 9:16106008960b | 736 | mibReq.Param.AdrEnable = LORAWAN_ADR_ON; |
abouillot | 9:16106008960b | 737 | LoRaMacMibSetRequestConfirm( &mibReq ); |
mluis | 0:45496a70a8a5 | 738 | #if defined( USE_BAND_868 ) |
abouillot | 9:16106008960b | 739 | LoRaMacTestSetDutyCycleOn( LORAWAN_DUTYCYCLE_ON ); |
mluis | 0:45496a70a8a5 | 740 | #endif |
abouillot | 9:16106008960b | 741 | break; |
abouillot | 9:16106008960b | 742 | case 1: // (iii, iv) |
abouillot | 9:16106008960b | 743 | AppDataSize = 2; |
abouillot | 9:16106008960b | 744 | break; |
abouillot | 9:16106008960b | 745 | case 2: // Enable confirmed messages (v) |
abouillot | 9:16106008960b | 746 | IsTxConfirmed = true; |
abouillot | 9:16106008960b | 747 | ComplianceTest.State = 1; |
abouillot | 9:16106008960b | 748 | break; |
abouillot | 9:16106008960b | 749 | case 3: // Disable confirmed messages (vi) |
abouillot | 9:16106008960b | 750 | IsTxConfirmed = false; |
abouillot | 9:16106008960b | 751 | ComplianceTest.State = 1; |
abouillot | 9:16106008960b | 752 | break; |
abouillot | 9:16106008960b | 753 | case 4: // (vii) |
abouillot | 9:16106008960b | 754 | AppDataSize = mcpsIndication->BufferSize; |
mluis | 0:45496a70a8a5 | 755 | |
abouillot | 9:16106008960b | 756 | AppData[0] = 4; |
abouillot | 9:16106008960b | 757 | for( uint8_t i = 1; i < AppDataSize; i++ ) { |
abouillot | 9:16106008960b | 758 | AppData[i] = mcpsIndication->Buffer[i] + 1; |
abouillot | 9:16106008960b | 759 | } |
abouillot | 9:16106008960b | 760 | break; |
abouillot | 9:16106008960b | 761 | case 5: { // (viii) |
abouillot | 9:16106008960b | 762 | MlmeReq_t mlmeReq; |
abouillot | 9:16106008960b | 763 | mlmeReq.Type = MLME_LINK_CHECK; |
abouillot | 9:16106008960b | 764 | LoRaMacMlmeRequest( &mlmeReq ); |
abouillot | 9:16106008960b | 765 | } |
abouillot | 9:16106008960b | 766 | break; |
abouillot | 9:16106008960b | 767 | case 6: { // (ix) |
abouillot | 9:16106008960b | 768 | MlmeReq_t mlmeReq; |
abouillot | 9:16106008960b | 769 | |
abouillot | 9:16106008960b | 770 | mlmeReq.Type = MLME_JOIN; |
abouillot | 9:16106008960b | 771 | |
abouillot | 9:16106008960b | 772 | mlmeReq.Req.Join.DevEui = DevEui; |
abouillot | 9:16106008960b | 773 | mlmeReq.Req.Join.AppEui = AppEui; |
abouillot | 9:16106008960b | 774 | mlmeReq.Req.Join.AppKey = AppKey; |
abouillot | 9:16106008960b | 775 | |
abouillot | 9:16106008960b | 776 | LoRaMacMlmeRequest( &mlmeReq ); |
abouillot | 9:16106008960b | 777 | DeviceState = DEVICE_STATE_SLEEP; |
abouillot | 9:16106008960b | 778 | } |
abouillot | 9:16106008960b | 779 | break; |
abouillot | 9:16106008960b | 780 | default: |
abouillot | 9:16106008960b | 781 | break; |
mluis | 0:45496a70a8a5 | 782 | } |
mluis | 0:45496a70a8a5 | 783 | } |
abouillot | 9:16106008960b | 784 | break; |
abouillot | 9:16106008960b | 785 | default: |
abouillot | 9:16106008960b | 786 | break; |
mluis | 0:45496a70a8a5 | 787 | } |
mluis | 0:45496a70a8a5 | 788 | } |
mluis | 0:45496a70a8a5 | 789 | |
mluis | 0:45496a70a8a5 | 790 | // Switch LED 2 ON for each received downlink |
mluis | 0:45496a70a8a5 | 791 | Led2State = true; |
mluis | 0:45496a70a8a5 | 792 | Led2StateChanged = true; |
mluis | 0:45496a70a8a5 | 793 | TimerStart( &Led2Timer ); |
mluis | 0:45496a70a8a5 | 794 | DownlinkStatusUpdated = true; |
mluis | 0:45496a70a8a5 | 795 | } |
mluis | 0:45496a70a8a5 | 796 | |
mluis | 0:45496a70a8a5 | 797 | /*! |
mluis | 0:45496a70a8a5 | 798 | * \brief MLME-Confirm event function |
mluis | 0:45496a70a8a5 | 799 | * |
mluis | 5:fa113b25f612 | 800 | * \param [IN] mlmeConfirm - Pointer to the confirm structure, |
mluis | 0:45496a70a8a5 | 801 | * containing confirm attributes. |
mluis | 0:45496a70a8a5 | 802 | */ |
mluis | 5:fa113b25f612 | 803 | static void MlmeConfirm( MlmeConfirm_t *mlmeConfirm ) |
mluis | 0:45496a70a8a5 | 804 | { |
abouillot | 9:16106008960b | 805 | if( mlmeConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK ) { |
abouillot | 9:16106008960b | 806 | switch( mlmeConfirm->MlmeRequest ) { |
abouillot | 9:16106008960b | 807 | case MLME_JOIN: { |
mluis | 0:45496a70a8a5 | 808 | // Status is OK, node has joined the network |
mluis | 0:45496a70a8a5 | 809 | IsNetworkJoinedStatusUpdate = true; |
mluis | 7:ceb4063e6863 | 810 | DeviceState = DEVICE_STATE_SEND; |
mluis | 7:ceb4063e6863 | 811 | NextTx = true; |
mluis | 0:45496a70a8a5 | 812 | break; |
mluis | 0:45496a70a8a5 | 813 | } |
abouillot | 9:16106008960b | 814 | case MLME_LINK_CHECK: { |
mluis | 0:45496a70a8a5 | 815 | // Check DemodMargin |
mluis | 0:45496a70a8a5 | 816 | // Check NbGateways |
abouillot | 9:16106008960b | 817 | if( ComplianceTest.Running == true ) { |
mluis | 0:45496a70a8a5 | 818 | ComplianceTest.LinkCheck = true; |
mluis | 5:fa113b25f612 | 819 | ComplianceTest.DemodMargin = mlmeConfirm->DemodMargin; |
mluis | 5:fa113b25f612 | 820 | ComplianceTest.NbGateways = mlmeConfirm->NbGateways; |
mluis | 0:45496a70a8a5 | 821 | } |
mluis | 0:45496a70a8a5 | 822 | break; |
mluis | 0:45496a70a8a5 | 823 | } |
mluis | 0:45496a70a8a5 | 824 | default: |
mluis | 0:45496a70a8a5 | 825 | break; |
mluis | 0:45496a70a8a5 | 826 | } |
mluis | 0:45496a70a8a5 | 827 | } |
mluis | 0:45496a70a8a5 | 828 | NextTx = true; |
mluis | 0:45496a70a8a5 | 829 | UplinkStatusUpdated = true; |
mluis | 0:45496a70a8a5 | 830 | } |
mluis | 0:45496a70a8a5 | 831 | |
abouillot | 9:16106008960b | 832 | /*! |
abouillot | 9:16106008960b | 833 | * \brief Function executed when user button is pressed |
abouillot | 9:16106008960b | 834 | */ |
abouillot | 9:16106008960b | 835 | void buttonPressed() |
abouillot | 9:16106008960b | 836 | { |
abouillot | 9:16106008960b | 837 | pc.printf("Button pressed\n"); |
abouillot | 9:16106008960b | 838 | DeviceState = DEVICE_STATE_SEND; |
abouillot | 9:16106008960b | 839 | } |
abouillot | 9:16106008960b | 840 | |
mluis | 0:45496a70a8a5 | 841 | /** |
mluis | 0:45496a70a8a5 | 842 | * Main application entry point. |
mluis | 0:45496a70a8a5 | 843 | */ |
mluis | 0:45496a70a8a5 | 844 | int main( void ) |
mluis | 0:45496a70a8a5 | 845 | { |
abouillot | 9:16106008960b | 846 | pc.printf("main entry\n"); |
mluis | 0:45496a70a8a5 | 847 | LoRaMacPrimitives_t LoRaMacPrimitives; |
mluis | 0:45496a70a8a5 | 848 | LoRaMacCallback_t LoRaMacCallbacks; |
mluis | 0:45496a70a8a5 | 849 | MibRequestConfirm_t mibReq; |
mluis | 0:45496a70a8a5 | 850 | |
abouillot | 9:16106008960b | 851 | pc.printf("Board init"); |
mluis | 0:45496a70a8a5 | 852 | BoardInit( ); |
abouillot | 9:16106008960b | 853 | pc.printf("Serial display"); |
mluis | 0:45496a70a8a5 | 854 | SerialDisplayInit( ); |
mluis | 0:45496a70a8a5 | 855 | |
mluis | 5:fa113b25f612 | 856 | LightMode = 0; // 0: manual, 1: automatic |
mluis | 5:fa113b25f612 | 857 | Buzzer = 0; // 0: OFF, 1: ON |
mluis | 5:fa113b25f612 | 858 | |
mluis | 7:ceb4063e6863 | 859 | SerialDisplayUpdateEui( 5, DevEui ); |
mluis | 7:ceb4063e6863 | 860 | SerialDisplayUpdateEui( 6, AppEui ); |
mluis | 7:ceb4063e6863 | 861 | SerialDisplayUpdateKey( 7, AppKey ); |
mluis | 7:ceb4063e6863 | 862 | |
mluis | 7:ceb4063e6863 | 863 | #if( OVER_THE_AIR_ACTIVATION == 0 ) |
mluis | 7:ceb4063e6863 | 864 | SerialDisplayUpdateNwkId( LORAWAN_NETWORK_ID ); |
mluis | 7:ceb4063e6863 | 865 | SerialDisplayUpdateDevAddr( DevAddr ); |
mluis | 7:ceb4063e6863 | 866 | SerialDisplayUpdateKey( 12, NwkSKey ); |
mluis | 7:ceb4063e6863 | 867 | SerialDisplayUpdateKey( 13, AppSKey ); |
mluis | 7:ceb4063e6863 | 868 | #endif |
mluis | 7:ceb4063e6863 | 869 | |
abouillot | 9:16106008960b | 870 | userButton.fall(&buttonPressed); |
abouillot | 9:16106008960b | 871 | |
mluis | 0:45496a70a8a5 | 872 | DeviceState = DEVICE_STATE_INIT; |
mluis | 0:45496a70a8a5 | 873 | |
abouillot | 9:16106008960b | 874 | while( 1 ) { |
mluis | 0:45496a70a8a5 | 875 | SerialRxProcess( ); |
abouillot | 9:16106008960b | 876 | if( IsNetworkJoinedStatusUpdate == true ) { |
mluis | 0:45496a70a8a5 | 877 | IsNetworkJoinedStatusUpdate = false; |
mluis | 0:45496a70a8a5 | 878 | mibReq.Type = MIB_NETWORK_JOINED; |
mluis | 0:45496a70a8a5 | 879 | LoRaMacMibGetRequestConfirm( &mibReq ); |
mluis | 0:45496a70a8a5 | 880 | SerialDisplayUpdateNetworkIsJoined( mibReq.Param.IsNetworkJoined ); |
mluis | 0:45496a70a8a5 | 881 | } |
abouillot | 9:16106008960b | 882 | if( Led1StateChanged == true ) { |
mluis | 0:45496a70a8a5 | 883 | Led1StateChanged = false; |
mluis | 0:45496a70a8a5 | 884 | SerialDisplayUpdateLedState( 1, Led1State ); |
mluis | 0:45496a70a8a5 | 885 | } |
abouillot | 9:16106008960b | 886 | if( Led2StateChanged == true ) { |
mluis | 0:45496a70a8a5 | 887 | Led2StateChanged = false; |
mluis | 0:45496a70a8a5 | 888 | SerialDisplayUpdateLedState( 2, Led2State ); |
mluis | 0:45496a70a8a5 | 889 | } |
abouillot | 9:16106008960b | 890 | if( Led3StateChanged == true ) { |
mluis | 0:45496a70a8a5 | 891 | Led3StateChanged = false; |
mluis | 0:45496a70a8a5 | 892 | SerialDisplayUpdateLedState( 3, AppLedStateOn ); |
mluis | 0:45496a70a8a5 | 893 | } |
abouillot | 9:16106008960b | 894 | if( UplinkStatusUpdated == true ) { |
mluis | 0:45496a70a8a5 | 895 | UplinkStatusUpdated = false; |
mluis | 0:45496a70a8a5 | 896 | SerialDisplayUpdateUplink( LoRaMacUplinkStatus.Acked, LoRaMacUplinkStatus.Datarate, LoRaMacUplinkStatus.UplinkCounter, LoRaMacUplinkStatus.Port, LoRaMacUplinkStatus.Buffer, LoRaMacUplinkStatus.BufferSize ); |
mluis | 0:45496a70a8a5 | 897 | } |
abouillot | 9:16106008960b | 898 | if( DownlinkStatusUpdated == true ) { |
mluis | 0:45496a70a8a5 | 899 | DownlinkStatusUpdated = false; |
mluis | 0:45496a70a8a5 | 900 | SerialDisplayUpdateLedState( 2, Led2State ); |
mluis | 0:45496a70a8a5 | 901 | SerialDisplayUpdateDownlink( LoRaMacDownlinkStatus.RxData, LoRaMacDownlinkStatus.Rssi, LoRaMacDownlinkStatus.Snr, LoRaMacDownlinkStatus.DownlinkCounter, LoRaMacDownlinkStatus.Port, LoRaMacDownlinkStatus.Buffer, LoRaMacDownlinkStatus.BufferSize ); |
mluis | 0:45496a70a8a5 | 902 | } |
abouillot | 9:16106008960b | 903 | |
abouillot | 9:16106008960b | 904 | switch( DeviceState ) { |
abouillot | 9:16106008960b | 905 | case DEVICE_STATE_INIT: { |
abouillot | 9:16106008960b | 906 | pc.printf("DEVICE_STATE_INIT\n"); |
mluis | 0:45496a70a8a5 | 907 | LoRaMacPrimitives.MacMcpsConfirm = McpsConfirm; |
mluis | 0:45496a70a8a5 | 908 | LoRaMacPrimitives.MacMcpsIndication = McpsIndication; |
mluis | 0:45496a70a8a5 | 909 | LoRaMacPrimitives.MacMlmeConfirm = MlmeConfirm; |
mluis | 0:45496a70a8a5 | 910 | LoRaMacCallbacks.GetBatteryLevel = BoardGetBatteryLevel; |
mluis | 0:45496a70a8a5 | 911 | LoRaMacInitialization( &LoRaMacPrimitives, &LoRaMacCallbacks ); |
mluis | 0:45496a70a8a5 | 912 | |
mluis | 0:45496a70a8a5 | 913 | TimerInit( &TxNextPacketTimer, OnTxNextPacketTimerEvent ); |
mluis | 0:45496a70a8a5 | 914 | |
mluis | 0:45496a70a8a5 | 915 | TimerInit( &Led1Timer, OnLed1TimerEvent ); |
mluis | 0:45496a70a8a5 | 916 | TimerSetValue( &Led1Timer, 25000 ); |
mluis | 0:45496a70a8a5 | 917 | |
mluis | 0:45496a70a8a5 | 918 | TimerInit( &Led2Timer, OnLed2TimerEvent ); |
mluis | 0:45496a70a8a5 | 919 | TimerSetValue( &Led2Timer, 25000 ); |
mluis | 0:45496a70a8a5 | 920 | |
mluis | 0:45496a70a8a5 | 921 | mibReq.Type = MIB_ADR; |
mluis | 0:45496a70a8a5 | 922 | mibReq.Param.AdrEnable = LORAWAN_ADR_ON; |
mluis | 0:45496a70a8a5 | 923 | LoRaMacMibSetRequestConfirm( &mibReq ); |
mluis | 0:45496a70a8a5 | 924 | |
mluis | 0:45496a70a8a5 | 925 | mibReq.Type = MIB_PUBLIC_NETWORK; |
mluis | 0:45496a70a8a5 | 926 | mibReq.Param.EnablePublicNetwork = LORAWAN_PUBLIC_NETWORK; |
mluis | 0:45496a70a8a5 | 927 | LoRaMacMibSetRequestConfirm( &mibReq ); |
mluis | 0:45496a70a8a5 | 928 | |
mluis | 0:45496a70a8a5 | 929 | #if defined( USE_BAND_868 ) |
mluis | 0:45496a70a8a5 | 930 | LoRaMacTestSetDutyCycleOn( LORAWAN_DUTYCYCLE_ON ); |
mluis | 0:45496a70a8a5 | 931 | SerialDisplayUpdateDutyCycle( LORAWAN_DUTYCYCLE_ON ); |
mluis | 5:fa113b25f612 | 932 | |
abouillot | 9:16106008960b | 933 | #if( USE_SEMTECH_DEFAULT_CHANNEL_LINEUP == 1 ) |
mluis | 5:fa113b25f612 | 934 | LoRaMacChannelAdd( 3, ( ChannelParams_t )LC4 ); |
mluis | 5:fa113b25f612 | 935 | LoRaMacChannelAdd( 4, ( ChannelParams_t )LC5 ); |
mluis | 5:fa113b25f612 | 936 | LoRaMacChannelAdd( 5, ( ChannelParams_t )LC6 ); |
mluis | 5:fa113b25f612 | 937 | LoRaMacChannelAdd( 6, ( ChannelParams_t )LC7 ); |
mluis | 5:fa113b25f612 | 938 | LoRaMacChannelAdd( 7, ( ChannelParams_t )LC8 ); |
mluis | 5:fa113b25f612 | 939 | LoRaMacChannelAdd( 8, ( ChannelParams_t )LC9 ); |
mluis | 5:fa113b25f612 | 940 | LoRaMacChannelAdd( 9, ( ChannelParams_t )LC10 ); |
mluis | 7:ceb4063e6863 | 941 | |
mluis | 7:ceb4063e6863 | 942 | mibReq.Type = MIB_RX2_CHANNEL; |
abouillot | 9:16106008960b | 943 | mibReq.Param.Rx2Channel = ( Rx2ChannelParams_t ) { |
abouillot | 9:16106008960b | 944 | 869525000, DR_3 |
abouillot | 9:16106008960b | 945 | }; |
mluis | 7:ceb4063e6863 | 946 | LoRaMacMibSetRequestConfirm( &mibReq ); |
mluis | 5:fa113b25f612 | 947 | #endif |
mluis | 5:fa113b25f612 | 948 | |
mluis | 0:45496a70a8a5 | 949 | #endif |
mluis | 0:45496a70a8a5 | 950 | SerialDisplayUpdateActivationMode( OVER_THE_AIR_ACTIVATION ); |
mluis | 0:45496a70a8a5 | 951 | SerialDisplayUpdateAdr( LORAWAN_ADR_ON ); |
mluis | 0:45496a70a8a5 | 952 | SerialDisplayUpdatePublicNetwork( LORAWAN_PUBLIC_NETWORK ); |
mluis | 0:45496a70a8a5 | 953 | |
mluis | 0:45496a70a8a5 | 954 | LoRaMacDownlinkStatus.DownlinkCounter = 0; |
mluis | 0:45496a70a8a5 | 955 | |
mluis | 0:45496a70a8a5 | 956 | DeviceState = DEVICE_STATE_JOIN; |
mluis | 0:45496a70a8a5 | 957 | break; |
mluis | 0:45496a70a8a5 | 958 | } |
abouillot | 9:16106008960b | 959 | case DEVICE_STATE_JOIN: { |
abouillot | 9:16106008960b | 960 | pc.printf("DEVICE_STATE_JOIN\n"); |
mluis | 0:45496a70a8a5 | 961 | #if( OVER_THE_AIR_ACTIVATION != 0 ) |
mluis | 0:45496a70a8a5 | 962 | MlmeReq_t mlmeReq; |
mluis | 0:45496a70a8a5 | 963 | |
mluis | 0:45496a70a8a5 | 964 | mlmeReq.Type = MLME_JOIN; |
mluis | 0:45496a70a8a5 | 965 | |
mluis | 0:45496a70a8a5 | 966 | mlmeReq.Req.Join.DevEui = DevEui; |
mluis | 0:45496a70a8a5 | 967 | mlmeReq.Req.Join.AppEui = AppEui; |
mluis | 0:45496a70a8a5 | 968 | mlmeReq.Req.Join.AppKey = AppKey; |
mluis | 0:45496a70a8a5 | 969 | |
abouillot | 9:16106008960b | 970 | if( NextTx == true ) { |
mluis | 0:45496a70a8a5 | 971 | LoRaMacMlmeRequest( &mlmeReq ); |
mluis | 0:45496a70a8a5 | 972 | } |
mluis | 7:ceb4063e6863 | 973 | DeviceState = DEVICE_STATE_SLEEP; |
mluis | 0:45496a70a8a5 | 974 | #else |
mluis | 0:45496a70a8a5 | 975 | mibReq.Type = MIB_NET_ID; |
mluis | 0:45496a70a8a5 | 976 | mibReq.Param.NetID = LORAWAN_NETWORK_ID; |
mluis | 0:45496a70a8a5 | 977 | LoRaMacMibSetRequestConfirm( &mibReq ); |
mluis | 0:45496a70a8a5 | 978 | |
mluis | 0:45496a70a8a5 | 979 | mibReq.Type = MIB_DEV_ADDR; |
mluis | 0:45496a70a8a5 | 980 | mibReq.Param.DevAddr = DevAddr; |
mluis | 0:45496a70a8a5 | 981 | LoRaMacMibSetRequestConfirm( &mibReq ); |
mluis | 0:45496a70a8a5 | 982 | |
mluis | 0:45496a70a8a5 | 983 | mibReq.Type = MIB_NWK_SKEY; |
mluis | 0:45496a70a8a5 | 984 | mibReq.Param.NwkSKey = NwkSKey; |
mluis | 0:45496a70a8a5 | 985 | LoRaMacMibSetRequestConfirm( &mibReq ); |
mluis | 0:45496a70a8a5 | 986 | |
mluis | 0:45496a70a8a5 | 987 | mibReq.Type = MIB_APP_SKEY; |
mluis | 0:45496a70a8a5 | 988 | mibReq.Param.AppSKey = AppSKey; |
mluis | 0:45496a70a8a5 | 989 | LoRaMacMibSetRequestConfirm( &mibReq ); |
mluis | 0:45496a70a8a5 | 990 | |
mluis | 0:45496a70a8a5 | 991 | mibReq.Type = MIB_NETWORK_JOINED; |
mluis | 0:45496a70a8a5 | 992 | mibReq.Param.IsNetworkJoined = true; |
mluis | 0:45496a70a8a5 | 993 | LoRaMacMibSetRequestConfirm( &mibReq ); |
mluis | 0:45496a70a8a5 | 994 | |
mluis | 0:45496a70a8a5 | 995 | DeviceState = DEVICE_STATE_SEND; |
mluis | 0:45496a70a8a5 | 996 | #endif |
mluis | 0:45496a70a8a5 | 997 | IsNetworkJoinedStatusUpdate = true; |
mluis | 0:45496a70a8a5 | 998 | break; |
mluis | 0:45496a70a8a5 | 999 | } |
abouillot | 9:16106008960b | 1000 | case DEVICE_STATE_SEND: { |
abouillot | 9:16106008960b | 1001 | pc.printf("DEVICE_STATE_SEND\n"); |
abouillot | 9:16106008960b | 1002 | if( NextTx == true ) { |
mluis | 0:45496a70a8a5 | 1003 | SerialDisplayUpdateUplinkAcked( false ); |
mluis | 0:45496a70a8a5 | 1004 | SerialDisplayUpdateDonwlinkRxData( false ); |
mluis | 0:45496a70a8a5 | 1005 | PrepareTxFrame( AppPort ); |
mluis | 0:45496a70a8a5 | 1006 | |
mluis | 0:45496a70a8a5 | 1007 | NextTx = SendFrame( ); |
mluis | 0:45496a70a8a5 | 1008 | } |
abouillot | 9:16106008960b | 1009 | if( ComplianceTest.Running == true ) { |
mluis | 5:fa113b25f612 | 1010 | // Schedule next packet transmission |
mluis | 5:fa113b25f612 | 1011 | TxDutyCycleTime = 5000000; // 5000000 us |
abouillot | 9:16106008960b | 1012 | } else { |
mluis | 0:45496a70a8a5 | 1013 | // Schedule next packet transmission |
mluis | 0:45496a70a8a5 | 1014 | TxDutyCycleTime = APP_TX_DUTYCYCLE + randr( -APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND ); |
mluis | 0:45496a70a8a5 | 1015 | } |
mluis | 0:45496a70a8a5 | 1016 | DeviceState = DEVICE_STATE_CYCLE; |
mluis | 0:45496a70a8a5 | 1017 | break; |
mluis | 0:45496a70a8a5 | 1018 | } |
abouillot | 9:16106008960b | 1019 | case DEVICE_STATE_CYCLE: { |
abouillot | 9:16106008960b | 1020 | pc.printf("DEVICE_STATE_CYCLE\n"); |
mluis | 5:fa113b25f612 | 1021 | DeviceState = DEVICE_STATE_SLEEP; |
mluis | 5:fa113b25f612 | 1022 | |
mluis | 0:45496a70a8a5 | 1023 | // Schedule next packet transmission |
mluis | 0:45496a70a8a5 | 1024 | TimerSetValue( &TxNextPacketTimer, TxDutyCycleTime ); |
mluis | 0:45496a70a8a5 | 1025 | TimerStart( &TxNextPacketTimer ); |
mluis | 0:45496a70a8a5 | 1026 | break; |
mluis | 0:45496a70a8a5 | 1027 | } |
abouillot | 9:16106008960b | 1028 | case DEVICE_STATE_SLEEP: { |
abouillot | 9:16106008960b | 1029 | // pc.printf("DEVICE_STATE_SLEEP\n"); |
mluis | 0:45496a70a8a5 | 1030 | // Wake up through events |
mluis | 0:45496a70a8a5 | 1031 | break; |
mluis | 0:45496a70a8a5 | 1032 | } |
abouillot | 9:16106008960b | 1033 | default: { |
mluis | 0:45496a70a8a5 | 1034 | DeviceState = DEVICE_STATE_INIT; |
mluis | 0:45496a70a8a5 | 1035 | break; |
mluis | 0:45496a70a8a5 | 1036 | } |
abouillot | 9:16106008960b | 1037 | |
GregCr | 2:48d8d4806d48 | 1038 | } |
mluis | 5:fa113b25f612 | 1039 | |
GregCr | 2:48d8d4806d48 | 1040 | // Read light sensor |
mluis | 5:fa113b25f612 | 1041 | LightValue = ( 1 - ( LightSens.read( ) * 1.65 ) ); |
mluis | 5:fa113b25f612 | 1042 | |
GregCr | 2:48d8d4806d48 | 1043 | // Set automatic RGB from light sensor |
abouillot | 9:16106008960b | 1044 | if( LightMode == 0 ) { |
mluis | 5:fa113b25f612 | 1045 | ColorLed.setColorRGB( 0, ( uint8_t )( 255 * LightValue ), ( uint8_t )( 255 * LightValue ), ( uint8_t )( 255 * LightValue ) ); |
mluis | 0:45496a70a8a5 | 1046 | } |
mluis | 0:45496a70a8a5 | 1047 | } |
mluis | 0:45496a70a8a5 | 1048 | } |