#include <lorawan.h>
//ABP Credentials
const char *devAddr = "bc92b8ff";
const char *nwkSKey = "ed78515e6def88880000000000000000";
const char *appSKey = "00000000000000005cf37517f5f76015";
const unsigned long interval = 500; // interval to send message
unsigned long previousMillis = 0; // will store last time message sent
unsigned int counter = 0; // message counter
char myStr[50];
byte outStr[255];
byte recvStatus = 0;
int port, channel, freq;
bool newmessage = false;
bool isSent; //Mengirim data hanya 1x
const sRFM_pins RFM_pins = {
.CS = 5,
.RST = 0,
.DIO0 = 27,
.DIO1 = 2,
};
void setup() {
// Setup loraid access
Serial.begin(9600);
delay(2000);
if (!lora.init()) {
Serial.println("RFM95 not detected");
delay(5000);
return;
}
// Set LoRaWAN Class change CLASS_A or CLASS_C
lora.setDeviceClass(CLASS_A);
// Set Data Rate
lora.setDataRate(SF12BW125);
// Set FramePort Tx
lora.setFramePortTx(5);
// set channel to random
lora.setChannel(MULTI);
// Set TxPower to 15 dBi (max)
lora.setTxPower(15);
// Put ABP Key and DevAddress here
lora.setNwkSKey(nwkSKey);
lora.setAppSKey(appSKey);
lora.setDevAddr(devAddr);
}
void loop() {
// Check interval overflow
/*if (millis() - previousMillis > interval) {
previousMillis = millis(); untuk looping*/
if (isSent==false && millis() - previousMillis > interval) {
previousMillis = millis();
isSent=true;
sprintf(myStr, "Lora Counter-%d", counter++); //kalo mau ngirim cmn 1x
Serial.print("Sending: ");
Serial.println(myStr);
lora.sendUplink(myStr, strlen(myStr), 0);
port = lora.getFramePortTx();
channel = lora.getChannel();
freq = lora.getChannelFreq(channel);
Serial.print(F("fport: ")); Serial.print(port);Serial.print(" ");
Serial.print(F("Ch: ")); Serial.print(channel);Serial.print(" ");
Serial.print(F("Freq: ")); Serial.print(freq);Serial.println(" ");
}
// Check Lora RX
lora.update();
recvStatus = lora.readDataByte(outStr);
if (recvStatus) {
newmessage = true;
int counter = 0;
port = lora.getFramePortRx();
channel = lora.getChannelRx();
freq = lora.getChannelRxFreq(channel);
for (int i = 0; i < recvStatus; i++)
{
if (((outStr[i] >= 32) && (outStr[i] <= 126)) || (outStr[i] == 10) || (outStr[i] == 13))
counter++;
}
if (port != 0)
{
if (counter == recvStatus)
{
Serial.print(F("Received String : "));
for (int i = 0; i < recvStatus; i++)
{
Serial.print(char(outStr[i]));
}
}
else
{
Serial.print(F("Received Hex: "));
for (int i = 0; i < recvStatus; i++)
{
Serial.print(outStr[i], HEX); Serial.print(" ");
}
}
Serial.println();
Serial.print(F("fport: ")); Serial.print(port);Serial.print(" ");
Serial.print(F("Ch: ")); Serial.print(channel);Serial.print(" ");
Serial.print(F("Freq: ")); Serial.println(freq);Serial.println(" ");
}
else
{
Serial.print(F("Received Mac Cmd : "));
for (int i = 0; i < recvStatus; i++)
{
Serial.print(outStr[i], HEX); Serial.print(" ");
}
Serial.println();
Serial.print(F("fport: ")); Serial.print(port);Serial.print(" ");
Serial.print(F("Ch: ")); Serial.print(channel);Serial.print(" ");
Serial.print(F("Freq: ")); Serial.println(freq);Serial.println(" ");
}
}
}