Supachai Vorapojpisut
/
skeletonMbed
mbed LoRaWAN stack
main.cpp@2:bbb1f6ce5970, 2018-06-05 (annotated)
- Committer:
- vsupacha
- Date:
- Tue Jun 05 08:29:27 2018 +0000
- Revision:
- 2:bbb1f6ce5970
- Parent:
- 0:3ddbd7a98a2d
Add trace feature
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vsupacha | 0:3ddbd7a98a2d | 1 | #include "mbed.h" |
vsupacha | 0:3ddbd7a98a2d | 2 | #include "lorawan/LoRaWANInterface.h" |
vsupacha | 0:3ddbd7a98a2d | 3 | #include "lorawan/system/lorawan_data_structures.h" |
vsupacha | 0:3ddbd7a98a2d | 4 | #include "events/EventQueue.h" |
vsupacha | 0:3ddbd7a98a2d | 5 | #include "lorawan/LoRaRadio.h" |
vsupacha | 0:3ddbd7a98a2d | 6 | #include "SX1276_LoRaRadio.h" |
vsupacha | 2:bbb1f6ce5970 | 7 | #include "trace_helper.h" |
vsupacha | 0:3ddbd7a98a2d | 8 | |
vsupacha | 0:3ddbd7a98a2d | 9 | #define MAX_NUMBER_OF_EVENTS 10 |
vsupacha | 0:3ddbd7a98a2d | 10 | #define CONFIRMED_MSG_RETRY_COUNTER 3 |
vsupacha | 0:3ddbd7a98a2d | 11 | #define SX1276 0xEE |
vsupacha | 0:3ddbd7a98a2d | 12 | |
vsupacha | 0:3ddbd7a98a2d | 13 | |
vsupacha | 0:3ddbd7a98a2d | 14 | SX1276_LoRaRadio radio(MBED_CONF_APP_LORA_SPI_MOSI, |
vsupacha | 0:3ddbd7a98a2d | 15 | MBED_CONF_APP_LORA_SPI_MISO, |
vsupacha | 0:3ddbd7a98a2d | 16 | MBED_CONF_APP_LORA_SPI_SCLK, |
vsupacha | 0:3ddbd7a98a2d | 17 | MBED_CONF_APP_LORA_CS, |
vsupacha | 0:3ddbd7a98a2d | 18 | MBED_CONF_APP_LORA_RESET, |
vsupacha | 0:3ddbd7a98a2d | 19 | MBED_CONF_APP_LORA_DIO0, |
vsupacha | 0:3ddbd7a98a2d | 20 | MBED_CONF_APP_LORA_DIO1, |
vsupacha | 0:3ddbd7a98a2d | 21 | MBED_CONF_APP_LORA_DIO2, |
vsupacha | 0:3ddbd7a98a2d | 22 | MBED_CONF_APP_LORA_DIO3, |
vsupacha | 0:3ddbd7a98a2d | 23 | MBED_CONF_APP_LORA_DIO4, |
vsupacha | 0:3ddbd7a98a2d | 24 | MBED_CONF_APP_LORA_DIO5, |
vsupacha | 0:3ddbd7a98a2d | 25 | MBED_CONF_APP_LORA_RF_SWITCH_CTL1, |
vsupacha | 0:3ddbd7a98a2d | 26 | MBED_CONF_APP_LORA_RF_SWITCH_CTL2, |
vsupacha | 0:3ddbd7a98a2d | 27 | MBED_CONF_APP_LORA_TXCTL, |
vsupacha | 0:3ddbd7a98a2d | 28 | MBED_CONF_APP_LORA_RXCTL, |
vsupacha | 0:3ddbd7a98a2d | 29 | MBED_CONF_APP_LORA_ANT_SWITCH, |
vsupacha | 0:3ddbd7a98a2d | 30 | MBED_CONF_APP_LORA_PWR_AMP_CTL, |
vsupacha | 0:3ddbd7a98a2d | 31 | MBED_CONF_APP_LORA_TCXO); |
vsupacha | 0:3ddbd7a98a2d | 32 | |
vsupacha | 0:3ddbd7a98a2d | 33 | static LoRaWANInterface lorawan(radio); |
vsupacha | 0:3ddbd7a98a2d | 34 | static lorawan_app_callbacks_t callbacks; |
vsupacha | 0:3ddbd7a98a2d | 35 | static void lora_event_handler(lorawan_event_t event); |
vsupacha | 0:3ddbd7a98a2d | 36 | |
vsupacha | 0:3ddbd7a98a2d | 37 | static EventQueue ev_queue(MAX_NUMBER_OF_EVENTS * EVENTS_EVENT_SIZE); |
vsupacha | 0:3ddbd7a98a2d | 38 | |
vsupacha | 0:3ddbd7a98a2d | 39 | uint8_t tx_buffer[LORAMAC_PHY_MAXPAYLOAD]; |
vsupacha | 0:3ddbd7a98a2d | 40 | uint8_t rx_buffer[LORAMAC_PHY_MAXPAYLOAD]; |
vsupacha | 0:3ddbd7a98a2d | 41 | |
vsupacha | 0:3ddbd7a98a2d | 42 | lorawan_channelplan_t channel_plan; |
vsupacha | 2:bbb1f6ce5970 | 43 | loramac_channel_t channels[8]; |
vsupacha | 2:bbb1f6ce5970 | 44 | //static const channel_params_t AS923_LC3 = { 923600000, 0, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }; |
vsupacha | 0:3ddbd7a98a2d | 45 | |
vsupacha | 0:3ddbd7a98a2d | 46 | int count = 0; |
vsupacha | 0:3ddbd7a98a2d | 47 | InterruptIn mybtn(USER_BUTTON); |
vsupacha | 0:3ddbd7a98a2d | 48 | Ticker sender; |
vsupacha | 0:3ddbd7a98a2d | 49 | |
vsupacha | 0:3ddbd7a98a2d | 50 | void countBtn() |
vsupacha | 0:3ddbd7a98a2d | 51 | { |
vsupacha | 0:3ddbd7a98a2d | 52 | count++; |
vsupacha | 0:3ddbd7a98a2d | 53 | } |
vsupacha | 0:3ddbd7a98a2d | 54 | |
vsupacha | 0:3ddbd7a98a2d | 55 | void send_message(void); |
vsupacha | 0:3ddbd7a98a2d | 56 | |
vsupacha | 0:3ddbd7a98a2d | 57 | int main() |
vsupacha | 0:3ddbd7a98a2d | 58 | { |
vsupacha | 0:3ddbd7a98a2d | 59 | mybtn.fall(&countBtn); |
vsupacha | 0:3ddbd7a98a2d | 60 | lorawan_status_t retcode; |
vsupacha | 0:3ddbd7a98a2d | 61 | |
vsupacha | 2:bbb1f6ce5970 | 62 | setup_trace(); |
vsupacha | 2:bbb1f6ce5970 | 63 | |
vsupacha | 0:3ddbd7a98a2d | 64 | // setup |
vsupacha | 0:3ddbd7a98a2d | 65 | if (lorawan.initialize(&ev_queue) == LORAWAN_STATUS_OK) { |
vsupacha | 0:3ddbd7a98a2d | 66 | printf("\r\n LoRaWAN stack initialization OK \r\n"); |
vsupacha | 0:3ddbd7a98a2d | 67 | } else { |
vsupacha | 0:3ddbd7a98a2d | 68 | printf("\r\n LoRaWAN stack initialization failed! \r\n"); |
vsupacha | 0:3ddbd7a98a2d | 69 | return -1; |
vsupacha | 0:3ddbd7a98a2d | 70 | } |
vsupacha | 0:3ddbd7a98a2d | 71 | callbacks.events = mbed::callback(lora_event_handler); |
vsupacha | 0:3ddbd7a98a2d | 72 | lorawan.add_app_callbacks(&callbacks); |
vsupacha | 0:3ddbd7a98a2d | 73 | if (lorawan.set_confirmed_msg_retries(CONFIRMED_MSG_RETRY_COUNTER) |
vsupacha | 0:3ddbd7a98a2d | 74 | == LORAWAN_STATUS_OK) { |
vsupacha | 0:3ddbd7a98a2d | 75 | printf("\r\n set_confirmed_msg_retries OK \r\n"); |
vsupacha | 0:3ddbd7a98a2d | 76 | } else { |
vsupacha | 0:3ddbd7a98a2d | 77 | printf("\r\n set_confirmed_msg_retries failed! \r\n"); |
vsupacha | 0:3ddbd7a98a2d | 78 | return -1; |
vsupacha | 0:3ddbd7a98a2d | 79 | } |
vsupacha | 0:3ddbd7a98a2d | 80 | if (lorawan.enable_adaptive_datarate() == LORAWAN_STATUS_OK) { |
vsupacha | 0:3ddbd7a98a2d | 81 | printf("\r\n enable_adaptive_datarate OK \r\n"); |
vsupacha | 0:3ddbd7a98a2d | 82 | } else { |
vsupacha | 0:3ddbd7a98a2d | 83 | printf("\r\n enable_adaptive_datarate failed! \r\n"); |
vsupacha | 0:3ddbd7a98a2d | 84 | return -1; |
vsupacha | 0:3ddbd7a98a2d | 85 | } |
vsupacha | 2:bbb1f6ce5970 | 86 | channel_plan.channels = channels; |
vsupacha | 2:bbb1f6ce5970 | 87 | retcode = lorawan.connect(); |
vsupacha | 0:3ddbd7a98a2d | 88 | |
vsupacha | 2:bbb1f6ce5970 | 89 | retcode = lorawan.get_channel_plan(channel_plan); |
vsupacha | 2:bbb1f6ce5970 | 90 | if (retcode == LORAWAN_STATUS_OK ) { |
vsupacha | 2:bbb1f6ce5970 | 91 | printf("No. channels: %d\n", channel_plan.nb_channels); |
vsupacha | 2:bbb1f6ce5970 | 92 | for (int i = 0; i < channel_plan.nb_channels; i++) { |
vsupacha | 2:bbb1f6ce5970 | 93 | printf("ID (%d): %d, RX1: %d\n", channel_plan.channels[i].id, channel_plan.channels[i].ch_param.frequency, channel_plan.channels[i].ch_param.rx1_frequency); |
vsupacha | 2:bbb1f6ce5970 | 94 | } |
vsupacha | 2:bbb1f6ce5970 | 95 | } else { |
vsupacha | 2:bbb1f6ce5970 | 96 | printf("retcode: %d\n", retcode); |
vsupacha | 2:bbb1f6ce5970 | 97 | } |
vsupacha | 0:3ddbd7a98a2d | 98 | |
vsupacha | 0:3ddbd7a98a2d | 99 | if ((retcode == LORAWAN_STATUS_OK) || |
vsupacha | 0:3ddbd7a98a2d | 100 | (retcode == LORAWAN_STATUS_CONNECT_IN_PROGRESS)) { |
vsupacha | 0:3ddbd7a98a2d | 101 | printf("\r\n Connecting \r\n"); |
vsupacha | 0:3ddbd7a98a2d | 102 | } else { |
vsupacha | 0:3ddbd7a98a2d | 103 | printf("\r\n Connection error, code = %d \r\n", retcode); |
vsupacha | 0:3ddbd7a98a2d | 104 | return -1; |
vsupacha | 0:3ddbd7a98a2d | 105 | } |
vsupacha | 0:3ddbd7a98a2d | 106 | |
vsupacha | 0:3ddbd7a98a2d | 107 | ev_queue.dispatch_forever(); |
vsupacha | 0:3ddbd7a98a2d | 108 | |
vsupacha | 0:3ddbd7a98a2d | 109 | return 0; |
vsupacha | 0:3ddbd7a98a2d | 110 | } |
vsupacha | 0:3ddbd7a98a2d | 111 | |
vsupacha | 0:3ddbd7a98a2d | 112 | static void lora_event_handler(lorawan_event_t event) |
vsupacha | 0:3ddbd7a98a2d | 113 | { |
vsupacha | 2:bbb1f6ce5970 | 114 | int retcode; |
vsupacha | 2:bbb1f6ce5970 | 115 | |
vsupacha | 2:bbb1f6ce5970 | 116 | channel_plan.channels = channels; |
vsupacha | 0:3ddbd7a98a2d | 117 | switch(event) { |
vsupacha | 0:3ddbd7a98a2d | 118 | case CONNECTED: |
vsupacha | 0:3ddbd7a98a2d | 119 | printf("\r\n Connected \r\n"); |
vsupacha | 0:3ddbd7a98a2d | 120 | //sender.attach(&send_message, 30.0); |
vsupacha | 2:bbb1f6ce5970 | 121 | retcode = lorawan.get_channel_plan(channel_plan); |
vsupacha | 2:bbb1f6ce5970 | 122 | if (retcode == LORAWAN_STATUS_OK ) { |
vsupacha | 2:bbb1f6ce5970 | 123 | printf("No. channels: %d\n", channel_plan.nb_channels); |
vsupacha | 2:bbb1f6ce5970 | 124 | for (int i = 0; i < channel_plan.nb_channels; i++) { |
vsupacha | 2:bbb1f6ce5970 | 125 | printf("ID (%d): %d, RX1: %d\n", channel_plan.channels[i].id, channel_plan.channels[i].ch_param.frequency, channel_plan.channels[i].ch_param.rx1_frequency); |
vsupacha | 2:bbb1f6ce5970 | 126 | } |
vsupacha | 2:bbb1f6ce5970 | 127 | } else { |
vsupacha | 2:bbb1f6ce5970 | 128 | printf("retcode: %d\n", retcode); |
vsupacha | 2:bbb1f6ce5970 | 129 | } |
vsupacha | 0:3ddbd7a98a2d | 130 | if (MBED_CONF_LORA_DUTY_CYCLE_ON) { |
vsupacha | 0:3ddbd7a98a2d | 131 | send_message(); |
vsupacha | 0:3ddbd7a98a2d | 132 | printf("\r\n Message in queue \r\n"); |
vsupacha | 0:3ddbd7a98a2d | 133 | } |
vsupacha | 0:3ddbd7a98a2d | 134 | break; |
vsupacha | 0:3ddbd7a98a2d | 135 | case DISCONNECTED: |
vsupacha | 0:3ddbd7a98a2d | 136 | case TX_DONE: |
vsupacha | 0:3ddbd7a98a2d | 137 | printf("\r\n Message Sent to Network Server \r\n"); |
vsupacha | 2:bbb1f6ce5970 | 138 | retcode = lorawan.get_channel_plan(channel_plan); |
vsupacha | 2:bbb1f6ce5970 | 139 | if (retcode == LORAWAN_STATUS_OK ) { |
vsupacha | 2:bbb1f6ce5970 | 140 | printf("No. channels: %d\n", channel_plan.nb_channels); |
vsupacha | 2:bbb1f6ce5970 | 141 | for (int i = 0; i < channel_plan.nb_channels; i++) { |
vsupacha | 2:bbb1f6ce5970 | 142 | printf("ID (%d): %d, RX1: %d\n", channel_plan.channels[i].id, channel_plan.channels[i].ch_param.frequency, channel_plan.channels[i].ch_param.rx1_frequency); |
vsupacha | 2:bbb1f6ce5970 | 143 | } |
vsupacha | 2:bbb1f6ce5970 | 144 | } else { |
vsupacha | 2:bbb1f6ce5970 | 145 | printf("retcode: %d\n", retcode); |
vsupacha | 2:bbb1f6ce5970 | 146 | } |
vsupacha | 0:3ddbd7a98a2d | 147 | if (MBED_CONF_LORA_DUTY_CYCLE_ON) { |
vsupacha | 0:3ddbd7a98a2d | 148 | send_message(); |
vsupacha | 0:3ddbd7a98a2d | 149 | printf("\r\n Message in queue \r\n"); |
vsupacha | 0:3ddbd7a98a2d | 150 | } |
vsupacha | 0:3ddbd7a98a2d | 151 | break; |
vsupacha | 0:3ddbd7a98a2d | 152 | case RX_DONE: |
vsupacha | 0:3ddbd7a98a2d | 153 | int sz = lorawan.receive(MBED_CONF_LORA_APP_PORT, rx_buffer, |
vsupacha | 0:3ddbd7a98a2d | 154 | LORAMAC_PHY_MAXPAYLOAD, |
vsupacha | 0:3ddbd7a98a2d | 155 | MSG_CONFIRMED_FLAG|MSG_UNCONFIRMED_FLAG); |
vsupacha | 0:3ddbd7a98a2d | 156 | if (sz > 0) { |
vsupacha | 0:3ddbd7a98a2d | 157 | printf("\r\n Got: "); |
vsupacha | 0:3ddbd7a98a2d | 158 | for (int i = 0; i < sz; i++) { |
vsupacha | 0:3ddbd7a98a2d | 159 | printf("%X ", rx_buffer[i]); |
vsupacha | 0:3ddbd7a98a2d | 160 | } |
vsupacha | 0:3ddbd7a98a2d | 161 | } else { |
vsupacha | 0:3ddbd7a98a2d | 162 | |
vsupacha | 0:3ddbd7a98a2d | 163 | } |
vsupacha | 0:3ddbd7a98a2d | 164 | break; |
vsupacha | 0:3ddbd7a98a2d | 165 | case TX_TIMEOUT: |
vsupacha | 0:3ddbd7a98a2d | 166 | case TX_ERROR: |
vsupacha | 0:3ddbd7a98a2d | 167 | case TX_CRYPTO_ERROR: |
vsupacha | 0:3ddbd7a98a2d | 168 | case TX_SCHEDULING_ERROR: |
vsupacha | 0:3ddbd7a98a2d | 169 | printf("\r\n Transmission Error - EventCode = %d \r\n", event); |
vsupacha | 0:3ddbd7a98a2d | 170 | break; |
vsupacha | 0:3ddbd7a98a2d | 171 | case RX_TIMEOUT: |
vsupacha | 0:3ddbd7a98a2d | 172 | case RX_ERROR: |
vsupacha | 0:3ddbd7a98a2d | 173 | case JOIN_FAILURE: |
vsupacha | 0:3ddbd7a98a2d | 174 | printf("\r\n Joining fails! \r\n"); |
vsupacha | 0:3ddbd7a98a2d | 175 | break; |
vsupacha | 0:3ddbd7a98a2d | 176 | default: |
vsupacha | 0:3ddbd7a98a2d | 177 | MBED_ASSERT("Unknown Event"); |
vsupacha | 0:3ddbd7a98a2d | 178 | } |
vsupacha | 0:3ddbd7a98a2d | 179 | } |
vsupacha | 0:3ddbd7a98a2d | 180 | |
vsupacha | 0:3ddbd7a98a2d | 181 | void send_message() |
vsupacha | 0:3ddbd7a98a2d | 182 | { |
vsupacha | 0:3ddbd7a98a2d | 183 | const uint16_t id = 0xFF; |
vsupacha | 0:3ddbd7a98a2d | 184 | |
vsupacha | 0:3ddbd7a98a2d | 185 | uint16_t *pbuf; |
vsupacha | 0:3ddbd7a98a2d | 186 | int retcode; |
vsupacha | 0:3ddbd7a98a2d | 187 | |
vsupacha | 0:3ddbd7a98a2d | 188 | pbuf = (uint16_t*)tx_buffer; |
vsupacha | 0:3ddbd7a98a2d | 189 | pbuf[0] = id; |
vsupacha | 0:3ddbd7a98a2d | 190 | pbuf[1] = count; |
vsupacha | 0:3ddbd7a98a2d | 191 | retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, tx_buffer, 4, MSG_CONFIRMED_FLAG); |
vsupacha | 0:3ddbd7a98a2d | 192 | if (retcode < 0) { |
vsupacha | 0:3ddbd7a98a2d | 193 | printf("Sending error\n"); |
vsupacha | 0:3ddbd7a98a2d | 194 | } |
vsupacha | 0:3ddbd7a98a2d | 195 | |
vsupacha | 0:3ddbd7a98a2d | 196 | memset(tx_buffer, 0, LORAMAC_PHY_MAXPAYLOAD); |
vsupacha | 0:3ddbd7a98a2d | 197 | } |