Nol.A SDK Library 26.11.2
For IoT System Software Development
Loading...
Searching...
No Matches
plm100s/PLS100W_HRXL-MaxSonar-WR/main.cpp
#include <cox.h>
#include <algorithm>
#include <LoRaMacKR920.hpp>
//#define SENSOR_TEST_MODE
nRF5xFlashPage configMemory(0x76000);
char keyBuf[128];
Timer timerKeyInput;
LoRaMacKR920 LoRaWAN = LoRaMacKR920(SX1276);
Timer timerSend;
static uint8_t devEui[8];
static uint8_t appKey[16];
static uint8_t buf_index = 5;
static char buf[5];
static void send(uint16_t val) {
LoRaMacFrame *f = new LoRaMacFrame(255);
if (!f || !f->buf) {
printf("* Out of memory\n");
return;
}
f->port = 1;
f->len = snprintf((char *) f->buf, 50, "\"mm\":%d", val);
/* Uncomment below lines to specify parameters manually. */
error_t err = LoRaWAN.send(f);
printf("* Sending periodic report (%p:%s (%u byte)): %d\n", f, (const char *) f->buf, f->len, err);
if (err != ERROR_SUCCESS) {
delete f;
}
}
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 (joined) {
printf("* Joined to the network!\n");
Serial2.listen();
timerSend.startPeriodic(10000);
} else {
printf("* Join failed. Retry to join\n");
LoRaWAN.beginJoining(devEui, appKey);
}
}
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");
}
if ( frame->result == RadioPacket::TOO_BIG){
printf("- TOO BIG\n");
delete frame;
return;
}
printf(" frame\n");
delete frame;
}
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 receivemsg(SerialPort &p) {
while (p.available() > 0){
char c = (char) p.read();
if (c == 'R') {
buf_index = 0;
} else if (c == '\r') {
if (buf_index == 4) {
Serial2.stopListening();
buf[4] = '\0';
uint16_t val = strtoul(buf, nullptr, 10);
printf("* %u mm\n", val);
#ifndef SENSOR_TEST_MODE
send(val);
#endif
} else {
buf_index = 5;
}
} else if (c >= '0' && c <= '9') {
buf[buf_index++] = c;
} else {
buf_index = 5;
}
}
}
static void beginLoRaWAN(void *) {
LoRaWAN.begin();
LoRaWAN.onSendDone(eventLoRaWANSendDone);
LoRaWAN.onReceive(eventLoRaWANReceive);
LoRaWAN.onJoin(eventLoRaWANJoin);
LoRaWAN.setPublicNetwork(true);
LoRaWAN.beginJoining(devEui, appKey);
}
static void eventAppKeyInput(SerialPort &) {
uint8_t numOctets = strlen(keyBuf);
if (numOctets == 32) {
numOctets /= 2;
char strOctet[3];
for (uint8_t j = 0; j < numOctets; j++) {
strOctet[0] = keyBuf[2 * j];
strOctet[1] = keyBuf[2 * j + 1];
strOctet[2] = '\0';
appKey[j] = strtoul(strOctet, NULL, 16);
}
printf("* New AppKey:");
for (uint8_t j = 0; j < numOctets; j++) {
printf(" %02X", appKey[j]);
}
printf(" (%u byte)\n", numOctets);
configMemory.write(appKey, 0, numOctets);
postTask(beginLoRaWAN);
return;
} else {
printf("* HEX string length MUST be 32-byte.");
}
Serial.inputKeyboard(keyBuf, sizeof(keyBuf) - 1);
}
static void eventKeyInput(SerialPort &) {
timerKeyInput.stop();
while (Serial.available() > 0) {
Serial.read();
}
Serial.onReceive(eventAppKeyInput);
Serial.listen();
Serial.inputKeyboard(keyBuf, sizeof(keyBuf) - 1);
Serial.println(
"* Enter a new appKey as a hexadecimal string "
"[ex. 00112233445566778899aabbccddeeff]"
);
}
void setup() {
Serial.begin(115200);
Serial.printf("\n*** [PLS100W] HRXL-MaxSonar-WR + LoRaWAN ***\n");
System.getEUI(devEui);
Serial.printf("* EUI: %02X-%02X-%02X-%02X-%02X-%02X-%02X-%02X\n",
devEui[0], devEui[1], devEui[2], devEui[3], devEui[4], devEui[5], devEui[6], devEui[7]);
configMemory.read(appKey, 0, 16);
Serial.printf("* AppKey: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n",
appKey[0], appKey[1], appKey[2], appKey[3],
appKey[4], appKey[5], appKey[6], appKey[7],
appKey[8], appKey[9], appKey[10], appKey[11],
appKey[12], appKey[13], appKey[14], appKey[15]);
Serial.printf("* Last reset reason:0x%02X\n", System.getResetReason());
if (System.getResetReason() & (System.RESET_REASON_DOG | System.RESET_REASON_SREQ)) {
const McuARMCortexM::CoreRegisterDump *last = System.getLastCoreRegisterDump();
Serial.printf(" - R0: 0x%08lx, R1: 0x%08lx, R2: 0x%08lx, R3: 0x%08lx\n", last->r0, last->r1, last->r2, last->r3);
Serial.printf(" - R12: 0x%08lx, LR: 0x%08lx, PC: 0x%08lx, PSR: 0x%08lx\n", last->r12, last->lr, last->pc, last->psr);
}
Serial2.begin(9600);
Serial2.onReceive(receivemsg);
timerSend.onFired([](void *) {
Serial2.listen();
});
#ifdef SENSOR_TEST_MODE
timerSend.startPeriodic(1000);
#else
if (memcmp(appKey, "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 16) == 0) {
/* The appKey is required to be entered by user.*/
eventKeyInput(Serial);
} else {
Serial.println("* Press any key to enter a new AppKey in 3 seconds...");
timerKeyInput.onFired(beginLoRaWAN);
timerKeyInput.startOneShot(3000);
Serial.listen();
Serial.onReceive(eventKeyInput);
}
#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 대역을 사용하는 LoRaMac
Definition LoRaMacKR920.hpp:25
LoRaWAN 단말 장치들을 위한 MAC 프로토콜
Definition LoRaMac.hpp:253
bool framePending
Frame pending.
Definition LoRaMac.hpp:464
virtual error_t begin(uint8_t(*getBatteryLevel)()=NULL)
LoRaMAC layer initialization.
virtual error_t beginJoining(const uint8_t *devEui, const uint8_t *appEui, const uint8_t *appKey)
Over-the-Air Activation (OTAA) 방식의 join을 시도합니다.
void onJoin(void(*func)(LoRaMac &mac, bool result, const uint8_t *devEui, const uint8_t *appEui, const uint8_t *appKey, const uint8_t *nwkSKey, const uint8_t *appSKey, uint32_t devAddr, const RadioPacket &frame, uint32_t airTime))
Join이 완료되었을 때 호출될 콜백함수를 지정합니다.
void onSendDone(void(*sendDoneHandler)(LoRaMac &, LoRaMacFrame *))
전송이 완료될 때 호출될 함수를 지정합니다.
void setPublicNetwork(bool enable)
공용 LoRaWAN 네트워크를 사용할지 설정합니다.
void onReceive(void(*receiveHandler)(LoRaMac &radio, const LoRaMacFrame *frame))
수신이 완료될 때 호출될 함수를 지정합니다.
error_t send(LoRaMacFrame *frame)
frame 을 전송합니다.
uint8_t getNumPendingSendFrames()
전송 대기 중인 프레임 갯수를 구합니다.
무선 패킷
Definition RadioPacket.hpp:59
Radio::Modulation_t modulation
송신시 사용할, 또는 수신시 사용된 변복조 방식
Definition RadioPacket.hpp:160
uint32_t freq
송신시 사용할, 또는 수신시 사용된 주파수 (단위: Hz)
Definition RadioPacket.hpp:143
@ TOO_BIG
프레임이 너무 커서 전송 불가
Definition RadioPacket.hpp:94
Result_t result
송신 또는 수신 패킷 처리 결과
Definition RadioPacket.hpp:131
시리얼 포트를 제어하기 위한 클래스
Definition SerialPort.hpp:76
virtual int read()
포트 내부 버퍼에 수신된 문자를 하다 꺼냅니다.
virtual int available()
포트 내부 수신버퍼에 수신된 문자의 갯수를 반환합니다.
Timer는 일정 시간 이후에 1회 실행될 작업, 매 일정 시간마다 실행될 작업 등을 위한 기능입니다.
Definition Timer.hpp:61
void stop()
예약된, 혹은 측정 중인 Timer를 중단합니다.
bool startOneShot(uint32_t millis, bool interrupt=false)
작업함수를 millis 밀리초 이후 1회 실행합니다.
bool startPeriodic(uint32_t millis, bool interrupt=false)
작업함수를 매 millis 밀리초 주기로 반복하여 실행합니다.
void onFired(void(*func)(void *), void *args=nullptr)
타이머 만료시 실행시킬 작업함수를 지정합니다.
nRF5x MCU 내부 플래시 페이지에 데이터를 읽고 쓰기 위한 클래스
Definition nRF5xFlashPage.hpp:54
API and definitions.
@ ERROR_SUCCESS
Definition nola-common.h:119
SerialPort & Serial2
bool postTask(void(*func)(void *), void *args)
작업을 나중에 실행되도록 게시합니다.
nRF5x flash page
LoRaBW_t
LoRa Bandwidth Type.
Definition Radio.hpp:107
@ MOD_FSK
Definition Radio.hpp:67
@ MOD_LORA
Definition Radio.hpp:68
Definition McuARMCortexM.hpp:62