1 /*
2 * Copyright (c) 2019 Manivannan Sadhasivam
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/device.h>
8 #include <zephyr/drivers/lora.h>
9 #include <errno.h>
10 #include <zephyr/sys/util.h>
11 #include <zephyr/kernel.h>
12
13 #define DEFAULT_RADIO_NODE DT_ALIAS(lora0)
14 BUILD_ASSERT(DT_NODE_HAS_STATUS_OKAY(DEFAULT_RADIO_NODE),
15 "No default LoRa radio specified in DT");
16
17 #define MAX_DATA_LEN 12
18
19 #define LOG_LEVEL CONFIG_LOG_DEFAULT_LEVEL
20 #include <zephyr/logging/log.h>
21 LOG_MODULE_REGISTER(lora_send);
22
23 char data[MAX_DATA_LEN] = {'h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd', ' ', '0'};
24
main(void)25 int main(void)
26 {
27 const struct device *const lora_dev = DEVICE_DT_GET(DEFAULT_RADIO_NODE);
28 struct lora_modem_config config = {0};
29 int ret;
30
31 if (!device_is_ready(lora_dev)) {
32 LOG_ERR("%s Device not ready", lora_dev->name);
33 return 0;
34 }
35
36 config.frequency = 865100000;
37 config.bandwidth = BW_125_KHZ;
38 config.datarate = SF_10;
39 config.preamble_len = 8;
40 config.coding_rate = CR_4_5;
41 config.iq_inverted = false;
42 config.public_network = false;
43 config.tx_power = 4;
44 config.tx = true;
45
46 ret = lora_config(lora_dev, &config);
47 if (ret < 0) {
48 LOG_ERR("LoRa config failed");
49 return 0;
50 }
51
52 LOG_INF("Expected packet airtime: %u ms", lora_airtime(lora_dev, MAX_DATA_LEN));
53
54 while (1) {
55 ret = lora_send(lora_dev, data, MAX_DATA_LEN);
56 if (ret < 0) {
57 LOG_ERR("LoRa send failed");
58 return 0;
59 }
60
61 LOG_INF("Data sent %c!", data[MAX_DATA_LEN - 1]);
62
63 /* Send data at 1s interval */
64 k_sleep(K_MSEC(1000));
65
66 /* Increment final character to differentiate packets */
67 if (data[MAX_DATA_LEN - 1] == '9') {
68 data[MAX_DATA_LEN - 1] = '0';
69 } else {
70 data[MAX_DATA_LEN - 1] += 1;
71 }
72 }
73 return 0;
74 }
75