Nol.A SDK Library 26.11.2
For IoT System Software Development
Loading...
Searching...
No Matches
plm100s/LoRaWAN-Class-A/main.cpp
#include <cox.h>
#include <algorithm>
#include "LoRaMacKR920SKT.hpp"
LoRaMacKR920SKT LoRaWAN(SX1276);
Timer timerSend;
#define OVER_THE_AIR_ACTIVATION 1
#if (OVER_THE_AIR_ACTIVATION == 1)
static const uint8_t devEui[] = "\x00\x11\x22\x33\x44\x55\x66\x77";
static const uint8_t appEui[] = "\x00\x00\x00\x00\x00\x00\x00\x00";
static const uint8_t appKey[] = "\xaa\xe1\x20\xe3\x36\x03\xcd\x09\xd9\xda\x4a\x61\x84\xc6\xf5\x19";
#else
static const uint8_t NwkSKey[] = "\xa4\x88\x55\xad\xe9\xf8\xf4\x6f\xa0\x94\xb1\x98\x36\xc3\xc0\x86";
static const uint8_t AppSKey[] = "\x7a\x56\x2a\x75\xd7\xa3\xbd\x89\xa3\xde\x53\xe1\xcf\x7f\x1c\xc7";
static uint32_t DevAddr = 0x06e632e8;
#endif //OVER_THE_AIR_ACTIVATION
static void taskPeriodicSend(void *) {
LoRaMacFrame *f = new LoRaMacFrame(255);
if (!f || !f->buf) {
printf("* Out of memory\n");
return;
}
f->port = 1;
strcpy((char *) f->buf, "Test");
f->len = strlen((char *) f->buf);
/* Uncomment below lines to specify parameters manually. */
LoRaWAN.useADR = false;
// f->freq = 922500000;
// f->power = 1; /* Index 1 => MaxEIRP - 2 dBm */
f->numTrials = 5;
error_t err = LoRaWAN.send(f);
printf("* Sending periodic report (%p:%s (%u byte)): %d\n", f, f->buf, f->len, err);
if (err != ERROR_SUCCESS) {
delete f;
timerSend.startOneShot(10000);
}
}
static void eventLoRaWANJoin(
bool joined,
const uint8_t *joinedDevEui,
const uint8_t *joinedAppEui,
const uint8_t *joinedAppKey,
const uint8_t *joinedNwkSKey,
const uint8_t *joinedAppSKey,
uint32_t joinedDevAddr,
const RadioPacket &frame,
uint32_t
) {
#if (OVER_THE_AIR_ACTIVATION == 1)
if (joined) {
if (joinedNwkSKey && joinedAppSKey) {
// Status is OK, node has joined the network
printf("* Joined to the network!\n");
postTask(taskPeriodicSend, NULL);
} else {
printf("* PseudoAppKey joining done!\n");
}
} else {
printf("* Join failed. Retry to join\n");
LoRaWAN.beginJoining(devEui, appEui, appKey);
}
#endif
}
static void eventLoRaWANSendDone(LoRaMac &, LoRaMacFrame *frame) {
printf("* Send done(%d): [%p] destined for port[%u], Freq:%lu Hz, Power:%d dBm, # of Tx:%u, ", frame->result, frame, frame->port, frame->freq, frame->power, frame->numTrials);
if (frame->modulation == Radio::MOD_LORA) {
const char *strBW[] = { "Unknown", "125kHz", "250kHz", "500kHz", "Unexpected value" };
if (frame->meta.LoRa.bw > 3) {
frame->meta.LoRa.bw = (Radio::LoRaBW_t) 4;
}
printf("LoRa, SF:%u, BW:%s, ", frame->meta.LoRa.sf, strBW[frame->meta.LoRa.bw]);
} else if (frame->modulation == Radio::MOD_FSK) {
printf("FSK, ");
} else {
printf("Unkndown modulation, ");
}
printf("UNCONFIRMED");
} else if (frame->type == LoRaMacFrame::CONFIRMED) {
printf("CONFIRMED");
} else if (frame->type == LoRaMacFrame::MULTICAST) {
printf("MULTICAST (error)");
} else if (frame->type == LoRaMacFrame::PROPRIETARY) {
printf("PROPRIETARY");
} else {
printf("unknown type");
}
printf(" frame\n");
delete frame;
timerSend.startOneShot(10000);
}
static void eventLoRaWANReceive(LoRaMac &lw, const LoRaMacFrame *frame) {
printf("* Received:");
for (uint8_t i = 0; i < frame->len; i++) {
printf(" %02X", frame->buf[i]);
}
printf(" (");
frame->printTo(Serial);
printf(")\n");
if (
) {
// If there is no pending send frames, send an empty frame to ack or pull more frames.
LoRaMacFrame *emptyFrame = new LoRaMacFrame(0);
if (emptyFrame) {
error_t err = LoRaWAN.send(emptyFrame);
if (err != ERROR_SUCCESS) {
delete emptyFrame;
}
}
}
}
static void eventLoRaWANJoinRequested(LoRaMac &, uint32_t frequencyHz, const LoRaMac::DatarateParams_t &dr) {
printf("* JoinRequested(Frequency: %lu Hz, Modulation: ", frequencyHz);
if (dr.mod == Radio::MOD_FSK) {
printf("FSK\n");
} else if (dr.mod == Radio::MOD_LORA) {
const char *strLoRaBW[] = { "UNKNOWN", "125kHz", "250kHz", "500kHz" };
printf("LoRa, SF:%u, BW:%s\n", dr.param.LoRa.sf, strLoRaBW[dr.param.LoRa.bw]);
}
}
static void eventLoRaWANLinkADRReqReceived(LoRaMac &l, const uint8_t *payload) {
printf("* LoRaWAN LinkADRReq received: [");
for (uint8_t i = 0; i < 4; i++) {
printf(" %02X", payload[i]);
}
printf(" ]\n");
}
static void printChannelInformation(LoRaMac &lw) {
for (uint8_t i = 0; i < lw.MaxNumChannels; i++) {
if (p) {
printf(" - [%u] Frequency:%lu Hz\n", i, p->Frequency);
} else {
printf(" - [%u] disabled\n", i);
}
}
printf(" - Default DR%u:", lw.getCurrentDatarateIndex());
if (dr->mod == Radio::MOD_LORA) {
printf("LoRa(SF%u BW:", dr->param.LoRa.sf);
switch (dr->param.LoRa.bw) {
case Radio::BW_125kHz: printf("125kHz)\n"); break;
case Radio::BW_250kHz: printf("250kHz)\n"); break;
case Radio::BW_500kHz: printf("500kHz)\n"); break;
default: printf("(unexpected:%u))\n", dr->param.LoRa.bw); break;
}
} else if (dr->mod == Radio::MOD_FSK) {
printf("FSK\n");
} else {
printf("Unknown modulation\n");
}
printf(" - Default Tx index: %d\n", lw.getCurrentTxPowerIndex());
printf(" - # of repetitions of unconfirmed uplink frames: %u\n", lw.getNumRepetitions());
}
static void eventLoRaWANLinkADRAnsSent(LoRaMac &l, uint8_t status) {
printf("* LoRaWAN LinkADRAns sent with status 0x%02X.\n", status);
printChannelInformation(l);
}
static void eventLoRaWANDutyCycleReqReceived(LoRaMac &lw, const uint8_t *payload) {
printf("* LoRaWAN DutyCycleReq received: [");
for (uint8_t i = 0; i < 1; i++) {
printf(" %02X", payload[i]);
}
printf(" ]\n");
}
static void eventLoRaWANDutyCycleAnsSent(LoRaMac &lw) {
printf("* LoRaWAN DutyCycleAns sent. Current MaxDCycle is %u.\n", lw.getMaxDutyCycle());
}
static void eventLoRaWANRxParamSetupReqReceived(LoRaMac &lw, const uint8_t *payload) {
printf("* LoRaWAN RxParamSetupReq received: [");
for (uint8_t i = 0; i < 4; i++) {
printf(" %02X", payload[i]);
}
printf(" ]\n");
}
static void eventLoRaWANRxParamSetupAnsSent(LoRaMac &lw, uint8_t status) {
printf("* LoRaWAN RxParamSetupAns sent with status 0x%02X. Current Rx1Offset is %u, and Rx2 channel is (DR%u, %lu Hz).\n", status, lw.getRx1DrOffset(), lw.getRx2Datarate(), lw.getRx2Frequency());
}
static void eventLoRaWANDevStatusReqReceived(LoRaMac &lw) {
printf("* LoRaWAN DevStatusReq received.\n");
}
static void eventLoRaWANDevStatusAnsSent(LoRaMac &lw, uint8_t bat, uint8_t margin) {
printf("* LoRaWAN DevStatusAns sent. (");
if (bat == 0) {
printf("Powered by external power source. ");
} else if (bat == 255) {
printf("Battery level cannot be measured. ");
} else {
printf("Battery: %lu %%. ", map(bat, 1, 254, 0, 100));
}
if (bitRead(margin, 5) == 1) {
margin |= bit(7) | bit(6);
}
printf(" SNR: %d)\n", (int8_t) margin);
}
static void eventLoRaWANNewChannelReqReceived(LoRaMac &lw, const uint8_t *payload) {
printf("* LoRaWAN NewChannelReq received [");
for (uint8_t i = 0; i < 5; i++) {
printf(" %02X", payload[i]);
}
printf(" ]\n");
}
static void eventLoRaWANNewChannelAnsSent(LoRaMac &lw, uint8_t status) {
printf("* LoRaWAN NewChannelAns sent with (Datarate range %s and channel frequency %s).\n", (bitRead(status, 1) == 1) ? "OK" : "NOT OK", (bitRead(status, 0) == 1) ? "OK" : "NOT OK");
for (uint8_t i = 0; i < lw.MaxNumChannels; i++) {
if (p) {
printf(" - [%u] Frequency:%lu Hz\n", i, p->Frequency);
} else {
printf(" - [%u] disabled\n", i);
}
}
}
static void eventLoRaWANRxTimingSetupReqReceived(LoRaMac &lw, const uint8_t *payload) {
printf("* LoRaWAN RxTimingSetupReq received: [");
for (uint8_t i = 0; i < 1; i++) {
printf(" %02X", payload[i]);
}
printf(" ]\n");
}
static void eventLoRaWANRxTimingSetupAnsSent(LoRaMac &lw) {
printf("* LoRaWAN RxTimingSetupAns sent. Current Rx1 delay is %u msec, and Rx2 delay is %u msec.\n", lw.getRx1Delay(), lw.getRx2Delay());
}
void setup() {
Serial.begin(115200);
Serial.printf("\n*** [PLM100S] LoRaWAN Class A Example ***\n");
Serial.printf("* Last reset reason:0x%02X\n", System.getResetReason());
timerSend.onFired(taskPeriodicSend, NULL);
// SX1276.usePABOOST = false; // If you want to use RFO_HF instead of PABOOST, uncomment this line.
LoRaWAN.begin();
LoRaWAN.onSendDone(eventLoRaWANSendDone);
LoRaWAN.onReceive(eventLoRaWANReceive);
LoRaWAN.onJoin(eventLoRaWANJoin);
LoRaWAN.onJoinRequested(eventLoRaWANJoinRequested);
LoRaWAN.onLinkADRReqReceived(eventLoRaWANLinkADRReqReceived);
LoRaWAN.onLinkADRAnsSent(eventLoRaWANLinkADRAnsSent);
LoRaWAN.onDutyCycleReqReceived(eventLoRaWANDutyCycleReqReceived);
LoRaWAN.onDutyCycleAnsSent(eventLoRaWANDutyCycleAnsSent);
LoRaWAN.onRxParamSetupReqReceived(eventLoRaWANRxParamSetupReqReceived);
LoRaWAN.onRxParamSetupAnsSent(eventLoRaWANRxParamSetupAnsSent);
LoRaWAN.onDevStatusReqReceived(eventLoRaWANDevStatusReqReceived);
LoRaWAN.onDevStatusAnsSent(eventLoRaWANDevStatusAnsSent);
LoRaWAN.onNewChannelReqReceived(eventLoRaWANNewChannelReqReceived);
LoRaWAN.onNewChannelAnsSent(eventLoRaWANNewChannelAnsSent);
LoRaWAN.onRxTimingSetupReqReceived(eventLoRaWANRxTimingSetupReqReceived);
LoRaWAN.onRxTimingSetupAnsSent(eventLoRaWANRxTimingSetupAnsSent);
LoRaWAN.setPublicNetwork(false);
printChannelInformation(LoRaWAN);
#if (OVER_THE_AIR_ACTIVATION == 0)
printf("ABP!\n");
LoRaWAN.setABP(NwkSKey, AppSKey, DevAddr);
LoRaWAN.setNetworkJoined(true);
postTask(taskPeriodicSend, NULL);
#else
printf("Trying to join\n");
#if 0
LoRaWAN->setNetworkJoined(false);
LoRaWAN->beginJoining(devEui, appEui, appKey);
#else
LoRaWAN.setNetworkJoined(true);
LoRaWAN.beginJoining(devEui, appEui, appKey);
#endif
#endif
}
LoRaWAN frame.
Definition LoRaMac.hpp:88
FrameType_t type
Frame type.
Definition LoRaMac.hpp:102
Radio::LoRaSF_t sf
송신시 사용할, 또는 수신시 사용된 spreading factor
Definition LoRaMac.hpp:189
int16_t power
송신시 사용할 Tx power table index (LoRaMac::MaxTxPower ~ LoRaMac::MinTxPower), 송신 완료 콜백시 출력 세기(단위: dBm) 또는...
Definition LoRaMac.hpp:146
Radio::LoRaBW_t bw
송신시 사용할, 또는 수신시 사용된 bandwidth
Definition LoRaMac.hpp:206
struct LoRaMacFrame::@024007217170142272131231362111253110374345035353::@215311014042340047327077224045033277162306213223 LoRa
modulation 이 Radio::MOD_LORA 인 경우 사용되는 변수
uint8_t numTrials
전송 횟수
Definition LoRaMac.hpp:116
union LoRaMacFrame::@024007217170142272131231362111253110374345035353 meta
변복조 방식에 대한 속성
@ MULTICAST
Definition LoRaMac.hpp:95
@ PROPRIETARY
Definition LoRaMac.hpp:96
@ UNCONFIRMED
Definition LoRaMac.hpp:93
@ CONFIRMED
Definition LoRaMac.hpp:94
uint8_t port
Port number.
Definition LoRaMac.hpp:107
대한민국 920~923MHz 대역을 사용하는 SK 텔레콤용 LoRaMac
Definition LoRaMacKR920SKT.hpp:23
Definition LoRaMac.hpp:272
uint32_t Frequency
Frequency in Hz.
Definition LoRaMac.hpp:277
LoRaWAN 단말 장치들을 위한 MAC 프로토콜
Definition LoRaMac.hpp:253
uint32_t getRx2Frequency()
Rx2에서 사용할 주파수를 가져옵니다.
Definition LoRaMac.hpp:1270
uint8_t getRx1DrOffset()
Rx1에 적용될 DR offset을 가져옵니다.
Definition LoRaMac.hpp:1286
bool framePending
Frame pending.
Definition LoRaMac.hpp:464
uint8_t getCurrentDatarateIndex()
현재 설정된 기본 datarate 인덱스를 가져옵니다.
uint8_t getRx2Datarate()
Rx2에서 사용할 DR을 가져옵니다.
Definition LoRaMac.hpp:1254
uint8_t getMaxDutyCycle()
현재의 최대 duty cycle을 가져옵니다.
virtual const ChannelParams_t * getChannel(uint8_t index)=0
채널 정보를 가져옵니다.
uint8_t getNumRepetitions()
현재 설정된 unconfirmed uplink frame에 적용되는 반복 전송 횟수를 가져옵니다.
uint8_t getCurrentTxPowerIndex()
기본 Tx power index를 가져옵니다.
uint8_t const MaxNumChannels
최대 채널 수
Definition LoRaMac.hpp:396
uint16_t getRx1Delay()
Tx 종료 후 Rx1 window가 열릴 때까지의 지연 시간(단위: 밀리초)을 가져옵니다.
const DatarateParams_t * getDatarate(uint8_t index) const
Datarate 정보를 가져옵니다.
uint16_t getRx2Delay()
Tx 종료 후 Rx2 window가 열릴 때까지의 지연 시간(단위: 밀리초)을 가져옵니다.
uint8_t getNumPendingSendFrames()
전송 대기 중인 프레임 갯수를 구합니다.
무선 패킷
Definition RadioPacket.hpp:59
Radio::Modulation_t modulation
송신시 사용할, 또는 수신시 사용된 변복조 방식
Definition RadioPacket.hpp:160
uint32_t freq
송신시 사용할, 또는 수신시 사용된 주파수 (단위: Hz)
Definition RadioPacket.hpp:143
Result_t result
송신 또는 수신 패킷 처리 결과
Definition RadioPacket.hpp:131
Timer는 일정 시간 이후에 1회 실행될 작업, 매 일정 시간마다 실행될 작업 등을 위한 기능입니다.
Definition Timer.hpp:61
bool startOneShot(uint32_t millis, bool interrupt=false)
작업함수를 millis 밀리초 이후 1회 실행합니다.
void onFired(void(*func)(void *), void *args=nullptr)
타이머 만료시 실행시킬 작업함수를 지정합니다.
API and definitions.
#define bitRead(v, n)
수의 한 비트를 읽습니다.
Definition nola-common.h:339
#define bit(n)
특정 비트를 구합니다.
Definition nola-common.h:330
long map(long x, long in_min, long in_max, long out_min, long out_max)
수를 어떤 범위에서 다른 범위로 매핑시킵니다.
@ ERROR_SUCCESS
Definition nola-common.h:119
bool postTask(void(*func)(void *), void *args)
작업을 나중에 실행되도록 게시합니다.
LoRaBW_t
LoRa Bandwidth Type.
Definition Radio.hpp:107
@ BW_250kHz
Definition Radio.hpp:110
@ BW_125kHz
Definition Radio.hpp:109
@ BW_500kHz
Definition Radio.hpp:111
@ MOD_FSK
Definition Radio.hpp:67
@ MOD_LORA
Definition Radio.hpp:68
@ SF7
Definition Radio.hpp:82
Definition LoRaMac.hpp:359