Projeto teste do Programa de integracao com biblioteca SGAM_MDW !! Aqui visa um usuario que usa o projeto implementado para a placa NUCLEO F429ZI
Dependencies: sgam_mdw sgam_mdw_NUCLEOF429ZI_impl Cayenne-LPP
Diff: main.cpp
- Revision:
- 20:b25436619b59
- Parent:
- 17:579f6353c6fb
- Child:
- 23:6869d0465b03
--- a/main.cpp Sun Jul 07 15:36:11 2019 +0000 +++ b/main.cpp Thu Jul 11 22:29:05 2019 -0300 @@ -29,13 +29,15 @@ EventQueue ev_queue; static lorawan_app_callbacks_t callbacks; +Communication<LoraData>* comm = NULL; + int main() { D_LOG("=============== INICIO ===============\r\n"); Control* control = ctrl.getControler(); control->initialize(); - Communication<LoraData>* comm = (Communication<LoraData>*)control->getCommunication("LoRAWAN"); + comm = (Communication<LoraData>*)control->getCommunication("LoRAWAN"); //////////////////////////////////////////////////////////////////////////////////////////// // 1 - Configura os callbacks do loran WAN e inicializa os dados !! @@ -52,6 +54,10 @@ callbacks.events = mbed::callback(lora_event_handler); data->prepareCallBack(callbacks); + // Flags de leitura !! + data->read_port = MBED_CONF_LORA_APP_PORT; + data->read_flags = MSG_CONFIRMED_FLAG|MSG_UNCONFIRMED_FLAG; + ///////////////////////////////////////////////////////////////////////////////////////////// // 2 - INICIALIZA A CONEXAO if( comm->initialize(data) != TRUE) { @@ -84,20 +90,18 @@ { printf("receive_message()\n"); - // uint8_t rx_buffer[50] = { 0 }; - // int16_t retcode = lorawan.receive(MBED_CONF_LORA_APP_PORT, rx_buffer, - // sizeof(rx_buffer), - // MSG_CONFIRMED_FLAG|MSG_UNCONFIRMED_FLAG); + uint8_t rx_buffer[50] = { 0 }; + int16_t retcode = comm->read( rx_buffer, sizeof(rx_buffer) ); + if (retcode < 0) { + printf("receive() - Error code %d \r\n", retcode); + return; + } - // if (retcode < 0) { - // printf("receive() - Error code %d \r\n", retcode); - // return; - // } + printf("RX Data (%d bytes): ", retcode); + for (uint8_t i = 0; i < retcode; i++) { + printf("%02x ", rx_buffer[i]); + } - // printf("RX Data (%d bytes): ", retcode); - // for (uint8_t i = 0; i < retcode; i++) { - // printf("%02x ", rx_buffer[i]); - // } printf("\r\n"); } @@ -105,85 +109,86 @@ * Sends a message to the Network Server */ static void send_message() { - //printf("send_message()\n"); + printf("send_message()\n"); // YOUR CODE HERE // int16_t temperature = 10; - // //printf("temperature = (%d)\n", temperature); + // printf("temperature = (%d)\n", temperature); + // CayenneLPP payload(50); // payload.addTemperature(1, temperature); + // int16_t retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, payload.getBuffer(), payload.getSize(), MSG_UNCONFIRMED_FLAG); - // //printf("lorawan.send = retcode [%d]\n",retcode); + // printf("lorawan.send = retcode [%d]\n",retcode); // if (retcode < 0) { - // retcode == LORAWAN_STATUS_WOULD_BLOCK ? printf("send - Duty cycle violation\r\n") - // : printf("send() - Error code %d \r\n", retcode); + // retcode == LORAWAN_STATUS_WOULD_BLOCK + // ? printf("send - Duty cycle violation\r\n") + // : printf("send() - Error code %d \r\n", retcode); // if (retcode == LORAWAN_STATUS_NO_ACTIVE_SESSIONS) // printf("\r\n|-1017 - LORAWAN_STATUS_NO_ACTIVE_SESSIONS"); - // if (retcode == LORAWAN_STATUS_WOULD_BLOCK) { - // //retry in 3 seconds + // if (retcode == LORAWAN_STATUS_WOULD_BLOCK) { //retry in 3 seconds // ev_queue.call_in(3000, send_message); - // } - // else { + // } else { // ev_queue.call_in(TX_INTERVAL, send_message); // } + // return; // } // ev_queue.call_in(TX_INTERVAL, send_message); - //receive_message(); - //printf("%d bytes scheduled for transmission \r\n", retcode); + // receive_message(); + // printf("%d bytes scheduled for transmission \r\n", retcode); } /** * Event handler */ -static void lora_event_handler(lorawan_event_t event) -{ - // switch (event) { - // case CONNECTED: - // printf("# Connection - Successful \r\n"); - // if (MBED_CONF_LORA_DUTY_CYCLE_ON) { - // send_message(); - // } else { - // // ev_queue.call_in(TX_INTERVAL, send_message); - // } - // break; +static void lora_event_handler(lorawan_event_t event) { + switch (event) { + case CONNECTED: + printf("# Connection - Successful \r\n"); + if (MBED_CONF_LORA_DUTY_CYCLE_ON) { + send_message(); + } else { + ev_queue.call_in(TX_INTERVAL, send_message); + } + break; - // case DISCONNECTED: - // // ev_queue.break_dispatch(); - // printf("# Disconnected Successfully \r\n"); - // break; - // case RX_DONE: - // printf("# Received message from Network Server \r\n"); - // receive_message(); - // break; - // case RX_TIMEOUT: - // case RX_ERROR: - // printf("# Error in reception - Code = %d \r\n", event); - // break; - // case TX_DONE: - // count_message++; - // printf("# Message Sent to Network Server - Count [%d] \r\n", count_message); - // break; - // case TX_TIMEOUT: - // case TX_ERROR: - // case TX_CRYPTO_ERROR: - // case TX_SCHEDULING_ERROR: - // printf("# Transmission Error - EventCode = %d \r\n", event); - // break; - // case JOIN_FAILURE: - // printf("# OTAA Failed - Check Keys \r\n"); - // break; - // case UPLINK_REQUIRED: - // printf("# Uplink required by NS \r\n"); - // send_message(); - // break; - // default: - // printf("# Unknown Event \r\n"); - // MBED_ASSERT("# Unknown Event"); - // } + case DISCONNECTED: + ev_queue.break_dispatch(); + printf("# Disconnected Successfully \r\n"); + break; + case RX_DONE: + printf("# Received message from Network Server \r\n"); + receive_message(); + break; + case RX_TIMEOUT: + case RX_ERROR: + printf("# Error in reception - Code = %d \r\n", event); + break; + case TX_DONE: + count_message++; + printf("# Message Sent to Network Server - Count [%d] \r\n", count_message); + break; + case TX_TIMEOUT: + case TX_ERROR: + case TX_CRYPTO_ERROR: + case TX_SCHEDULING_ERROR: + printf("# Transmission Error - EventCode = %d \r\n", event); + break; + case JOIN_FAILURE: + printf("# OTAA Failed - Check Keys \r\n"); + break; + case UPLINK_REQUIRED: + printf("# Uplink required by NS \r\n"); + send_message(); + break; + default: + printf("# Unknown Event \r\n"); + // MBED_ASSERT("# Unknown Event"); + } }