Fabio Gatti
/
poc_lorawan
poc lorawan using disco_l475vg and sx1272mb2xas
main.cpp@49:bf339fabb590, 2019-03-14 (annotated)
- Committer:
- fabio_gatti
- Date:
- Thu Mar 14 22:05:11 2019 +0000
- Revision:
- 49:bf339fabb590
- Parent:
- 47:b6d132f1079f
initial relase.; LORAWAN poc using disco_l475vg and sx1272mb2xas
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 | |
fabio_gatti | 49:bf339fabb590 | 48 | |
fabio_gatti | 49:bf339fabb590 | 49 | // Maximum number of retries for CONFIRMED messages before giving up |
fabio_gatti | 49:bf339fabb590 | 50 | #define LORAWAN_CONFIRMED_MSG_RETRY_COUNTER 3 |
fabio_gatti | 49:bf339fabb590 | 51 | //DR_5=SF_7; DR_4=SF_8; DR_3=SF_9; DR_2=SF_10; DR_1=SF_11; DR_0=SF_12 |
fabio_gatti | 49:bf339fabb590 | 52 | #define LORAWAN_DATA_RATE DR_5 |
fabio_gatti | 49:bf339fabb590 | 53 | // app port |
fabio_gatti | 49:bf339fabb590 | 54 | #define LORAWAN_APP_PORT 15 |
fabio_gatti | 49:bf339fabb590 | 55 | // tx message type |
fabio_gatti | 49:bf339fabb590 | 56 | #define LORAWAN_TX_MSG_TYPE MSG_UNCONFIRMED_FLAG |
fabio_gatti | 49:bf339fabb590 | 57 | // number of channel |
fabio_gatti | 49:bf339fabb590 | 58 | #define LORAWAN_CHANNEL_NBR 3 |
fabio_gatti | 49:bf339fabb590 | 59 | // timeout retry when channel is block in msec |
fabio_gatti | 49:bf339fabb590 | 60 | #define LORAWAN_CHANNEL_RETRY 3000 |
fabio_gatti | 49:bf339fabb590 | 61 | |
fabio_gatti | 49:bf339fabb590 | 62 | |
mbed_official | 0:7037ed05f54f | 63 | |
mbed_official | 0:7037ed05f54f | 64 | /** |
mbed_official | 0:7037ed05f54f | 65 | * Dummy pin for dummy sensor |
mbed_official | 0:7037ed05f54f | 66 | */ |
mbed_official | 0:7037ed05f54f | 67 | #define PC_9 0 |
mbed_official | 0:7037ed05f54f | 68 | |
mbed_official | 0:7037ed05f54f | 69 | /** |
mbed_official | 0:7037ed05f54f | 70 | * Dummy sensor class object |
mbed_official | 0:7037ed05f54f | 71 | */ |
mbed_official | 0:7037ed05f54f | 72 | DS1820 ds1820(PC_9); |
mbed_official | 0:7037ed05f54f | 73 | |
mbed_official | 0:7037ed05f54f | 74 | /** |
mbed_official | 0:7037ed05f54f | 75 | * This event queue is the global event queue for both the |
mbed_official | 0:7037ed05f54f | 76 | * application and stack. To conserve memory, the stack is designed to run |
mbed_official | 0:7037ed05f54f | 77 | * in the same thread as the application and the application is responsible for |
mbed_official | 0:7037ed05f54f | 78 | * providing an event queue to the stack that will be used for ISR deferment as |
mbed_official | 0:7037ed05f54f | 79 | * well as application information event queuing. |
mbed_official | 0:7037ed05f54f | 80 | */ |
mbed_official | 46:a124538e2891 | 81 | static EventQueue ev_queue(MAX_NUMBER_OF_EVENTS *EVENTS_EVENT_SIZE); |
mbed_official | 0:7037ed05f54f | 82 | |
mbed_official | 0:7037ed05f54f | 83 | /** |
mbed_official | 0:7037ed05f54f | 84 | * Event handler. |
mbed_official | 0:7037ed05f54f | 85 | * |
mbed_official | 0:7037ed05f54f | 86 | * This will be passed to the LoRaWAN stack to queue events for the |
mbed_official | 0:7037ed05f54f | 87 | * application which in turn drive the application. |
mbed_official | 0:7037ed05f54f | 88 | */ |
mbed_official | 0:7037ed05f54f | 89 | static void lora_event_handler(lorawan_event_t event); |
mbed_official | 0:7037ed05f54f | 90 | |
mbed_official | 0:7037ed05f54f | 91 | /** |
mbed_official | 46:a124538e2891 | 92 | * Constructing Mbed LoRaWANInterface and passing it the radio object from lora_radio_helper. |
mbed_official | 0:7037ed05f54f | 93 | */ |
mbed_official | 2:dc95ac6d6d4e | 94 | static LoRaWANInterface lorawan(radio); |
mbed_official | 0:7037ed05f54f | 95 | |
mbed_official | 0:7037ed05f54f | 96 | /** |
mbed_official | 0:7037ed05f54f | 97 | * Application specific callbacks |
mbed_official | 0:7037ed05f54f | 98 | */ |
mbed_official | 0:7037ed05f54f | 99 | static lorawan_app_callbacks_t callbacks; |
mbed_official | 0:7037ed05f54f | 100 | |
fabio_gatti | 49:bf339fabb590 | 101 | |
fabio_gatti | 49:bf339fabb590 | 102 | static void LoRa_PrintChannels() { |
fabio_gatti | 49:bf339fabb590 | 103 | /* print list of all channel frequencies */ |
fabio_gatti | 49:bf339fabb590 | 104 | lorawan_channelplan_t channelPlan {}; |
fabio_gatti | 49:bf339fabb590 | 105 | static loramac_channel_t channelbuf[10]; |
fabio_gatti | 49:bf339fabb590 | 106 | |
fabio_gatti | 49:bf339fabb590 | 107 | channelPlan.channels = channelbuf; |
fabio_gatti | 49:bf339fabb590 | 108 | if (lorawan.get_channel_plan(channelPlan) == LORAWAN_STATUS_OK) { |
fabio_gatti | 49:bf339fabb590 | 109 | for (uint8_t i = 0; i < channelPlan.nb_channels; i++) { |
fabio_gatti | 49:bf339fabb590 | 110 | loramac_channel_t chan = channelPlan.channels[i]; |
fabio_gatti | 49:bf339fabb590 | 111 | printf(" CHAN %d ID %d FREQ %lu RX1FREQ %lu Band %d DR min %d max %d\n", |
fabio_gatti | 49:bf339fabb590 | 112 | (int) i, (int) chan.id, chan.ch_param.frequency, |
fabio_gatti | 49:bf339fabb590 | 113 | chan.ch_param.rx1_frequency, (int) chan.ch_param.band, |
fabio_gatti | 49:bf339fabb590 | 114 | (int) chan.ch_param.dr_range.fields.min, |
fabio_gatti | 49:bf339fabb590 | 115 | (int) chan.ch_param.dr_range.fields.max); |
fabio_gatti | 49:bf339fabb590 | 116 | } |
fabio_gatti | 49:bf339fabb590 | 117 | } else { |
fabio_gatti | 49:bf339fabb590 | 118 | printf(" COULD NOT GET CHANNEL PLAN\n"); |
fabio_gatti | 49:bf339fabb590 | 119 | } |
fabio_gatti | 49:bf339fabb590 | 120 | } |
fabio_gatti | 49:bf339fabb590 | 121 | |
mbed_official | 0:7037ed05f54f | 122 | /** |
mbed_official | 0:7037ed05f54f | 123 | * Entry point for application |
mbed_official | 0:7037ed05f54f | 124 | */ |
mbed_official | 46:a124538e2891 | 125 | int main(void) |
mbed_official | 0:7037ed05f54f | 126 | { |
fabio_gatti | 49:bf339fabb590 | 127 | static loramac_channel_t ttnChannels[] = { |
fabio_gatti | 49:bf339fabb590 | 128 | {0, {868100000, 0, {(DR_5 << 4) | DR_0}, 1}}, |
fabio_gatti | 49:bf339fabb590 | 129 | {1, {868300000, 0, {(DR_5 << 4) | DR_0}, 1}}, |
fabio_gatti | 49:bf339fabb590 | 130 | {2, {868500000, 0, {(DR_5 << 4) | DR_0}, 1}}, |
fabio_gatti | 49:bf339fabb590 | 131 | {3, {867100000, 0, {(DR_5 << 4) | DR_0}, 0}}, |
fabio_gatti | 49:bf339fabb590 | 132 | {4, {867300000, 0, {(DR_5 << 4) | DR_0}, 0}}, |
fabio_gatti | 49:bf339fabb590 | 133 | {5, {867500000, 0, {(DR_5 << 4) | DR_0}, 0}}, |
fabio_gatti | 49:bf339fabb590 | 134 | {6, {867700000, 0, {(DR_5 << 4) | DR_0}, 0}}, |
fabio_gatti | 49:bf339fabb590 | 135 | {7, {867900000, 0, {(DR_5 << 4) | DR_0}, 0}} |
fabio_gatti | 49:bf339fabb590 | 136 | }; |
fabio_gatti | 49:bf339fabb590 | 137 | lorawan_channelplan_t channelPlan {}; |
mbed_official | 0:7037ed05f54f | 138 | // setup tracing |
mbed_official | 0:7037ed05f54f | 139 | setup_trace(); |
mbed_official | 0:7037ed05f54f | 140 | |
mbed_official | 0:7037ed05f54f | 141 | // stores the status of a call to LoRaWAN protocol |
mbed_official | 0:7037ed05f54f | 142 | lorawan_status_t retcode; |
fabio_gatti | 49:bf339fabb590 | 143 | printf("---------------------------- \n"); |
fabio_gatti | 49:bf339fabb590 | 144 | // LORAWAN: Initialize LoRaWAN stack |
mbed_official | 2:dc95ac6d6d4e | 145 | if (lorawan.initialize(&ev_queue) != LORAWAN_STATUS_OK) { |
fabio_gatti | 49:bf339fabb590 | 146 | printf(" LoRa initialization failed! \n"); |
mbed_official | 0:7037ed05f54f | 147 | return -1; |
mbed_official | 0:7037ed05f54f | 148 | } |
fabio_gatti | 49:bf339fabb590 | 149 | printf(" Mbed LoRaWANStack initialized \n"); |
mbed_official | 0:7037ed05f54f | 150 | |
fabio_gatti | 49:bf339fabb590 | 151 | // LORAWAN: prepare application callbacks |
mbed_official | 0:7037ed05f54f | 152 | callbacks.events = mbed::callback(lora_event_handler); |
mbed_official | 2:dc95ac6d6d4e | 153 | lorawan.add_app_callbacks(&callbacks); |
mbed_official | 0:7037ed05f54f | 154 | |
fabio_gatti | 49:bf339fabb590 | 155 | // LORAWAN: Set number of retries in case of CONFIRMED messages |
fabio_gatti | 49:bf339fabb590 | 156 | if (lorawan.set_confirmed_msg_retries(LORAWAN_CONFIRMED_MSG_RETRY_COUNTER) |
mbed_official | 46:a124538e2891 | 157 | != LORAWAN_STATUS_OK) { |
fabio_gatti | 49:bf339fabb590 | 158 | printf(" Set_confirmed_msg_retries failed! \n"); |
mbed_official | 0:7037ed05f54f | 159 | return -1; |
mbed_official | 0:7037ed05f54f | 160 | } |
fabio_gatti | 49:bf339fabb590 | 161 | printf(" CONFIRMED message retries : %d \n", |
fabio_gatti | 49:bf339fabb590 | 162 | LORAWAN_CONFIRMED_MSG_RETRY_COUNTER); |
fabio_gatti | 49:bf339fabb590 | 163 | |
fabio_gatti | 49:bf339fabb590 | 164 | // LORAWAN: settaggio canali |
fabio_gatti | 49:bf339fabb590 | 165 | channelPlan.channels = (loramac_channel_t*) ttnChannels; |
fabio_gatti | 49:bf339fabb590 | 166 | channelPlan.nb_channels = LORAWAN_CHANNEL_NBR; |
fabio_gatti | 49:bf339fabb590 | 167 | if (lorawan.set_channel_plan(channelPlan) == LORAWAN_STATUS_OK) { |
fabio_gatti | 49:bf339fabb590 | 168 | printf(" [+] Setting TTN channels\n"); |
fabio_gatti | 49:bf339fabb590 | 169 | } else { |
fabio_gatti | 49:bf339fabb590 | 170 | printf(" [-] Failed to set TTN channels! Debug return code.\n"); |
fabio_gatti | 49:bf339fabb590 | 171 | } |
fabio_gatti | 49:bf339fabb590 | 172 | LoRa_PrintChannels(); |
fabio_gatti | 49:bf339fabb590 | 173 | |
fabio_gatti | 49:bf339fabb590 | 174 | // LORAWAN: data rate |
fabio_gatti | 49:bf339fabb590 | 175 | // if (lorawan.enable_adaptive_datarate() != LORAWAN_STATUS_OK) { |
fabio_gatti | 49:bf339fabb590 | 176 | // printf("\r\n enable_adaptive_datarate failed! \r\n"); |
fabio_gatti | 49:bf339fabb590 | 177 | // return -1; |
fabio_gatti | 49:bf339fabb590 | 178 | // } |
fabio_gatti | 49:bf339fabb590 | 179 | |
mbed_official | 0:7037ed05f54f | 180 | // Enable adaptive data rate |
fabio_gatti | 49:bf339fabb590 | 181 | if (lorawan.disable_adaptive_datarate() != LORAWAN_STATUS_OK) { |
fabio_gatti | 49:bf339fabb590 | 182 | printf(" disable_adaptive_datarate failed! \r\n"); |
mbed_official | 0:7037ed05f54f | 183 | return -1; |
mbed_official | 0:7037ed05f54f | 184 | } |
fabio_gatti | 49:bf339fabb590 | 185 | printf(" Adaptive data rate (ADR) - disabled \r\n"); |
fabio_gatti | 49:bf339fabb590 | 186 | lorawan.set_datarate(LORAWAN_DATA_RATE); |
mbed_official | 0:7037ed05f54f | 187 | |
mbed_official | 2:dc95ac6d6d4e | 188 | retcode = lorawan.connect(); |
mbed_official | 0:7037ed05f54f | 189 | |
mbed_official | 0:7037ed05f54f | 190 | if (retcode == LORAWAN_STATUS_OK || |
mbed_official | 46:a124538e2891 | 191 | retcode == LORAWAN_STATUS_CONNECT_IN_PROGRESS) { |
mbed_official | 0:7037ed05f54f | 192 | } else { |
fabio_gatti | 49:bf339fabb590 | 193 | printf(" Connection error, code = %d \n", retcode); |
mbed_official | 0:7037ed05f54f | 194 | return -1; |
mbed_official | 0:7037ed05f54f | 195 | } |
mbed_official | 0:7037ed05f54f | 196 | |
fabio_gatti | 49:bf339fabb590 | 197 | printf(" Connection - In Progress ...\n"); |
mbed_official | 0:7037ed05f54f | 198 | |
mbed_official | 0:7037ed05f54f | 199 | // make your event queue dispatching events forever |
mbed_official | 0:7037ed05f54f | 200 | ev_queue.dispatch_forever(); |
mbed_official | 3:8c7198d1a2a1 | 201 | |
mbed_official | 3:8c7198d1a2a1 | 202 | return 0; |
mbed_official | 0:7037ed05f54f | 203 | } |
mbed_official | 0:7037ed05f54f | 204 | |
mbed_official | 0:7037ed05f54f | 205 | /** |
mbed_official | 0:7037ed05f54f | 206 | * Sends a message to the Network Server |
mbed_official | 0:7037ed05f54f | 207 | */ |
mbed_official | 0:7037ed05f54f | 208 | static void send_message() |
mbed_official | 0:7037ed05f54f | 209 | { |
mbed_official | 0:7037ed05f54f | 210 | uint16_t packet_len; |
mbed_official | 0:7037ed05f54f | 211 | int16_t retcode; |
mbed_official | 0:7037ed05f54f | 212 | float sensor_value; |
fabio_gatti | 49:bf339fabb590 | 213 | |
mbed_official | 0:7037ed05f54f | 214 | |
mbed_official | 0:7037ed05f54f | 215 | if (ds1820.begin()) { |
mbed_official | 0:7037ed05f54f | 216 | ds1820.startConversion(); |
mbed_official | 0:7037ed05f54f | 217 | sensor_value = ds1820.read(); |
fabio_gatti | 49:bf339fabb590 | 218 | printf("\n -------------------------\n"); |
fabio_gatti | 49:bf339fabb590 | 219 | printf(" Dummy Sensor Value = %3.1f \n", sensor_value); |
mbed_official | 0:7037ed05f54f | 220 | ds1820.startConversion(); |
mbed_official | 0:7037ed05f54f | 221 | } else { |
fabio_gatti | 49:bf339fabb590 | 222 | printf(" No sensor found \n"); |
mbed_official | 0:7037ed05f54f | 223 | return; |
mbed_official | 0:7037ed05f54f | 224 | } |
fabio_gatti | 49:bf339fabb590 | 225 | |
fabio_gatti | 49:bf339fabb590 | 226 | packet_len = sprintf((char *) tx_buffer, " Dummy Sensor Value is %3.1f", |
mbed_official | 46:a124538e2891 | 227 | sensor_value); |
mbed_official | 0:7037ed05f54f | 228 | |
fabio_gatti | 49:bf339fabb590 | 229 | retcode = lorawan.send(LORAWAN_APP_PORT, tx_buffer, packet_len, |
fabio_gatti | 49:bf339fabb590 | 230 | LORAWAN_TX_MSG_TYPE); |
mbed_official | 0:7037ed05f54f | 231 | |
mbed_official | 0:7037ed05f54f | 232 | if (retcode < 0) { |
fabio_gatti | 49:bf339fabb590 | 233 | retcode == LORAWAN_STATUS_WOULD_BLOCK ? printf(" send - WOULD BLOCK\r\n") |
fabio_gatti | 49:bf339fabb590 | 234 | : printf(" send() - Error code %d \n", retcode); |
mbed_official | 26:f07f5febf97f | 235 | |
mbed_official | 26:f07f5febf97f | 236 | if (retcode == LORAWAN_STATUS_WOULD_BLOCK) { |
mbed_official | 26:f07f5febf97f | 237 | //retry in 3 seconds |
mbed_official | 26:f07f5febf97f | 238 | if (MBED_CONF_LORA_DUTY_CYCLE_ON) { |
fabio_gatti | 49:bf339fabb590 | 239 | ev_queue.call_in(LORAWAN_CHANNEL_RETRY, send_message); |
mbed_official | 26:f07f5febf97f | 240 | } |
mbed_official | 26:f07f5febf97f | 241 | } |
mbed_official | 0:7037ed05f54f | 242 | return; |
mbed_official | 0:7037ed05f54f | 243 | } |
fabio_gatti | 49:bf339fabb590 | 244 | |
fabio_gatti | 49:bf339fabb590 | 245 | printf(" %d bytes scheduled for transmission \n", retcode); |
mbed_official | 12:5015dfead3f2 | 246 | memset(tx_buffer, 0, sizeof(tx_buffer)); |
mbed_official | 0:7037ed05f54f | 247 | } |
mbed_official | 0:7037ed05f54f | 248 | |
mbed_official | 0:7037ed05f54f | 249 | /** |
mbed_official | 0:7037ed05f54f | 250 | * Receive a message from the Network Server |
mbed_official | 0:7037ed05f54f | 251 | */ |
mbed_official | 0:7037ed05f54f | 252 | static void receive_message() |
mbed_official | 0:7037ed05f54f | 253 | { |
mbed_official | 47:b6d132f1079f | 254 | uint8_t port; |
mbed_official | 47:b6d132f1079f | 255 | int flags; |
mbed_official | 47:b6d132f1079f | 256 | int16_t retcode = lorawan.receive(rx_buffer, sizeof(rx_buffer), port, flags); |
mbed_official | 0:7037ed05f54f | 257 | |
mbed_official | 0:7037ed05f54f | 258 | if (retcode < 0) { |
fabio_gatti | 49:bf339fabb590 | 259 | printf(" receive() - Error code %d \r\n", retcode); |
mbed_official | 0:7037ed05f54f | 260 | return; |
mbed_official | 0:7037ed05f54f | 261 | } |
mbed_official | 0:7037ed05f54f | 262 | |
mbed_official | 47:b6d132f1079f | 263 | printf(" RX Data on port %u (%d bytes): ", port, retcode); |
mbed_official | 0:7037ed05f54f | 264 | for (uint8_t i = 0; i < retcode; i++) { |
mbed_official | 47:b6d132f1079f | 265 | printf("%02x ", rx_buffer[i]); |
mbed_official | 0:7037ed05f54f | 266 | } |
fabio_gatti | 49:bf339fabb590 | 267 | printf("\n"); |
mbed_official | 47:b6d132f1079f | 268 | |
mbed_official | 12:5015dfead3f2 | 269 | memset(rx_buffer, 0, sizeof(rx_buffer)); |
mbed_official | 0:7037ed05f54f | 270 | } |
mbed_official | 0:7037ed05f54f | 271 | |
mbed_official | 0:7037ed05f54f | 272 | /** |
mbed_official | 0:7037ed05f54f | 273 | * Event handler |
mbed_official | 0:7037ed05f54f | 274 | */ |
mbed_official | 0:7037ed05f54f | 275 | static void lora_event_handler(lorawan_event_t event) |
mbed_official | 0:7037ed05f54f | 276 | { |
fabio_gatti | 49:bf339fabb590 | 277 | int16_t retcode; |
fabio_gatti | 49:bf339fabb590 | 278 | lorawan_tx_metadata additional_data; |
fabio_gatti | 49:bf339fabb590 | 279 | int backoff_data; |
fabio_gatti | 49:bf339fabb590 | 280 | |
mbed_official | 0:7037ed05f54f | 281 | switch (event) { |
mbed_official | 0:7037ed05f54f | 282 | case CONNECTED: |
fabio_gatti | 49:bf339fabb590 | 283 | printf(" Connection - Successful \n"); |
mbed_official | 0:7037ed05f54f | 284 | if (MBED_CONF_LORA_DUTY_CYCLE_ON) { |
mbed_official | 0:7037ed05f54f | 285 | send_message(); |
mbed_official | 0:7037ed05f54f | 286 | } else { |
mbed_official | 0:7037ed05f54f | 287 | ev_queue.call_every(TX_TIMER, send_message); |
mbed_official | 0:7037ed05f54f | 288 | } |
mbed_official | 0:7037ed05f54f | 289 | |
mbed_official | 0:7037ed05f54f | 290 | break; |
mbed_official | 0:7037ed05f54f | 291 | case DISCONNECTED: |
mbed_official | 0:7037ed05f54f | 292 | ev_queue.break_dispatch(); |
fabio_gatti | 49:bf339fabb590 | 293 | printf(" Disconnected Successfully \n"); |
mbed_official | 0:7037ed05f54f | 294 | break; |
mbed_official | 0:7037ed05f54f | 295 | case TX_DONE: |
fabio_gatti | 49:bf339fabb590 | 296 | printf(" Message Sent to Network Server \n"); |
fabio_gatti | 49:bf339fabb590 | 297 | |
fabio_gatti | 49:bf339fabb590 | 298 | retcode = lorawan.get_tx_metadata(additional_data); |
fabio_gatti | 49:bf339fabb590 | 299 | switch (retcode) |
fabio_gatti | 49:bf339fabb590 | 300 | { |
fabio_gatti | 49:bf339fabb590 | 301 | case LORAWAN_STATUS_NOT_INITIALIZED: |
fabio_gatti | 49:bf339fabb590 | 302 | printf(" Lorawan stack not initialized\n"); |
fabio_gatti | 49:bf339fabb590 | 303 | break; |
fabio_gatti | 49:bf339fabb590 | 304 | |
fabio_gatti | 49:bf339fabb590 | 305 | case LORAWAN_STATUS_METADATA_NOT_AVAILABLE: |
fabio_gatti | 49:bf339fabb590 | 306 | printf(" Metadata not available\n"); |
fabio_gatti | 49:bf339fabb590 | 307 | break; |
fabio_gatti | 49:bf339fabb590 | 308 | |
fabio_gatti | 49:bf339fabb590 | 309 | case LORAWAN_STATUS_OK : |
fabio_gatti | 49:bf339fabb590 | 310 | printf(" TX Channel: %d \n",additional_data.channel); |
fabio_gatti | 49:bf339fabb590 | 311 | printf(" TOA (msec): %d \n",additional_data.tx_toa); |
fabio_gatti | 49:bf339fabb590 | 312 | printf(" Data rate: %u \n",additional_data.data_rate); |
fabio_gatti | 49:bf339fabb590 | 313 | break; |
fabio_gatti | 49:bf339fabb590 | 314 | } |
fabio_gatti | 49:bf339fabb590 | 315 | |
fabio_gatti | 49:bf339fabb590 | 316 | retcode = lorawan.get_backoff_metadata(backoff_data); |
fabio_gatti | 49:bf339fabb590 | 317 | switch (retcode) |
fabio_gatti | 49:bf339fabb590 | 318 | { |
fabio_gatti | 49:bf339fabb590 | 319 | case LORAWAN_STATUS_NOT_INITIALIZED: |
fabio_gatti | 49:bf339fabb590 | 320 | printf(" Lorawan stack not initialized\n"); |
fabio_gatti | 49:bf339fabb590 | 321 | break; |
fabio_gatti | 49:bf339fabb590 | 322 | |
fabio_gatti | 49:bf339fabb590 | 323 | case LORAWAN_STATUS_METADATA_NOT_AVAILABLE: |
fabio_gatti | 49:bf339fabb590 | 324 | printf(" Backoff not available\n"); |
fabio_gatti | 49:bf339fabb590 | 325 | break; |
fabio_gatti | 49:bf339fabb590 | 326 | |
fabio_gatti | 49:bf339fabb590 | 327 | case LORAWAN_STATUS_OK : |
fabio_gatti | 49:bf339fabb590 | 328 | printf(" Backoff: %d \n",backoff_data); |
fabio_gatti | 49:bf339fabb590 | 329 | break; |
fabio_gatti | 49:bf339fabb590 | 330 | } |
mbed_official | 0:7037ed05f54f | 331 | if (MBED_CONF_LORA_DUTY_CYCLE_ON) { |
mbed_official | 0:7037ed05f54f | 332 | send_message(); |
mbed_official | 0:7037ed05f54f | 333 | } |
mbed_official | 0:7037ed05f54f | 334 | break; |
mbed_official | 0:7037ed05f54f | 335 | case TX_TIMEOUT: |
mbed_official | 0:7037ed05f54f | 336 | case TX_ERROR: |
mbed_official | 0:7037ed05f54f | 337 | case TX_CRYPTO_ERROR: |
mbed_official | 0:7037ed05f54f | 338 | case TX_SCHEDULING_ERROR: |
fabio_gatti | 49:bf339fabb590 | 339 | printf(" Transmission Error - EventCode = %d \r\n", event); |
mbed_official | 0:7037ed05f54f | 340 | // try again |
mbed_official | 0:7037ed05f54f | 341 | if (MBED_CONF_LORA_DUTY_CYCLE_ON) { |
mbed_official | 0:7037ed05f54f | 342 | send_message(); |
mbed_official | 0:7037ed05f54f | 343 | } |
mbed_official | 0:7037ed05f54f | 344 | break; |
mbed_official | 0:7037ed05f54f | 345 | case RX_DONE: |
fabio_gatti | 49:bf339fabb590 | 346 | printf(" Received message from Network Server \r\n"); |
mbed_official | 0:7037ed05f54f | 347 | receive_message(); |
mbed_official | 0:7037ed05f54f | 348 | break; |
mbed_official | 0:7037ed05f54f | 349 | case RX_TIMEOUT: |
mbed_official | 0:7037ed05f54f | 350 | case RX_ERROR: |
fabio_gatti | 49:bf339fabb590 | 351 | printf(" Error in reception - Code = %d \r\n", event); |
mbed_official | 0:7037ed05f54f | 352 | break; |
mbed_official | 0:7037ed05f54f | 353 | case JOIN_FAILURE: |
fabio_gatti | 49:bf339fabb590 | 354 | printf(" OTAA Failed - Check Keys \r\n"); |
mbed_official | 0:7037ed05f54f | 355 | break; |
mbed_official | 26:f07f5febf97f | 356 | case UPLINK_REQUIRED: |
fabio_gatti | 49:bf339fabb590 | 357 | printf(" Uplink required by NS \r\n"); |
mbed_official | 26:f07f5febf97f | 358 | if (MBED_CONF_LORA_DUTY_CYCLE_ON) { |
mbed_official | 26:f07f5febf97f | 359 | send_message(); |
mbed_official | 26:f07f5febf97f | 360 | } |
mbed_official | 26:f07f5febf97f | 361 | break; |
fabio_gatti | 49:bf339fabb590 | 362 | default: |
fabio_gatti | 49:bf339fabb590 | 363 | MBED_ASSERT(" Unknown Event"); |
mbed_official | 0:7037ed05f54f | 364 | } |
mbed_official | 0:7037ed05f54f | 365 | } |
mbed_official | 0:7037ed05f54f | 366 | |
fabio_gatti | 49:bf339fabb590 | 367 | |
mbed_official | 0:7037ed05f54f | 368 | // EOF |