diff --git a/samples/lorawan/class_a/CMakeLists.txt b/samples/lorawan/class_a/CMakeLists.txt new file mode 100644 index 00000000000..6c5f3cd9b9b --- /dev/null +++ b/samples/lorawan/class_a/CMakeLists.txt @@ -0,0 +1,9 @@ +# SPDX-License-Identifier: Apache-2.0 + +cmake_minimum_required(VERSION 3.13.1) + +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(lorawan) + +FILE(GLOB app_sources src/*.c) +target_sources(app PRIVATE ${app_sources}) diff --git a/samples/lorawan/class_a/boards/96b_wistrio.conf b/samples/lorawan/class_a/boards/96b_wistrio.conf new file mode 100644 index 00000000000..be022a25e69 --- /dev/null +++ b/samples/lorawan/class_a/boards/96b_wistrio.conf @@ -0,0 +1 @@ +CONFIG_SPI_1=y diff --git a/samples/lorawan/class_a/prj.conf b/samples/lorawan/class_a/prj.conf new file mode 100644 index 00000000000..ec54d78f51e --- /dev/null +++ b/samples/lorawan/class_a/prj.conf @@ -0,0 +1,7 @@ +CONFIG_LOG=y +CONFIG_SPI=y +CONFIG_GPIO=y +CONFIG_LORA=y +CONFIG_LORA_SX12XX=y +CONFIG_LORAWAN=y +CONFIG_LORAMAC_REGION_IN865=y diff --git a/samples/lorawan/class_a/sample.yaml b/samples/lorawan/class_a/sample.yaml new file mode 100644 index 00000000000..41e47e68017 --- /dev/null +++ b/samples/lorawan/class_a/sample.yaml @@ -0,0 +1,8 @@ +common: + tags: lorawan +sample: + description: Demonstration of Class-A LoRaWAN functionality + name: LoRaWAN Class-A +tests: + samples.lorawan.class_a: + platform_allow: 96b_wistrio diff --git a/samples/lorawan/class_a/src/main.c b/samples/lorawan/class_a/src/main.c new file mode 100644 index 00000000000..cfc517ee43f --- /dev/null +++ b/samples/lorawan/class_a/src/main.c @@ -0,0 +1,94 @@ +/* + * Class A LoRaWAN sample application + * + * Copyright (c) 2020 Manivannan Sadhasivam + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include + +#define DEFAULT_RADIO_NODE DT_ALIAS(lora0) +BUILD_ASSERT(DT_NODE_HAS_STATUS(DEFAULT_RADIO_NODE, okay), + "No default LoRa radio specified in DT"); +#define DEFAULT_RADIO DT_LABEL(DEFAULT_RADIO_NODE) + +/* Customize based on network configuration */ +#define LORAWAN_DEV_EUI { 0xDD, 0xEE, 0xAA, 0xDD, 0xBB, 0xEE,\ + 0xEE, 0xFF } +#define LORAWAN_JOIN_EUI { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\ + 0x00, 0x00 } +#define LORAWAN_APP_KEY { 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE,\ + 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88,\ + 0x09, 0xCF, 0x4F, 0x3C } + +#define DELAY K_MSEC(10000) + +#define LOG_LEVEL CONFIG_LOG_DEFAULT_LEVEL +#include +LOG_MODULE_REGISTER(lorawan_class_a); + +char data[] = {'h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd'}; + +void main(void) +{ + const struct device *lora_dev; + struct lorawan_join_config join_cfg; + uint8_t dev_eui[] = LORAWAN_DEV_EUI; + uint8_t join_eui[] = LORAWAN_JOIN_EUI; + uint8_t app_key[] = LORAWAN_APP_KEY; + int ret; + + lora_dev = device_get_binding(DEFAULT_RADIO); + if (!lora_dev) { + LOG_ERR("%s Device not found", DEFAULT_RADIO); + return; + } + + ret = lorawan_start(); + if (ret < 0) { + LOG_ERR("lorawan_start failed: %d", ret); + return; + } + + join_cfg.mode = LORAWAN_CLASS_A; + join_cfg.dev_eui = dev_eui; + join_cfg.otaa.join_eui = join_eui; + join_cfg.otaa.app_key = app_key; + join_cfg.otaa.nwk_key = app_key; + + LOG_INF("Joining network over OTAA"); + ret = lorawan_join(&join_cfg); + if (ret < 0) { + LOG_ERR("lorawan_join_network failed: %d", ret); + return; + } + + LOG_INF("Sending data..."); + while (1) { + ret = lorawan_send(2, data, sizeof(data), + LORAWAN_MSG_CONFIRMED); + + /* + * Note: The stack may return -EAGAIN if the provided data + * length exceeds the maximum possible one for the region and + * datarate. But since we are just sending the same data here, + * we'll just continue. + */ + if (ret == -EAGAIN) { + LOG_ERR("lorawan_send failed: %d. Continuing...", ret); + k_sleep(DELAY); + continue; + } + + if (ret < 0) { + LOG_ERR("lorawan_send failed: %d", ret); + return; + } + + LOG_INF("Data sent!"); + k_sleep(DELAY); + } +}