123 lines
2.9 KiB
C
123 lines
2.9 KiB
C
#include <zephyr/kernel.h>
|
|
#include <zephyr/lorawan/lorawan.h>
|
|
#include <zephyr/logging/log.h>
|
|
|
|
#include <lora/lorawan.h>
|
|
|
|
/*
|
|
TODO:
|
|
- DEV NONCE in non volatile memory and increase
|
|
- format packets
|
|
- send: check if still in network
|
|
- downlink callback
|
|
|
|
|
|
// Build payload byte array
|
|
uint8_t uplinkPayload[6];
|
|
uplinkPayload[0] = uint8_t (int(temp_room_mlx));
|
|
uplinkPayload[1] = uint8_t (int((temp_room_mlx-int(temp_room_mlx)*100))); // See notes for high/lowByte functions
|
|
uplinkPayload[2] = uint8_t (int(temp_floor));
|
|
uplinkPayload[3] = uint8_t (int((temp_floor-int(temp_floor)*100))); // See notes for high/lowByte functions
|
|
uplinkPayload[4] = uint8_t (int(humidity));
|
|
uplinkPayload[5] = uint8_t (relaisstate); // See notes for high/lowByte functions
|
|
*/
|
|
|
|
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;
|
|
|
|
void init_lorawan() {
|
|
int ret;
|
|
|
|
struct lorawan_downlink_cb downlink_cb = {
|
|
.port = LW_RECV_PORT_ANY,
|
|
.cb = dl_callback};
|
|
|
|
lora_dev = DEVICE_DT_GET(DT_ALIAS(lora0));
|
|
if (!device_is_ready(lora_dev))
|
|
{
|
|
LOG_ERR("%s: device not ready.", lora_dev->name);
|
|
return 0;
|
|
}
|
|
|
|
ret = lorawan_start();
|
|
if (ret < 0)
|
|
{
|
|
LOG_ERR("lorawan_start failed: %d", ret);
|
|
return 0;
|
|
}
|
|
else
|
|
{
|
|
LOG_INF("LoraWan started successfully");
|
|
}
|
|
|
|
lorawan_register_downlink_callback(&downlink_cb);
|
|
lorawan_register_dr_changed_callback(lorwan_datarate_changed);
|
|
|
|
join_cfg.mode = LORAWAN_ACT_OTAA;
|
|
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;
|
|
join_cfg.otaa.dev_nonce = 14u; // TODO
|
|
}
|
|
|
|
void join_network_otaa() {
|
|
int8_t status = -1;
|
|
while (lorawan_join(&join_cfg) < 0) {
|
|
LOG_ERR("lorawan_join_network failed: %d, retrying..", ret);
|
|
k_sleep(K_MSEC(5000));
|
|
}
|
|
LOG_INF("Joining network over OTAA");
|
|
}
|
|
|
|
void send_data_packet(char *data) {
|
|
LOG_INF("Sending data...");
|
|
while (1)
|
|
{
|
|
int8_t ret = lorawan_send(LORAWAN_PORT, data, sizeof(data), LORAWAN_MSG_CONFIRMED);
|
|
|
|
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);
|
|
k_sleep(DELAY);
|
|
continue;
|
|
}
|
|
else
|
|
{
|
|
LOG_INF("Data sent!");
|
|
}
|
|
|
|
k_sleep(DELAY);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
static void dl_callback(uint8_t port, uint8_t flags, int16_t rssi, int8_t snr, uint8_t len,
|
|
const uint8_t *hex_data)
|
|
{
|
|
LOG_INF("Port %d, Pending %d, RSSI %ddB, SNR %ddBm, Time %d", port,
|
|
flags & LORAWAN_DATA_PENDING, rssi, snr, !!(flags & LORAWAN_TIME_UPDATED));
|
|
if (hex_data)
|
|
{
|
|
LOG_HEXDUMP_INF(hex_data, len, "Payload: ");
|
|
}
|
|
}
|
|
|
|
static void lorwan_datarate_changed(enum lorawan_datarate dr)
|
|
{
|
|
uint8_t unused, max_size;
|
|
|
|
lorawan_get_payload_sizes(&unused, &max_size);
|
|
LOG_INF("New Datarate: DR_%d, Max Payload %d", dr, max_size);
|
|
} |