CY Lim
/
mbed-os-example-lorawan
WPG
Fork of mbed-os-example-mbed5-lorawan by
main.cpp@30:99137c499371, 2018-07-27 (annotated)
- Committer:
- limchy
- Date:
- Fri Jul 27 03:36:51 2018 +0000
- Revision:
- 30:99137c499371
- Parent:
- 26:f07f5febf97f
Ver1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 0:7037ed05f54f | 1 | /** |
mbed_official | 0:7037ed05f54f | 2 | * Copyright (c) 2017, Arm Limited and affiliates. |
mbed_official | 0:7037ed05f54f | 3 | * SPDX-License-Identifier: Apache-2.0 |
mbed_official | 0:7037ed05f54f | 4 | * |
mbed_official | 0:7037ed05f54f | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mbed_official | 0:7037ed05f54f | 6 | * you may not use this file except in compliance with the License. |
mbed_official | 0:7037ed05f54f | 7 | * You may obtain a copy of the License at |
mbed_official | 0:7037ed05f54f | 8 | * |
mbed_official | 0:7037ed05f54f | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
mbed_official | 0:7037ed05f54f | 10 | * |
mbed_official | 0:7037ed05f54f | 11 | * Unless required by applicable law or agreed to in writing, software |
mbed_official | 0:7037ed05f54f | 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
mbed_official | 0:7037ed05f54f | 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mbed_official | 0:7037ed05f54f | 14 | * See the License for the specific language governing permissions and |
mbed_official | 0:7037ed05f54f | 15 | * limitations under the License. |
mbed_official | 0:7037ed05f54f | 16 | */ |
mbed_official | 0:7037ed05f54f | 17 | #include <stdio.h> |
mbed_official | 3:8c7198d1a2a1 | 18 | |
mbed_official | 0:7037ed05f54f | 19 | #include "lorawan/LoRaWANInterface.h" |
mbed_official | 0:7037ed05f54f | 20 | #include "lorawan/system/lorawan_data_structures.h" |
mbed_official | 0:7037ed05f54f | 21 | #include "events/EventQueue.h" |
mbed_official | 0:7037ed05f54f | 22 | |
mbed_official | 0:7037ed05f54f | 23 | // Application helpers |
mbed_official | 0:7037ed05f54f | 24 | #include "DummySensor.h" |
mbed_official | 0:7037ed05f54f | 25 | #include "trace_helper.h" |
mbed_official | 0:7037ed05f54f | 26 | #include "lora_radio_helper.h" |
mbed_official | 0:7037ed05f54f | 27 | |
mbed_official | 0:7037ed05f54f | 28 | using namespace events; |
mbed_official | 0:7037ed05f54f | 29 | |
mbed_official | 12:5015dfead3f2 | 30 | // Max payload size can be LORAMAC_PHY_MAXPAYLOAD. |
mbed_official | 12:5015dfead3f2 | 31 | // This example only communicates with much shorter messages (<30 bytes). |
mbed_official | 12:5015dfead3f2 | 32 | // If longer messages are used, these buffers must be changed accordingly. |
mbed_official | 12:5015dfead3f2 | 33 | uint8_t tx_buffer[30]; |
mbed_official | 12:5015dfead3f2 | 34 | uint8_t rx_buffer[30]; |
mbed_official | 0:7037ed05f54f | 35 | |
mbed_official | 0:7037ed05f54f | 36 | /* |
mbed_official | 0:7037ed05f54f | 37 | * Sets up an application dependent transmission timer in ms. Used only when Duty Cycling is off for testing |
mbed_official | 0:7037ed05f54f | 38 | */ |
mbed_official | 0:7037ed05f54f | 39 | #define TX_TIMER 10000 |
mbed_official | 0:7037ed05f54f | 40 | |
mbed_official | 0:7037ed05f54f | 41 | /** |
mbed_official | 0:7037ed05f54f | 42 | * Maximum number of events for the event queue. |
mbed_official | 12:5015dfead3f2 | 43 | * 10 is the safe number for the stack events, however, if application |
mbed_official | 0:7037ed05f54f | 44 | * also uses the queue for whatever purposes, this number should be increased. |
mbed_official | 0:7037ed05f54f | 45 | */ |
mbed_official | 12:5015dfead3f2 | 46 | #define MAX_NUMBER_OF_EVENTS 10 |
mbed_official | 0:7037ed05f54f | 47 | |
mbed_official | 0:7037ed05f54f | 48 | /** |
mbed_official | 0:7037ed05f54f | 49 | * Maximum number of retries for CONFIRMED messages before giving up |
mbed_official | 0:7037ed05f54f | 50 | */ |
mbed_official | 0:7037ed05f54f | 51 | #define CONFIRMED_MSG_RETRY_COUNTER 3 |
mbed_official | 0:7037ed05f54f | 52 | |
mbed_official | 0:7037ed05f54f | 53 | /** |
mbed_official | 0:7037ed05f54f | 54 | * Dummy pin for dummy sensor |
mbed_official | 0:7037ed05f54f | 55 | */ |
mbed_official | 0:7037ed05f54f | 56 | #define PC_9 0 |
mbed_official | 0:7037ed05f54f | 57 | |
mbed_official | 0:7037ed05f54f | 58 | /** |
mbed_official | 0:7037ed05f54f | 59 | * Dummy sensor class object |
mbed_official | 0:7037ed05f54f | 60 | */ |
mbed_official | 0:7037ed05f54f | 61 | DS1820 ds1820(PC_9); |
mbed_official | 0:7037ed05f54f | 62 | |
mbed_official | 0:7037ed05f54f | 63 | /** |
mbed_official | 0:7037ed05f54f | 64 | * This event queue is the global event queue for both the |
mbed_official | 0:7037ed05f54f | 65 | * application and stack. To conserve memory, the stack is designed to run |
mbed_official | 0:7037ed05f54f | 66 | * in the same thread as the application and the application is responsible for |
mbed_official | 0:7037ed05f54f | 67 | * providing an event queue to the stack that will be used for ISR deferment as |
mbed_official | 0:7037ed05f54f | 68 | * well as application information event queuing. |
mbed_official | 0:7037ed05f54f | 69 | */ |
mbed_official | 0:7037ed05f54f | 70 | static EventQueue ev_queue(MAX_NUMBER_OF_EVENTS * EVENTS_EVENT_SIZE); |
mbed_official | 0:7037ed05f54f | 71 | |
mbed_official | 0:7037ed05f54f | 72 | /** |
mbed_official | 0:7037ed05f54f | 73 | * Event handler. |
mbed_official | 0:7037ed05f54f | 74 | * |
mbed_official | 0:7037ed05f54f | 75 | * This will be passed to the LoRaWAN stack to queue events for the |
mbed_official | 0:7037ed05f54f | 76 | * application which in turn drive the application. |
mbed_official | 0:7037ed05f54f | 77 | */ |
mbed_official | 0:7037ed05f54f | 78 | static void lora_event_handler(lorawan_event_t event); |
mbed_official | 0:7037ed05f54f | 79 | |
mbed_official | 0:7037ed05f54f | 80 | /** |
mbed_official | 2:dc95ac6d6d4e | 81 | * Constructing Mbed LoRaWANInterface and passing it down the radio object. |
mbed_official | 0:7037ed05f54f | 82 | */ |
mbed_official | 2:dc95ac6d6d4e | 83 | static LoRaWANInterface lorawan(radio); |
mbed_official | 0:7037ed05f54f | 84 | |
mbed_official | 0:7037ed05f54f | 85 | /** |
mbed_official | 0:7037ed05f54f | 86 | * Application specific callbacks |
mbed_official | 0:7037ed05f54f | 87 | */ |
mbed_official | 0:7037ed05f54f | 88 | static lorawan_app_callbacks_t callbacks; |
mbed_official | 0:7037ed05f54f | 89 | |
mbed_official | 0:7037ed05f54f | 90 | /** |
mbed_official | 0:7037ed05f54f | 91 | * Entry point for application |
mbed_official | 0:7037ed05f54f | 92 | */ |
mbed_official | 0:7037ed05f54f | 93 | int main (void) |
mbed_official | 0:7037ed05f54f | 94 | { |
mbed_official | 0:7037ed05f54f | 95 | // setup tracing |
mbed_official | 0:7037ed05f54f | 96 | setup_trace(); |
mbed_official | 0:7037ed05f54f | 97 | |
mbed_official | 0:7037ed05f54f | 98 | // stores the status of a call to LoRaWAN protocol |
mbed_official | 0:7037ed05f54f | 99 | lorawan_status_t retcode; |
mbed_official | 0:7037ed05f54f | 100 | |
mbed_official | 0:7037ed05f54f | 101 | // Initialize LoRaWAN stack |
mbed_official | 2:dc95ac6d6d4e | 102 | if (lorawan.initialize(&ev_queue) != LORAWAN_STATUS_OK) { |
mbed_official | 0:7037ed05f54f | 103 | printf("\r\n LoRa initialization failed! \r\n"); |
mbed_official | 0:7037ed05f54f | 104 | return -1; |
mbed_official | 0:7037ed05f54f | 105 | } |
mbed_official | 0:7037ed05f54f | 106 | |
mbed_official | 0:7037ed05f54f | 107 | printf("\r\n Mbed LoRaWANStack initialized \r\n"); |
mbed_official | 0:7037ed05f54f | 108 | |
mbed_official | 0:7037ed05f54f | 109 | // prepare application callbacks |
mbed_official | 0:7037ed05f54f | 110 | callbacks.events = mbed::callback(lora_event_handler); |
mbed_official | 2:dc95ac6d6d4e | 111 | lorawan.add_app_callbacks(&callbacks); |
mbed_official | 0:7037ed05f54f | 112 | |
mbed_official | 0:7037ed05f54f | 113 | // Set number of retries in case of CONFIRMED messages |
mbed_official | 2:dc95ac6d6d4e | 114 | if (lorawan.set_confirmed_msg_retries(CONFIRMED_MSG_RETRY_COUNTER) |
mbed_official | 0:7037ed05f54f | 115 | != LORAWAN_STATUS_OK) { |
mbed_official | 0:7037ed05f54f | 116 | printf("\r\n set_confirmed_msg_retries failed! \r\n\r\n"); |
mbed_official | 0:7037ed05f54f | 117 | return -1; |
mbed_official | 0:7037ed05f54f | 118 | } |
mbed_official | 0:7037ed05f54f | 119 | |
mbed_official | 0:7037ed05f54f | 120 | printf("\r\n CONFIRMED message retries : %d \r\n", |
mbed_official | 0:7037ed05f54f | 121 | CONFIRMED_MSG_RETRY_COUNTER); |
mbed_official | 0:7037ed05f54f | 122 | |
mbed_official | 0:7037ed05f54f | 123 | // Enable adaptive data rate |
mbed_official | 2:dc95ac6d6d4e | 124 | if (lorawan.enable_adaptive_datarate() != LORAWAN_STATUS_OK) { |
mbed_official | 0:7037ed05f54f | 125 | printf("\r\n enable_adaptive_datarate failed! \r\n"); |
mbed_official | 0:7037ed05f54f | 126 | return -1; |
mbed_official | 0:7037ed05f54f | 127 | } |
mbed_official | 0:7037ed05f54f | 128 | |
mbed_official | 0:7037ed05f54f | 129 | printf("\r\n Adaptive data rate (ADR) - Enabled \r\n"); |
mbed_official | 0:7037ed05f54f | 130 | |
mbed_official | 2:dc95ac6d6d4e | 131 | retcode = lorawan.connect(); |
mbed_official | 0:7037ed05f54f | 132 | |
mbed_official | 0:7037ed05f54f | 133 | if (retcode == LORAWAN_STATUS_OK || |
mbed_official | 0:7037ed05f54f | 134 | retcode == LORAWAN_STATUS_CONNECT_IN_PROGRESS) { |
mbed_official | 0:7037ed05f54f | 135 | } else { |
mbed_official | 0:7037ed05f54f | 136 | printf("\r\n Connection error, code = %d \r\n", retcode); |
mbed_official | 0:7037ed05f54f | 137 | return -1; |
mbed_official | 0:7037ed05f54f | 138 | } |
mbed_official | 0:7037ed05f54f | 139 | |
mbed_official | 0:7037ed05f54f | 140 | printf("\r\n Connection - In Progress ...\r\n"); |
mbed_official | 0:7037ed05f54f | 141 | |
mbed_official | 0:7037ed05f54f | 142 | // make your event queue dispatching events forever |
mbed_official | 0:7037ed05f54f | 143 | ev_queue.dispatch_forever(); |
mbed_official | 3:8c7198d1a2a1 | 144 | |
mbed_official | 3:8c7198d1a2a1 | 145 | return 0; |
mbed_official | 0:7037ed05f54f | 146 | } |
mbed_official | 0:7037ed05f54f | 147 | |
mbed_official | 0:7037ed05f54f | 148 | /** |
mbed_official | 0:7037ed05f54f | 149 | * Sends a message to the Network Server |
mbed_official | 0:7037ed05f54f | 150 | */ |
mbed_official | 0:7037ed05f54f | 151 | static void send_message() |
mbed_official | 0:7037ed05f54f | 152 | { |
mbed_official | 0:7037ed05f54f | 153 | uint16_t packet_len; |
mbed_official | 0:7037ed05f54f | 154 | int16_t retcode; |
mbed_official | 0:7037ed05f54f | 155 | float sensor_value; |
mbed_official | 0:7037ed05f54f | 156 | |
mbed_official | 0:7037ed05f54f | 157 | if (ds1820.begin()) { |
mbed_official | 0:7037ed05f54f | 158 | ds1820.startConversion(); |
mbed_official | 0:7037ed05f54f | 159 | sensor_value = ds1820.read(); |
mbed_official | 0:7037ed05f54f | 160 | printf("\r\n Dummy Sensor Value = %3.1f \r\n", sensor_value); |
mbed_official | 0:7037ed05f54f | 161 | ds1820.startConversion(); |
mbed_official | 0:7037ed05f54f | 162 | } else { |
mbed_official | 0:7037ed05f54f | 163 | printf("\r\n No sensor found \r\n"); |
mbed_official | 0:7037ed05f54f | 164 | return; |
mbed_official | 0:7037ed05f54f | 165 | } |
mbed_official | 0:7037ed05f54f | 166 | |
mbed_official | 0:7037ed05f54f | 167 | packet_len = sprintf((char*) tx_buffer, "Dummy Sensor Value is %3.1f", |
mbed_official | 0:7037ed05f54f | 168 | sensor_value); |
mbed_official | 0:7037ed05f54f | 169 | |
mbed_official | 2:dc95ac6d6d4e | 170 | retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, tx_buffer, packet_len, |
mbed_official | 0:7037ed05f54f | 171 | MSG_CONFIRMED_FLAG); |
mbed_official | 0:7037ed05f54f | 172 | |
mbed_official | 0:7037ed05f54f | 173 | if (retcode < 0) { |
mbed_official | 0:7037ed05f54f | 174 | retcode == LORAWAN_STATUS_WOULD_BLOCK ? printf("send - WOULD BLOCK\r\n") |
mbed_official | 0:7037ed05f54f | 175 | : printf("\r\n send() - Error code %d \r\n", retcode); |
mbed_official | 26:f07f5febf97f | 176 | |
mbed_official | 26:f07f5febf97f | 177 | if (retcode == LORAWAN_STATUS_WOULD_BLOCK) { |
mbed_official | 26:f07f5febf97f | 178 | //retry in 3 seconds |
mbed_official | 26:f07f5febf97f | 179 | if (MBED_CONF_LORA_DUTY_CYCLE_ON) { |
mbed_official | 26:f07f5febf97f | 180 | ev_queue.call_in(3000, send_message); |
mbed_official | 26:f07f5febf97f | 181 | } |
mbed_official | 26:f07f5febf97f | 182 | } |
mbed_official | 0:7037ed05f54f | 183 | return; |
mbed_official | 0:7037ed05f54f | 184 | } |
mbed_official | 0:7037ed05f54f | 185 | |
mbed_official | 0:7037ed05f54f | 186 | printf("\r\n %d bytes scheduled for transmission \r\n", retcode); |
mbed_official | 12:5015dfead3f2 | 187 | memset(tx_buffer, 0, sizeof(tx_buffer)); |
mbed_official | 0:7037ed05f54f | 188 | } |
mbed_official | 0:7037ed05f54f | 189 | |
mbed_official | 0:7037ed05f54f | 190 | /** |
mbed_official | 0:7037ed05f54f | 191 | * Receive a message from the Network Server |
mbed_official | 0:7037ed05f54f | 192 | */ |
mbed_official | 0:7037ed05f54f | 193 | static void receive_message() |
mbed_official | 0:7037ed05f54f | 194 | { |
mbed_official | 0:7037ed05f54f | 195 | int16_t retcode; |
limchy | 30:99137c499371 | 196 | lorawan_rx_metadata metadata; |
mbed_official | 2:dc95ac6d6d4e | 197 | retcode = lorawan.receive(MBED_CONF_LORA_APP_PORT, rx_buffer, |
mbed_official | 12:5015dfead3f2 | 198 | sizeof(rx_buffer), |
mbed_official | 0:7037ed05f54f | 199 | MSG_CONFIRMED_FLAG|MSG_UNCONFIRMED_FLAG); |
mbed_official | 0:7037ed05f54f | 200 | |
mbed_official | 0:7037ed05f54f | 201 | if (retcode < 0) { |
mbed_official | 0:7037ed05f54f | 202 | printf("\r\n receive() - Error code %d \r\n", retcode); |
mbed_official | 0:7037ed05f54f | 203 | return; |
mbed_official | 0:7037ed05f54f | 204 | } |
mbed_official | 0:7037ed05f54f | 205 | |
mbed_official | 0:7037ed05f54f | 206 | printf(" Data:"); |
mbed_official | 0:7037ed05f54f | 207 | |
mbed_official | 0:7037ed05f54f | 208 | for (uint8_t i = 0; i < retcode; i++) { |
mbed_official | 0:7037ed05f54f | 209 | printf("%x", rx_buffer[i]); |
mbed_official | 0:7037ed05f54f | 210 | } |
mbed_official | 0:7037ed05f54f | 211 | |
mbed_official | 0:7037ed05f54f | 212 | printf("\r\n Data Length: %d\r\n", retcode); |
limchy | 30:99137c499371 | 213 | lorawan.get_rx_metadata(metadata); |
limchy | 30:99137c499371 | 214 | printf("\r\n RSSI: %d DR:%d SNR: %d\r\n", metadata.rssi,metadata.rx_datarate,metadata.snr); |
mbed_official | 12:5015dfead3f2 | 215 | memset(rx_buffer, 0, sizeof(rx_buffer)); |
mbed_official | 0:7037ed05f54f | 216 | } |
mbed_official | 0:7037ed05f54f | 217 | |
mbed_official | 0:7037ed05f54f | 218 | /** |
mbed_official | 0:7037ed05f54f | 219 | * Event handler |
mbed_official | 0:7037ed05f54f | 220 | */ |
mbed_official | 0:7037ed05f54f | 221 | static void lora_event_handler(lorawan_event_t event) |
mbed_official | 0:7037ed05f54f | 222 | { |
limchy | 30:99137c499371 | 223 | |
mbed_official | 0:7037ed05f54f | 224 | switch (event) { |
mbed_official | 0:7037ed05f54f | 225 | case CONNECTED: |
mbed_official | 0:7037ed05f54f | 226 | printf("\r\n Connection - Successful \r\n"); |
mbed_official | 0:7037ed05f54f | 227 | if (MBED_CONF_LORA_DUTY_CYCLE_ON) { |
mbed_official | 0:7037ed05f54f | 228 | send_message(); |
mbed_official | 0:7037ed05f54f | 229 | } else { |
mbed_official | 0:7037ed05f54f | 230 | ev_queue.call_every(TX_TIMER, send_message); |
mbed_official | 0:7037ed05f54f | 231 | } |
mbed_official | 0:7037ed05f54f | 232 | |
mbed_official | 0:7037ed05f54f | 233 | break; |
mbed_official | 0:7037ed05f54f | 234 | case DISCONNECTED: |
mbed_official | 0:7037ed05f54f | 235 | ev_queue.break_dispatch(); |
mbed_official | 0:7037ed05f54f | 236 | printf("\r\n Disconnected Successfully \r\n"); |
mbed_official | 0:7037ed05f54f | 237 | break; |
mbed_official | 0:7037ed05f54f | 238 | case TX_DONE: |
mbed_official | 0:7037ed05f54f | 239 | printf("\r\n Message Sent to Network Server \r\n"); |
mbed_official | 0:7037ed05f54f | 240 | if (MBED_CONF_LORA_DUTY_CYCLE_ON) { |
mbed_official | 0:7037ed05f54f | 241 | send_message(); |
mbed_official | 0:7037ed05f54f | 242 | } |
mbed_official | 0:7037ed05f54f | 243 | break; |
mbed_official | 0:7037ed05f54f | 244 | case TX_TIMEOUT: |
mbed_official | 0:7037ed05f54f | 245 | case TX_ERROR: |
mbed_official | 0:7037ed05f54f | 246 | case TX_CRYPTO_ERROR: |
mbed_official | 0:7037ed05f54f | 247 | case TX_SCHEDULING_ERROR: |
mbed_official | 0:7037ed05f54f | 248 | printf("\r\n Transmission Error - EventCode = %d \r\n", event); |
mbed_official | 0:7037ed05f54f | 249 | // try again |
mbed_official | 0:7037ed05f54f | 250 | if (MBED_CONF_LORA_DUTY_CYCLE_ON) { |
mbed_official | 0:7037ed05f54f | 251 | send_message(); |
mbed_official | 0:7037ed05f54f | 252 | } |
mbed_official | 0:7037ed05f54f | 253 | break; |
mbed_official | 0:7037ed05f54f | 254 | case RX_DONE: |
mbed_official | 0:7037ed05f54f | 255 | printf("\r\n Received message from Network Server \r\n"); |
limchy | 30:99137c499371 | 256 | |
mbed_official | 0:7037ed05f54f | 257 | receive_message(); |
mbed_official | 0:7037ed05f54f | 258 | break; |
mbed_official | 0:7037ed05f54f | 259 | case RX_TIMEOUT: |
mbed_official | 0:7037ed05f54f | 260 | case RX_ERROR: |
mbed_official | 0:7037ed05f54f | 261 | printf("\r\n Error in reception - Code = %d \r\n", event); |
mbed_official | 0:7037ed05f54f | 262 | break; |
mbed_official | 0:7037ed05f54f | 263 | case JOIN_FAILURE: |
mbed_official | 0:7037ed05f54f | 264 | printf("\r\n OTAA Failed - Check Keys \r\n"); |
mbed_official | 0:7037ed05f54f | 265 | break; |
mbed_official | 26:f07f5febf97f | 266 | case UPLINK_REQUIRED: |
mbed_official | 26:f07f5febf97f | 267 | printf("\r\n Uplink required by NS \r\n"); |
mbed_official | 26:f07f5febf97f | 268 | if (MBED_CONF_LORA_DUTY_CYCLE_ON) { |
mbed_official | 26:f07f5febf97f | 269 | send_message(); |
mbed_official | 26:f07f5febf97f | 270 | } |
mbed_official | 26:f07f5febf97f | 271 | break; |
mbed_official | 0:7037ed05f54f | 272 | default: |
mbed_official | 0:7037ed05f54f | 273 | MBED_ASSERT("Unknown Event"); |
mbed_official | 0:7037ed05f54f | 274 | } |
mbed_official | 0:7037ed05f54f | 275 | } |
mbed_official | 0:7037ed05f54f | 276 | |
mbed_official | 0:7037ed05f54f | 277 | // EOF |