example using mbed loramac with GPS in application layer
This repository can work on NAmote72.
This main.cpp generates Cayenne LPP GPS payload.
See mbed documentation for provisioning instructions.
Revision 0:2e040cc7f7b8, committed 2019-01-03
- Comitter:
- Wayne Roberts
- Date:
- Thu Jan 03 16:18:09 2019 -0800
- Commit message:
- initial commit
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Jenkinsfile Thu Jan 03 16:18:09 2019 -0800 @@ -0,0 +1,174 @@ +properties ([[$class: 'ParametersDefinitionProperty', parameterDefinitions: [ + [$class: 'StringParameterDefinition', name: 'mbed_os_revision', defaultValue: '', description: 'Revision of mbed-os to build. To access mbed-os PR use format "pull/PR number/head"'], + [$class: 'BooleanParameterDefinition', name: 'regions_build_test', defaultValue: true, description: 'Test build all available regions'] + ]]]) + +library 'mbed-lib' + +if (env.MBED_OS_REVISION == null) { + echo 'First run in this branch, using default parameter values' + env.MBED_OS_REVISION = '' +} +if (env.MBED_OS_REVISION == '') { + echo 'Using mbed OS revision from mbed-os.lib' +} else { + echo "Using given mbed OS revision: ${env.MBED_OS_REVISION}" + if (env.MBED_OS_REVISION.matches('pull/\\d+/head')) { + echo "Revision is a Pull Request" + } +} + +// All available regions +def regions = [ + "\"0\"", "\"1\"", "\"2\"", "\"3\"", "\"4\"", "\"5\"", "\"6\"", "\"7\"", "\"8\"", + "\"EU868\"", "\"AS923\"", "\"AU915\"", "\"CN470\"", "\"CN779\"", "\"EU433\"", + "\"IN865\"", "\"KR920\"", "\"US915\"" +] + +// Supported targets +def targets = [ + "K64F", + "MTB_MTS_XDOT", + "MTB_MURATA_ABZ", + "MTS_MDOT_F411RE", + "DISCO_L072CZ_LRWAN1", + "MTB_ADV_WISE_1510", + "MTB_RAK811" +] + +// Map toolchains to compilers +def toolchains = [ + ARM: "armcc", + GCC_ARM: "arm-none-eabi-gcc", + IAR: "iar_arm", + ARMC6: "arm6" +] + +def stepsForParallel = [:] + +// Jenkins pipeline does not support map.each, we need to use oldschool for loop +for (int i = 0; i < targets.size(); i++) { + for(int j = 0; j < toolchains.size(); j++) { + def target = targets.get(i) + def toolchain = toolchains.keySet().asList().get(j) + def compilerLabel = toolchains.get(toolchain) + + // Skip unwanted combination + if (target == "MTB_MURATA_ABZ" && toolchain == "GCC_ARM") { + continue + } + if (target == "DISCO_L072CZ_LRWAN1" && toolchain == "GCC_ARM") { + continue + } + if (target == "MTB_RAK811" && toolchain == "GCC_ARM") { + continue + } + + def stepName = "${target} ${toolchain}" + + stepsForParallel[stepName] = buildStep(target, compilerLabel, toolchain) + } +} + +def stepsForRegional = [:] + +if (params.regions_build_test == true) { + stepsForRegional["REGION BUILDER"] = build_regions(regions) +} + +timestamps { + parallel stepsForParallel + parallel stepsForRegional +} + +def buildStep(target, compilerLabel, toolchain) { + return { + stage ("${target}_${compilerLabel}") { + node ("${compilerLabel}") { + deleteDir() + dir("mbed-os-example-lorawan") { + checkout scm + execute("mbed deploy --protocol ssh") + + // Set mbed-os to revision received as parameter + if (env.MBED_OS_REVISION != '') { + dir("mbed-os") { + if (env.MBED_OS_REVISION.matches('pull/\\d+/head')) { + // Use mbed-os PR and switch to branch created + execute("git fetch origin ${env.MBED_OS_REVISION}:_PR_") + execute("git checkout _PR_") + } else { + execute("git checkout ${env.MBED_OS_REVISION}") + } + } + } + + // Adjust stack size and crystal values + if ("${target}" == "DISCO_L072CZ_LRWAN1") { + execute("sed -i 's/#define RCC_HSICALIBRATION_DEFAULT ((uint32_t)0x10)/#define RCC_HSICALIBRATION_DEFAULT ((uint32_t)0x13)/' \ + mbed-os/targets/TARGET_STM/TARGET_STM32L0/device/stm32l0xx_hal_rcc.h") + } + + if ("${target}" == "MTB_MURATA_ABZ") { + execute("sed -i 's/define symbol __size_heap__ = 0x800;/define symbol __size_heap__ = 0x1000;/' \ + mbed-os/targets/TARGET_STM/TARGET_STM32L0/TARGET_STM32L0x2xZ/device/TOOLCHAIN_IAR/stm32l082xZ.icf") + execute("sed -i 's/#define RCC_HSICALIBRATION_DEFAULT ((uint32_t)0x10)/#define RCC_HSICALIBRATION_DEFAULT ((uint32_t)0x16)/' \ + mbed-os/targets/TARGET_STM/TARGET_STM32L0/device/stm32l0xx_hal_rcc.h") + } + + if ("${target}" == "MTB_MTS_XDOT") { + execute("sed -i 's/define symbol __size_heap__ = 0x800;/define symbol __size_heap__ = 0x1800;/' \ + mbed-os/targets/TARGET_STM/TARGET_STM32L1/TARGET_MTB_MTS_XDOT/device/TOOLCHAIN_IAR/stm32l152xc.icf") + } + + if ("${target}" == "MTB_RAK811") { + execute("sed -i 's/#define RCC_HSICALIBRATION_DEFAULT (0x10U)/#define RCC_HSICALIBRATION_DEFAULT (0x11U)/' \ + mbed-os/targets/TARGET_STM/TARGET_STM32L1/device/stm32l1xx_hal_rcc.h") + execute("sed -i 's/define symbol __size_heap__ = 0x800;/define symbol __size_heap__ = 0x1800;/' \ + mbed-os/targets/TARGET_STM/TARGET_STM32L1/TARGET_MTB_RAK811/device/TOOLCHAIN_IAR/stm32l152xba.icf") + } + + execute("mbed compile --build out/${target}_${toolchain}/ -m ${target} -t ${toolchain} -c") + } + stash name: "${target}_${toolchain}", includes: '**/mbed-os-example-lorawan.bin' + archive '**/mbed-os-example-lorawan.bin' + step([$class: 'WsCleanup']) + } + } + } +} + +def build_regions(regions) { + return { + stage ("region_builder_K64F_GCC_ARM") { + node ("arm-none-eabi-gcc") { + deleteDir() + dir("mbed-os-example-lorawan") { + checkout scm + execute("mbed deploy --protocol ssh") + + if (env.MBED_OS_REVISION != '') { + dir("mbed-os") { + if (env.MBED_OS_REVISION.matches('pull/\\d+/head')) { + execute("git fetch origin ${env.MBED_OS_REVISION}:_PR_") + execute("git checkout _PR_") + } else { + execute("git checkout ${env.MBED_OS_REVISION}") + } + } + } + //Initial sed to string format for find & replacing + execute("sed -i 's/\"lora.phy\": 0,/\"lora.phy\": \"0\",/' mbed_app.json") + //lora.phy 0 build tested above already + for (int i = 1; i < regions.size(); i++) { + def curr_region = regions.get(i) + def prev_region = regions.get(i-1) + execute("sed -i 's/\"lora.phy\": ${prev_region},/\"lora.phy\": ${curr_region},/' mbed_app.json") + echo "Building region: ${curr_region}" + execute("mbed compile -t GCC_ARM -m K64F") + } + } + } + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/README.md Thu Jan 03 16:18:09 2019 -0800 @@ -0,0 +1,173 @@ +# Example LoRaWAN application for Mbed-OS + +This is an example application based on `Mbed-OS` LoRaWAN protocol APIs. The Mbed-OS LoRaWAN stack implementation is compliant with LoRaWAN v1.0.2 specification. + +## Getting started + +This application can work with any Network Server if you have correct credentials for the said Network Server. + +### Download the application + +```sh +$ mbed import mbed-os-example-lorawan +$ cd mbed-os-example-lorawan + +#OR + +$ git clone git@github.com:ARMmbed/mbed-os-example-lorawan.git +$ cd mbed-os-example-lorawan +$ mbed deploy +``` + +### Selecting radio + +Mbed OS provides inherent support for a variety of modules. If your device is one of the those modules, you may skip this part. The correct radio type and pin set is already provided for the modules in the `target-overrides` field. For more information on supported modules, please refer to the [module support section](#module-support) + +If you are using an Mbed Enabled radio shield such as [Mbed SX1276 shield LoRa](https://os.mbed.com/components/SX1276MB1xAS/) or [Mbed SX1272 LoRa shield ](https://os.mbed.com/components/SX1272MB2xAS/) with any Mbed Enabled board, this part is relevant. You can use any Mbed Enabled board that comes with an arduino form factor. + +Please select your radio type by modifying the `lora-radio` field and providing a pin set if it is different from the default. For example: + +```json +"lora-radio": { + "help": "Which radio to use (options: SX1272,SX1276)", + "value": "SX1272" +}, +``` + +### Add network credentials + +Open the file `mbed_app.json` in the root directory of your application. This file contains all the user specific configurations your application and the Mbed OS LoRaWAN stack need. Network credentials are typically provided by LoRa network provider. + +#### For OTAA + +Please add `Device EUI`, `Application EUI` and `Application Key` needed for Over-the-air-activation(OTAA). For example: + +```json + +"lora.device-eui": "{ YOUR_DEVICE_EUI }", +"lora.application-eui": "{ YOUR_APPLICATION_EUI }", +"lora.application-key": "{ YOUR_APPLICATION_KEY }" +``` + +#### For ABP + +For Activation-By-Personalization (ABP) connection method, modify the `mbed_app.json` to enable ABP. You can do it by simply turning off OTAA. For example: + +```json +"lora.over-the-air-activation": false, +``` + +In addition to that, you need to provide `Application Session Key`, `Network Session Key` and `Device Address`. For example: + +```json +"lora.appskey": "{ YOUR_APPLICATION_SESSION_KEY }", +"lora.nwkskey": "{ YOUR_NETWORK_SESSION_KEY }", +"lora.device-address": " YOUR_DEVICE_ADDRESS_IN_HEX " +``` + +## Configuring the application + +The Mbed OS LoRaWAN stack provides a lot of configuration controls to the application through the Mbed OS configuration system. The previous section discusses some of these controls. This section highlights some useful features that you can configure. + +### Selecting a PHY + +The LoRaWAN protocol is subject to various country specific regulations concerning radio emissions. That's why the Mbed OS LoRaWAN stack provides a `LoRaPHY` class that you can use to implement any region specific PHY layer. Currently, the Mbed OS LoRaWAN stack provides 10 different country specific implementations of `LoRaPHY` class. Selection of a specific PHY layer happens at compile time. By default, the Mbed OS LoRaWAN stack uses `EU 868 MHz` PHY. An example of selecting a PHY can be: + +```josn + "phy": { + "help": "LoRa PHY region. 0 = EU868 (default), 1 = AS923, 2 = AU915, 3 = CN470, 4 = CN779, 5 = EU433, 6 = IN865, 7 = KR920, 8 = US915, 9 = US915_HYBRID", + "value": "0" + }, +``` + +### Duty cycling + +LoRaWAN v1.0.2 specifcation is exclusively duty cycle based. This application comes with duty cycle enabled by default. In other words, the Mbed OS LoRaWAN stack enforces duty cycle. The stack keeps track of transmissions on the channels in use and schedules transmissions on channels that become available in the shortest time possible. We recommend you keep duty cycle on for compliance with your country specific regulations. + +However, you can define a timer value in the application, which you can use to perform a periodic uplink when the duty cycle is turned off. Such a setup should be used only for testing or with a large enough timer value. For example: + +```josn +"target_overrides": { + "*": { + "lora.duty-cycle-on": false + }, + } +} +``` + +## Module support + +Here is a nonexhaustive list of boards and modules that we have tested with the Mbed OS LoRaWAN stack. + +- MultiTech mDot. +- MultiTech xDot. +- LTEK_FF1705. +- Advantech Wise 1510. +- ST B-L072Z-LRWAN1 LoRa®Discovery kit (with muRata radio chip). + +## Compiling the application + +Use Mbed CLI commands to generate a binary for the application. +For example: + +```sh +$ mbed compile -m YOUR_TARGET -t ARM +``` + +## Running the application + +Drag and drop the application binary from `BUILD/YOUR_TARGET/ARM/mbed-os-example-lora.bin` to your Mbed enabled target hardware, which appears as a USB device on your host machine. + +Attach a serial console emulator of your choice (for example, PuTTY, Minicom or screen) to your USB device. Set the baudrate to 115200 bit/s, and reset your board by pressing the reset button. + +You should see an output similar to this: + +``` +Mbed LoRaWANStack initialized + + CONFIRMED message retries : 3 + + Adaptive data rate (ADR) - Enabled + + Connection - In Progress ... + + Connection - Successful + + Dummy Sensor Value = 2.1 + + 25 bytes scheduled for transmission + + Message Sent to Network Server + +``` + +## [Optional] Adding trace library +To enable Mbed trace, add to your `mbed_app.json` the following fields: + +```json + "target_overrides": { + "*": { + "mbed-trace.enable": true + } + } +``` +The trace is disabled by default to save RAM and reduce main stack usage (see chapter Memory optimization). + +**Please note that some targets with small RAM size (e.g. DISCO_L072CZ_LRWAN1 and MTB_MURATA_ABZ) mbed traces cannot be enabled without increasing the default** `"main_stack_size": 1024`**.** + +## [Optional] Memory optimization + +Using `Arm CC compiler` instead of `GCC` reduces `3K` of RAM. Currently the application takes about `15K` of static RAM with Arm CC, which spills over for the platforms with `20K` of RAM because you need to leave space, about `5K`, for dynamic allocation. So if you reduce the application stack size, you can barely fit into the 20K platforms. + +For example, add the following into `config` section in your `mbed_app.json`: + +``` +"main_stack_size": { + "value": 2048 +} +``` + +Essentially you can make the whole application with Mbed LoRaWAN stack in 6K if you drop the RTOS from Mbed OS and use a smaller standard C/C++ library like new-lib-nano. Please find instructions [here](https://os.mbed.com/blog/entry/Reducing-memory-usage-with-a-custom-prin/). + + +For more information, please follow this [blog post](https://os.mbed.com/blog/entry/Reducing-memory-usage-by-tuning-RTOS-con/).
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib_gps.lib Thu Jan 03 16:18:09 2019 -0800 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/dudmuck/code/lib_gps/#65fdafeb57e0
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lora_radio_helper.h Thu Jan 03 16:18:09 2019 -0800 @@ -0,0 +1,75 @@ +/** + * 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 "lorawan/LoRaRadio.h" + +#ifndef APP_LORA_RADIO_HELPER_H_ +#define APP_LORA_RADIO_HELPER_H_ + +#include "SX1272_LoRaRadio.h" +#include "SX1276_LoRaRadio.h" + +#define SX1272 0xFF +#define SX1276 0xEE + +#if (MBED_CONF_APP_LORA_RADIO == SX1272) + + SX1272_LoRaRadio radio(MBED_CONF_APP_LORA_SPI_MOSI, + MBED_CONF_APP_LORA_SPI_MISO, + MBED_CONF_APP_LORA_SPI_SCLK, + MBED_CONF_APP_LORA_CS, + MBED_CONF_APP_LORA_RESET, + MBED_CONF_APP_LORA_DIO0, + MBED_CONF_APP_LORA_DIO1, + MBED_CONF_APP_LORA_DIO2, + MBED_CONF_APP_LORA_DIO3, + MBED_CONF_APP_LORA_DIO4, + MBED_CONF_APP_LORA_DIO5, + MBED_CONF_APP_LORA_RF_SWITCH_CTL1, + MBED_CONF_APP_LORA_RF_SWITCH_CTL2, + MBED_CONF_APP_LORA_TXCTL, + MBED_CONF_APP_LORA_RXCTL, + MBED_CONF_APP_LORA_ANT_SWITCH, + MBED_CONF_APP_LORA_PWR_AMP_CTL, + MBED_CONF_APP_LORA_TCXO); + +#elif (MBED_CONF_APP_LORA_RADIO == SX1276) + + SX1276_LoRaRadio radio(MBED_CONF_APP_LORA_SPI_MOSI, + MBED_CONF_APP_LORA_SPI_MISO, + MBED_CONF_APP_LORA_SPI_SCLK, + MBED_CONF_APP_LORA_CS, + MBED_CONF_APP_LORA_RESET, + MBED_CONF_APP_LORA_DIO0, + MBED_CONF_APP_LORA_DIO1, + MBED_CONF_APP_LORA_DIO2, + MBED_CONF_APP_LORA_DIO3, + MBED_CONF_APP_LORA_DIO4, + MBED_CONF_APP_LORA_DIO5, + MBED_CONF_APP_LORA_RF_SWITCH_CTL1, + MBED_CONF_APP_LORA_RF_SWITCH_CTL2, + MBED_CONF_APP_LORA_TXCTL, + MBED_CONF_APP_LORA_RXCTL, + MBED_CONF_APP_LORA_ANT_SWITCH, + MBED_CONF_APP_LORA_PWR_AMP_CTL, + MBED_CONF_APP_LORA_TCXO); + +#else + #error "Unknown LoRa radio specified (SX1272,SX1276 are valid)" +#endif + +#endif /* APP_LORA_RADIO_HELPER_H_ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Thu Jan 03 16:18:09 2019 -0800 @@ -0,0 +1,406 @@ +/** + * 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 "lorawan/LoRaWANInterface.h" +#include "lorawan/system/lorawan_data_structures.h" +#include "events/EventQueue.h" + +#include "mbed.h" +// Application helpers +#include "gps.h" +#include "trace_helper.h" +#include "lora_radio_helper.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. +uint8_t tx_buffer[30]; +uint8_t rx_buffer[30]; + +/* + * Sets up an application dependent transmission timer in ms. Used only when Duty Cycling is off for testing + */ +#define TX_TIMER 10000 + +/** + * Maximum number of events for the event queue. + * 10 is the safe number for the stack events, however, if application + * also uses the queue for whatever purposes, this number should be increased. + */ +#define MAX_NUMBER_OF_EVENTS 10 + +/** + * Maximum number of retries for CONFIRMED messages before giving up + */ +#define CONFIRMED_MSG_RETRY_COUNTER 3 + +/** + * Dummy pin for dummy sensor + */ +#define PC_9 0 + +/** + * Dummy sensor class object + */ +//DS1820 ds1820(PC_9); + +/** +* This event queue is the global event queue for both the +* application and stack. To conserve memory, the stack is designed to run +* in the same thread as the application and the application is responsible for +* providing an event queue to the stack that will be used for ISR deferment as +* well as application information event queuing. +*/ +static EventQueue ev_queue(MAX_NUMBER_OF_EVENTS * EVENTS_EVENT_SIZE); + +/** + * Event handler. + * + * 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); + +/** + * Constructing Mbed LoRaWANInterface and passing it down the radio object. + */ +static LoRaWANInterface lorawan(radio); + +/** + * Application specific callbacks + */ +static lorawan_app_callbacks_t callbacks; + +#ifdef TARGET_MOTE_L152RC + GPS gps(PB_6, PB_7, PB_11); // on-board GPS pins (tx, rx, en) +#elif defined(TARGET_FF_MORPHO) + /* https://www.sparkfun.com/products/13740 */ + #ifdef TARGET_DISCO_L072CZ_LRWAN1 + GPS gps(PA_9, PA_10, NC); + #else + GPS gps(PC_10, PC_11, NC); + #endif +#endif + +void gps_service() +{ + //int alt; + + gps.service(); + + //alt = atoi( gps.NmeaGpsData.NmeaAltitude ); + //printf("lat:%f lon:%f alt:%d\r\n", gps.Latitude, gps.Longitude, alt); +} + +#ifdef TARGET_MOTE_L152RC +typedef enum +{ + BOARD_VERSION_NONE = 0, + BOARD_VERSION_2, + BOARD_VERSION_3, +}BoardVersion_t; + +DigitalOut Pc7( PC_7 ); +DigitalIn Pc1( PC_1 ); +BoardVersion_t BoardGetVersion( void ) +{ + Pc7 = 1; + char first = Pc1; + Pc7 = 0; + + if( first && !Pc1 ) + { + return BOARD_VERSION_2; + } + else + { + return BOARD_VERSION_3; + } +} +AnalogIn *Battery; +#endif /* TARGET_MOTE_L152RC */ + +/** + * Entry point for application + */ +int main (void) +{ + // setup tracing + setup_trace(); + + // stores the status of a call to LoRaWAN protocol + lorawan_status_t retcode; + +#ifdef TARGET_MOTE_L152RC + switch( BoardGetVersion( ) ) + { + case BOARD_VERSION_2: + Battery = new AnalogIn( PA_0 ); + gps.en_invert = true; + printf("v2-mote\r\n"); + break; + case BOARD_VERSION_3: + Battery = new AnalogIn( PA_1 ); + gps.en_invert = false; + printf("v3-mote\r\n"); + break; + default: + break; + } +#endif /* TARGET_MOTE_L152RC */ + gps.init(); + gps.enable(1); + gps.m_uart.baud(9600); // override platform serial baud rate + + // Initialize LoRaWAN stack + if (lorawan.initialize(&ev_queue) != LORAWAN_STATUS_OK) { + printf("\r\n LoRa initialization failed! \r\n"); + return -1; + } + + printf("\r\n Mbed LoRaWANStack initialized \r\n"); + + // prepare application callbacks + callbacks.events = mbed::callback(lora_event_handler); + lorawan.add_app_callbacks(&callbacks); + + // Set number of retries in case of CONFIRMED messages + if (lorawan.set_confirmed_msg_retries(CONFIRMED_MSG_RETRY_COUNTER) + != LORAWAN_STATUS_OK) { + printf("\r\n set_confirmed_msg_retries failed! \r\n\r\n"); + return -1; + } + + 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) { + printf("\r\n enable_adaptive_datarate failed! \r\n"); + return -1; + } + + printf("\r\n Adaptive data rate (ADR) - Enabled \r\n"); + + retcode = lorawan.connect(); + + if (retcode == LORAWAN_STATUS_OK || + retcode == LORAWAN_STATUS_CONNECT_IN_PROGRESS) { + } else { + printf("\r\n Connection error, code = %d \r\n", retcode); + return -1; + } + + printf("\r\n Connection - In Progress ...\r\n"); + + // make your event queue dispatching events forever + ev_queue.dispatch_forever(); + + return 0; +} + +#define LPP_DIGITAL_INPUT 0 // 1 byte +#define LPP_DIGITAL_OUTPUT 1 // 1 byte +#define LPP_ANALOG_INPUT 2 // 2 bytes, 0.01 signed +#define LPP_ANALOG_OUTPUT 3 // 2 bytes, 0.01 signed +#define LPP_LUMINOSITY 101 // 2 bytes, 1 lux unsigned +#define LPP_PRESENCE 102 // 1 byte, 1 +#define LPP_TEMPERATURE 103 // 2 bytes, 0.1°C signed +#define LPP_RELATIVE_HUMIDITY 104 // 1 byte, 0.5% unsigned +#define LPP_ACCELEROMETER 113 // 2 bytes per axis, 0.001G +#define LPP_BAROMETRIC_PRESSURE 115 // 2 bytes 0.1 hPa Unsigned +#define LPP_GYROMETER 134 // 2 bytes per axis, 0.01 °/s +#define LPP_GPS 136 // 3 byte lon/lat 0.0001 °, 3 bytes alt 0.01m + + +// Data ID + Data Type + Data Size +#define LPP_DIGITAL_INPUT_SIZE 3 +#define LPP_DIGITAL_OUTPUT_SIZE 3 +#define LPP_ANALOG_INPUT_SIZE 4 +#define LPP_ANALOG_OUTPUT_SIZE 4 +#define LPP_LUMINOSITY_SIZE 4 +#define LPP_PRESENCE_SIZE 3 +#define LPP_TEMPERATURE_SIZE 4 +#define LPP_RELATIVE_HUMIDITY_SIZE 3 +#define LPP_ACCELEROMETER_SIZE 8 +#define LPP_BAROMETRIC_PRESSURE_SIZE 4 +#define LPP_GYROMETER_SIZE 8 +#define LPP_GPS_SIZE 11 + +#define CAYENNE_CH_GPS 5 + + +/** + * Sends a message to the Network Server + */ +static void send_message() +{ + int32_t lat, lon, alt; + uint16_t packet_len; + int16_t retcode; + + lat = gps.Latitude * 10000; + lon = gps.Longitude * 10000; + alt = atoi(gps.NmeaGpsData.NmeaAltitude) * 100; + + packet_len = 0; + tx_buffer[packet_len++] = CAYENNE_CH_GPS; + tx_buffer[packet_len++] = LPP_GPS; + tx_buffer[packet_len++] = lat >> 16; + tx_buffer[packet_len++] = lat >> 8; + tx_buffer[packet_len++] = lat; + tx_buffer[packet_len++] = lon >> 16; + tx_buffer[packet_len++] = lon >> 8; + tx_buffer[packet_len++] = lon; + tx_buffer[packet_len++] = alt >> 16; + tx_buffer[packet_len++] = alt >> 8; + tx_buffer[packet_len++] = alt; + + retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, tx_buffer, packet_len, + MSG_CONFIRMED_FLAG); + + if (retcode < 0) { + retcode == LORAWAN_STATUS_WOULD_BLOCK ? printf("send - WOULD BLOCK\r\n") + : printf("\r\n send() - Error code %d \r\n", retcode); + + if (retcode == LORAWAN_STATUS_WOULD_BLOCK) { + //retry in 3 seconds + if (MBED_CONF_LORA_DUTY_CYCLE_ON) { + ev_queue.call_in(3000, send_message); + } + } + return; + } + for (alt = 0; alt < packet_len; alt++) { + printf("%02x", tx_buffer[alt]); + } + + alt = atoi( gps.NmeaGpsData.NmeaAltitude ); + printf("\r\n %d bytes scheduled for tx\tlat:%f lon:%f alt:%d\r\n", retcode, gps.Latitude, gps.Longitude, (int)alt); + memset(tx_buffer, 0, sizeof(tx_buffer)); +} + +/** + * Receive a message from the Network Server + */ +static void receive_message() +{ + int16_t retcode; + retcode = lorawan.receive(MBED_CONF_LORA_APP_PORT, rx_buffer, + sizeof(rx_buffer), + MSG_CONFIRMED_FLAG|MSG_UNCONFIRMED_FLAG); + + if (retcode < 0) { + printf("\r\n receive() - Error code %d \r\n", retcode); + return; + } + + printf(" Data:"); + + for (uint8_t i = 0; i < retcode; i++) { + printf("%x", rx_buffer[i]); + } + + printf("\r\n Data Length: %d\r\n", retcode); + + memset(rx_buffer, 0, sizeof(rx_buffer)); +} + +/** + * Event handler + */ +static void lora_event_handler(lorawan_event_t event) +{ + + switch (event) { + case CONNECTED: + printf("\r\n Connection - Successful "); + ev_queue.call_every(1000, gps_service); + if (MBED_CONF_LORA_DUTY_CYCLE_ON) { + printf("DUTY_ON\r\n"); + send_message(); + } else { + printf("(duty off)\r\n"); + ev_queue.call_every(TX_TIMER, send_message); + } + + break; + case DISCONNECTED: + ev_queue.break_dispatch(); + printf("\r\n Disconnected Successfully \r\n"); + break; + case TX_DONE: + printf("\r\n Message Sent to Network Server \r\n"); + /*if (MBED_CONF_LORA_DUTY_CYCLE_ON) { + send_message(); + }*/ + break; + case TX_TIMEOUT: + printf("\r\nTX_TIMEOUT\r\n"); + // try again + /*if (MBED_CONF_LORA_DUTY_CYCLE_ON) { + send_message(); + }*/ + break; + case TX_ERROR: + // no ack was received after enough retries + printf("\r\nTX_ERROR\r\n"); + // try again + /*if (MBED_CONF_LORA_DUTY_CYCLE_ON) { + send_message(); + }*/ + break; + case TX_CRYPTO_ERROR: + printf("\r\nTX_CRYPTO_ERROR\r\n"); + // try again + /*if (MBED_CONF_LORA_DUTY_CYCLE_ON) { + send_message(); + }*/ + break; + case TX_SCHEDULING_ERROR: + printf("\r\nTX_SCHEDULING_ERROR\r\n"); + // try again + /*if (MBED_CONF_LORA_DUTY_CYCLE_ON) { + send_message(); + }*/ + break; + case RX_DONE: + printf("\r\n Received message from Network Server \r\n"); + receive_message(); + break; + case RX_TIMEOUT: + case RX_ERROR: + printf("\r\n Error in reception - Code = %d \r\n", 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"); + if (MBED_CONF_LORA_DUTY_CYCLE_ON) { + send_message(); + } + break; + default: + MBED_ASSERT("Unknown Event"); + } +} + +// EOF
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-os.lib Thu Jan 03 16:18:09 2019 -0800 @@ -0,0 +1,1 @@ +https://github.com/ARMmbed/mbed-os/#6a0a86538c0b9b2bfcc4583b1e2b7fea8f4e71e9
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-semtech-lora-rf-drivers.lib Thu Jan 03 16:18:09 2019 -0800 @@ -0,0 +1,1 @@ +https://github.com/ARMmbed/mbed-semtech-lora-rf-drivers/#16958f814d505cfbbedfa16d9bf8b9dff0e0442b
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed_app.json Thu Jan 03 16:18:09 2019 -0800 @@ -0,0 +1,261 @@ +{ + "config": { + "lora-radio": { + "help": "Which radio to use (options: SX1272,SX1276)", + "value": "SX1272" + }, + "main_stack_size": { "value": 4096 }, + + "lora-spi-mosi": { "value": "NC" }, + "lora-spi-miso": { "value": "NC" }, + "lora-spi-sclk": { "value": "NC" }, + "lora-cs": { "value": "NC" }, + "lora-reset": { "value": "NC" }, + "lora-dio0": { "value": "NC" }, + "lora-dio1": { "value": "NC" }, + "lora-dio2": { "value": "NC" }, + "lora-dio3": { "value": "NC" }, + "lora-dio4": { "value": "NC" }, + "lora-dio5": { "value": "NC" }, + "lora-rf-switch-ctl1": { "value": "NC" }, + "lora-rf-switch-ctl2": { "value": "NC" }, + "lora-txctl": { "value": "NC" }, + "lora-rxctl": { "value": "NC" }, + "lora-ant-switch": { "value": "NC" }, + "lora-pwr-amp-ctl": { "value": "NC" }, + "lora-tcxo": { "value": "NC" } + }, + "target_overrides": { + "*": { + "platform.stdio-convert-newlines": true, + "platform.stdio-baud-rate": 115200, + "platform.default-serial-baud-rate": 115200, + "lora.over-the-air-activation": true, + "lora.duty-cycle-on": false, + "lora.phy": "US915", + "lora.device-eui": "{0x00,0x25,0x0C,0x01,0x00,0x00,0x00,0x08}", + "lora.application-eui": "{0x00,0x25,0x0C,0x00,0x00,0x01,0x00,0x01}", + "lora.application-key": "{0x3D,0xFD,0xF3,0x80,0x45,0x0E,0x8B,0x8D,0x3E,0xD5,0x89,0x25,0xAA,0xD4,0x23,0x53}" + }, + + "MOTE_L152RC": { + "lora-spi-mosi": "PB_15", + "lora-spi-miso": "PB_14", + "lora-spi-sclk": "PB_13", + "lora-cs": "PB_12", + "lora-reset": "PC_2", + "lora-dio0": "PC_6", + "lora-dio1": "PC_10", + "lora-dio2": "PC_11", + "lora-dio3": "PC_8", + "lora-dio4": "PC_9", + "lora-dio5": "PC_12", + "lora-rf-switch-ctl1": "PC_4", + "lora-rf-switch-ctl2": "PC_13", + "lora-txctl": "NC", + "lora-rxctl": "NC", + "lora-ant-switch": "NC", + "lora-pwr-amp-ctl": "NC", + "lora-tcxo": "NC" + }, + "K64F": { + "lora-spi-mosi": "D11", + "lora-spi-miso": "D12", + "lora-spi-sclk": "D13", + "lora-cs": "D10", + "lora-reset": "A0", + "lora-dio0": "D2", + "lora-dio1": "D3", + "lora-dio2": "D4", + "lora-dio3": "D5", + "lora-dio4": "D8", + "lora-dio5": "D9", + "lora-rf-switch-ctl1": "NC", + "lora-rf-switch-ctl2": "NC", + "lora-txctl": "NC", + "lora-rxctl": "NC", + "lora-ant-switch": "A4", + "lora-pwr-amp-ctl": "NC", + "lora-tcxo": "NC" + }, + + "DISCO_L072CZ_LRWAN1": { + "main_stack_size": 1024, + "lora-radio": "SX1276", + "lora-spi-mosi": "PA_7", + "lora-spi-miso": "PA_6", + "lora-spi-sclk": "PB_3", + "lora-cs": "PA_15", + "lora-reset": "PC_0", + "lora-dio0": "PB_4", + "lora-dio1": "PB_1", + "lora-dio2": "PB_0", + "lora-dio3": "PC_13", + "lora-dio4": "NC", + "lora-dio5": "NC", + "lora-rf-switch-ctl1": "NC", + "lora-rf-switch-ctl2": "NC", + "lora-txctl": "PC_2", + "lora-rxctl": "PA_1", + "lora-ant-switch": "NC", + "lora-pwr-amp-ctl": "PC_1", + "lora-tcxo": "PA_12" + }, + + "MTB_MURATA_ABZ": { + "main_stack_size": 1024, + "lora-radio": "SX1276", + "lora-spi-mosi": "PA_7", + "lora-spi-miso": "PA_6", + "lora-spi-sclk": "PB_3", + "lora-cs": "PA_15", + "lora-reset": "PC_0", + "lora-dio0": "PB_4", + "lora-dio1": "PB_1", + "lora-dio2": "PB_0", + "lora-dio3": "PC_13", + "lora-dio4": "NC", + "lora-dio5": "NC", + "lora-rf-switch-ctl1": "NC", + "lora-rf-switch-ctl2": "NC", + "lora-txctl": "PC_2", + "lora-rxctl": "PA_1", + "lora-ant-switch": "NC", + "lora-pwr-amp-ctl": "PC_1", + "lora-tcxo": "PA_12" + }, + + "XDOT_L151CC": { + "lora-radio": "SX1272", + "lora-spi-mosi": "LORA_MOSI", + "lora-spi-miso": "LORA_MISO", + "lora-spi-sclk": "LORA_SCK", + "lora-cs": "LORA_NSS", + "lora-reset": "LORA_RESET", + "lora-dio0": "LORA_DIO0", + "lora-dio1": "LORA_DIO1", + "lora-dio2": "LORA_DIO2", + "lora-dio3": "LORA_DIO3", + "lora-dio4": "LORA_DIO4", + "lora-dio5": "NC", + "lora-rf-switch-ctl1": "NC", + "lora-rf-switch-ctl2": "NC", + "lora-txctl": "NC", + "lora-rxctl": "NC", + "lora-ant-switch": "NC", + "lora-pwr-amp-ctl": "NC", + "lora-tcxo": "NC" + }, + + "MTB_MTS_XDOT": { + "lora-radio": "SX1272", + "lora-spi-mosi": "LORA_MOSI", + "lora-spi-miso": "LORA_MISO", + "lora-spi-sclk": "LORA_SCK", + "lora-cs": "LORA_NSS", + "lora-reset": "LORA_RESET", + "lora-dio0": "LORA_DIO0", + "lora-dio1": "LORA_DIO1", + "lora-dio2": "LORA_DIO2", + "lora-dio3": "LORA_DIO3", + "lora-dio4": "LORA_DIO4", + "lora-dio5": "NC", + "lora-rf-switch-ctl1": "NC", + "lora-rf-switch-ctl2": "NC", + "lora-txctl": "NC", + "lora-rxctl": "NC", + "lora-ant-switch": "NC", + "lora-pwr-amp-ctl": "NC", + "lora-tcxo": "NC" + }, + + "FF1705_L151CC": { + "lora-radio": "SX1272", + "lora-spi-mosi": "LORA_MOSI", + "lora-spi-miso": "LORA_MISO", + "lora-spi-sclk": "LORA_SCK", + "lora-cs": "LORA_NSS", + "lora-reset": "LORA_RESET", + "lora-dio0": "LORA_DIO0", + "lora-dio1": "LORA_DIO1", + "lora-dio2": "LORA_DIO2", + "lora-dio3": "LORA_DIO3", + "lora-dio4": "LORA_DIO4", + "lora-dio5": "NC", + "lora-rf-switch-ctl1": "NC", + "lora-rf-switch-ctl2": "NC", + "lora-txctl": "NC", + "lora-rxctl": "NC", + "lora-ant-switch": "NC", + "lora-pwr-amp-ctl": "NC", + "lora-tcxo": "NC" + }, + + "MTS_MDOT_F411RE": { + "lora-radio": "SX1272", + "lora-spi-mosi": "LORA_MOSI", + "lora-spi-miso": "LORA_MISO", + "lora-spi-sclk": "LORA_SCK", + "lora-cs": "LORA_NSS", + "lora-reset": "LORA_RESET", + "lora-dio0": "LORA_DIO0", + "lora-dio1": "LORA_DIO1", + "lora-dio2": "LORA_DIO2", + "lora-dio3": "LORA_DIO3", + "lora-dio4": "LORA_DIO4", + "lora-dio5": "LORA_DIO5", + "lora-rf-switch-ctl1": "NC", + "lora-rf-switch-ctl2": "NC", + "lora-txctl": "LORA_TXCTL", + "lora-rxctl": "LORA_RXCTL", + "lora-ant-switch": "NC", + "lora-pwr-amp-ctl": "NC", + "lora-tcxo": "NC" + }, + + "MTB_ADV_WISE_1510": { + "lora-radio": "SX1276", + "lora-spi-mosi": "SPI_RF_MOSI", + "lora-spi-miso": "SPI_RF_MISO", + "lora-spi-sclk": "SPI_RF_SCK", + "lora-cs": "SPI_RF_CS", + "lora-reset": "SPI_RF_RESET", + "lora-dio0": "DIO0", + "lora-dio1": "DIO1", + "lora-dio2": "DIO2", + "lora-dio3": "DIO3", + "lora-dio4": "DIO4", + "lora-dio5": "DIO5", + "lora-rf-switch-ctl1": "NC", + "lora-rf-switch-ctl2": "NC", + "lora-txctl": "NC", + "lora-rxctl": "NC", + "lora-ant-switch": "ANT_SWITCH", + "lora-pwr-amp-ctl": "NC", + "lora-tcxo": "NC" + }, + + "MTB_RAK811": { + "lora-radio": "SX1276", + "lora-spi-mosi": "SPI_RF_MOSI", + "lora-spi-miso": "SPI_RF_MISO", + "lora-spi-sclk": "SPI_RF_SCK", + "lora-cs": "SPI_RF_CS", + "lora-reset": "SPI_RF_RESET", + "lora-dio0": "DIO0", + "lora-dio1": "DIO1", + "lora-dio2": "DIO2", + "lora-dio3": "DIO3", + "lora-dio4": "DIO4", + "lora-dio5": "NC", + "lora-rf-switch-ctl1": "NC", + "lora-rf-switch-ctl2": "NC", + "lora-txctl": "ANT_CTX_PA", + "lora-rxctl": "ANT_CRX_RX", + "lora-ant-switch": "NC", + "lora-pwr-amp-ctl": "NC", + "lora-tcxo": "RF_TCXO_EN" + } + }, + "macros": ["MBEDTLS_USER_CONFIG_FILE=\"mbedtls_lora_config.h\""] +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbedtls_lora_config.h Thu Jan 03 16:18:09 2019 -0800 @@ -0,0 +1,43 @@ +/** + * 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. + */ + +#ifndef MBEDTLS_LORA_CONFIG_H +#define MBEDTLS_LORA_CONFIG_H + +/* + * Configure mbedtls for LoRa stack. + * + * These settings are just a customization of the main mbedtls config + * mbed-os/features/mbedtls/inc/mbedtls/config.h + */ + +// Ensure LoRa required ciphers are enabled +#define MBEDTLS_CIPHER_C +#define MBEDTLS_AES_C +#define MBEDTLS_CMAC_C + +// Reduce ROM usage by optimizing some mbedtls features. +// These are only reference configurations for this LoRa example application. +// Other LoRa applications might need different configurations. +#define MBEDTLS_AES_FEWER_TABLES + +#undef MBEDTLS_GCM_C +#undef MBEDTLS_CHACHA20_C +#undef MBEDTLS_CHACHAPOLY_C +#undef MBEDTLS_POLY1305_C + +#endif /* MBEDTLS_LORA_CONFIG_H */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/trace_helper.cpp Thu Jan 03 16:18:09 2019 -0800 @@ -0,0 +1,72 @@ +/** + * 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. + */ + +/** + * If we have tracing library available, we can see traces from within the + * stack. The library could be made unavailable by removing FEATURE_COMMON_PAL + * from the mbed_app.json to save RAM. + */ + +#include "mbed_trace.h" + +#ifdef FEA_TRACE_SUPPORT +#include "platform/PlatformMutex.h" + +/** + * Local mutex object for synchronization + */ +static PlatformMutex mutex; + +static void serial_lock(); +static void serial_unlock(); + +/** + * Sets up trace for the application + * Wouldn't do anything if the FEATURE_COMMON_PAL is not added + * or if the trace is disabled using mbed_app.json + */ +void setup_trace() +{ + // setting up Mbed trace. + mbed_trace_mutex_wait_function_set(serial_lock); + mbed_trace_mutex_release_function_set(serial_unlock); + mbed_trace_init(); +} + +/** + * Lock provided for serial printing used by trace library + */ +static void serial_lock() +{ + mutex.lock(); +} + +/** + * Releasing lock provided for serial printing used by trace library + */ +static void serial_unlock() +{ + mutex.unlock(); +} +#else +void setup_trace() +{ + +} +#endif + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/trace_helper.h Thu Jan 03 16:18:09 2019 -0800 @@ -0,0 +1,28 @@ +/** + * 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. + */ + +#ifndef APP_TRACE_HELPER_H_ +#define APP_TRACE_HELPER_H_ + +/** + * Helper function for the application to setup Mbed trace. + * It Wouldn't do anything if the FEATURE_COMMON_PAL is not added + * or if the trace is disabled using mbed_app.json + */ +void setup_trace(); + +#endif /* APP_TRACE_HELPER_H_ */