Amir Chaudhary
/
Hexmodal-firmware_v3-1_hex_w_3d_14
Implemented LED Indicator Patterns
app/main.cpp@11:e433cbbfd436, 2019-01-02 (annotated)
- Committer:
- uss1994
- Date:
- Wed Jan 02 02:27:07 2019 +0000
- Revision:
- 11:e433cbbfd436
- Parent:
- 10:9a4efdd07a77
- Child:
- 12:c1cf0e717684
Adding code to allow activating tests with 2 or 3 button taps
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mluis | 0:92bca02df485 | 1 | /* |
mluis | 0:92bca02df485 | 2 | / _____) _ | | |
mluis | 0:92bca02df485 | 3 | ( (____ _____ ____ _| |_ _____ ____| |__ |
mluis | 0:92bca02df485 | 4 | \____ \| ___ | (_ _) ___ |/ ___) _ \ |
mluis | 0:92bca02df485 | 5 | _____) ) ____| | | || |_| ____( (___| | | | |
mluis | 0:92bca02df485 | 6 | (______/|_____)_|_|_| \__)_____)\____)_| |_| |
mluis | 0:92bca02df485 | 7 | (C)2015 Semtech |
mluis | 0:92bca02df485 | 8 | |
mluis | 0:92bca02df485 | 9 | Description: LoRaMac classA device implementation |
mluis | 0:92bca02df485 | 10 | |
mluis | 0:92bca02df485 | 11 | License: Revised BSD License, see LICENSE.TXT file include in the project |
mluis | 0:92bca02df485 | 12 | |
mluis | 0:92bca02df485 | 13 | Maintainer: Miguel Luis and Gregory Cristian |
mluis | 0:92bca02df485 | 14 | */ |
mluis | 0:92bca02df485 | 15 | #include "mbed.h" |
mluis | 0:92bca02df485 | 16 | #include "board.h" |
mluis | 0:92bca02df485 | 17 | #include "radio.h" |
mluis | 0:92bca02df485 | 18 | |
mluis | 0:92bca02df485 | 19 | #include "LoRaMac.h" |
mluis | 9:ee9dcbb9708d | 20 | #include "Commissioning.h" |
mluis | 0:92bca02df485 | 21 | #include "SerialDisplay.h" |
mluis | 0:92bca02df485 | 22 | |
mluis | 0:92bca02df485 | 23 | /*! |
mluis | 9:ee9dcbb9708d | 24 | * Defines the application data transmission duty cycle. 5s, value in [ms]. |
mluis | 0:92bca02df485 | 25 | */ |
mluis | 9:ee9dcbb9708d | 26 | #define APP_TX_DUTYCYCLE 5000 |
mluis | 1:352f608c3337 | 27 | |
mluis | 1:352f608c3337 | 28 | /*! |
mluis | 1:352f608c3337 | 29 | * Defines a random delay for application data transmission duty cycle. 1s, |
mluis | 9:ee9dcbb9708d | 30 | * value in [ms]. |
mluis | 1:352f608c3337 | 31 | */ |
mluis | 9:ee9dcbb9708d | 32 | #define APP_TX_DUTYCYCLE_RND 1000 |
mluis | 0:92bca02df485 | 33 | |
mluis | 0:92bca02df485 | 34 | /*! |
mluis | 5:1e9f6a365854 | 35 | * Default datarate |
mluis | 3:9c6f7f082151 | 36 | */ |
mluis | 3:9c6f7f082151 | 37 | #define LORAWAN_DEFAULT_DATARATE DR_0 |
mluis | 3:9c6f7f082151 | 38 | |
mluis | 3:9c6f7f082151 | 39 | /*! |
mluis | 0:92bca02df485 | 40 | * LoRaWAN confirmed messages |
mluis | 0:92bca02df485 | 41 | */ |
mluis | 0:92bca02df485 | 42 | #define LORAWAN_CONFIRMED_MSG_ON true |
mluis | 0:92bca02df485 | 43 | |
mluis | 0:92bca02df485 | 44 | /*! |
mluis | 3:9c6f7f082151 | 45 | * LoRaWAN Adaptive Data Rate |
mluis | 0:92bca02df485 | 46 | * |
mluis | 0:92bca02df485 | 47 | * \remark Please note that when ADR is enabled the end-device should be static |
mluis | 0:92bca02df485 | 48 | */ |
mluis | 0:92bca02df485 | 49 | #define LORAWAN_ADR_ON 1 |
mluis | 0:92bca02df485 | 50 | |
mluis | 1:352f608c3337 | 51 | #if defined( USE_BAND_868 ) |
mluis | 1:352f608c3337 | 52 | |
mluis | 3:9c6f7f082151 | 53 | #include "LoRaMacTest.h" |
mluis | 3:9c6f7f082151 | 54 | |
mluis | 0:92bca02df485 | 55 | /*! |
mluis | 0:92bca02df485 | 56 | * LoRaWAN ETSI duty cycle control enable/disable |
mluis | 0:92bca02df485 | 57 | * |
mluis | 0:92bca02df485 | 58 | * \remark Please note that ETSI mandates duty cycled transmissions. Use only for test purposes |
mluis | 0:92bca02df485 | 59 | */ |
mluis | 9:ee9dcbb9708d | 60 | #define LORAWAN_DUTYCYCLE_ON false |
mluis | 0:92bca02df485 | 61 | |
mluis | 5:1e9f6a365854 | 62 | #define USE_SEMTECH_DEFAULT_CHANNEL_LINEUP 1 |
mluis | 5:1e9f6a365854 | 63 | |
mluis | 9:ee9dcbb9708d | 64 | #if( USE_SEMTECH_DEFAULT_CHANNEL_LINEUP == 1 ) |
mluis | 5:1e9f6a365854 | 65 | |
mluis | 5:1e9f6a365854 | 66 | #define LC4 { 867100000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 } |
mluis | 5:1e9f6a365854 | 67 | #define LC5 { 867300000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 } |
mluis | 5:1e9f6a365854 | 68 | #define LC6 { 867500000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 } |
mluis | 5:1e9f6a365854 | 69 | #define LC7 { 867700000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 } |
mluis | 5:1e9f6a365854 | 70 | #define LC8 { 867900000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 } |
mluis | 5:1e9f6a365854 | 71 | #define LC9 { 868800000, { ( ( DR_7 << 4 ) | DR_7 ) }, 2 } |
mluis | 5:1e9f6a365854 | 72 | #define LC10 { 868300000, { ( ( DR_6 << 4 ) | DR_6 ) }, 1 } |
mluis | 5:1e9f6a365854 | 73 | |
mluis | 5:1e9f6a365854 | 74 | #endif |
mluis | 5:1e9f6a365854 | 75 | |
mluis | 1:352f608c3337 | 76 | #endif |
mluis | 1:352f608c3337 | 77 | |
mluis | 0:92bca02df485 | 78 | /*! |
mluis | 0:92bca02df485 | 79 | * LoRaWAN application port |
mluis | 0:92bca02df485 | 80 | */ |
mluis | 0:92bca02df485 | 81 | #define LORAWAN_APP_PORT 15 |
mluis | 0:92bca02df485 | 82 | |
mluis | 0:92bca02df485 | 83 | /*! |
mluis | 0:92bca02df485 | 84 | * User application data buffer size |
mluis | 0:92bca02df485 | 85 | */ |
uss1994 | 10:9a4efdd07a77 | 86 | #define LORAWAN_APP_DATA_SIZE 41 |
mluis | 0:92bca02df485 | 87 | |
mluis | 0:92bca02df485 | 88 | static uint8_t DevEui[] = LORAWAN_DEVICE_EUI; |
mluis | 0:92bca02df485 | 89 | static uint8_t AppEui[] = LORAWAN_APPLICATION_EUI; |
mluis | 0:92bca02df485 | 90 | static uint8_t AppKey[] = LORAWAN_APPLICATION_KEY; |
mluis | 0:92bca02df485 | 91 | |
mluis | 7:3173f0508a98 | 92 | #if( OVER_THE_AIR_ACTIVATION == 0 ) |
mluis | 0:92bca02df485 | 93 | |
mluis | 0:92bca02df485 | 94 | static uint8_t NwkSKey[] = LORAWAN_NWKSKEY; |
mluis | 0:92bca02df485 | 95 | static uint8_t AppSKey[] = LORAWAN_APPSKEY; |
mluis | 0:92bca02df485 | 96 | |
mluis | 3:9c6f7f082151 | 97 | /*! |
mluis | 3:9c6f7f082151 | 98 | * Device address |
mluis | 3:9c6f7f082151 | 99 | */ |
mluis | 3:9c6f7f082151 | 100 | static uint32_t DevAddr = LORAWAN_DEVICE_ADDRESS; |
mluis | 0:92bca02df485 | 101 | |
mluis | 3:9c6f7f082151 | 102 | #endif |
mluis | 0:92bca02df485 | 103 | |
mluis | 0:92bca02df485 | 104 | /*! |
mluis | 1:352f608c3337 | 105 | * Application port |
mluis | 1:352f608c3337 | 106 | */ |
mluis | 1:352f608c3337 | 107 | static uint8_t AppPort = LORAWAN_APP_PORT; |
mluis | 1:352f608c3337 | 108 | |
mluis | 1:352f608c3337 | 109 | /*! |
mluis | 1:352f608c3337 | 110 | * User application data size |
mluis | 1:352f608c3337 | 111 | */ |
mluis | 1:352f608c3337 | 112 | static uint8_t AppDataSize = LORAWAN_APP_DATA_SIZE; |
mluis | 1:352f608c3337 | 113 | |
mluis | 1:352f608c3337 | 114 | /*! |
mluis | 1:352f608c3337 | 115 | * User application data buffer size |
mluis | 1:352f608c3337 | 116 | */ |
mluis | 1:352f608c3337 | 117 | #define LORAWAN_APP_DATA_MAX_SIZE 64 |
mluis | 1:352f608c3337 | 118 | |
mluis | 1:352f608c3337 | 119 | /*! |
mluis | 0:92bca02df485 | 120 | * User application data |
mluis | 0:92bca02df485 | 121 | */ |
mluis | 1:352f608c3337 | 122 | static uint8_t AppData[LORAWAN_APP_DATA_MAX_SIZE]; |
mluis | 1:352f608c3337 | 123 | |
mluis | 1:352f608c3337 | 124 | /*! |
mluis | 1:352f608c3337 | 125 | * Indicates if the node is sending confirmed or unconfirmed messages |
mluis | 1:352f608c3337 | 126 | */ |
mluis | 1:352f608c3337 | 127 | static uint8_t IsTxConfirmed = LORAWAN_CONFIRMED_MSG_ON; |
mluis | 0:92bca02df485 | 128 | |
mluis | 0:92bca02df485 | 129 | /*! |
mluis | 0:92bca02df485 | 130 | * Defines the application data transmission duty cycle |
mluis | 0:92bca02df485 | 131 | */ |
mluis | 0:92bca02df485 | 132 | static uint32_t TxDutyCycleTime; |
mluis | 0:92bca02df485 | 133 | |
mluis | 1:352f608c3337 | 134 | /*! |
mluis | 1:352f608c3337 | 135 | * Timer to handle the application data transmission duty cycle |
mluis | 1:352f608c3337 | 136 | */ |
mluis | 1:352f608c3337 | 137 | static TimerEvent_t TxNextPacketTimer; |
mluis | 0:92bca02df485 | 138 | |
mluis | 3:9c6f7f082151 | 139 | /*! |
uss1994 | 10:9a4efdd07a77 | 140 | * SOME APPLICATION PARAMETERS |
uss1994 | 10:9a4efdd07a77 | 141 | */ |
uss1994 | 10:9a4efdd07a77 | 142 | |
uss1994 | 10:9a4efdd07a77 | 143 | DigitalOut myled(D7); |
uss1994 | 10:9a4efdd07a77 | 144 | |
uss1994 | 10:9a4efdd07a77 | 145 | DigitalOut relayPin(D6); |
uss1994 | 10:9a4efdd07a77 | 146 | AnalogIn BAT_PIN(A1); |
uss1994 | 10:9a4efdd07a77 | 147 | AnalogIn LIGHT_1_PIN(A2); |
uss1994 | 10:9a4efdd07a77 | 148 | AnalogIn LIGHT_2_PIN(A5); |
uss1994 | 10:9a4efdd07a77 | 149 | AnalogIn VCE_PIN(PB_1); |
uss1994 | 10:9a4efdd07a77 | 150 | |
uss1994 | 11:e433cbbfd436 | 151 | InterruptIn button1(USER_BUTTON); |
uss1994 | 11:e433cbbfd436 | 152 | volatile bool button1_pressed = false; // Used in the main loop |
uss1994 | 11:e433cbbfd436 | 153 | volatile bool button1_enabled = true; // Used for debouncing |
uss1994 | 11:e433cbbfd436 | 154 | Timeout button1_timeout; // Used for debouncing |
uss1994 | 11:e433cbbfd436 | 155 | int press_count; |
uss1994 | 11:e433cbbfd436 | 156 | Timeout buttonOptions_timeout; // Used for determining number of presses |
uss1994 | 11:e433cbbfd436 | 157 | |
uss1994 | 10:9a4efdd07a77 | 158 | unsigned int time_window = 5; // Default 3600 seconds in an hour |
uss1994 | 10:9a4efdd07a77 | 159 | unsigned long long_interval = 604800; // Default 31557600 seconds in a year |
uss1994 | 10:9a4efdd07a77 | 160 | unsigned int hb_interval = 3600; // Default 86400 seconds in a day |
uss1994 | 10:9a4efdd07a77 | 161 | unsigned long test_interval = 86400; // Default 2592000 seconds in a month |
uss1994 | 10:9a4efdd07a77 | 162 | |
uss1994 | 10:9a4efdd07a77 | 163 | time_t next_stest; |
uss1994 | 10:9a4efdd07a77 | 164 | time_t next_ltest; |
uss1994 | 10:9a4efdd07a77 | 165 | |
uss1994 | 10:9a4efdd07a77 | 166 | uint8_t hb_data[5]; |
uss1994 | 10:9a4efdd07a77 | 167 | uint8_t short_tdata[14]; |
uss1994 | 10:9a4efdd07a77 | 168 | uint8_t long_tdata[41]; |
uss1994 | 10:9a4efdd07a77 | 169 | |
uss1994 | 10:9a4efdd07a77 | 170 | uint8_t received_data[28]; |
uss1994 | 10:9a4efdd07a77 | 171 | |
uss1994 | 10:9a4efdd07a77 | 172 | uint8_t *data; |
uss1994 | 10:9a4efdd07a77 | 173 | uint8_t data_len; |
uss1994 | 10:9a4efdd07a77 | 174 | |
uss1994 | 10:9a4efdd07a77 | 175 | bool running_test; |
uss1994 | 10:9a4efdd07a77 | 176 | bool joining; |
uss1994 | 10:9a4efdd07a77 | 177 | bool received_downlink; |
uss1994 | 10:9a4efdd07a77 | 178 | |
uss1994 | 10:9a4efdd07a77 | 179 | time_t current_time; |
uss1994 | 10:9a4efdd07a77 | 180 | |
uss1994 | 10:9a4efdd07a77 | 181 | unsigned long last_measurement; |
uss1994 | 10:9a4efdd07a77 | 182 | unsigned long last_hb; |
uss1994 | 10:9a4efdd07a77 | 183 | unsigned long test_start; |
uss1994 | 10:9a4efdd07a77 | 184 | |
uss1994 | 10:9a4efdd07a77 | 185 | int measureCount; |
uss1994 | 10:9a4efdd07a77 | 186 | uint8_t s_measurements[4]; |
uss1994 | 10:9a4efdd07a77 | 187 | uint8_t l_measurements[22]; |
uss1994 | 10:9a4efdd07a77 | 188 | uint8_t *measurements; |
uss1994 | 10:9a4efdd07a77 | 189 | int available_slots = 9; |
uss1994 | 10:9a4efdd07a77 | 190 | |
uss1994 | 10:9a4efdd07a77 | 191 | unsigned long measurement_interval; |
uss1994 | 10:9a4efdd07a77 | 192 | unsigned long test_duration; |
uss1994 | 10:9a4efdd07a77 | 193 | |
uss1994 | 10:9a4efdd07a77 | 194 | /*! |
mluis | 3:9c6f7f082151 | 195 | * Specifies the state of the application LED |
mluis | 3:9c6f7f082151 | 196 | */ |
mluis | 3:9c6f7f082151 | 197 | static bool AppLedStateOn = false; |
mluis | 3:9c6f7f082151 | 198 | volatile bool Led3StateChanged = false; |
mluis | 0:92bca02df485 | 199 | /*! |
mluis | 3:9c6f7f082151 | 200 | * Timer to handle the state of LED1 |
mluis | 0:92bca02df485 | 201 | */ |
mluis | 3:9c6f7f082151 | 202 | static TimerEvent_t Led1Timer; |
mluis | 3:9c6f7f082151 | 203 | volatile bool Led1State = false; |
mluis | 3:9c6f7f082151 | 204 | volatile bool Led1StateChanged = false; |
mluis | 3:9c6f7f082151 | 205 | /*! |
mluis | 3:9c6f7f082151 | 206 | * Timer to handle the state of LED2 |
mluis | 3:9c6f7f082151 | 207 | */ |
mluis | 3:9c6f7f082151 | 208 | static TimerEvent_t Led2Timer; |
mluis | 3:9c6f7f082151 | 209 | volatile bool Led2State = false; |
mluis | 3:9c6f7f082151 | 210 | volatile bool Led2StateChanged = false; |
mluis | 0:92bca02df485 | 211 | |
mluis | 0:92bca02df485 | 212 | /*! |
mluis | 0:92bca02df485 | 213 | * Indicates if a new packet can be sent |
mluis | 0:92bca02df485 | 214 | */ |
mluis | 3:9c6f7f082151 | 215 | static bool NextTx = true; |
mluis | 0:92bca02df485 | 216 | |
mluis | 3:9c6f7f082151 | 217 | /*! |
mluis | 3:9c6f7f082151 | 218 | * Device states |
mluis | 3:9c6f7f082151 | 219 | */ |
mluis | 9:ee9dcbb9708d | 220 | static enum eDeviceState |
mluis | 3:9c6f7f082151 | 221 | { |
mluis | 3:9c6f7f082151 | 222 | DEVICE_STATE_INIT, |
mluis | 3:9c6f7f082151 | 223 | DEVICE_STATE_JOIN, |
mluis | 3:9c6f7f082151 | 224 | DEVICE_STATE_SEND, |
mluis | 3:9c6f7f082151 | 225 | DEVICE_STATE_CYCLE, |
mluis | 3:9c6f7f082151 | 226 | DEVICE_STATE_SLEEP |
mluis | 3:9c6f7f082151 | 227 | }DeviceState; |
mluis | 0:92bca02df485 | 228 | |
uss1994 | 10:9a4efdd07a77 | 229 | static enum eMessageType |
uss1994 | 10:9a4efdd07a77 | 230 | { |
uss1994 | 10:9a4efdd07a77 | 231 | MESSAGE_TYPE_HB, |
uss1994 | 10:9a4efdd07a77 | 232 | MESSAGE_TYPE_SHORT_TEST, |
uss1994 | 10:9a4efdd07a77 | 233 | MESSAGE_TYPE_LONG_TEST |
uss1994 | 10:9a4efdd07a77 | 234 | }MessageType; |
uss1994 | 10:9a4efdd07a77 | 235 | |
mluis | 3:9c6f7f082151 | 236 | /*! |
mluis | 3:9c6f7f082151 | 237 | * LoRaWAN compliance tests support data |
mluis | 3:9c6f7f082151 | 238 | */ |
mluis | 3:9c6f7f082151 | 239 | struct ComplianceTest_s |
mluis | 3:9c6f7f082151 | 240 | { |
mluis | 3:9c6f7f082151 | 241 | bool Running; |
mluis | 3:9c6f7f082151 | 242 | uint8_t State; |
mluis | 3:9c6f7f082151 | 243 | bool IsTxConfirmed; |
mluis | 3:9c6f7f082151 | 244 | uint8_t AppPort; |
mluis | 3:9c6f7f082151 | 245 | uint8_t AppDataSize; |
mluis | 3:9c6f7f082151 | 246 | uint8_t *AppDataBuffer; |
mluis | 3:9c6f7f082151 | 247 | uint16_t DownLinkCounter; |
mluis | 3:9c6f7f082151 | 248 | bool LinkCheck; |
mluis | 3:9c6f7f082151 | 249 | uint8_t DemodMargin; |
mluis | 3:9c6f7f082151 | 250 | uint8_t NbGateways; |
mluis | 3:9c6f7f082151 | 251 | }ComplianceTest; |
mluis | 0:92bca02df485 | 252 | |
mluis | 3:9c6f7f082151 | 253 | /* |
mluis | 3:9c6f7f082151 | 254 | * SerialDisplay managment variables |
mluis | 3:9c6f7f082151 | 255 | */ |
mluis | 1:352f608c3337 | 256 | |
mluis | 3:9c6f7f082151 | 257 | /*! |
mluis | 3:9c6f7f082151 | 258 | * Indicates if the MAC layer network join status has changed. |
mluis | 3:9c6f7f082151 | 259 | */ |
mluis | 3:9c6f7f082151 | 260 | static bool IsNetworkJoinedStatusUpdate = false; |
mluis | 3:9c6f7f082151 | 261 | |
mluis | 3:9c6f7f082151 | 262 | /*! |
mluis | 3:9c6f7f082151 | 263 | * Strucure containing the Uplink status |
mluis | 3:9c6f7f082151 | 264 | */ |
mluis | 0:92bca02df485 | 265 | struct sLoRaMacUplinkStatus |
mluis | 0:92bca02df485 | 266 | { |
mluis | 0:92bca02df485 | 267 | uint8_t Acked; |
mluis | 0:92bca02df485 | 268 | int8_t Datarate; |
mluis | 0:92bca02df485 | 269 | uint16_t UplinkCounter; |
mluis | 0:92bca02df485 | 270 | uint8_t Port; |
mluis | 0:92bca02df485 | 271 | uint8_t *Buffer; |
mluis | 0:92bca02df485 | 272 | uint8_t BufferSize; |
mluis | 0:92bca02df485 | 273 | }LoRaMacUplinkStatus; |
mluis | 3:9c6f7f082151 | 274 | volatile bool UplinkStatusUpdated = false; |
mluis | 0:92bca02df485 | 275 | |
mluis | 3:9c6f7f082151 | 276 | /*! |
mluis | 3:9c6f7f082151 | 277 | * Strucure containing the Downlink status |
mluis | 3:9c6f7f082151 | 278 | */ |
mluis | 0:92bca02df485 | 279 | struct sLoRaMacDownlinkStatus |
mluis | 0:92bca02df485 | 280 | { |
mluis | 0:92bca02df485 | 281 | int16_t Rssi; |
mluis | 0:92bca02df485 | 282 | int8_t Snr; |
mluis | 0:92bca02df485 | 283 | uint16_t DownlinkCounter; |
mluis | 0:92bca02df485 | 284 | bool RxData; |
mluis | 0:92bca02df485 | 285 | uint8_t Port; |
mluis | 0:92bca02df485 | 286 | uint8_t *Buffer; |
mluis | 0:92bca02df485 | 287 | uint8_t BufferSize; |
mluis | 0:92bca02df485 | 288 | }LoRaMacDownlinkStatus; |
mluis | 3:9c6f7f082151 | 289 | volatile bool DownlinkStatusUpdated = false; |
mluis | 0:92bca02df485 | 290 | |
mluis | 0:92bca02df485 | 291 | void SerialDisplayRefresh( void ) |
mluis | 0:92bca02df485 | 292 | { |
mluis | 3:9c6f7f082151 | 293 | MibRequestConfirm_t mibReq; |
mluis | 3:9c6f7f082151 | 294 | |
mluis | 0:92bca02df485 | 295 | SerialDisplayInit( ); |
mluis | 0:92bca02df485 | 296 | SerialDisplayUpdateActivationMode( OVER_THE_AIR_ACTIVATION ); |
mluis | 0:92bca02df485 | 297 | |
mluis | 0:92bca02df485 | 298 | #if( OVER_THE_AIR_ACTIVATION == 0 ) |
mluis | 0:92bca02df485 | 299 | SerialDisplayUpdateNwkId( LORAWAN_NETWORK_ID ); |
mluis | 3:9c6f7f082151 | 300 | SerialDisplayUpdateDevAddr( DevAddr ); |
mluis | 0:92bca02df485 | 301 | SerialDisplayUpdateKey( 12, NwkSKey ); |
mluis | 0:92bca02df485 | 302 | SerialDisplayUpdateKey( 13, AppSKey ); |
mluis | 7:3173f0508a98 | 303 | #endif |
mluis | 0:92bca02df485 | 304 | SerialDisplayUpdateEui( 5, DevEui ); |
mluis | 0:92bca02df485 | 305 | SerialDisplayUpdateEui( 6, AppEui ); |
mluis | 0:92bca02df485 | 306 | SerialDisplayUpdateKey( 7, AppKey ); |
mluis | 3:9c6f7f082151 | 307 | |
mluis | 3:9c6f7f082151 | 308 | mibReq.Type = MIB_NETWORK_JOINED; |
mluis | 3:9c6f7f082151 | 309 | LoRaMacMibGetRequestConfirm( &mibReq ); |
mluis | 3:9c6f7f082151 | 310 | SerialDisplayUpdateNetworkIsJoined( mibReq.Param.IsNetworkJoined ); |
mluis | 0:92bca02df485 | 311 | |
mluis | 0:92bca02df485 | 312 | SerialDisplayUpdateAdr( LORAWAN_ADR_ON ); |
mluis | 1:352f608c3337 | 313 | #if defined( USE_BAND_868 ) |
mluis | 0:92bca02df485 | 314 | SerialDisplayUpdateDutyCycle( LORAWAN_DUTYCYCLE_ON ); |
mluis | 1:352f608c3337 | 315 | #else |
mluis | 1:352f608c3337 | 316 | SerialDisplayUpdateDutyCycle( false ); |
mluis | 1:352f608c3337 | 317 | #endif |
mluis | 0:92bca02df485 | 318 | SerialDisplayUpdatePublicNetwork( LORAWAN_PUBLIC_NETWORK ); |
mluis | 0:92bca02df485 | 319 | |
mluis | 0:92bca02df485 | 320 | SerialDisplayUpdateLedState( 3, AppLedStateOn ); |
mluis | 0:92bca02df485 | 321 | } |
mluis | 0:92bca02df485 | 322 | |
mluis | 0:92bca02df485 | 323 | void SerialRxProcess( void ) |
mluis | 0:92bca02df485 | 324 | { |
mluis | 0:92bca02df485 | 325 | if( SerialDisplayReadable( ) == true ) |
mluis | 0:92bca02df485 | 326 | { |
mluis | 0:92bca02df485 | 327 | switch( SerialDisplayGetChar( ) ) |
mluis | 0:92bca02df485 | 328 | { |
mluis | 0:92bca02df485 | 329 | case 'R': |
mluis | 0:92bca02df485 | 330 | case 'r': |
mluis | 0:92bca02df485 | 331 | // Refresh Serial screen |
mluis | 0:92bca02df485 | 332 | SerialDisplayRefresh( ); |
mluis | 0:92bca02df485 | 333 | break; |
mluis | 0:92bca02df485 | 334 | default: |
mluis | 0:92bca02df485 | 335 | break; |
mluis | 0:92bca02df485 | 336 | } |
mluis | 0:92bca02df485 | 337 | } |
mluis | 0:92bca02df485 | 338 | } |
mluis | 0:92bca02df485 | 339 | |
uss1994 | 10:9a4efdd07a77 | 340 | void flash_builtin() { |
uss1994 | 10:9a4efdd07a77 | 341 | myled = 1; // turn the LED on (HIGH is the voltage level) |
uss1994 | 10:9a4efdd07a77 | 342 | wait(2); // wait for a second |
uss1994 | 10:9a4efdd07a77 | 343 | myled = 0; // turn the LED off by making the voltage LOW |
uss1994 | 10:9a4efdd07a77 | 344 | wait(1); // wait for a second |
uss1994 | 10:9a4efdd07a77 | 345 | } |
uss1994 | 10:9a4efdd07a77 | 346 | |
uss1994 | 11:e433cbbfd436 | 347 | // Enables button when bouncing is over |
uss1994 | 11:e433cbbfd436 | 348 | void button1_enabled_cb(void) |
uss1994 | 11:e433cbbfd436 | 349 | { |
uss1994 | 11:e433cbbfd436 | 350 | button1_enabled = true; |
uss1994 | 11:e433cbbfd436 | 351 | } |
uss1994 | 11:e433cbbfd436 | 352 | |
uss1994 | 11:e433cbbfd436 | 353 | // ISR handling button pressed event |
uss1994 | 11:e433cbbfd436 | 354 | void button1_onpressed_cb(void) |
uss1994 | 11:e433cbbfd436 | 355 | { |
uss1994 | 11:e433cbbfd436 | 356 | if (button1_enabled) { // Disabled while the button is bouncing |
uss1994 | 11:e433cbbfd436 | 357 | button1_enabled = false; |
uss1994 | 11:e433cbbfd436 | 358 | button1_pressed = true; // To be read by the main loop |
uss1994 | 11:e433cbbfd436 | 359 | button1_timeout.attach(callback(button1_enabled_cb), 0.3); // Debounce time 300 ms |
uss1994 | 11:e433cbbfd436 | 360 | } |
uss1994 | 11:e433cbbfd436 | 361 | } |
uss1994 | 11:e433cbbfd436 | 362 | |
uss1994 | 10:9a4efdd07a77 | 363 | // **************************** COMMUNICATION PACKET DEFINITION METHODS ******************************** // |
uss1994 | 10:9a4efdd07a77 | 364 | |
uss1994 | 10:9a4efdd07a77 | 365 | void heartbeat_message(uint8_t *hb_data, uint8_t *current_date) { |
uss1994 | 10:9a4efdd07a77 | 366 | hb_data[0] = 0xD2; |
uss1994 | 10:9a4efdd07a77 | 367 | memcpy(hb_data+1, current_date, 4); |
uss1994 | 10:9a4efdd07a77 | 368 | } |
uss1994 | 10:9a4efdd07a77 | 369 | |
uss1994 | 10:9a4efdd07a77 | 370 | void short_test_result(uint8_t *short_tdata, uint8_t *test_start_date, uint8_t *next_test_date, uint8_t *measurements) { |
uss1994 | 10:9a4efdd07a77 | 371 | short_tdata[0] = 0xD7; |
uss1994 | 10:9a4efdd07a77 | 372 | memcpy(short_tdata+1, test_start_date, 4); |
uss1994 | 10:9a4efdd07a77 | 373 | memcpy(short_tdata+5, next_test_date, 4); |
uss1994 | 10:9a4efdd07a77 | 374 | short_tdata[9] = (uint8_t) 2; |
uss1994 | 10:9a4efdd07a77 | 375 | memcpy(short_tdata+10, measurements, 4); |
uss1994 | 10:9a4efdd07a77 | 376 | } |
uss1994 | 10:9a4efdd07a77 | 377 | |
uss1994 | 10:9a4efdd07a77 | 378 | void long_test_result(uint8_t *long_tdata, uint8_t *test_start_date, uint8_t *next_test_date, uint8_t *measurements) { |
uss1994 | 10:9a4efdd07a77 | 379 | long_tdata[0] = 0xD8; |
uss1994 | 10:9a4efdd07a77 | 380 | memcpy(long_tdata+1, test_start_date, 4); |
uss1994 | 10:9a4efdd07a77 | 381 | memcpy(long_tdata+5, next_test_date, 4); |
uss1994 | 10:9a4efdd07a77 | 382 | long_tdata[9] = (uint8_t) 2; |
uss1994 | 10:9a4efdd07a77 | 383 | memcpy(long_tdata+10, measurements, 31); |
uss1994 | 10:9a4efdd07a77 | 384 | } |
uss1994 | 10:9a4efdd07a77 | 385 | |
mluis | 0:92bca02df485 | 386 | /*! |
mluis | 3:9c6f7f082151 | 387 | * \brief Prepares the payload of the frame |
mluis | 0:92bca02df485 | 388 | */ |
mluis | 0:92bca02df485 | 389 | static void PrepareTxFrame( uint8_t port ) |
mluis | 0:92bca02df485 | 390 | { |
mluis | 1:352f608c3337 | 391 | switch( port ) |
mluis | 1:352f608c3337 | 392 | { |
mluis | 1:352f608c3337 | 393 | case 15: |
uss1994 | 10:9a4efdd07a77 | 394 | { |
uss1994 | 10:9a4efdd07a77 | 395 | switch ( MessageType ) |
mluis | 1:352f608c3337 | 396 | { |
uss1994 | 10:9a4efdd07a77 | 397 | case MESSAGE_TYPE_HB: |
uss1994 | 10:9a4efdd07a77 | 398 | { |
uss1994 | 10:9a4efdd07a77 | 399 | AppDataSize = 5; |
uss1994 | 10:9a4efdd07a77 | 400 | heartbeat_message(AppData, (uint8_t *)¤t_time); |
uss1994 | 10:9a4efdd07a77 | 401 | break; |
uss1994 | 10:9a4efdd07a77 | 402 | } |
uss1994 | 10:9a4efdd07a77 | 403 | case MESSAGE_TYPE_SHORT_TEST: |
uss1994 | 10:9a4efdd07a77 | 404 | { |
uss1994 | 10:9a4efdd07a77 | 405 | AppDataSize = 14; |
uss1994 | 10:9a4efdd07a77 | 406 | short_test_result(AppData, (uint8_t *)¤t_time, (uint8_t *)&next_stest, s_measurements); |
uss1994 | 10:9a4efdd07a77 | 407 | break; |
uss1994 | 10:9a4efdd07a77 | 408 | } |
uss1994 | 10:9a4efdd07a77 | 409 | case MESSAGE_TYPE_LONG_TEST: |
uss1994 | 10:9a4efdd07a77 | 410 | { |
uss1994 | 10:9a4efdd07a77 | 411 | AppDataSize = 32; |
uss1994 | 10:9a4efdd07a77 | 412 | long_test_result(AppData, (uint8_t *)¤t_time, (uint8_t *)&next_ltest, l_measurements); |
uss1994 | 10:9a4efdd07a77 | 413 | break; |
uss1994 | 10:9a4efdd07a77 | 414 | } |
uss1994 | 10:9a4efdd07a77 | 415 | default: |
uss1994 | 10:9a4efdd07a77 | 416 | { |
uss1994 | 10:9a4efdd07a77 | 417 | AppDataSize = 5; |
uss1994 | 10:9a4efdd07a77 | 418 | heartbeat_message(AppData, (uint8_t *)¤t_time); |
uss1994 | 10:9a4efdd07a77 | 419 | break; |
uss1994 | 10:9a4efdd07a77 | 420 | } |
mluis | 1:352f608c3337 | 421 | } |
mluis | 1:352f608c3337 | 422 | } |
mluis | 1:352f608c3337 | 423 | break; |
mluis | 1:352f608c3337 | 424 | case 224: |
mluis | 3:9c6f7f082151 | 425 | if( ComplianceTest.LinkCheck == true ) |
mluis | 1:352f608c3337 | 426 | { |
mluis | 3:9c6f7f082151 | 427 | ComplianceTest.LinkCheck = false; |
mluis | 1:352f608c3337 | 428 | AppDataSize = 3; |
mluis | 1:352f608c3337 | 429 | AppData[0] = 5; |
mluis | 3:9c6f7f082151 | 430 | AppData[1] = ComplianceTest.DemodMargin; |
mluis | 3:9c6f7f082151 | 431 | AppData[2] = ComplianceTest.NbGateways; |
mluis | 3:9c6f7f082151 | 432 | ComplianceTest.State = 1; |
mluis | 1:352f608c3337 | 433 | } |
mluis | 1:352f608c3337 | 434 | else |
mluis | 1:352f608c3337 | 435 | { |
mluis | 3:9c6f7f082151 | 436 | switch( ComplianceTest.State ) |
mluis | 1:352f608c3337 | 437 | { |
mluis | 1:352f608c3337 | 438 | case 4: |
mluis | 3:9c6f7f082151 | 439 | ComplianceTest.State = 1; |
mluis | 1:352f608c3337 | 440 | break; |
mluis | 1:352f608c3337 | 441 | case 1: |
mluis | 1:352f608c3337 | 442 | AppDataSize = 2; |
mluis | 3:9c6f7f082151 | 443 | AppData[0] = ComplianceTest.DownLinkCounter >> 8; |
mluis | 3:9c6f7f082151 | 444 | AppData[1] = ComplianceTest.DownLinkCounter; |
mluis | 1:352f608c3337 | 445 | break; |
mluis | 1:352f608c3337 | 446 | } |
mluis | 1:352f608c3337 | 447 | } |
mluis | 1:352f608c3337 | 448 | break; |
mluis | 3:9c6f7f082151 | 449 | default: |
mluis | 3:9c6f7f082151 | 450 | break; |
mluis | 1:352f608c3337 | 451 | } |
mluis | 0:92bca02df485 | 452 | } |
mluis | 0:92bca02df485 | 453 | |
mluis | 3:9c6f7f082151 | 454 | /*! |
mluis | 3:9c6f7f082151 | 455 | * \brief Prepares the payload of the frame |
mluis | 3:9c6f7f082151 | 456 | * |
mluis | 3:9c6f7f082151 | 457 | * \retval [0: frame could be send, 1: error] |
mluis | 3:9c6f7f082151 | 458 | */ |
mluis | 0:92bca02df485 | 459 | static bool SendFrame( void ) |
mluis | 0:92bca02df485 | 460 | { |
mluis | 3:9c6f7f082151 | 461 | McpsReq_t mcpsReq; |
mluis | 3:9c6f7f082151 | 462 | LoRaMacTxInfo_t txInfo; |
mluis | 9:ee9dcbb9708d | 463 | |
mluis | 3:9c6f7f082151 | 464 | if( LoRaMacQueryTxPossible( AppDataSize, &txInfo ) != LORAMAC_STATUS_OK ) |
mluis | 1:352f608c3337 | 465 | { |
mluis | 3:9c6f7f082151 | 466 | // Send empty frame in order to flush MAC commands |
mluis | 3:9c6f7f082151 | 467 | mcpsReq.Type = MCPS_UNCONFIRMED; |
mluis | 3:9c6f7f082151 | 468 | mcpsReq.Req.Unconfirmed.fBuffer = NULL; |
mluis | 3:9c6f7f082151 | 469 | mcpsReq.Req.Unconfirmed.fBufferSize = 0; |
mluis | 3:9c6f7f082151 | 470 | mcpsReq.Req.Unconfirmed.Datarate = LORAWAN_DEFAULT_DATARATE; |
mluis | 5:1e9f6a365854 | 471 | |
mluis | 3:9c6f7f082151 | 472 | LoRaMacUplinkStatus.Acked = false; |
mluis | 3:9c6f7f082151 | 473 | LoRaMacUplinkStatus.Port = 0; |
mluis | 3:9c6f7f082151 | 474 | LoRaMacUplinkStatus.Buffer = NULL; |
mluis | 3:9c6f7f082151 | 475 | LoRaMacUplinkStatus.BufferSize = 0; |
mluis | 3:9c6f7f082151 | 476 | SerialDisplayUpdateFrameType( false ); |
mluis | 1:352f608c3337 | 477 | } |
mluis | 1:352f608c3337 | 478 | else |
mluis | 1:352f608c3337 | 479 | { |
mluis | 3:9c6f7f082151 | 480 | LoRaMacUplinkStatus.Acked = false; |
mluis | 3:9c6f7f082151 | 481 | LoRaMacUplinkStatus.Port = AppPort; |
mluis | 3:9c6f7f082151 | 482 | LoRaMacUplinkStatus.Buffer = AppData; |
mluis | 3:9c6f7f082151 | 483 | LoRaMacUplinkStatus.BufferSize = AppDataSize; |
mluis | 3:9c6f7f082151 | 484 | SerialDisplayUpdateFrameType( IsTxConfirmed ); |
mluis | 3:9c6f7f082151 | 485 | |
mluis | 3:9c6f7f082151 | 486 | if( IsTxConfirmed == false ) |
mluis | 3:9c6f7f082151 | 487 | { |
mluis | 3:9c6f7f082151 | 488 | mcpsReq.Type = MCPS_UNCONFIRMED; |
mluis | 3:9c6f7f082151 | 489 | mcpsReq.Req.Unconfirmed.fPort = AppPort; |
mluis | 3:9c6f7f082151 | 490 | mcpsReq.Req.Unconfirmed.fBuffer = AppData; |
mluis | 3:9c6f7f082151 | 491 | mcpsReq.Req.Unconfirmed.fBufferSize = AppDataSize; |
mluis | 3:9c6f7f082151 | 492 | mcpsReq.Req.Unconfirmed.Datarate = LORAWAN_DEFAULT_DATARATE; |
mluis | 3:9c6f7f082151 | 493 | } |
mluis | 3:9c6f7f082151 | 494 | else |
mluis | 3:9c6f7f082151 | 495 | { |
mluis | 3:9c6f7f082151 | 496 | mcpsReq.Type = MCPS_CONFIRMED; |
mluis | 3:9c6f7f082151 | 497 | mcpsReq.Req.Confirmed.fPort = AppPort; |
mluis | 3:9c6f7f082151 | 498 | mcpsReq.Req.Confirmed.fBuffer = AppData; |
mluis | 3:9c6f7f082151 | 499 | mcpsReq.Req.Confirmed.fBufferSize = AppDataSize; |
mluis | 4:00cf2370c99d | 500 | mcpsReq.Req.Confirmed.NbTrials = 8; |
mluis | 3:9c6f7f082151 | 501 | mcpsReq.Req.Confirmed.Datarate = LORAWAN_DEFAULT_DATARATE; |
mluis | 3:9c6f7f082151 | 502 | } |
mluis | 1:352f608c3337 | 503 | } |
mluis | 1:352f608c3337 | 504 | |
mluis | 3:9c6f7f082151 | 505 | if( LoRaMacMcpsRequest( &mcpsReq ) == LORAMAC_STATUS_OK ) |
mluis | 0:92bca02df485 | 506 | { |
mluis | 0:92bca02df485 | 507 | return false; |
mluis | 0:92bca02df485 | 508 | } |
mluis | 3:9c6f7f082151 | 509 | return true; |
mluis | 0:92bca02df485 | 510 | } |
mluis | 0:92bca02df485 | 511 | |
mluis | 0:92bca02df485 | 512 | /*! |
mluis | 0:92bca02df485 | 513 | * \brief Function executed on TxNextPacket Timeout event |
mluis | 0:92bca02df485 | 514 | */ |
mluis | 0:92bca02df485 | 515 | static void OnTxNextPacketTimerEvent( void ) |
mluis | 0:92bca02df485 | 516 | { |
mluis | 3:9c6f7f082151 | 517 | MibRequestConfirm_t mibReq; |
mluis | 3:9c6f7f082151 | 518 | LoRaMacStatus_t status; |
mluis | 3:9c6f7f082151 | 519 | |
mluis | 0:92bca02df485 | 520 | TimerStop( &TxNextPacketTimer ); |
mluis | 3:9c6f7f082151 | 521 | |
mluis | 3:9c6f7f082151 | 522 | mibReq.Type = MIB_NETWORK_JOINED; |
mluis | 3:9c6f7f082151 | 523 | status = LoRaMacMibGetRequestConfirm( &mibReq ); |
mluis | 3:9c6f7f082151 | 524 | |
mluis | 3:9c6f7f082151 | 525 | if( status == LORAMAC_STATUS_OK ) |
mluis | 3:9c6f7f082151 | 526 | { |
mluis | 3:9c6f7f082151 | 527 | if( mibReq.Param.IsNetworkJoined == true ) |
mluis | 3:9c6f7f082151 | 528 | { |
mluis | 3:9c6f7f082151 | 529 | DeviceState = DEVICE_STATE_SEND; |
mluis | 3:9c6f7f082151 | 530 | NextTx = true; |
mluis | 3:9c6f7f082151 | 531 | } |
mluis | 3:9c6f7f082151 | 532 | else |
mluis | 3:9c6f7f082151 | 533 | { |
mluis | 3:9c6f7f082151 | 534 | DeviceState = DEVICE_STATE_JOIN; |
mluis | 3:9c6f7f082151 | 535 | } |
mluis | 3:9c6f7f082151 | 536 | } |
mluis | 0:92bca02df485 | 537 | } |
mluis | 0:92bca02df485 | 538 | |
mluis | 0:92bca02df485 | 539 | /*! |
mluis | 0:92bca02df485 | 540 | * \brief Function executed on Led 1 Timeout event |
mluis | 0:92bca02df485 | 541 | */ |
mluis | 0:92bca02df485 | 542 | static void OnLed1TimerEvent( void ) |
mluis | 0:92bca02df485 | 543 | { |
mluis | 0:92bca02df485 | 544 | TimerStop( &Led1Timer ); |
mluis | 3:9c6f7f082151 | 545 | // Switch LED 1 OFF |
mluis | 1:352f608c3337 | 546 | Led1State = false; |
mluis | 0:92bca02df485 | 547 | Led1StateChanged = true; |
mluis | 0:92bca02df485 | 548 | } |
mluis | 0:92bca02df485 | 549 | |
mluis | 0:92bca02df485 | 550 | /*! |
mluis | 0:92bca02df485 | 551 | * \brief Function executed on Led 2 Timeout event |
mluis | 0:92bca02df485 | 552 | */ |
mluis | 0:92bca02df485 | 553 | static void OnLed2TimerEvent( void ) |
mluis | 0:92bca02df485 | 554 | { |
mluis | 0:92bca02df485 | 555 | TimerStop( &Led2Timer ); |
mluis | 3:9c6f7f082151 | 556 | // Switch LED 2 OFF |
mluis | 1:352f608c3337 | 557 | Led2State = false; |
mluis | 0:92bca02df485 | 558 | Led2StateChanged = true; |
mluis | 0:92bca02df485 | 559 | } |
mluis | 0:92bca02df485 | 560 | |
mluis | 0:92bca02df485 | 561 | /*! |
mluis | 3:9c6f7f082151 | 562 | * \brief MCPS-Confirm event function |
mluis | 3:9c6f7f082151 | 563 | * |
mluis | 5:1e9f6a365854 | 564 | * \param [IN] mcpsConfirm - Pointer to the confirm structure, |
mluis | 3:9c6f7f082151 | 565 | * containing confirm attributes. |
mluis | 0:92bca02df485 | 566 | */ |
mluis | 5:1e9f6a365854 | 567 | static void McpsConfirm( McpsConfirm_t *mcpsConfirm ) |
mluis | 0:92bca02df485 | 568 | { |
mluis | 5:1e9f6a365854 | 569 | if( mcpsConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK ) |
mluis | 0:92bca02df485 | 570 | { |
mluis | 5:1e9f6a365854 | 571 | switch( mcpsConfirm->McpsRequest ) |
mluis | 3:9c6f7f082151 | 572 | { |
mluis | 3:9c6f7f082151 | 573 | case MCPS_UNCONFIRMED: |
mluis | 3:9c6f7f082151 | 574 | { |
mluis | 3:9c6f7f082151 | 575 | // Check Datarate |
mluis | 3:9c6f7f082151 | 576 | // Check TxPower |
mluis | 3:9c6f7f082151 | 577 | break; |
mluis | 3:9c6f7f082151 | 578 | } |
mluis | 3:9c6f7f082151 | 579 | case MCPS_CONFIRMED: |
mluis | 3:9c6f7f082151 | 580 | { |
mluis | 3:9c6f7f082151 | 581 | // Check Datarate |
mluis | 3:9c6f7f082151 | 582 | // Check TxPower |
mluis | 3:9c6f7f082151 | 583 | // Check AckReceived |
mluis | 5:1e9f6a365854 | 584 | // Check NbTrials |
mluis | 5:1e9f6a365854 | 585 | LoRaMacUplinkStatus.Acked = mcpsConfirm->AckReceived; |
mluis | 3:9c6f7f082151 | 586 | break; |
mluis | 3:9c6f7f082151 | 587 | } |
mluis | 3:9c6f7f082151 | 588 | case MCPS_PROPRIETARY: |
mluis | 3:9c6f7f082151 | 589 | { |
mluis | 3:9c6f7f082151 | 590 | break; |
mluis | 3:9c6f7f082151 | 591 | } |
mluis | 3:9c6f7f082151 | 592 | default: |
mluis | 3:9c6f7f082151 | 593 | break; |
mluis | 3:9c6f7f082151 | 594 | } |
mluis | 5:1e9f6a365854 | 595 | LoRaMacUplinkStatus.Datarate = mcpsConfirm->Datarate; |
mluis | 5:1e9f6a365854 | 596 | LoRaMacUplinkStatus.UplinkCounter = mcpsConfirm->UpLinkCounter; |
mluis | 5:1e9f6a365854 | 597 | |
mluis | 7:3173f0508a98 | 598 | // Switch LED 1 ON |
mluis | 7:3173f0508a98 | 599 | Led1State = true; |
mluis | 7:3173f0508a98 | 600 | Led1StateChanged = true; |
mluis | 7:3173f0508a98 | 601 | TimerStart( &Led1Timer ); |
mluis | 7:3173f0508a98 | 602 | |
mluis | 3:9c6f7f082151 | 603 | UplinkStatusUpdated = true; |
mluis | 3:9c6f7f082151 | 604 | } |
mluis | 3:9c6f7f082151 | 605 | NextTx = true; |
mluis | 3:9c6f7f082151 | 606 | } |
mluis | 3:9c6f7f082151 | 607 | |
mluis | 3:9c6f7f082151 | 608 | /*! |
mluis | 3:9c6f7f082151 | 609 | * \brief MCPS-Indication event function |
mluis | 3:9c6f7f082151 | 610 | * |
mluis | 5:1e9f6a365854 | 611 | * \param [IN] mcpsIndication - Pointer to the indication structure, |
mluis | 3:9c6f7f082151 | 612 | * containing indication attributes. |
mluis | 3:9c6f7f082151 | 613 | */ |
mluis | 5:1e9f6a365854 | 614 | static void McpsIndication( McpsIndication_t *mcpsIndication ) |
mluis | 3:9c6f7f082151 | 615 | { |
mluis | 5:1e9f6a365854 | 616 | if( mcpsIndication->Status != LORAMAC_EVENT_INFO_STATUS_OK ) |
mluis | 3:9c6f7f082151 | 617 | { |
mluis | 3:9c6f7f082151 | 618 | return; |
mluis | 3:9c6f7f082151 | 619 | } |
mluis | 3:9c6f7f082151 | 620 | |
mluis | 5:1e9f6a365854 | 621 | switch( mcpsIndication->McpsIndication ) |
mluis | 3:9c6f7f082151 | 622 | { |
mluis | 3:9c6f7f082151 | 623 | case MCPS_UNCONFIRMED: |
mluis | 3:9c6f7f082151 | 624 | { |
mluis | 3:9c6f7f082151 | 625 | break; |
mluis | 3:9c6f7f082151 | 626 | } |
mluis | 3:9c6f7f082151 | 627 | case MCPS_CONFIRMED: |
mluis | 3:9c6f7f082151 | 628 | { |
mluis | 3:9c6f7f082151 | 629 | break; |
mluis | 3:9c6f7f082151 | 630 | } |
mluis | 3:9c6f7f082151 | 631 | case MCPS_PROPRIETARY: |
mluis | 3:9c6f7f082151 | 632 | { |
mluis | 3:9c6f7f082151 | 633 | break; |
mluis | 3:9c6f7f082151 | 634 | } |
mluis | 3:9c6f7f082151 | 635 | case MCPS_MULTICAST: |
mluis | 3:9c6f7f082151 | 636 | { |
mluis | 3:9c6f7f082151 | 637 | break; |
mluis | 3:9c6f7f082151 | 638 | } |
mluis | 3:9c6f7f082151 | 639 | default: |
mluis | 3:9c6f7f082151 | 640 | break; |
mluis | 3:9c6f7f082151 | 641 | } |
mluis | 3:9c6f7f082151 | 642 | |
mluis | 3:9c6f7f082151 | 643 | // Check Multicast |
mluis | 3:9c6f7f082151 | 644 | // Check Port |
mluis | 3:9c6f7f082151 | 645 | // Check Datarate |
mluis | 3:9c6f7f082151 | 646 | // Check FramePending |
mluis | 3:9c6f7f082151 | 647 | // Check Buffer |
mluis | 3:9c6f7f082151 | 648 | // Check BufferSize |
mluis | 3:9c6f7f082151 | 649 | // Check Rssi |
mluis | 3:9c6f7f082151 | 650 | // Check Snr |
mluis | 3:9c6f7f082151 | 651 | // Check RxSlot |
mluis | 5:1e9f6a365854 | 652 | LoRaMacDownlinkStatus.Rssi = mcpsIndication->Rssi; |
mluis | 5:1e9f6a365854 | 653 | if( mcpsIndication->Snr & 0x80 ) // The SNR sign bit is 1 |
mluis | 3:9c6f7f082151 | 654 | { |
mluis | 3:9c6f7f082151 | 655 | // Invert and divide by 4 |
mluis | 5:1e9f6a365854 | 656 | LoRaMacDownlinkStatus.Snr = ( ( ~mcpsIndication->Snr + 1 ) & 0xFF ) >> 2; |
mluis | 3:9c6f7f082151 | 657 | LoRaMacDownlinkStatus.Snr = -LoRaMacDownlinkStatus.Snr; |
mluis | 0:92bca02df485 | 658 | } |
mluis | 0:92bca02df485 | 659 | else |
mluis | 0:92bca02df485 | 660 | { |
mluis | 3:9c6f7f082151 | 661 | // Divide by 4 |
mluis | 5:1e9f6a365854 | 662 | LoRaMacDownlinkStatus.Snr = ( mcpsIndication->Snr & 0xFF ) >> 2; |
mluis | 3:9c6f7f082151 | 663 | } |
mluis | 3:9c6f7f082151 | 664 | LoRaMacDownlinkStatus.DownlinkCounter++; |
mluis | 5:1e9f6a365854 | 665 | LoRaMacDownlinkStatus.RxData = mcpsIndication->RxData; |
mluis | 5:1e9f6a365854 | 666 | LoRaMacDownlinkStatus.Port = mcpsIndication->Port; |
mluis | 5:1e9f6a365854 | 667 | LoRaMacDownlinkStatus.Buffer = mcpsIndication->Buffer; |
mluis | 5:1e9f6a365854 | 668 | LoRaMacDownlinkStatus.BufferSize = mcpsIndication->BufferSize; |
mluis | 3:9c6f7f082151 | 669 | |
mluis | 3:9c6f7f082151 | 670 | if( ComplianceTest.Running == true ) |
mluis | 3:9c6f7f082151 | 671 | { |
mluis | 3:9c6f7f082151 | 672 | ComplianceTest.DownLinkCounter++; |
mluis | 3:9c6f7f082151 | 673 | } |
mluis | 0:92bca02df485 | 674 | |
mluis | 5:1e9f6a365854 | 675 | if( mcpsIndication->RxData == true ) |
mluis | 3:9c6f7f082151 | 676 | { |
mluis | 5:1e9f6a365854 | 677 | switch( mcpsIndication->Port ) |
mluis | 0:92bca02df485 | 678 | { |
mluis | 3:9c6f7f082151 | 679 | case 1: // The application LED can be controlled on port 1 or 2 |
mluis | 3:9c6f7f082151 | 680 | case 2: |
mluis | 5:1e9f6a365854 | 681 | if( mcpsIndication->BufferSize == 1 ) |
mluis | 1:352f608c3337 | 682 | { |
mluis | 5:1e9f6a365854 | 683 | AppLedStateOn = mcpsIndication->Buffer[0] & 0x01; |
mluis | 3:9c6f7f082151 | 684 | Led3StateChanged = true; |
mluis | 3:9c6f7f082151 | 685 | } |
mluis | 3:9c6f7f082151 | 686 | break; |
mluis | 3:9c6f7f082151 | 687 | case 224: |
mluis | 3:9c6f7f082151 | 688 | if( ComplianceTest.Running == false ) |
mluis | 3:9c6f7f082151 | 689 | { |
mluis | 3:9c6f7f082151 | 690 | // Check compliance test enable command (i) |
mluis | 5:1e9f6a365854 | 691 | if( ( mcpsIndication->BufferSize == 4 ) && |
mluis | 5:1e9f6a365854 | 692 | ( mcpsIndication->Buffer[0] == 0x01 ) && |
mluis | 5:1e9f6a365854 | 693 | ( mcpsIndication->Buffer[1] == 0x01 ) && |
mluis | 5:1e9f6a365854 | 694 | ( mcpsIndication->Buffer[2] == 0x01 ) && |
mluis | 5:1e9f6a365854 | 695 | ( mcpsIndication->Buffer[3] == 0x01 ) ) |
mluis | 1:352f608c3337 | 696 | { |
mluis | 3:9c6f7f082151 | 697 | IsTxConfirmed = false; |
mluis | 3:9c6f7f082151 | 698 | AppPort = 224; |
mluis | 3:9c6f7f082151 | 699 | AppDataSize = 2; |
mluis | 3:9c6f7f082151 | 700 | ComplianceTest.DownLinkCounter = 0; |
mluis | 3:9c6f7f082151 | 701 | ComplianceTest.LinkCheck = false; |
mluis | 3:9c6f7f082151 | 702 | ComplianceTest.DemodMargin = 0; |
mluis | 3:9c6f7f082151 | 703 | ComplianceTest.NbGateways = 0; |
mluis | 3:9c6f7f082151 | 704 | ComplianceTest.Running = true; |
mluis | 3:9c6f7f082151 | 705 | ComplianceTest.State = 1; |
mluis | 9:ee9dcbb9708d | 706 | |
mluis | 3:9c6f7f082151 | 707 | MibRequestConfirm_t mibReq; |
mluis | 3:9c6f7f082151 | 708 | mibReq.Type = MIB_ADR; |
mluis | 3:9c6f7f082151 | 709 | mibReq.Param.AdrEnable = true; |
mluis | 3:9c6f7f082151 | 710 | LoRaMacMibSetRequestConfirm( &mibReq ); |
mluis | 3:9c6f7f082151 | 711 | |
mluis | 3:9c6f7f082151 | 712 | #if defined( USE_BAND_868 ) |
mluis | 3:9c6f7f082151 | 713 | LoRaMacTestSetDutyCycleOn( false ); |
mluis | 3:9c6f7f082151 | 714 | #endif |
mluis | 1:352f608c3337 | 715 | } |
mluis | 1:352f608c3337 | 716 | } |
mluis | 0:92bca02df485 | 717 | else |
mluis | 0:92bca02df485 | 718 | { |
mluis | 5:1e9f6a365854 | 719 | ComplianceTest.State = mcpsIndication->Buffer[0]; |
mluis | 3:9c6f7f082151 | 720 | switch( ComplianceTest.State ) |
mluis | 3:9c6f7f082151 | 721 | { |
mluis | 3:9c6f7f082151 | 722 | case 0: // Check compliance test disable command (ii) |
mluis | 3:9c6f7f082151 | 723 | IsTxConfirmed = LORAWAN_CONFIRMED_MSG_ON; |
mluis | 3:9c6f7f082151 | 724 | AppPort = LORAWAN_APP_PORT; |
mluis | 3:9c6f7f082151 | 725 | AppDataSize = LORAWAN_APP_DATA_SIZE; |
mluis | 3:9c6f7f082151 | 726 | ComplianceTest.DownLinkCounter = 0; |
mluis | 3:9c6f7f082151 | 727 | ComplianceTest.Running = false; |
mluis | 9:ee9dcbb9708d | 728 | |
mluis | 3:9c6f7f082151 | 729 | MibRequestConfirm_t mibReq; |
mluis | 3:9c6f7f082151 | 730 | mibReq.Type = MIB_ADR; |
mluis | 3:9c6f7f082151 | 731 | mibReq.Param.AdrEnable = LORAWAN_ADR_ON; |
mluis | 3:9c6f7f082151 | 732 | LoRaMacMibSetRequestConfirm( &mibReq ); |
mluis | 3:9c6f7f082151 | 733 | #if defined( USE_BAND_868 ) |
mluis | 3:9c6f7f082151 | 734 | LoRaMacTestSetDutyCycleOn( LORAWAN_DUTYCYCLE_ON ); |
mluis | 3:9c6f7f082151 | 735 | #endif |
mluis | 3:9c6f7f082151 | 736 | break; |
mluis | 3:9c6f7f082151 | 737 | case 1: // (iii, iv) |
mluis | 3:9c6f7f082151 | 738 | AppDataSize = 2; |
mluis | 3:9c6f7f082151 | 739 | break; |
mluis | 3:9c6f7f082151 | 740 | case 2: // Enable confirmed messages (v) |
mluis | 3:9c6f7f082151 | 741 | IsTxConfirmed = true; |
mluis | 3:9c6f7f082151 | 742 | ComplianceTest.State = 1; |
mluis | 3:9c6f7f082151 | 743 | break; |
mluis | 3:9c6f7f082151 | 744 | case 3: // Disable confirmed messages (vi) |
mluis | 3:9c6f7f082151 | 745 | IsTxConfirmed = false; |
mluis | 3:9c6f7f082151 | 746 | ComplianceTest.State = 1; |
mluis | 3:9c6f7f082151 | 747 | break; |
mluis | 3:9c6f7f082151 | 748 | case 4: // (vii) |
mluis | 5:1e9f6a365854 | 749 | AppDataSize = mcpsIndication->BufferSize; |
mluis | 3:9c6f7f082151 | 750 | |
mluis | 3:9c6f7f082151 | 751 | AppData[0] = 4; |
mluis | 3:9c6f7f082151 | 752 | for( uint8_t i = 1; i < AppDataSize; i++ ) |
mluis | 3:9c6f7f082151 | 753 | { |
mluis | 5:1e9f6a365854 | 754 | AppData[i] = mcpsIndication->Buffer[i] + 1; |
mluis | 3:9c6f7f082151 | 755 | } |
mluis | 3:9c6f7f082151 | 756 | break; |
mluis | 3:9c6f7f082151 | 757 | case 5: // (viii) |
mluis | 3:9c6f7f082151 | 758 | { |
mluis | 3:9c6f7f082151 | 759 | MlmeReq_t mlmeReq; |
mluis | 3:9c6f7f082151 | 760 | mlmeReq.Type = MLME_LINK_CHECK; |
mluis | 3:9c6f7f082151 | 761 | LoRaMacMlmeRequest( &mlmeReq ); |
mluis | 3:9c6f7f082151 | 762 | } |
mluis | 3:9c6f7f082151 | 763 | break; |
mluis | 7:3173f0508a98 | 764 | case 6: // (ix) |
mluis | 7:3173f0508a98 | 765 | { |
mluis | 7:3173f0508a98 | 766 | MlmeReq_t mlmeReq; |
mluis | 7:3173f0508a98 | 767 | |
mluis | 9:ee9dcbb9708d | 768 | // Disable TestMode and revert back to normal operation |
mluis | 9:ee9dcbb9708d | 769 | IsTxConfirmed = LORAWAN_CONFIRMED_MSG_ON; |
mluis | 9:ee9dcbb9708d | 770 | AppPort = LORAWAN_APP_PORT; |
mluis | 9:ee9dcbb9708d | 771 | AppDataSize = LORAWAN_APP_DATA_SIZE; |
mluis | 9:ee9dcbb9708d | 772 | ComplianceTest.DownLinkCounter = 0; |
mluis | 9:ee9dcbb9708d | 773 | ComplianceTest.Running = false; |
mluis | 9:ee9dcbb9708d | 774 | |
mluis | 9:ee9dcbb9708d | 775 | MibRequestConfirm_t mibReq; |
mluis | 9:ee9dcbb9708d | 776 | mibReq.Type = MIB_ADR; |
mluis | 9:ee9dcbb9708d | 777 | mibReq.Param.AdrEnable = LORAWAN_ADR_ON; |
mluis | 9:ee9dcbb9708d | 778 | LoRaMacMibSetRequestConfirm( &mibReq ); |
mluis | 9:ee9dcbb9708d | 779 | #if defined( USE_BAND_868 ) |
mluis | 9:ee9dcbb9708d | 780 | LoRaMacTestSetDutyCycleOn( LORAWAN_DUTYCYCLE_ON ); |
mluis | 9:ee9dcbb9708d | 781 | #endif |
mluis | 9:ee9dcbb9708d | 782 | |
mluis | 7:3173f0508a98 | 783 | mlmeReq.Type = MLME_JOIN; |
mluis | 7:3173f0508a98 | 784 | |
mluis | 7:3173f0508a98 | 785 | mlmeReq.Req.Join.DevEui = DevEui; |
mluis | 7:3173f0508a98 | 786 | mlmeReq.Req.Join.AppEui = AppEui; |
mluis | 7:3173f0508a98 | 787 | mlmeReq.Req.Join.AppKey = AppKey; |
mluis | 9:ee9dcbb9708d | 788 | mlmeReq.Req.Join.NbTrials = 3; |
mluis | 7:3173f0508a98 | 789 | |
mluis | 7:3173f0508a98 | 790 | LoRaMacMlmeRequest( &mlmeReq ); |
mluis | 7:3173f0508a98 | 791 | DeviceState = DEVICE_STATE_SLEEP; |
mluis | 7:3173f0508a98 | 792 | } |
mluis | 7:3173f0508a98 | 793 | break; |
mluis | 9:ee9dcbb9708d | 794 | case 7: // (x) |
mluis | 9:ee9dcbb9708d | 795 | { |
mluis | 9:ee9dcbb9708d | 796 | if( mcpsIndication->BufferSize == 3 ) |
mluis | 9:ee9dcbb9708d | 797 | { |
mluis | 9:ee9dcbb9708d | 798 | MlmeReq_t mlmeReq; |
mluis | 9:ee9dcbb9708d | 799 | mlmeReq.Type = MLME_TXCW; |
mluis | 9:ee9dcbb9708d | 800 | mlmeReq.Req.TxCw.Timeout = ( uint16_t )( ( mcpsIndication->Buffer[1] << 8 ) | mcpsIndication->Buffer[2] ); |
mluis | 9:ee9dcbb9708d | 801 | LoRaMacMlmeRequest( &mlmeReq ); |
mluis | 9:ee9dcbb9708d | 802 | } |
mluis | 9:ee9dcbb9708d | 803 | else if( mcpsIndication->BufferSize == 7 ) |
mluis | 9:ee9dcbb9708d | 804 | { |
mluis | 9:ee9dcbb9708d | 805 | MlmeReq_t mlmeReq; |
mluis | 9:ee9dcbb9708d | 806 | mlmeReq.Type = MLME_TXCW_1; |
mluis | 9:ee9dcbb9708d | 807 | mlmeReq.Req.TxCw.Timeout = ( uint16_t )( ( mcpsIndication->Buffer[1] << 8 ) | mcpsIndication->Buffer[2] ); |
mluis | 9:ee9dcbb9708d | 808 | mlmeReq.Req.TxCw.Frequency = ( uint32_t )( ( mcpsIndication->Buffer[3] << 16 ) | ( mcpsIndication->Buffer[4] << 8 ) | mcpsIndication->Buffer[5] ) * 100; |
mluis | 9:ee9dcbb9708d | 809 | mlmeReq.Req.TxCw.Power = mcpsIndication->Buffer[6]; |
mluis | 9:ee9dcbb9708d | 810 | LoRaMacMlmeRequest( &mlmeReq ); |
mluis | 9:ee9dcbb9708d | 811 | } |
mluis | 9:ee9dcbb9708d | 812 | ComplianceTest.State = 1; |
mluis | 9:ee9dcbb9708d | 813 | } |
mluis | 9:ee9dcbb9708d | 814 | break; |
mluis | 3:9c6f7f082151 | 815 | default: |
mluis | 3:9c6f7f082151 | 816 | break; |
mluis | 3:9c6f7f082151 | 817 | } |
mluis | 0:92bca02df485 | 818 | } |
mluis | 3:9c6f7f082151 | 819 | break; |
mluis | 3:9c6f7f082151 | 820 | default: |
mluis | 3:9c6f7f082151 | 821 | break; |
mluis | 0:92bca02df485 | 822 | } |
mluis | 0:92bca02df485 | 823 | } |
mluis | 1:352f608c3337 | 824 | |
mluis | 3:9c6f7f082151 | 825 | // Switch LED 2 ON for each received downlink |
mluis | 3:9c6f7f082151 | 826 | Led2State = true; |
mluis | 3:9c6f7f082151 | 827 | Led2StateChanged = true; |
mluis | 3:9c6f7f082151 | 828 | TimerStart( &Led2Timer ); |
mluis | 3:9c6f7f082151 | 829 | DownlinkStatusUpdated = true; |
mluis | 3:9c6f7f082151 | 830 | } |
mluis | 3:9c6f7f082151 | 831 | |
mluis | 3:9c6f7f082151 | 832 | /*! |
mluis | 3:9c6f7f082151 | 833 | * \brief MLME-Confirm event function |
mluis | 3:9c6f7f082151 | 834 | * |
mluis | 5:1e9f6a365854 | 835 | * \param [IN] mlmeConfirm - Pointer to the confirm structure, |
mluis | 3:9c6f7f082151 | 836 | * containing confirm attributes. |
mluis | 3:9c6f7f082151 | 837 | */ |
mluis | 5:1e9f6a365854 | 838 | static void MlmeConfirm( MlmeConfirm_t *mlmeConfirm ) |
mluis | 3:9c6f7f082151 | 839 | { |
mluis | 9:ee9dcbb9708d | 840 | switch( mlmeConfirm->MlmeRequest ) |
mluis | 3:9c6f7f082151 | 841 | { |
mluis | 9:ee9dcbb9708d | 842 | case MLME_JOIN: |
mluis | 3:9c6f7f082151 | 843 | { |
mluis | 9:ee9dcbb9708d | 844 | if( mlmeConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK ) |
mluis | 3:9c6f7f082151 | 845 | { |
mluis | 3:9c6f7f082151 | 846 | // Status is OK, node has joined the network |
mluis | 3:9c6f7f082151 | 847 | IsNetworkJoinedStatusUpdate = true; |
mluis | 7:3173f0508a98 | 848 | DeviceState = DEVICE_STATE_SEND; |
uss1994 | 10:9a4efdd07a77 | 849 | |
uss1994 | 10:9a4efdd07a77 | 850 | |
uss1994 | 10:9a4efdd07a77 | 851 | MessageType = MESSAGE_TYPE_HB; |
uss1994 | 10:9a4efdd07a77 | 852 | joining = false; |
uss1994 | 10:9a4efdd07a77 | 853 | next_stest = current_time + 5; |
uss1994 | 10:9a4efdd07a77 | 854 | next_ltest = current_time + long_interval; |
mluis | 9:ee9dcbb9708d | 855 | } |
mluis | 9:ee9dcbb9708d | 856 | else |
mluis | 9:ee9dcbb9708d | 857 | { |
mluis | 9:ee9dcbb9708d | 858 | // Join was not successful. Try to join again |
mluis | 9:ee9dcbb9708d | 859 | DeviceState = DEVICE_STATE_JOIN; |
mluis | 3:9c6f7f082151 | 860 | } |
mluis | 9:ee9dcbb9708d | 861 | break; |
mluis | 9:ee9dcbb9708d | 862 | } |
mluis | 9:ee9dcbb9708d | 863 | case MLME_LINK_CHECK: |
mluis | 9:ee9dcbb9708d | 864 | { |
mluis | 9:ee9dcbb9708d | 865 | if( mlmeConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK ) |
mluis | 3:9c6f7f082151 | 866 | { |
mluis | 3:9c6f7f082151 | 867 | // Check DemodMargin |
mluis | 3:9c6f7f082151 | 868 | // Check NbGateways |
mluis | 3:9c6f7f082151 | 869 | if( ComplianceTest.Running == true ) |
mluis | 3:9c6f7f082151 | 870 | { |
mluis | 3:9c6f7f082151 | 871 | ComplianceTest.LinkCheck = true; |
mluis | 5:1e9f6a365854 | 872 | ComplianceTest.DemodMargin = mlmeConfirm->DemodMargin; |
mluis | 5:1e9f6a365854 | 873 | ComplianceTest.NbGateways = mlmeConfirm->NbGateways; |
mluis | 3:9c6f7f082151 | 874 | } |
mluis | 3:9c6f7f082151 | 875 | } |
mluis | 9:ee9dcbb9708d | 876 | break; |
mluis | 3:9c6f7f082151 | 877 | } |
mluis | 9:ee9dcbb9708d | 878 | default: |
mluis | 9:ee9dcbb9708d | 879 | break; |
mluis | 3:9c6f7f082151 | 880 | } |
mluis | 3:9c6f7f082151 | 881 | NextTx = true; |
mluis | 3:9c6f7f082151 | 882 | UplinkStatusUpdated = true; |
mluis | 0:92bca02df485 | 883 | } |
mluis | 0:92bca02df485 | 884 | |
uss1994 | 10:9a4efdd07a77 | 885 | |
uss1994 | 10:9a4efdd07a77 | 886 | // **************************** TEST METHODS ******************************** // |
uss1994 | 10:9a4efdd07a77 | 887 | |
uss1994 | 10:9a4efdd07a77 | 888 | int takeMeasurement(uint8_t *measurements, int count) { |
uss1994 | 10:9a4efdd07a77 | 889 | // This method should only run for long tests |
uss1994 | 10:9a4efdd07a77 | 890 | int battery_reading = BAT_PIN.read()*1000; |
uss1994 | 10:9a4efdd07a77 | 891 | int vce_reading = VCE_PIN.read()*1000; |
uss1994 | 10:9a4efdd07a77 | 892 | int light_1_reading = LIGHT_1_PIN.read()*1000 - vce_reading; |
uss1994 | 10:9a4efdd07a77 | 893 | int light_2_reading = LIGHT_2_PIN.read()*1000 - vce_reading; |
uss1994 | 10:9a4efdd07a77 | 894 | |
uss1994 | 10:9a4efdd07a77 | 895 | int index = count; |
uss1994 | 10:9a4efdd07a77 | 896 | |
uss1994 | 10:9a4efdd07a77 | 897 | measurements[(uint8_t)index] = (uint8_t) (battery_reading/10); |
uss1994 | 10:9a4efdd07a77 | 898 | measurements[(uint8_t)index+10] = (uint8_t) light_1_reading; |
uss1994 | 10:9a4efdd07a77 | 899 | measurements[(uint8_t)index+20] = (uint8_t) light_2_reading; |
uss1994 | 10:9a4efdd07a77 | 900 | return index+1; |
uss1994 | 10:9a4efdd07a77 | 901 | } |
uss1994 | 10:9a4efdd07a77 | 902 | |
uss1994 | 10:9a4efdd07a77 | 903 | void startTest(uint8_t *measurements) { |
uss1994 | 10:9a4efdd07a77 | 904 | // Signal that test is running |
uss1994 | 10:9a4efdd07a77 | 905 | flash_builtin(); |
uss1994 | 10:9a4efdd07a77 | 906 | flash_builtin(); |
uss1994 | 10:9a4efdd07a77 | 907 | |
uss1994 | 10:9a4efdd07a77 | 908 | // Determine test length & measurement interval |
uss1994 | 10:9a4efdd07a77 | 909 | switch (MessageType) |
uss1994 | 10:9a4efdd07a77 | 910 | { |
uss1994 | 10:9a4efdd07a77 | 911 | case MESSAGE_TYPE_SHORT_TEST: |
uss1994 | 10:9a4efdd07a77 | 912 | test_duration = 30; |
uss1994 | 10:9a4efdd07a77 | 913 | // set interval to twice the test length so that it never happens |
uss1994 | 10:9a4efdd07a77 | 914 | measurement_interval = 2*test_duration; |
uss1994 | 10:9a4efdd07a77 | 915 | break; |
uss1994 | 10:9a4efdd07a77 | 916 | case MESSAGE_TYPE_LONG_TEST: |
uss1994 | 10:9a4efdd07a77 | 917 | test_duration = 5400; |
uss1994 | 10:9a4efdd07a77 | 918 | // interval is divided by two longer than available measurement |
uss1994 | 10:9a4efdd07a77 | 919 | // slots so that it completes before the end of the test |
uss1994 | 10:9a4efdd07a77 | 920 | measurement_interval = test_duration/(available_slots + 2); |
uss1994 | 10:9a4efdd07a77 | 921 | break; |
uss1994 | 10:9a4efdd07a77 | 922 | default: |
uss1994 | 10:9a4efdd07a77 | 923 | test_duration = 0; |
uss1994 | 10:9a4efdd07a77 | 924 | break; |
uss1994 | 10:9a4efdd07a77 | 925 | } |
uss1994 | 10:9a4efdd07a77 | 926 | |
uss1994 | 10:9a4efdd07a77 | 927 | // Measure voltage preval |
uss1994 | 10:9a4efdd07a77 | 928 | measurements[0] = (uint8_t) (BAT_PIN.read()*1000)/10; |
uss1994 | 10:9a4efdd07a77 | 929 | |
uss1994 | 10:9a4efdd07a77 | 930 | // Set measurement count to 1 |
uss1994 | 10:9a4efdd07a77 | 931 | measureCount = 1; |
uss1994 | 10:9a4efdd07a77 | 932 | |
uss1994 | 10:9a4efdd07a77 | 933 | // Start test |
uss1994 | 10:9a4efdd07a77 | 934 | relayPin = 1; |
uss1994 | 10:9a4efdd07a77 | 935 | } |
uss1994 | 10:9a4efdd07a77 | 936 | |
uss1994 | 10:9a4efdd07a77 | 937 | void endTest(uint8_t *measurements) { |
uss1994 | 10:9a4efdd07a77 | 938 | // Measure endvals |
uss1994 | 10:9a4efdd07a77 | 939 | int battery_reading = BAT_PIN.read()*1000; |
uss1994 | 10:9a4efdd07a77 | 940 | int vce_reading = VCE_PIN.read()*1000; |
uss1994 | 10:9a4efdd07a77 | 941 | int light_1_reading = LIGHT_1_PIN.read()*1000 - vce_reading; |
uss1994 | 10:9a4efdd07a77 | 942 | int light_2_reading = LIGHT_2_PIN.read()*1000 - vce_reading; |
uss1994 | 10:9a4efdd07a77 | 943 | |
uss1994 | 10:9a4efdd07a77 | 944 | switch (MessageType) |
uss1994 | 10:9a4efdd07a77 | 945 | { |
uss1994 | 10:9a4efdd07a77 | 946 | case MESSAGE_TYPE_SHORT_TEST: |
uss1994 | 10:9a4efdd07a77 | 947 | measurements[1] = (uint8_t) (battery_reading/10); |
uss1994 | 10:9a4efdd07a77 | 948 | measurements[2] = (uint8_t) light_1_reading; |
uss1994 | 10:9a4efdd07a77 | 949 | measurements[3] = (uint8_t) light_2_reading; |
uss1994 | 10:9a4efdd07a77 | 950 | break; |
uss1994 | 10:9a4efdd07a77 | 951 | default: |
uss1994 | 10:9a4efdd07a77 | 952 | measurements[10] = (uint8_t) (battery_reading/10); |
uss1994 | 10:9a4efdd07a77 | 953 | measurements[20] = (uint8_t) light_1_reading; |
uss1994 | 10:9a4efdd07a77 | 954 | measurements[30] = (uint8_t) light_2_reading; |
uss1994 | 10:9a4efdd07a77 | 955 | break; |
uss1994 | 10:9a4efdd07a77 | 956 | } |
uss1994 | 10:9a4efdd07a77 | 957 | |
uss1994 | 10:9a4efdd07a77 | 958 | // End test |
uss1994 | 10:9a4efdd07a77 | 959 | relayPin = 0; |
uss1994 | 10:9a4efdd07a77 | 960 | } |
uss1994 | 10:9a4efdd07a77 | 961 | |
uss1994 | 11:e433cbbfd436 | 962 | void button_options_handler(void) |
uss1994 | 11:e433cbbfd436 | 963 | { |
uss1994 | 11:e433cbbfd436 | 964 | if (press_count >= 3) { |
uss1994 | 11:e433cbbfd436 | 965 | // Run long test |
uss1994 | 11:e433cbbfd436 | 966 | MessageType = MESSAGE_TYPE_LONG_TEST; |
uss1994 | 11:e433cbbfd436 | 967 | measurements = l_measurements; |
uss1994 | 11:e433cbbfd436 | 968 | |
uss1994 | 11:e433cbbfd436 | 969 | startTest(measurements); |
uss1994 | 11:e433cbbfd436 | 970 | |
uss1994 | 11:e433cbbfd436 | 971 | running_test = true; |
uss1994 | 11:e433cbbfd436 | 972 | test_start = current_time; |
uss1994 | 11:e433cbbfd436 | 973 | last_measurement = current_time; |
uss1994 | 11:e433cbbfd436 | 974 | } |
uss1994 | 11:e433cbbfd436 | 975 | if (press_count == 2) { |
uss1994 | 11:e433cbbfd436 | 976 | // Run short test |
uss1994 | 11:e433cbbfd436 | 977 | MessageType = MESSAGE_TYPE_SHORT_TEST; |
uss1994 | 11:e433cbbfd436 | 978 | measurements = s_measurements; |
uss1994 | 11:e433cbbfd436 | 979 | |
uss1994 | 11:e433cbbfd436 | 980 | startTest(measurements); |
uss1994 | 11:e433cbbfd436 | 981 | |
uss1994 | 11:e433cbbfd436 | 982 | running_test = true; |
uss1994 | 11:e433cbbfd436 | 983 | test_start = current_time; |
uss1994 | 11:e433cbbfd436 | 984 | last_measurement = current_time; |
uss1994 | 11:e433cbbfd436 | 985 | } |
uss1994 | 11:e433cbbfd436 | 986 | |
uss1994 | 11:e433cbbfd436 | 987 | // Reset count |
uss1994 | 11:e433cbbfd436 | 988 | press_count = 0; |
uss1994 | 11:e433cbbfd436 | 989 | } |
uss1994 | 11:e433cbbfd436 | 990 | |
uss1994 | 10:9a4efdd07a77 | 991 | |
mluis | 0:92bca02df485 | 992 | /** |
mluis | 0:92bca02df485 | 993 | * Main application entry point. |
mluis | 0:92bca02df485 | 994 | */ |
mluis | 0:92bca02df485 | 995 | int main( void ) |
mluis | 0:92bca02df485 | 996 | { |
mluis | 3:9c6f7f082151 | 997 | LoRaMacPrimitives_t LoRaMacPrimitives; |
mluis | 3:9c6f7f082151 | 998 | LoRaMacCallback_t LoRaMacCallbacks; |
mluis | 3:9c6f7f082151 | 999 | MibRequestConfirm_t mibReq; |
mluis | 0:92bca02df485 | 1000 | |
mluis | 0:92bca02df485 | 1001 | BoardInit( ); |
mluis | 3:9c6f7f082151 | 1002 | SerialDisplayInit( ); |
mluis | 0:92bca02df485 | 1003 | |
mluis | 7:3173f0508a98 | 1004 | SerialDisplayUpdateEui( 5, DevEui ); |
mluis | 7:3173f0508a98 | 1005 | SerialDisplayUpdateEui( 6, AppEui ); |
mluis | 7:3173f0508a98 | 1006 | SerialDisplayUpdateKey( 7, AppKey ); |
mluis | 7:3173f0508a98 | 1007 | |
mluis | 7:3173f0508a98 | 1008 | #if( OVER_THE_AIR_ACTIVATION == 0 ) |
mluis | 7:3173f0508a98 | 1009 | SerialDisplayUpdateNwkId( LORAWAN_NETWORK_ID ); |
mluis | 7:3173f0508a98 | 1010 | SerialDisplayUpdateDevAddr( DevAddr ); |
mluis | 7:3173f0508a98 | 1011 | SerialDisplayUpdateKey( 12, NwkSKey ); |
mluis | 7:3173f0508a98 | 1012 | SerialDisplayUpdateKey( 13, AppSKey ); |
mluis | 7:3173f0508a98 | 1013 | #endif |
mluis | 7:3173f0508a98 | 1014 | |
mluis | 3:9c6f7f082151 | 1015 | DeviceState = DEVICE_STATE_INIT; |
uss1994 | 10:9a4efdd07a77 | 1016 | set_time(1514764800); |
uss1994 | 10:9a4efdd07a77 | 1017 | relayPin = 0; |
uss1994 | 10:9a4efdd07a77 | 1018 | |
uss1994 | 10:9a4efdd07a77 | 1019 | flash_builtin(); |
uss1994 | 10:9a4efdd07a77 | 1020 | flash_builtin(); |
uss1994 | 10:9a4efdd07a77 | 1021 | flash_builtin(); |
uss1994 | 10:9a4efdd07a77 | 1022 | flash_builtin(); |
uss1994 | 10:9a4efdd07a77 | 1023 | |
uss1994 | 10:9a4efdd07a77 | 1024 | running_test = false; |
uss1994 | 10:9a4efdd07a77 | 1025 | joining = true; |
uss1994 | 10:9a4efdd07a77 | 1026 | received_downlink = false; |
uss1994 | 10:9a4efdd07a77 | 1027 | time_t start_time = time(NULL); |
uss1994 | 10:9a4efdd07a77 | 1028 | |
uss1994 | 10:9a4efdd07a77 | 1029 | last_hb = start_time; |
uss1994 | 10:9a4efdd07a77 | 1030 | last_measurement = start_time; |
uss1994 | 11:e433cbbfd436 | 1031 | |
uss1994 | 11:e433cbbfd436 | 1032 | //button1.mode(PullUp); // Activate pull-up |
uss1994 | 11:e433cbbfd436 | 1033 | button1.fall(callback(button1_onpressed_cb)); // Attach ISR to handle button press event |
mluis | 0:92bca02df485 | 1034 | |
mluis | 0:92bca02df485 | 1035 | while( 1 ) |
mluis | 0:92bca02df485 | 1036 | { |
uss1994 | 10:9a4efdd07a77 | 1037 | current_time = time(NULL); |
mluis | 0:92bca02df485 | 1038 | SerialRxProcess( ); |
mluis | 0:92bca02df485 | 1039 | if( IsNetworkJoinedStatusUpdate == true ) |
mluis | 0:92bca02df485 | 1040 | { |
mluis | 0:92bca02df485 | 1041 | IsNetworkJoinedStatusUpdate = false; |
mluis | 3:9c6f7f082151 | 1042 | mibReq.Type = MIB_NETWORK_JOINED; |
mluis | 3:9c6f7f082151 | 1043 | LoRaMacMibGetRequestConfirm( &mibReq ); |
mluis | 3:9c6f7f082151 | 1044 | SerialDisplayUpdateNetworkIsJoined( mibReq.Param.IsNetworkJoined ); |
mluis | 0:92bca02df485 | 1045 | } |
mluis | 0:92bca02df485 | 1046 | if( Led1StateChanged == true ) |
mluis | 0:92bca02df485 | 1047 | { |
mluis | 0:92bca02df485 | 1048 | Led1StateChanged = false; |
mluis | 1:352f608c3337 | 1049 | SerialDisplayUpdateLedState( 1, Led1State ); |
mluis | 0:92bca02df485 | 1050 | } |
mluis | 0:92bca02df485 | 1051 | if( Led2StateChanged == true ) |
mluis | 0:92bca02df485 | 1052 | { |
mluis | 0:92bca02df485 | 1053 | Led2StateChanged = false; |
mluis | 1:352f608c3337 | 1054 | SerialDisplayUpdateLedState( 2, Led2State ); |
mluis | 0:92bca02df485 | 1055 | } |
mluis | 0:92bca02df485 | 1056 | if( Led3StateChanged == true ) |
mluis | 0:92bca02df485 | 1057 | { |
mluis | 0:92bca02df485 | 1058 | Led3StateChanged = false; |
mluis | 0:92bca02df485 | 1059 | SerialDisplayUpdateLedState( 3, AppLedStateOn ); |
mluis | 0:92bca02df485 | 1060 | } |
mluis | 3:9c6f7f082151 | 1061 | if( UplinkStatusUpdated == true ) |
mluis | 0:92bca02df485 | 1062 | { |
mluis | 3:9c6f7f082151 | 1063 | UplinkStatusUpdated = false; |
mluis | 3:9c6f7f082151 | 1064 | SerialDisplayUpdateUplink( LoRaMacUplinkStatus.Acked, LoRaMacUplinkStatus.Datarate, LoRaMacUplinkStatus.UplinkCounter, LoRaMacUplinkStatus.Port, LoRaMacUplinkStatus.Buffer, LoRaMacUplinkStatus.BufferSize ); |
mluis | 3:9c6f7f082151 | 1065 | } |
mluis | 3:9c6f7f082151 | 1066 | if( DownlinkStatusUpdated == true ) |
mluis | 3:9c6f7f082151 | 1067 | { |
mluis | 3:9c6f7f082151 | 1068 | DownlinkStatusUpdated = false; |
mluis | 1:352f608c3337 | 1069 | SerialDisplayUpdateLedState( 2, Led2State ); |
mluis | 0:92bca02df485 | 1070 | SerialDisplayUpdateDownlink( LoRaMacDownlinkStatus.RxData, LoRaMacDownlinkStatus.Rssi, LoRaMacDownlinkStatus.Snr, LoRaMacDownlinkStatus.DownlinkCounter, LoRaMacDownlinkStatus.Port, LoRaMacDownlinkStatus.Buffer, LoRaMacDownlinkStatus.BufferSize ); |
mluis | 0:92bca02df485 | 1071 | } |
mluis | 3:9c6f7f082151 | 1072 | |
mluis | 3:9c6f7f082151 | 1073 | switch( DeviceState ) |
mluis | 0:92bca02df485 | 1074 | { |
mluis | 3:9c6f7f082151 | 1075 | case DEVICE_STATE_INIT: |
mluis | 3:9c6f7f082151 | 1076 | { |
mluis | 3:9c6f7f082151 | 1077 | LoRaMacPrimitives.MacMcpsConfirm = McpsConfirm; |
mluis | 3:9c6f7f082151 | 1078 | LoRaMacPrimitives.MacMcpsIndication = McpsIndication; |
mluis | 3:9c6f7f082151 | 1079 | LoRaMacPrimitives.MacMlmeConfirm = MlmeConfirm; |
mluis | 3:9c6f7f082151 | 1080 | LoRaMacCallbacks.GetBatteryLevel = BoardGetBatteryLevel; |
mluis | 3:9c6f7f082151 | 1081 | LoRaMacInitialization( &LoRaMacPrimitives, &LoRaMacCallbacks ); |
mluis | 3:9c6f7f082151 | 1082 | |
mluis | 3:9c6f7f082151 | 1083 | TimerInit( &TxNextPacketTimer, OnTxNextPacketTimerEvent ); |
mluis | 3:9c6f7f082151 | 1084 | |
mluis | 3:9c6f7f082151 | 1085 | TimerInit( &Led1Timer, OnLed1TimerEvent ); |
mluis | 9:ee9dcbb9708d | 1086 | TimerSetValue( &Led1Timer, 25 ); |
mluis | 3:9c6f7f082151 | 1087 | |
mluis | 3:9c6f7f082151 | 1088 | TimerInit( &Led2Timer, OnLed2TimerEvent ); |
mluis | 9:ee9dcbb9708d | 1089 | TimerSetValue( &Led2Timer, 25 ); |
mluis | 3:9c6f7f082151 | 1090 | |
mluis | 3:9c6f7f082151 | 1091 | mibReq.Type = MIB_ADR; |
mluis | 3:9c6f7f082151 | 1092 | mibReq.Param.AdrEnable = LORAWAN_ADR_ON; |
mluis | 3:9c6f7f082151 | 1093 | LoRaMacMibSetRequestConfirm( &mibReq ); |
mluis | 3:9c6f7f082151 | 1094 | |
mluis | 3:9c6f7f082151 | 1095 | mibReq.Type = MIB_PUBLIC_NETWORK; |
mluis | 3:9c6f7f082151 | 1096 | mibReq.Param.EnablePublicNetwork = LORAWAN_PUBLIC_NETWORK; |
mluis | 3:9c6f7f082151 | 1097 | LoRaMacMibSetRequestConfirm( &mibReq ); |
mluis | 3:9c6f7f082151 | 1098 | |
mluis | 3:9c6f7f082151 | 1099 | #if defined( USE_BAND_868 ) |
mluis | 3:9c6f7f082151 | 1100 | LoRaMacTestSetDutyCycleOn( LORAWAN_DUTYCYCLE_ON ); |
mluis | 3:9c6f7f082151 | 1101 | SerialDisplayUpdateDutyCycle( LORAWAN_DUTYCYCLE_ON ); |
mluis | 5:1e9f6a365854 | 1102 | |
mluis | 9:ee9dcbb9708d | 1103 | #if( USE_SEMTECH_DEFAULT_CHANNEL_LINEUP == 1 ) |
mluis | 5:1e9f6a365854 | 1104 | LoRaMacChannelAdd( 3, ( ChannelParams_t )LC4 ); |
mluis | 5:1e9f6a365854 | 1105 | LoRaMacChannelAdd( 4, ( ChannelParams_t )LC5 ); |
mluis | 5:1e9f6a365854 | 1106 | LoRaMacChannelAdd( 5, ( ChannelParams_t )LC6 ); |
mluis | 5:1e9f6a365854 | 1107 | LoRaMacChannelAdd( 6, ( ChannelParams_t )LC7 ); |
mluis | 5:1e9f6a365854 | 1108 | LoRaMacChannelAdd( 7, ( ChannelParams_t )LC8 ); |
mluis | 5:1e9f6a365854 | 1109 | LoRaMacChannelAdd( 8, ( ChannelParams_t )LC9 ); |
mluis | 5:1e9f6a365854 | 1110 | LoRaMacChannelAdd( 9, ( ChannelParams_t )LC10 ); |
mluis | 7:3173f0508a98 | 1111 | |
mluis | 9:ee9dcbb9708d | 1112 | mibReq.Type = MIB_RX2_DEFAULT_CHANNEL; |
mluis | 9:ee9dcbb9708d | 1113 | mibReq.Param.Rx2DefaultChannel = ( Rx2ChannelParams_t ){ 869525000, DR_3 }; |
mluis | 9:ee9dcbb9708d | 1114 | LoRaMacMibSetRequestConfirm( &mibReq ); |
mluis | 9:ee9dcbb9708d | 1115 | |
mluis | 7:3173f0508a98 | 1116 | mibReq.Type = MIB_RX2_CHANNEL; |
mluis | 7:3173f0508a98 | 1117 | mibReq.Param.Rx2Channel = ( Rx2ChannelParams_t ){ 869525000, DR_3 }; |
mluis | 7:3173f0508a98 | 1118 | LoRaMacMibSetRequestConfirm( &mibReq ); |
mluis | 5:1e9f6a365854 | 1119 | #endif |
mluis | 5:1e9f6a365854 | 1120 | |
mluis | 3:9c6f7f082151 | 1121 | #endif |
mluis | 3:9c6f7f082151 | 1122 | SerialDisplayUpdateActivationMode( OVER_THE_AIR_ACTIVATION ); |
mluis | 3:9c6f7f082151 | 1123 | SerialDisplayUpdateAdr( LORAWAN_ADR_ON ); |
mluis | 3:9c6f7f082151 | 1124 | SerialDisplayUpdatePublicNetwork( LORAWAN_PUBLIC_NETWORK ); |
mluis | 3:9c6f7f082151 | 1125 | |
mluis | 3:9c6f7f082151 | 1126 | LoRaMacDownlinkStatus.DownlinkCounter = 0; |
mluis | 3:9c6f7f082151 | 1127 | |
mluis | 3:9c6f7f082151 | 1128 | DeviceState = DEVICE_STATE_JOIN; |
mluis | 3:9c6f7f082151 | 1129 | break; |
mluis | 3:9c6f7f082151 | 1130 | } |
mluis | 3:9c6f7f082151 | 1131 | case DEVICE_STATE_JOIN: |
mluis | 3:9c6f7f082151 | 1132 | { |
mluis | 3:9c6f7f082151 | 1133 | #if( OVER_THE_AIR_ACTIVATION != 0 ) |
mluis | 3:9c6f7f082151 | 1134 | MlmeReq_t mlmeReq; |
mluis | 3:9c6f7f082151 | 1135 | |
mluis | 3:9c6f7f082151 | 1136 | mlmeReq.Type = MLME_JOIN; |
mluis | 3:9c6f7f082151 | 1137 | |
mluis | 3:9c6f7f082151 | 1138 | mlmeReq.Req.Join.DevEui = DevEui; |
mluis | 3:9c6f7f082151 | 1139 | mlmeReq.Req.Join.AppEui = AppEui; |
mluis | 3:9c6f7f082151 | 1140 | mlmeReq.Req.Join.AppKey = AppKey; |
mluis | 3:9c6f7f082151 | 1141 | |
mluis | 3:9c6f7f082151 | 1142 | if( NextTx == true ) |
mluis | 3:9c6f7f082151 | 1143 | { |
mluis | 3:9c6f7f082151 | 1144 | LoRaMacMlmeRequest( &mlmeReq ); |
mluis | 3:9c6f7f082151 | 1145 | } |
mluis | 7:3173f0508a98 | 1146 | DeviceState = DEVICE_STATE_SLEEP; |
mluis | 3:9c6f7f082151 | 1147 | #else |
mluis | 3:9c6f7f082151 | 1148 | mibReq.Type = MIB_NET_ID; |
mluis | 3:9c6f7f082151 | 1149 | mibReq.Param.NetID = LORAWAN_NETWORK_ID; |
mluis | 3:9c6f7f082151 | 1150 | LoRaMacMibSetRequestConfirm( &mibReq ); |
mluis | 3:9c6f7f082151 | 1151 | |
mluis | 3:9c6f7f082151 | 1152 | mibReq.Type = MIB_DEV_ADDR; |
mluis | 3:9c6f7f082151 | 1153 | mibReq.Param.DevAddr = DevAddr; |
mluis | 3:9c6f7f082151 | 1154 | LoRaMacMibSetRequestConfirm( &mibReq ); |
mluis | 3:9c6f7f082151 | 1155 | |
mluis | 3:9c6f7f082151 | 1156 | mibReq.Type = MIB_NWK_SKEY; |
mluis | 3:9c6f7f082151 | 1157 | mibReq.Param.NwkSKey = NwkSKey; |
mluis | 3:9c6f7f082151 | 1158 | LoRaMacMibSetRequestConfirm( &mibReq ); |
mluis | 3:9c6f7f082151 | 1159 | |
mluis | 3:9c6f7f082151 | 1160 | mibReq.Type = MIB_APP_SKEY; |
mluis | 3:9c6f7f082151 | 1161 | mibReq.Param.AppSKey = AppSKey; |
mluis | 3:9c6f7f082151 | 1162 | LoRaMacMibSetRequestConfirm( &mibReq ); |
mluis | 3:9c6f7f082151 | 1163 | |
mluis | 3:9c6f7f082151 | 1164 | mibReq.Type = MIB_NETWORK_JOINED; |
mluis | 3:9c6f7f082151 | 1165 | mibReq.Param.IsNetworkJoined = true; |
mluis | 3:9c6f7f082151 | 1166 | LoRaMacMibSetRequestConfirm( &mibReq ); |
mluis | 3:9c6f7f082151 | 1167 | |
mluis | 3:9c6f7f082151 | 1168 | DeviceState = DEVICE_STATE_SEND; |
mluis | 3:9c6f7f082151 | 1169 | #endif |
mluis | 3:9c6f7f082151 | 1170 | IsNetworkJoinedStatusUpdate = true; |
mluis | 3:9c6f7f082151 | 1171 | break; |
mluis | 3:9c6f7f082151 | 1172 | } |
mluis | 3:9c6f7f082151 | 1173 | case DEVICE_STATE_SEND: |
mluis | 3:9c6f7f082151 | 1174 | { |
mluis | 3:9c6f7f082151 | 1175 | if( NextTx == true ) |
mluis | 3:9c6f7f082151 | 1176 | { |
mluis | 3:9c6f7f082151 | 1177 | SerialDisplayUpdateUplinkAcked( false ); |
mluis | 3:9c6f7f082151 | 1178 | SerialDisplayUpdateDonwlinkRxData( false ); |
mluis | 3:9c6f7f082151 | 1179 | PrepareTxFrame( AppPort ); |
mluis | 3:9c6f7f082151 | 1180 | |
mluis | 3:9c6f7f082151 | 1181 | NextTx = SendFrame( ); |
mluis | 3:9c6f7f082151 | 1182 | } |
mluis | 3:9c6f7f082151 | 1183 | if( ComplianceTest.Running == true ) |
mluis | 3:9c6f7f082151 | 1184 | { |
mluis | 5:1e9f6a365854 | 1185 | // Schedule next packet transmission |
mluis | 9:ee9dcbb9708d | 1186 | TxDutyCycleTime = 5000; // 5000 ms |
mluis | 3:9c6f7f082151 | 1187 | } |
mluis | 3:9c6f7f082151 | 1188 | else |
mluis | 3:9c6f7f082151 | 1189 | { |
mluis | 3:9c6f7f082151 | 1190 | // Schedule next packet transmission |
mluis | 3:9c6f7f082151 | 1191 | TxDutyCycleTime = APP_TX_DUTYCYCLE + randr( -APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND ); |
mluis | 3:9c6f7f082151 | 1192 | } |
mluis | 3:9c6f7f082151 | 1193 | DeviceState = DEVICE_STATE_CYCLE; |
mluis | 3:9c6f7f082151 | 1194 | break; |
mluis | 3:9c6f7f082151 | 1195 | } |
mluis | 3:9c6f7f082151 | 1196 | case DEVICE_STATE_CYCLE: |
mluis | 3:9c6f7f082151 | 1197 | { |
uss1994 | 10:9a4efdd07a77 | 1198 | // DeviceState = DEVICE_STATE_SLEEP; |
mluis | 5:1e9f6a365854 | 1199 | |
mluis | 3:9c6f7f082151 | 1200 | // Schedule next packet transmission |
uss1994 | 10:9a4efdd07a77 | 1201 | // TimerSetValue( &TxNextPacketTimer, TxDutyCycleTime ); |
uss1994 | 10:9a4efdd07a77 | 1202 | // TimerStart( &TxNextPacketTimer ); |
uss1994 | 10:9a4efdd07a77 | 1203 | |
uss1994 | 10:9a4efdd07a77 | 1204 | |
uss1994 | 10:9a4efdd07a77 | 1205 | |
uss1994 | 10:9a4efdd07a77 | 1206 | // Is a test is running |
uss1994 | 10:9a4efdd07a77 | 1207 | if (running_test) { |
uss1994 | 10:9a4efdd07a77 | 1208 | // If it is |
uss1994 | 10:9a4efdd07a77 | 1209 | //check if it's time to take the next interval measurement |
uss1994 | 10:9a4efdd07a77 | 1210 | if (current_time - last_measurement >= measurement_interval) { |
uss1994 | 10:9a4efdd07a77 | 1211 | if (measureCount <= available_slots) { |
uss1994 | 10:9a4efdd07a77 | 1212 | measureCount = takeMeasurement(measurements, measureCount); |
uss1994 | 10:9a4efdd07a77 | 1213 | } |
uss1994 | 10:9a4efdd07a77 | 1214 | last_measurement = current_time; |
uss1994 | 10:9a4efdd07a77 | 1215 | } |
uss1994 | 10:9a4efdd07a77 | 1216 | |
uss1994 | 10:9a4efdd07a77 | 1217 | //check if it's time to end the test |
uss1994 | 10:9a4efdd07a77 | 1218 | if (current_time - test_start >= test_duration) { |
uss1994 | 10:9a4efdd07a77 | 1219 | endTest(measurements); |
uss1994 | 10:9a4efdd07a77 | 1220 | // sendResult(measurements, ttype); |
uss1994 | 10:9a4efdd07a77 | 1221 | DeviceState = DEVICE_STATE_SEND; |
uss1994 | 10:9a4efdd07a77 | 1222 | last_hb = current_time; |
uss1994 | 10:9a4efdd07a77 | 1223 | |
uss1994 | 10:9a4efdd07a77 | 1224 | // Set the times for the next tests |
uss1994 | 10:9a4efdd07a77 | 1225 | switch ( MessageType ) |
uss1994 | 10:9a4efdd07a77 | 1226 | { |
uss1994 | 10:9a4efdd07a77 | 1227 | case MESSAGE_TYPE_SHORT_TEST: |
uss1994 | 10:9a4efdd07a77 | 1228 | { |
uss1994 | 10:9a4efdd07a77 | 1229 | next_stest = test_start + test_interval; |
uss1994 | 10:9a4efdd07a77 | 1230 | break; |
uss1994 | 10:9a4efdd07a77 | 1231 | } |
uss1994 | 10:9a4efdd07a77 | 1232 | default: |
uss1994 | 10:9a4efdd07a77 | 1233 | { |
uss1994 | 10:9a4efdd07a77 | 1234 | // If long test, reset both test types, |
uss1994 | 10:9a4efdd07a77 | 1235 | // to prevent two tests at the same time |
uss1994 | 10:9a4efdd07a77 | 1236 | next_stest = test_start + test_interval; |
uss1994 | 10:9a4efdd07a77 | 1237 | next_ltest = test_start + long_interval; |
uss1994 | 10:9a4efdd07a77 | 1238 | break; |
uss1994 | 10:9a4efdd07a77 | 1239 | } |
uss1994 | 10:9a4efdd07a77 | 1240 | } |
uss1994 | 10:9a4efdd07a77 | 1241 | |
uss1994 | 10:9a4efdd07a77 | 1242 | running_test = false; |
uss1994 | 10:9a4efdd07a77 | 1243 | } |
uss1994 | 10:9a4efdd07a77 | 1244 | } else { |
uss1994 | 10:9a4efdd07a77 | 1245 | // If it isn't |
uss1994 | 10:9a4efdd07a77 | 1246 | |
uss1994 | 10:9a4efdd07a77 | 1247 | // check if downlink action is required |
uss1994 | 10:9a4efdd07a77 | 1248 | |
uss1994 | 10:9a4efdd07a77 | 1249 | //if (received_downlink == true) { |
uss1994 | 10:9a4efdd07a77 | 1250 | // dl_handler(received_data); |
uss1994 | 10:9a4efdd07a77 | 1251 | //} |
uss1994 | 10:9a4efdd07a77 | 1252 | |
uss1994 | 11:e433cbbfd436 | 1253 | //check if button is pressed |
uss1994 | 11:e433cbbfd436 | 1254 | if (button1_pressed) { // Set when button is pressed |
uss1994 | 11:e433cbbfd436 | 1255 | if (press_count == 0) { |
uss1994 | 11:e433cbbfd436 | 1256 | buttonOptions_timeout.attach(callback(button_options_handler), 2); // Debounce time 300 ms |
uss1994 | 11:e433cbbfd436 | 1257 | } |
uss1994 | 11:e433cbbfd436 | 1258 | button1_pressed = false; |
uss1994 | 11:e433cbbfd436 | 1259 | press_count++; |
uss1994 | 11:e433cbbfd436 | 1260 | } |
uss1994 | 11:e433cbbfd436 | 1261 | |
uss1994 | 10:9a4efdd07a77 | 1262 | //check if it's time to run a test |
uss1994 | 10:9a4efdd07a77 | 1263 | if (current_time >= next_stest || current_time >= next_ltest) { |
uss1994 | 10:9a4efdd07a77 | 1264 | |
uss1994 | 10:9a4efdd07a77 | 1265 | // Check what kind of test to run |
uss1994 | 10:9a4efdd07a77 | 1266 | if (current_time >= next_ltest) { |
uss1994 | 10:9a4efdd07a77 | 1267 | MessageType = MESSAGE_TYPE_LONG_TEST; |
uss1994 | 10:9a4efdd07a77 | 1268 | measurements = l_measurements; |
uss1994 | 10:9a4efdd07a77 | 1269 | } else { |
uss1994 | 10:9a4efdd07a77 | 1270 | MessageType = MESSAGE_TYPE_SHORT_TEST; |
uss1994 | 10:9a4efdd07a77 | 1271 | measurements = s_measurements; |
uss1994 | 10:9a4efdd07a77 | 1272 | } |
uss1994 | 10:9a4efdd07a77 | 1273 | |
uss1994 | 10:9a4efdd07a77 | 1274 | startTest(measurements); |
uss1994 | 10:9a4efdd07a77 | 1275 | |
uss1994 | 10:9a4efdd07a77 | 1276 | running_test = true; |
uss1994 | 10:9a4efdd07a77 | 1277 | test_start = current_time; |
uss1994 | 10:9a4efdd07a77 | 1278 | last_measurement = current_time; |
uss1994 | 10:9a4efdd07a77 | 1279 | |
uss1994 | 10:9a4efdd07a77 | 1280 | |
uss1994 | 10:9a4efdd07a77 | 1281 | //check if it's time for a heartbeat |
uss1994 | 10:9a4efdd07a77 | 1282 | } else if (current_time - last_hb >= hb_interval) { |
uss1994 | 10:9a4efdd07a77 | 1283 | // sendHb(); |
uss1994 | 10:9a4efdd07a77 | 1284 | DeviceState = DEVICE_STATE_SEND; |
uss1994 | 10:9a4efdd07a77 | 1285 | MessageType = MESSAGE_TYPE_HB; |
uss1994 | 10:9a4efdd07a77 | 1286 | last_hb = current_time; |
uss1994 | 10:9a4efdd07a77 | 1287 | } |
uss1994 | 10:9a4efdd07a77 | 1288 | } |
uss1994 | 10:9a4efdd07a77 | 1289 | |
uss1994 | 10:9a4efdd07a77 | 1290 | |
mluis | 3:9c6f7f082151 | 1291 | break; |
mluis | 3:9c6f7f082151 | 1292 | } |
mluis | 3:9c6f7f082151 | 1293 | case DEVICE_STATE_SLEEP: |
mluis | 3:9c6f7f082151 | 1294 | { |
mluis | 3:9c6f7f082151 | 1295 | // Wake up through events |
mluis | 3:9c6f7f082151 | 1296 | break; |
mluis | 3:9c6f7f082151 | 1297 | } |
mluis | 3:9c6f7f082151 | 1298 | default: |
mluis | 3:9c6f7f082151 | 1299 | { |
mluis | 3:9c6f7f082151 | 1300 | DeviceState = DEVICE_STATE_INIT; |
mluis | 3:9c6f7f082151 | 1301 | break; |
mluis | 3:9c6f7f082151 | 1302 | } |
mluis | 0:92bca02df485 | 1303 | } |
mluis | 0:92bca02df485 | 1304 | } |
mluis | 0:92bca02df485 | 1305 | } |