getting location and activity

Dependencies:   Cayenne-LPP GPS

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers lorawan_network.h Source File

lorawan_network.h

00001 #ifndef _LORAWAN_NETWORK_H_
00002 #define _LORAWAN_NETWORK_H_
00003 
00004 #include "mbed.h"
00005 #include "mbed_trace.h"
00006 #include "mbed_events.h"
00007 #include "LoRaWANInterface.h"
00008 #include "SX1276_LoRaRadio.h"
00009 #include "CayenneLPP.h"
00010 #include "lora_radio_helper.h"
00011 
00012 #define MBED_CONF_LORA_APP_PORT     15
00013 
00014 // EventQueue is required to dispatch events around
00015 EventQueue ev_queue;
00016 
00017 // Constructing Mbed LoRaWANInterface and passing it down the radio object.
00018 static LoRaWANInterface lorawan(radio);
00019 
00020 // Application specific callbacks
00021 static lorawan_app_callbacks_t callbacks;
00022 
00023 // LoRaWAN stack event handler
00024 static void lora_event_handler(lorawan_event_t event);
00025 
00026 static void lorawan_setup(uint32_t devAddr, uint8_t nwkSKey[16], uint8_t appSKey[16], Callback<void(lorawan_event_t)> lw_event_handler) {
00027     if (devAddr == 0x0) {
00028         printf("Set your LoRaWAN credentials first!\n");
00029         return;
00030     }
00031 
00032     // Enable trace output for this demo, so we can see what the LoRaWAN stack does
00033     mbed_trace_init();
00034 
00035     if (lorawan.initialize(&ev_queue) != LORAWAN_STATUS_OK) {
00036         printf("[LNWK][ERR ] LoRa initialization failed!\n");
00037         return;
00038     }
00039 
00040     // prepare application callbacks
00041     callbacks.events = lw_event_handler;
00042     lorawan.add_app_callbacks(&callbacks);
00043 
00044     // Disable adaptive data rating
00045     if (lorawan.disable_adaptive_datarate() != LORAWAN_STATUS_OK) {
00046         printf("[LNWK][ERR ] disable_adaptive_datarate failed!\n");
00047         return;
00048     }
00049 
00050     lorawan.set_datarate(0); // SF12BW125
00051 
00052     lorawan_connect_t connect_params;
00053     connect_params.connect_type = LORAWAN_CONNECTION_ABP;
00054     // connect_params.connection_u.otaa.dev_eui = DEV_EUI;
00055     // connect_params.connection_u.otaa.app_eui = APP_EUI;
00056     // connect_params.connection_u.otaa.app_key = APP_KEY;
00057     // connect_params.connection_u.otaa.nb_trials = 3;
00058 
00059     connect_params.connection_u.abp.dev_addr = devAddr;
00060     connect_params.connection_u.abp.nwk_skey = nwkSKey;
00061     connect_params.connection_u.abp.app_skey = appSKey;
00062 
00063     lorawan_status_t retcode = lorawan.connect(connect_params);
00064 
00065     if (retcode == LORAWAN_STATUS_OK ||
00066         retcode == LORAWAN_STATUS_CONNECT_IN_PROGRESS) {
00067     } else {
00068         printf("[LNWK][ERR ] Connection error, code = %d\n", retcode);
00069         return;
00070     }
00071 
00072     printf("[LNWK][INFO] Connection - In Progress...\n");
00073 }
00074 
00075 // This is called from RX_DONE, so whenever a message came in
00076 static void receive_message()
00077 {
00078     uint8_t rx_buffer[50] = { 0 };
00079     int16_t retcode;
00080     retcode = lorawan.receive(MBED_CONF_LORA_APP_PORT, rx_buffer,
00081                               sizeof(rx_buffer),
00082                               MSG_UNCONFIRMED_FLAG);
00083 
00084     if (retcode < 0) {
00085         printf("[LNWK][WARN] receive() - Error code %d\n", retcode);
00086         return;
00087     }
00088 
00089     printf("[LNWK][INFO] Data received on port %d (length %d): ", MBED_CONF_LORA_APP_PORT, retcode);
00090 
00091     for (uint8_t i = 0; i < retcode; i++) {
00092         printf("%02x ", rx_buffer[i]);
00093     }
00094     printf("\n");
00095 }
00096 
00097 // Event handler
00098 bool lorawan_send(CayenneLPP *payload) {
00099     int16_t retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, payload->getBuffer(), payload->getSize(), MSG_UNCONFIRMED_FLAG);
00100 
00101     // 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
00102     if (retcode < 0) {
00103         retcode == LORAWAN_STATUS_WOULD_BLOCK ? printf("[LNWK][WARN] send - duty cycle violation\n")
00104                 : printf("[LNWK][WARN] send() - Error code %d\n", retcode);
00105         return false;
00106     }
00107 
00108     printf("[LNWK][INFO] %d bytes scheduled for transmission\n", retcode);
00109     return true;
00110 }
00111 
00112 #endif // _LORAWAN_NETWORK_H_