contain lorawan with serial_rx enabled
Dependencies: pulga-lorawan-drv SPI_MX25R Si1133 BME280
Revision 64:ed68ddac6360, committed 2021-03-02
- Comitter:
- ruschigo
- Date:
- Tue Mar 02 16:57:31 2021 +0000
- Parent:
- 63:4ec1808fb547
- Child:
- 65:4090220e19d2
- Commit message:
- testing Lora Splited
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lora_radio.cpp Tue Mar 02 16:57:31 2021 +0000 @@ -0,0 +1,132 @@ +/** + * Copyright (c) 2017, Arm Limited and affiliates. + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "lora_radio.h" +#include "lora_radio_helper.h" + +//#include "serial.h" + +using namespace events; + +// Max payload size can be LORAMAC_PHY_MAXPAYLOAD. +// This example only communicates with much shorter messages (<30 bytes). +// If longer messages are used, these buffers must be changed accordingly. + +static uint8_t lora_rx_buffer[30]; + + +/** + * Constructing Mbed LoRaWANInterface and passing it the radio object from lora_radio_helper. + */ +static LoRaWANInterface lorawan(radio); + + +void lorawan_add_callbacks(lorawan_app_callbacks_t LoraWanCallbacks){ + lorawan.add_app_callbacks(&LoraWanCallbacks); +} + +int lorawan_connect(){ + lorawan_status_t retcode; + retcode = lorawan.connect(); + + if (retcode == LORAWAN_STATUS_OK || + retcode == LORAWAN_STATUS_CONNECT_IN_PROGRESS) { + return 0; + } else { + return -1; + } +} + +int lorawan_enable_adaptive_datarate(){ + if (lorawan.enable_adaptive_datarate() != LORAWAN_STATUS_OK) { + return -1; + }else{ + return 0; + } +} + +int lorawan_set_confirmed_msg_retries(unsigned int number_of_retries){ + if (lorawan.set_confirmed_msg_retries(number_of_retries) + != LORAWAN_STATUS_OK) { + return -1; + }else{ + return 0; + } +} + +int lorawan_initialize_stack(EventQueue *ev_queue){ + if(lorawan.initialize(ev_queue) != LORAWAN_STATUS_OK){ + return -1; + }else + return 0; +} + +/** + * Sends a message to the Network Server + */ +int lora_send_message(uint8_t *msg_to_transmit, uint16_t packet_len) +{ + int16_t retcode; + int32_t sensor_value; + + retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, msg_to_transmit, packet_len,MSG_UNCONFIRMED_FLAG); + + if (retcode < 0) { + /*retcode == LORAWAN_STATUS_WOULD_BLOCK ? PrintDebugMsg("send - WOULD BLOCK\r\n") + : PrintDebugMsg("\r\n send() Error \r\n"); + */ + /*if (retcode == LORAWAN_STATUS_WOULD_BLOCK) { + //retry in 3 seconds + if (MBED_CONF_LORA_DUTY_CYCLE_ON) { + ev_queue.call_in(10000, send_message); + } + }*/ + + } + return (int)retcode; +} + +/** + * Receive a message from the Network Server + */ +int lora_receive_message() +{ + uint8_t port; + int flags; + int16_t retcode = lorawan.receive(lora_rx_buffer, sizeof(lora_rx_buffer), port, flags); + + if (retcode < 0) { + return (int)retcode; + } + + /*Justo for Debug + for (uint8_t i = 0; i < retcode; i++) { + pc.printf("%02x ", rx_buffer[i]); + } + pc.printf("\r\n"); + */ + /* + it clean the buffer, so useless + memset(rx_buffer, 0, sizeof(rx_buffer)); + */ + + return (int)retcode; +} + + + +// EOF
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lora_radio.h Tue Mar 02 16:57:31 2021 +0000 @@ -0,0 +1,53 @@ +#ifndef _LORA_RADIO_H +#define _LORA_RADIO_H +/** + * Copyright (c) 2017, Arm Limited and affiliates. + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include <stdio.h> +#include "mbed.h" + +#include "events/EventQueue.h" + +#include "lorawan/LoRaWANInterface.h" +#include "lorawan/system/lorawan_data_structures.h" + + +//#include "lora_radio_helper.h" + +extern void lorawan_add_callbacks(lorawan_app_callbacks_t LoraWanCallbacks); + +extern void lora_event_handler(lorawan_event_t event); + +extern int lorawan_connect(void); + +extern int lorawan_enable_adaptive_datarate(void); + +extern int lorawan_set_confirmed_msg_retries(unsigned int number_of_retries); + + +extern int lorawan_initialize_stack(EventQueue *ev_queue); + +/** + * Sends a message to the Network Server + */ +extern int lora_send_message(uint8_t *msg_to_transmit, uint16_t pkt_len); + +/** + * Receive a message from the Network Server + */ +extern int lora_receive_message(); + +#endif \ No newline at end of file
--- a/lora_radio_helper.h Fri Feb 26 18:11:06 2021 +0000 +++ b/lora_radio_helper.h Tue Mar 02 16:57:31 2021 +0000 @@ -1,3 +1,4 @@ + /** * Copyright (c) 2017, Arm Limited and affiliates. * SPDX-License-Identifier: Apache-2.0
--- a/main.cpp Fri Feb 26 18:11:06 2021 +0000 +++ b/main.cpp Tue Mar 02 16:57:31 2021 +0000 @@ -17,19 +17,20 @@ #include <stdio.h> #include "mbed.h" -#include "lorawan/LoRaWANInterface.h" -#include "lorawan/system/lorawan_data_structures.h" -#include "events/EventQueue.h" +//#include "lorawan/LoRaWANInterface.h" +//#include "lorawan/system/lorawan_data_structures.h" +//#include "events/EventQueue.h" // Application helpers //#include "DummySensor.h" #include "trace_helper.h" -#include "lora_radio_helper.h" +//#include "lora_radio_helper.h" //#include "BME280.h" #include "serial.h" #include "gps.h" + using namespace events; @@ -38,8 +39,8 @@ // Max payload size can be LORAMAC_PHY_MAXPAYLOAD. // This example only communicates with much shorter messages (<30 bytes). // If longer messages are used, these buffers must be changed accordingly. -uint8_t tx_buffer[256]; -uint8_t rx_buffer[30]; +//uint8_t tx_buffer[256]; +//uint8_t rx_buffer[30]; /* * Sets up an application dependent transmission timer in ms. Used only when Duty Cycling is off for testing @@ -93,12 +94,12 @@ * This will be passed to the LoRaWAN stack to queue events for the * application which in turn drive the application. */ -static void lora_event_handler(lorawan_event_t event); +//static void lora_event_handler(lorawan_event_t event); /** * Constructing Mbed LoRaWANInterface and passing it the radio object from lora_radio_helper. */ -static LoRaWANInterface lorawan(radio); +//static LoRaWANInterface lorawan(radio); /** * Application specific callbacks @@ -109,8 +110,8 @@ * Entry point for application */ -mbed::DigitalOut _alive_led(P1_13, 0); -mbed::DigitalOut _actuated_led(P1_14,1); +//mbed::DigitalOut _alive_led(P1_13, 0); +//mbed::DigitalOut _actuated_led(P1_14,1); //int lat=0; //int lon=0; int latitude=0; @@ -170,7 +171,7 @@ // led1 = !led1; } } - + void serial_rx(){ if(pc.readable()){ pc.printf("rx: %c\n", pc.getc()); @@ -254,35 +255,57 @@ lorawan_status_t retcode; // Initialize LoRaWAN stack - if (lorawan.initialize(&ev_queue) != LORAWAN_STATUS_OK) { + /*if (lorawan.initialize(&ev_queue) != LORAWAN_STATUS_OK) { pc.printf("\r\n LoRa initialization failed! \r\n"); return -1; + }*/ + + if(lorawan_initialize_stack(&ev_queue) != 0){ + return -1; } pc.printf("\r\n Mbed LoRaWANStack initialized \r\n"); // prepare application callbacks - callbacks.events = mbed::callback(lora_event_handler); + /*callbacks.events = mbed::callback(lora_event_handler); lorawan.add_app_callbacks(&callbacks); - + */ + + callbacks.events = mbed::callback(lora_event_handler); + lorawan_add_callbacks(callbacks); + // Set number of retries in case of CONFIRMED messages - if (lorawan.set_confirmed_msg_retries(CONFIRMED_MSG_RETRY_COUNTER) + /*if (lorawan.set_confirmed_msg_retries(CONFIRMED_MSG_RETRY_COUNTER) != LORAWAN_STATUS_OK) { pc.printf("\r\n set_confirmed_msg_retries failed! \r\n\r\n"); return -1; + }*/ + + if(lorawan_set_confirmed_msg_retries(CONFIRMED_MSG_RETRY_COUNTER) != 0){ + pc.printf("\r\n set_confirmed_msg_retries failed! \r\n\r\n"); + return -1; } + pc.printf("\r\n CONFIRMED message retries : %d \r\n", CONFIRMED_MSG_RETRY_COUNTER); - + /* // Enable adaptive data rate if (lorawan.enable_adaptive_datarate() != LORAWAN_STATUS_OK) { pc.printf("\r\n enable_adaptive_datarate failed! \r\n"); return -1; } + */ + + if(lorawan_enable_adaptive_datarate() != 0){ + pc.printf("\r\n enable_adaptive_datarate failed! \r\n"); + return -1; + } + pc.printf("\r\n Adaptive data rate (ADR) - Enabled \r\n"); - + + /* retcode = lorawan.connect(); if (retcode == LORAWAN_STATUS_OK || @@ -291,19 +314,26 @@ pc.printf("\r\n Connection error, code = %d \r\n", retcode); return -1; } - + */ + + if(lorawan_connect() != 0){ + pc.printf("\r\n Connection error, code = %d \r\n", retcode); + return -1; + } + pc.printf("\r\n Connection - In Progress ...\r\n"); - _actuated_led =0; + //_actuated_led =0; + // // make your event queue dispatching events forever ev_queue.dispatch_forever(); - return 0; } /** * Sends a message to the Network Server */ + /* static void send_message() { uint16_t packet_len; @@ -336,11 +366,12 @@ // printf("\r\n %d bytes scheduled for transmission \r\n", retcode); memset(tx_buffer, 0, sizeof(tx_buffer)); } +*/ /** * Receive a message from the Network Server */ -static void receive_message() +/*static void receive_message() { uint8_t port; int flags; @@ -359,20 +390,22 @@ memset(rx_buffer, 0, sizeof(rx_buffer)); } +*/ /** * Event handler */ -static void lora_event_handler(lorawan_event_t event) +void lora_event_handler(lorawan_event_t event) { switch (event) { case CONNECTED: pc.printf("\r\n Connection - Successful \r\n"); if (MBED_CONF_LORA_DUTY_CYCLE_ON) { - send_message(); - } else { - ev_queue.call_every(TX_TIMER, send_message); - } + //send_message(); + lora_send_message((uint8_t*)"testeLora", (uint16_t)10); + } //else { + //ev_queue.call_every(TX_TIMER, (void)lora_send_message((uint8_t*)"testeLoraEvery", (uint16_t)15)); + //} break; case DISCONNECTED: @@ -382,7 +415,7 @@ case TX_DONE: // printf("\r\n Message Sent to Network Server \r\n"); if (MBED_CONF_LORA_DUTY_CYCLE_ON) { - send_message(); + lora_send_message((uint8_t*)"teste", (uint16_t)6); } break; case TX_TIMEOUT: @@ -398,12 +431,12 @@ // printf("\r\n Transmission Error - EventCode = %d \r\n", event); // try again if (MBED_CONF_LORA_DUTY_CYCLE_ON) { - send_message(); + lora_send_message((uint8_t*)"teste2", (uint16_t)7); } break; case RX_DONE: // printf("\r\n Received message from Network Server \r\n"); - receive_message(); + lora_receive_message(); break; case RX_TIMEOUT: // printf("\r\n Transmission Error RX_Timeout"); @@ -417,7 +450,7 @@ case UPLINK_REQUIRED: // printf("\r\n Uplink required by NS \r\n"); if (MBED_CONF_LORA_DUTY_CYCLE_ON) { - send_message(); + lora_send_message((uint8_t*)"uplink", (uint16_t)7); } break; default:
--- a/serial.cpp Fri Feb 26 18:11:06 2021 +0000 +++ b/serial.cpp Tue Mar 02 16:57:31 2021 +0000 @@ -155,3 +155,8 @@ pc.attach(&serial_post_to_queue, RawSerial::RxIrq); return; } + + +void PrintDebugMsg(char *msg){ + pc.printf(msg); +} \ No newline at end of file
--- a/serial.h Fri Feb 26 18:11:06 2021 +0000 +++ b/serial.h Tue Mar 02 16:57:31 2021 +0000 @@ -3,7 +3,10 @@ #include "mbed.h" #include "gps.h" +#include "lora_radio.h" + #define CMD_PRINT_LAST_GPS 'g' +#define CMD_SEND_HELLO 'h' extern RawSerial pc; @@ -13,4 +16,6 @@ extern void serial_post_to_queue(void); +extern void PrintDebugMsg(char *msg); + #endif
--- a/serial_cmds.cpp Fri Feb 26 18:11:06 2021 +0000 +++ b/serial_cmds.cpp Tue Mar 02 16:57:31 2021 +0000 @@ -1,5 +1,6 @@ #include "serial.h" + bool SerialCommandRun(const char *msg) { char Option = msg[1]; /// recebo opcao @@ -13,10 +14,19 @@ //get_latitude pc.printf("<g%d,%d\n>", get_latitude(), get_longitude()); break; + + case CMD_SEND_HELLO: + pc.printf("<hHELLOANTONIO>"); + char hello[100] = "Ola Antonio!\n"; + int ret; + ret = lora_send_message((uint8_t*)hello, (uint16_t)sizeof(hello)); + if(ret > 0) + pc.printf("Send OK\n"); + else + pc.printf("Fail to Send\n"); + + break; - - default: - break; } return true;