samples: lorawan: Add Class-A LoRaWAN sample application

This sample application shows how to configure an end node in Class-A
mode and to send data to network server via gateway.

Signed-off-by: Manivannan Sadhasivam <mani@kernel.org>
This commit is contained in:
Manivannan Sadhasivam 2020-03-04 19:59:49 +05:30 committed by Carles Cufí
parent 3ce8540f3a
commit 0ea457e0dc
5 changed files with 119 additions and 0 deletions

View File

@ -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})

View File

@ -0,0 +1 @@
CONFIG_SPI_1=y

View File

@ -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

View File

@ -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

View File

@ -0,0 +1,94 @@
/*
* Class A LoRaWAN sample application
*
* Copyright (c) 2020 Manivannan Sadhasivam <mani@kernel.org>
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <device.h>
#include <lorawan/lorawan.h>
#include <zephyr.h>
#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 <logging/log.h>
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);
}
}