Fork of the official LoRaWAN example adapted for Future Electronic's Sequana board with SX1272MB2DAS LoRa shield.
Diff: main.cpp
- Revision:
- 58:d86aa57863ed
- Parent:
- 56:39847849d219
--- a/main.cpp Tue Sep 03 14:01:54 2019 +0100 +++ b/main.cpp Wed Sep 11 16:08:09 2019 +0000 @@ -19,13 +19,19 @@ #include "lorawan/LoRaWANInterface.h" #include "lorawan/system/lorawan_data_structures.h" #include "events/EventQueue.h" +#include "psoc6_utils.h" +#include "mbed.h" // Application helpers #include "DummySensor.h" #include "trace_helper.h" #include "lora_radio_helper.h" +#include "BD2808.h" + using namespace events; +using namespace mbed; + // Max payload size can be LORAMAC_PHY_MAXPAYLOAD. // This example only communicates with much shorter messages (<30 bytes). @@ -87,17 +93,47 @@ */ static lorawan_app_callbacks_t callbacks; + +Timer g_timer; +DigitalOut led(LED1); +BD2808 leds; + +static uint32_t uid = 20; + +bool frame_send = false; + /** * Entry point for application */ int main(void) { + static uint8_t mac_address[6]; + static uint8_t device_eui[8] = {0x00, 0x00, 0x00, 0xff, 0xfe, 0x00, 0x00, 0x00}; + static const uint8_t application_eui[] = MBED_CONF_LORA_APPLICATION_EUI; + static const uint8_t application_key[] = MBED_CONF_LORA_APPLICATION_KEY; + static lorawan_connect_t connect_params = { + .connect_type = LORAWAN_CONNECTION_OTAA, + }; + // setup tracing setup_trace(); // stores the status of a call to LoRaWAN protocol lorawan_status_t retcode; + // Create unique, hardware-dependent EUI. + cy_get_bd_mac_address(mac_address); + // MAC address is in reverse sequence. + for (int i = 0; i < 3; ++i) { + device_eui[i] = mac_address[5 - i]; + device_eui[i + 5] = mac_address[2 - i]; + } + printf("\r\nDevice EUI is %02X", device_eui[0]); + for (int i = 1; i < 8; ++i) { + printf(":%02X", device_eui[i]); + } + printf("\r\n"); + // Initialize LoRaWAN stack if (lorawan.initialize(&ev_queue) != LORAWAN_STATUS_OK) { printf("\r\n LoRa initialization failed! \r\n"); @@ -128,7 +164,22 @@ printf("\r\n Adaptive data rate (ADR) - Enabled \r\n"); - retcode = lorawan.connect(); + // Specify class C + if (lorawan.set_device_class(CLASS_C) != LORAWAN_STATUS_OK) { + printf("\r\n setting class C failed! \r\n"); + return -1; + } + + printf("\r\n Class C - Enabled \r\n"); + + connect_params.connection_u.otaa.dev_eui = device_eui; + connect_params.connection_u.otaa.app_eui = const_cast<uint8_t *>(application_eui); + connect_params.connection_u.otaa.app_key = const_cast<uint8_t *>(application_key); + connect_params.connection_u.otaa.nb_trials = MBED_CONF_LORA_NB_TRIALS; + + g_timer.start(); + + retcode = lorawan.connect(connect_params); if (retcode == LORAWAN_STATUS_OK || retcode == LORAWAN_STATUS_CONNECT_IN_PROGRESS) { @@ -148,12 +199,14 @@ /** * Sends a message to the Network Server */ -static void send_message() +static void send_message(int entry) { uint16_t packet_len; int16_t retcode; int32_t sensor_value; + printf("\r\n[%8u] Sending message (%d) \r\n", g_timer.read_ms(), entry); + if (ds1820.begin()) { ds1820.startConversion(); sensor_value = ds1820.read(); @@ -167,6 +220,7 @@ packet_len = sprintf((char *) tx_buffer, "Dummy Sensor Value is %d", sensor_value); + led = 1; retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, tx_buffer, packet_len, MSG_UNCONFIRMED_FLAG); @@ -175,15 +229,18 @@ : printf("\r\n send() - Error code %d \r\n", retcode); if (retcode == LORAWAN_STATUS_WOULD_BLOCK) { + lorawan.cancel_sending(); + led = 0; //retry in 3 seconds if (MBED_CONF_LORA_DUTY_CYCLE_ON) { - ev_queue.call_in(3000, send_message); + ev_queue.call_in(3000, send_message, 3); } } return; } - printf("\r\n %d bytes scheduled for transmission \r\n", retcode); + frame_send = true; + printf("\r\n[%8u] %d bytes scheduled for transmission \r\n", g_timer.read_ms(), retcode); memset(tx_buffer, 0, sizeof(tx_buffer)); } @@ -201,12 +258,24 @@ return; } - printf(" RX Data on port %u (%d bytes): ", port, retcode); + printf("[%8u] RX Data on port %u (%d bytes): ", g_timer.read_ms(), port, retcode); for (uint8_t i = 0; i < retcode; i++) { printf("%02x ", rx_buffer[i]); } printf("\r\n"); - + + if (rx_buffer[0] != 0) { + for (int i = 0; i < 8; ++i) { + leds.set_color(i, BGR24_color_t(200,200,200)); + } + leds.refresh(); + } else { + for (int i = 0; i < 8; ++i) { + leds.set_color(i, BGR24_color_t(0,0,0)); + } + leds.refresh(); + } + memset(rx_buffer, 0, sizeof(rx_buffer)); } @@ -217,49 +286,62 @@ { switch (event) { case CONNECTED: - printf("\r\n Connection - Successful \r\n"); + printf("\r\n[%8u] Connection - Successful \r\n", g_timer.read_ms()); if (MBED_CONF_LORA_DUTY_CYCLE_ON) { - send_message(); + send_message(0); } else { - ev_queue.call_every(TX_TIMER, send_message); + printf("\r\n Scheduling new transmission every %u ms\r\n", TX_TIMER); + ev_queue.call_every(TX_TIMER, send_message, 1); } break; case DISCONNECTED: + led = 0; + frame_send = false; ev_queue.break_dispatch(); - printf("\r\n Disconnected Successfully \r\n"); + printf("\r\n[%8u] Disconnected Successfully \r\n", g_timer.read_ms()); break; case TX_DONE: - printf("\r\n Message Sent to Network Server \r\n"); - if (MBED_CONF_LORA_DUTY_CYCLE_ON) { - send_message(); + led = 0; + if (frame_send) { + frame_send = false; + printf("\r\n[%8u] Message Sent to Network Server \r\n", g_timer.read_ms()); + if (MBED_CONF_LORA_DUTY_CYCLE_ON) { + printf(" Scheduling message %u\r\n", uid); + //send_message(); + ev_queue.call_in(10000, send_message, uid++); + } + } else { + printf("\r\n[%8u] Duplicate TX_DONE !!! \r\n", g_timer.read_ms()); } break; case TX_TIMEOUT: case TX_ERROR: case TX_CRYPTO_ERROR: case TX_SCHEDULING_ERROR: - printf("\r\n Transmission Error - EventCode = %d \r\n", event); + led = 0; + frame_send = false; + printf("\r\n[%8u] Transmission Error - EventCode = %d \r\n", g_timer.read_ms(), event); // try again if (MBED_CONF_LORA_DUTY_CYCLE_ON) { - send_message(); + send_message(2); } break; case RX_DONE: - printf("\r\n Received message from Network Server \r\n"); + printf("\r\n[%8u] Received message from Network Server \r\n", g_timer.read_ms()); receive_message(); break; case RX_TIMEOUT: case RX_ERROR: - printf("\r\n Error in reception - Code = %d \r\n", event); + printf("\r\n[%8u] Error in reception - Code = %d \r\n", g_timer.read_ms(), event); break; case JOIN_FAILURE: printf("\r\n OTAA Failed - Check Keys \r\n"); break; case UPLINK_REQUIRED: - printf("\r\n Uplink required by NS \r\n"); + printf("\r\n[%8u] Uplink required by NS \r\n", g_timer.read_ms()); if (MBED_CONF_LORA_DUTY_CYCLE_ON) { - send_message(); + send_message(4); } break; default: