Level measurement using range finder and lora technology
Dependencies: Cayenne-LPP SDBlockDevice
inc/lorawan_network.h@0:f930f0440fd5, 2019-06-26 (annotated)
- Committer:
- wamae
- Date:
- Wed Jun 26 10:35:50 2019 +0000
- Revision:
- 0:f930f0440fd5
better copy
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wamae | 0:f930f0440fd5 | 1 | #ifndef _LORAWAN_NETWORK_H_ |
wamae | 0:f930f0440fd5 | 2 | #define _LORAWAN_NETWORK_H_ |
wamae | 0:f930f0440fd5 | 3 | |
wamae | 0:f930f0440fd5 | 4 | #include "mbed.h" |
wamae | 0:f930f0440fd5 | 5 | #include "mbed_trace.h" |
wamae | 0:f930f0440fd5 | 6 | #include "mbed_events.h" |
wamae | 0:f930f0440fd5 | 7 | #include "LoRaWANInterface.h" |
wamae | 0:f930f0440fd5 | 8 | #include "SX1276_LoRaRadio.h" |
wamae | 0:f930f0440fd5 | 9 | #include "CayenneLPP.h" |
wamae | 0:f930f0440fd5 | 10 | #include "lora_radio_helper.h" |
wamae | 0:f930f0440fd5 | 11 | |
wamae | 0:f930f0440fd5 | 12 | #define MBED_CONF_LORA_APP_PORT 15 |
wamae | 0:f930f0440fd5 | 13 | |
wamae | 0:f930f0440fd5 | 14 | // EventQueue is required to dispatch events around |
wamae | 0:f930f0440fd5 | 15 | EventQueue ev_queue; |
wamae | 0:f930f0440fd5 | 16 | |
wamae | 0:f930f0440fd5 | 17 | // Constructing Mbed LoRaWANInterface and passing it down the radio object. |
wamae | 0:f930f0440fd5 | 18 | static LoRaWANInterface lorawan(radio); |
wamae | 0:f930f0440fd5 | 19 | |
wamae | 0:f930f0440fd5 | 20 | // Application specific callbacks |
wamae | 0:f930f0440fd5 | 21 | static lorawan_app_callbacks_t callbacks; |
wamae | 0:f930f0440fd5 | 22 | |
wamae | 0:f930f0440fd5 | 23 | // LoRaWAN stack event handler |
wamae | 0:f930f0440fd5 | 24 | static void lora_event_handler(lorawan_event_t event); |
wamae | 0:f930f0440fd5 | 25 | |
wamae | 0:f930f0440fd5 | 26 | static void lorawan_setup(uint32_t devAddr, uint8_t nwkSKey[16], uint8_t appSKey[16], Callback<void(lorawan_event_t)> lw_event_handler) { |
wamae | 0:f930f0440fd5 | 27 | if (devAddr == 0x0) { |
wamae | 0:f930f0440fd5 | 28 | printf("Set your LoRaWAN credentials first!\n"); |
wamae | 0:f930f0440fd5 | 29 | return; |
wamae | 0:f930f0440fd5 | 30 | } |
wamae | 0:f930f0440fd5 | 31 | |
wamae | 0:f930f0440fd5 | 32 | // Enable trace output for this demo, so we can see what the LoRaWAN stack does |
wamae | 0:f930f0440fd5 | 33 | mbed_trace_init(); |
wamae | 0:f930f0440fd5 | 34 | |
wamae | 0:f930f0440fd5 | 35 | if (lorawan.initialize(&ev_queue) != LORAWAN_STATUS_OK) { |
wamae | 0:f930f0440fd5 | 36 | printf("[LNWK][ERR ] LoRa initialization failed!\n"); |
wamae | 0:f930f0440fd5 | 37 | return; |
wamae | 0:f930f0440fd5 | 38 | } |
wamae | 0:f930f0440fd5 | 39 | |
wamae | 0:f930f0440fd5 | 40 | // prepare application callbacks |
wamae | 0:f930f0440fd5 | 41 | callbacks.events = lw_event_handler; |
wamae | 0:f930f0440fd5 | 42 | lorawan.add_app_callbacks(&callbacks); |
wamae | 0:f930f0440fd5 | 43 | |
wamae | 0:f930f0440fd5 | 44 | // Disable adaptive data rating |
wamae | 0:f930f0440fd5 | 45 | if (lorawan.disable_adaptive_datarate() != LORAWAN_STATUS_OK) { |
wamae | 0:f930f0440fd5 | 46 | printf("[LNWK][ERR ] disable_adaptive_datarate failed!\n"); |
wamae | 0:f930f0440fd5 | 47 | return; |
wamae | 0:f930f0440fd5 | 48 | } |
wamae | 0:f930f0440fd5 | 49 | |
wamae | 0:f930f0440fd5 | 50 | lorawan.set_datarate(0); // SF12BW125 |
wamae | 0:f930f0440fd5 | 51 | |
wamae | 0:f930f0440fd5 | 52 | lorawan_connect_t connect_params; |
wamae | 0:f930f0440fd5 | 53 | connect_params.connect_type = LORAWAN_CONNECTION_ABP; |
wamae | 0:f930f0440fd5 | 54 | // connect_params.connection_u.otaa.dev_eui = DEV_EUI; |
wamae | 0:f930f0440fd5 | 55 | // connect_params.connection_u.otaa.app_eui = APP_EUI; |
wamae | 0:f930f0440fd5 | 56 | // connect_params.connection_u.otaa.app_key = APP_KEY; |
wamae | 0:f930f0440fd5 | 57 | // connect_params.connection_u.otaa.nb_trials = 3; |
wamae | 0:f930f0440fd5 | 58 | |
wamae | 0:f930f0440fd5 | 59 | connect_params.connection_u.abp.dev_addr = devAddr; |
wamae | 0:f930f0440fd5 | 60 | connect_params.connection_u.abp.nwk_skey = nwkSKey; |
wamae | 0:f930f0440fd5 | 61 | connect_params.connection_u.abp.app_skey = appSKey; |
wamae | 0:f930f0440fd5 | 62 | |
wamae | 0:f930f0440fd5 | 63 | lorawan_status_t retcode = lorawan.connect(connect_params); |
wamae | 0:f930f0440fd5 | 64 | |
wamae | 0:f930f0440fd5 | 65 | if (retcode == LORAWAN_STATUS_OK || |
wamae | 0:f930f0440fd5 | 66 | retcode == LORAWAN_STATUS_CONNECT_IN_PROGRESS) { |
wamae | 0:f930f0440fd5 | 67 | } else { |
wamae | 0:f930f0440fd5 | 68 | printf("[LNWK][ERR ] Connection error, code = %d\n", retcode); |
wamae | 0:f930f0440fd5 | 69 | return; |
wamae | 0:f930f0440fd5 | 70 | } |
wamae | 0:f930f0440fd5 | 71 | |
wamae | 0:f930f0440fd5 | 72 | printf("[LNWK][INFO] Connection - In Progress...\n"); |
wamae | 0:f930f0440fd5 | 73 | } |
wamae | 0:f930f0440fd5 | 74 | |
wamae | 0:f930f0440fd5 | 75 | // This is called from RX_DONE, so whenever a message came in |
wamae | 0:f930f0440fd5 | 76 | static void receive_message() |
wamae | 0:f930f0440fd5 | 77 | { |
wamae | 0:f930f0440fd5 | 78 | uint8_t rx_buffer[50] = { 0 }; |
wamae | 0:f930f0440fd5 | 79 | int16_t retcode; |
wamae | 0:f930f0440fd5 | 80 | retcode = lorawan.receive(MBED_CONF_LORA_APP_PORT, rx_buffer, |
wamae | 0:f930f0440fd5 | 81 | sizeof(rx_buffer), |
wamae | 0:f930f0440fd5 | 82 | MSG_UNCONFIRMED_FLAG); |
wamae | 0:f930f0440fd5 | 83 | |
wamae | 0:f930f0440fd5 | 84 | if (retcode < 0) { |
wamae | 0:f930f0440fd5 | 85 | printf("[LNWK][WARN] receive() - Error code %d\n", retcode); |
wamae | 0:f930f0440fd5 | 86 | return; |
wamae | 0:f930f0440fd5 | 87 | } |
wamae | 0:f930f0440fd5 | 88 | |
wamae | 0:f930f0440fd5 | 89 | printf("[LNWK][INFO] Data received on port %d (length %d): ", MBED_CONF_LORA_APP_PORT, retcode); |
wamae | 0:f930f0440fd5 | 90 | |
wamae | 0:f930f0440fd5 | 91 | for (uint8_t i = 0; i < retcode; i++) { |
wamae | 0:f930f0440fd5 | 92 | printf("%02x ", rx_buffer[i]); |
wamae | 0:f930f0440fd5 | 93 | } |
wamae | 0:f930f0440fd5 | 94 | printf("\n"); |
wamae | 0:f930f0440fd5 | 95 | } |
wamae | 0:f930f0440fd5 | 96 | |
wamae | 0:f930f0440fd5 | 97 | // Event handler |
wamae | 0:f930f0440fd5 | 98 | bool lorawan_send(CayenneLPP *payload) { |
wamae | 0:f930f0440fd5 | 99 | int16_t retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, payload->getBuffer(), payload->getSize(), MSG_UNCONFIRMED_FLAG); |
wamae | 0:f930f0440fd5 | 100 | |
wamae | 0:f930f0440fd5 | 101 | // for some reason send() ret\urns -1... I cannot find out why, the stack returns the right number. I feel that this is some weird Emscripten quirk |
wamae | 0:f930f0440fd5 | 102 | if (retcode < 0) { |
wamae | 0:f930f0440fd5 | 103 | retcode == LORAWAN_STATUS_WOULD_BLOCK ? printf("[LNWK][WARN] send - duty cycle violation\n") |
wamae | 0:f930f0440fd5 | 104 | : printf("[LNWK][WARN] send() - Error code %d\n", retcode); |
wamae | 0:f930f0440fd5 | 105 | return false; |
wamae | 0:f930f0440fd5 | 106 | } |
wamae | 0:f930f0440fd5 | 107 | |
wamae | 0:f930f0440fd5 | 108 | printf("[LNWK][INFO] %d bytes scheduled for transmission\n", retcode); |
wamae | 0:f930f0440fd5 | 109 | return true; |
wamae | 0:f930f0440fd5 | 110 | } |
wamae | 0:f930f0440fd5 | 111 | |
wamae | 0:f930f0440fd5 | 112 | #endif // _LORAWAN_NETWORK_H_ |