How to measure water in a reservoir

Dependencies:   Cayenne-MQTT-mbed Cayenne-LPP

Committer:
wamae
Date:
Fri Mar 08 11:34:56 2019 +0000
Revision:
0:7bfeb237e600
working code

Who changed what in which revision?

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