Supachai Vorapojpisut
/
skeletonMbed
mbed LoRaWAN stack
main.cpp@0:3ddbd7a98a2d, 2018-05-29 (annotated)
- Committer:
- vsupacha
- Date:
- Tue May 29 04:54:26 2018 +0000
- Revision:
- 0:3ddbd7a98a2d
- Child:
- 2:bbb1f6ce5970
Initial update
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 | 0:3ddbd7a98a2d | 7 | |
vsupacha | 0:3ddbd7a98a2d | 8 | #define MAX_NUMBER_OF_EVENTS 10 |
vsupacha | 0:3ddbd7a98a2d | 9 | #define CONFIRMED_MSG_RETRY_COUNTER 3 |
vsupacha | 0:3ddbd7a98a2d | 10 | #define SX1276 0xEE |
vsupacha | 0:3ddbd7a98a2d | 11 | |
vsupacha | 0:3ddbd7a98a2d | 12 | |
vsupacha | 0:3ddbd7a98a2d | 13 | SX1276_LoRaRadio radio(MBED_CONF_APP_LORA_SPI_MOSI, |
vsupacha | 0:3ddbd7a98a2d | 14 | MBED_CONF_APP_LORA_SPI_MISO, |
vsupacha | 0:3ddbd7a98a2d | 15 | MBED_CONF_APP_LORA_SPI_SCLK, |
vsupacha | 0:3ddbd7a98a2d | 16 | MBED_CONF_APP_LORA_CS, |
vsupacha | 0:3ddbd7a98a2d | 17 | MBED_CONF_APP_LORA_RESET, |
vsupacha | 0:3ddbd7a98a2d | 18 | MBED_CONF_APP_LORA_DIO0, |
vsupacha | 0:3ddbd7a98a2d | 19 | MBED_CONF_APP_LORA_DIO1, |
vsupacha | 0:3ddbd7a98a2d | 20 | MBED_CONF_APP_LORA_DIO2, |
vsupacha | 0:3ddbd7a98a2d | 21 | MBED_CONF_APP_LORA_DIO3, |
vsupacha | 0:3ddbd7a98a2d | 22 | MBED_CONF_APP_LORA_DIO4, |
vsupacha | 0:3ddbd7a98a2d | 23 | MBED_CONF_APP_LORA_DIO5, |
vsupacha | 0:3ddbd7a98a2d | 24 | MBED_CONF_APP_LORA_RF_SWITCH_CTL1, |
vsupacha | 0:3ddbd7a98a2d | 25 | MBED_CONF_APP_LORA_RF_SWITCH_CTL2, |
vsupacha | 0:3ddbd7a98a2d | 26 | MBED_CONF_APP_LORA_TXCTL, |
vsupacha | 0:3ddbd7a98a2d | 27 | MBED_CONF_APP_LORA_RXCTL, |
vsupacha | 0:3ddbd7a98a2d | 28 | MBED_CONF_APP_LORA_ANT_SWITCH, |
vsupacha | 0:3ddbd7a98a2d | 29 | MBED_CONF_APP_LORA_PWR_AMP_CTL, |
vsupacha | 0:3ddbd7a98a2d | 30 | MBED_CONF_APP_LORA_TCXO); |
vsupacha | 0:3ddbd7a98a2d | 31 | |
vsupacha | 0:3ddbd7a98a2d | 32 | static LoRaWANInterface lorawan(radio); |
vsupacha | 0:3ddbd7a98a2d | 33 | static lorawan_app_callbacks_t callbacks; |
vsupacha | 0:3ddbd7a98a2d | 34 | static void lora_event_handler(lorawan_event_t event); |
vsupacha | 0:3ddbd7a98a2d | 35 | |
vsupacha | 0:3ddbd7a98a2d | 36 | static EventQueue ev_queue(MAX_NUMBER_OF_EVENTS * EVENTS_EVENT_SIZE); |
vsupacha | 0:3ddbd7a98a2d | 37 | |
vsupacha | 0:3ddbd7a98a2d | 38 | uint8_t tx_buffer[LORAMAC_PHY_MAXPAYLOAD]; |
vsupacha | 0:3ddbd7a98a2d | 39 | uint8_t rx_buffer[LORAMAC_PHY_MAXPAYLOAD]; |
vsupacha | 0:3ddbd7a98a2d | 40 | |
vsupacha | 0:3ddbd7a98a2d | 41 | lorawan_channelplan_t channel_plan; |
vsupacha | 0:3ddbd7a98a2d | 42 | static const channel_params_t AS923_LC3 = { 923600000, 0, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }; |
vsupacha | 0:3ddbd7a98a2d | 43 | static const channel_params_t AS923_LC4 = { 923800000, 0, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }; |
vsupacha | 0:3ddbd7a98a2d | 44 | static const channel_params_t AS923_LC5 = { 924000000, 0, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }; |
vsupacha | 0:3ddbd7a98a2d | 45 | static const channel_params_t AS923_LC6 = { 924200000, 0, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }; |
vsupacha | 0:3ddbd7a98a2d | 46 | static const channel_params_t AS923_LC7 = { 924400000, 0, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }; |
vsupacha | 0:3ddbd7a98a2d | 47 | static const channel_params_t AS923_LC8 = { 924600000, 0, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }; |
vsupacha | 0:3ddbd7a98a2d | 48 | |
vsupacha | 0:3ddbd7a98a2d | 49 | int count = 0; |
vsupacha | 0:3ddbd7a98a2d | 50 | InterruptIn mybtn(USER_BUTTON); |
vsupacha | 0:3ddbd7a98a2d | 51 | Ticker sender; |
vsupacha | 0:3ddbd7a98a2d | 52 | |
vsupacha | 0:3ddbd7a98a2d | 53 | void countBtn() |
vsupacha | 0:3ddbd7a98a2d | 54 | { |
vsupacha | 0:3ddbd7a98a2d | 55 | count++; |
vsupacha | 0:3ddbd7a98a2d | 56 | } |
vsupacha | 0:3ddbd7a98a2d | 57 | |
vsupacha | 0:3ddbd7a98a2d | 58 | void send_message(void); |
vsupacha | 0:3ddbd7a98a2d | 59 | |
vsupacha | 0:3ddbd7a98a2d | 60 | int main() |
vsupacha | 0:3ddbd7a98a2d | 61 | { |
vsupacha | 0:3ddbd7a98a2d | 62 | mybtn.fall(&countBtn); |
vsupacha | 0:3ddbd7a98a2d | 63 | lorawan_status_t retcode; |
vsupacha | 0:3ddbd7a98a2d | 64 | |
vsupacha | 0:3ddbd7a98a2d | 65 | // setup |
vsupacha | 0:3ddbd7a98a2d | 66 | if (lorawan.initialize(&ev_queue) == LORAWAN_STATUS_OK) { |
vsupacha | 0:3ddbd7a98a2d | 67 | printf("\r\n LoRaWAN stack initialization OK \r\n"); |
vsupacha | 0:3ddbd7a98a2d | 68 | } else { |
vsupacha | 0:3ddbd7a98a2d | 69 | printf("\r\n LoRaWAN stack initialization failed! \r\n"); |
vsupacha | 0:3ddbd7a98a2d | 70 | return -1; |
vsupacha | 0:3ddbd7a98a2d | 71 | } |
vsupacha | 0:3ddbd7a98a2d | 72 | callbacks.events = mbed::callback(lora_event_handler); |
vsupacha | 0:3ddbd7a98a2d | 73 | lorawan.add_app_callbacks(&callbacks); |
vsupacha | 0:3ddbd7a98a2d | 74 | if (lorawan.set_confirmed_msg_retries(CONFIRMED_MSG_RETRY_COUNTER) |
vsupacha | 0:3ddbd7a98a2d | 75 | == LORAWAN_STATUS_OK) { |
vsupacha | 0:3ddbd7a98a2d | 76 | printf("\r\n set_confirmed_msg_retries OK \r\n"); |
vsupacha | 0:3ddbd7a98a2d | 77 | } else { |
vsupacha | 0:3ddbd7a98a2d | 78 | printf("\r\n set_confirmed_msg_retries failed! \r\n"); |
vsupacha | 0:3ddbd7a98a2d | 79 | return -1; |
vsupacha | 0:3ddbd7a98a2d | 80 | } |
vsupacha | 0:3ddbd7a98a2d | 81 | if (lorawan.enable_adaptive_datarate() == LORAWAN_STATUS_OK) { |
vsupacha | 0:3ddbd7a98a2d | 82 | printf("\r\n enable_adaptive_datarate OK \r\n"); |
vsupacha | 0:3ddbd7a98a2d | 83 | } else { |
vsupacha | 0:3ddbd7a98a2d | 84 | printf("\r\n enable_adaptive_datarate failed! \r\n"); |
vsupacha | 0:3ddbd7a98a2d | 85 | return -1; |
vsupacha | 0:3ddbd7a98a2d | 86 | } |
vsupacha | 0:3ddbd7a98a2d | 87 | |
vsupacha | 0:3ddbd7a98a2d | 88 | retcode = lorawan.connect(); |
vsupacha | 0:3ddbd7a98a2d | 89 | |
vsupacha | 0:3ddbd7a98a2d | 90 | if ((retcode == LORAWAN_STATUS_OK) || |
vsupacha | 0:3ddbd7a98a2d | 91 | (retcode == LORAWAN_STATUS_CONNECT_IN_PROGRESS)) { |
vsupacha | 0:3ddbd7a98a2d | 92 | printf("\r\n Connecting \r\n"); |
vsupacha | 0:3ddbd7a98a2d | 93 | } else { |
vsupacha | 0:3ddbd7a98a2d | 94 | printf("\r\n Connection error, code = %d \r\n", retcode); |
vsupacha | 0:3ddbd7a98a2d | 95 | return -1; |
vsupacha | 0:3ddbd7a98a2d | 96 | } |
vsupacha | 0:3ddbd7a98a2d | 97 | |
vsupacha | 0:3ddbd7a98a2d | 98 | ev_queue.dispatch_forever(); |
vsupacha | 0:3ddbd7a98a2d | 99 | |
vsupacha | 0:3ddbd7a98a2d | 100 | return 0; |
vsupacha | 0:3ddbd7a98a2d | 101 | } |
vsupacha | 0:3ddbd7a98a2d | 102 | |
vsupacha | 0:3ddbd7a98a2d | 103 | static void lora_event_handler(lorawan_event_t event) |
vsupacha | 0:3ddbd7a98a2d | 104 | { |
vsupacha | 0:3ddbd7a98a2d | 105 | switch(event) { |
vsupacha | 0:3ddbd7a98a2d | 106 | case CONNECTED: |
vsupacha | 0:3ddbd7a98a2d | 107 | printf("\r\n Connected \r\n"); |
vsupacha | 0:3ddbd7a98a2d | 108 | //sender.attach(&send_message, 30.0); |
vsupacha | 0:3ddbd7a98a2d | 109 | if (MBED_CONF_LORA_DUTY_CYCLE_ON) { |
vsupacha | 0:3ddbd7a98a2d | 110 | send_message(); |
vsupacha | 0:3ddbd7a98a2d | 111 | printf("\r\n Message in queue \r\n"); |
vsupacha | 0:3ddbd7a98a2d | 112 | } |
vsupacha | 0:3ddbd7a98a2d | 113 | break; |
vsupacha | 0:3ddbd7a98a2d | 114 | case DISCONNECTED: |
vsupacha | 0:3ddbd7a98a2d | 115 | case TX_DONE: |
vsupacha | 0:3ddbd7a98a2d | 116 | printf("\r\n Message Sent to Network Server \r\n"); |
vsupacha | 0:3ddbd7a98a2d | 117 | if (MBED_CONF_LORA_DUTY_CYCLE_ON) { |
vsupacha | 0:3ddbd7a98a2d | 118 | send_message(); |
vsupacha | 0:3ddbd7a98a2d | 119 | printf("\r\n Message in queue \r\n"); |
vsupacha | 0:3ddbd7a98a2d | 120 | } |
vsupacha | 0:3ddbd7a98a2d | 121 | break; |
vsupacha | 0:3ddbd7a98a2d | 122 | case RX_DONE: |
vsupacha | 0:3ddbd7a98a2d | 123 | int sz = lorawan.receive(MBED_CONF_LORA_APP_PORT, rx_buffer, |
vsupacha | 0:3ddbd7a98a2d | 124 | LORAMAC_PHY_MAXPAYLOAD, |
vsupacha | 0:3ddbd7a98a2d | 125 | MSG_CONFIRMED_FLAG|MSG_UNCONFIRMED_FLAG); |
vsupacha | 0:3ddbd7a98a2d | 126 | if (sz > 0) { |
vsupacha | 0:3ddbd7a98a2d | 127 | printf("\r\n Got: "); |
vsupacha | 0:3ddbd7a98a2d | 128 | for (int i = 0; i < sz; i++) { |
vsupacha | 0:3ddbd7a98a2d | 129 | printf("%X ", rx_buffer[i]); |
vsupacha | 0:3ddbd7a98a2d | 130 | } |
vsupacha | 0:3ddbd7a98a2d | 131 | } else { |
vsupacha | 0:3ddbd7a98a2d | 132 | |
vsupacha | 0:3ddbd7a98a2d | 133 | } |
vsupacha | 0:3ddbd7a98a2d | 134 | break; |
vsupacha | 0:3ddbd7a98a2d | 135 | case TX_TIMEOUT: |
vsupacha | 0:3ddbd7a98a2d | 136 | case TX_ERROR: |
vsupacha | 0:3ddbd7a98a2d | 137 | case TX_CRYPTO_ERROR: |
vsupacha | 0:3ddbd7a98a2d | 138 | case TX_SCHEDULING_ERROR: |
vsupacha | 0:3ddbd7a98a2d | 139 | printf("\r\n Transmission Error - EventCode = %d \r\n", event); |
vsupacha | 0:3ddbd7a98a2d | 140 | break; |
vsupacha | 0:3ddbd7a98a2d | 141 | case RX_TIMEOUT: |
vsupacha | 0:3ddbd7a98a2d | 142 | case RX_ERROR: |
vsupacha | 0:3ddbd7a98a2d | 143 | case JOIN_FAILURE: |
vsupacha | 0:3ddbd7a98a2d | 144 | printf("\r\n Joining fails! \r\n"); |
vsupacha | 0:3ddbd7a98a2d | 145 | break; |
vsupacha | 0:3ddbd7a98a2d | 146 | default: |
vsupacha | 0:3ddbd7a98a2d | 147 | MBED_ASSERT("Unknown Event"); |
vsupacha | 0:3ddbd7a98a2d | 148 | } |
vsupacha | 0:3ddbd7a98a2d | 149 | } |
vsupacha | 0:3ddbd7a98a2d | 150 | |
vsupacha | 0:3ddbd7a98a2d | 151 | void send_message() |
vsupacha | 0:3ddbd7a98a2d | 152 | { |
vsupacha | 0:3ddbd7a98a2d | 153 | const uint16_t id = 0xFF; |
vsupacha | 0:3ddbd7a98a2d | 154 | |
vsupacha | 0:3ddbd7a98a2d | 155 | uint16_t *pbuf; |
vsupacha | 0:3ddbd7a98a2d | 156 | int retcode; |
vsupacha | 0:3ddbd7a98a2d | 157 | |
vsupacha | 0:3ddbd7a98a2d | 158 | pbuf = (uint16_t*)tx_buffer; |
vsupacha | 0:3ddbd7a98a2d | 159 | pbuf[0] = id; |
vsupacha | 0:3ddbd7a98a2d | 160 | pbuf[1] = count; |
vsupacha | 0:3ddbd7a98a2d | 161 | retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, tx_buffer, 4, MSG_CONFIRMED_FLAG); |
vsupacha | 0:3ddbd7a98a2d | 162 | if (retcode < 0) { |
vsupacha | 0:3ddbd7a98a2d | 163 | printf("Sending error\n"); |
vsupacha | 0:3ddbd7a98a2d | 164 | } |
vsupacha | 0:3ddbd7a98a2d | 165 | |
vsupacha | 0:3ddbd7a98a2d | 166 | memset(tx_buffer, 0, LORAMAC_PHY_MAXPAYLOAD); |
vsupacha | 0:3ddbd7a98a2d | 167 | } |