#include <lorawan.h>
// ABP Credentials
const char *devAddr = "669dd80b"; // Replace with the Device Address that you have in the Antares console
const char *nwkSKey = "669dd80b610dd1bba4a5b011e8002249"; // Replace with the Network Session Key that you have in the Antares console
const char *appSKey = "669dd80be09eafff281b1c0ee0fb66c0"; // Replace with the Application Session Key that you have in the Antares console
const unsigned long interval = 10000; // 10 s interval to send message
unsigned long previousMillis = 0; // will store last time message sent
unsigned int counter = 0; // message counter
const int sensorpin = 34; // moisture sensor pin
int soilMoistureValue;
char myStr[50];
char msg[100];
char outStr[255];
byte recvStatus = 0;
int channel;
#if defined (ARDUINO_ESP32_DEV)
const sRFM_pins RFM_pins = {
.CS = 2, // LYNX32 to RFM NSS
.RST = 18, // LYNX32 to RFM RST
.DIO0 = 14, // LYNX32 to RFM DIO0
.DIO1 = 12, // LYNX32 to RFM DIO1
};
#elif defined (ARDUINO_B_L072Z_LRWAN1)
const sRFM_pins RFM_pins = {
.CS = PA15, // STM32L0 to NSS
.RST = PC0, // STM32L0 to RST
.DIO0 = PB4, // STM32L0 to DIO0
.DIO1 = PB1, // STM32L0 to DIO1
};
#else
#error *** ONLY SUPPORTS LYNX32 AND B-L072Z-LRWAN1 ***
#endif
void setup() {
#if defined (ARDUINO_B_L072Z_LRWAN1)
SPI.setMOSI(PA7);
SPI.setMISO(PA6);
SPI.setSCLK(PB3);
SPI.begin();
#elif !defined (ARDUINO_ESP32_DEV)
#error *** ONLY SUPPORTS LYNX32 AND B-L072Z-LRWAN1 ***
#endif
// Setup loraid access
Serial.begin(115200);
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(SF10BW125);
// set channel to random
lora.setChannel(MULTI);
// Set TxPower to 15 dBi (max)
lora.setTxPower1(15);
// Put ABP Key and DevAddress here
lora.setNwkSKey(nwkSKey);
lora.setAppSKey(appSKey);
lora.setDevAddr(devAddr);
Serial.println("LoRaWAN setup completed");
}
void loop() {
// Check interval overflow
if (millis() - previousMillis > interval) {
previousMillis = millis();
soilMoistureValue = analogRead(sensorpin);
sprintf(myStr, "Lora Counter-%d", counter);
// Create JSON string
snprintf(msg, sizeof(msg), "{\"counter\": %d, \"soilMoisture\": %d}", counter, soilMoistureValue);
Serial.print("Sending : ");
Serial.println(myStr);
Serial.println(msg);
// Send uplink messages
lora.sendUplink(msg, strlen(msg), 0, 5);
Serial.println("Messages sent");
counter++;
channel = lora.getChannel();
Serial.print(F("Ch : "));
Serial.print(channel);
Serial.println(" ");
}
// Check Lora RX
lora.update();
recvStatus = lora.readData(outStr);
if (recvStatus) {
int counter = 0;
for (int i = 0; i < recvStatus; i++) {
if (((outStr[i] >= 32) && (outStr[i] <= 126)) || (outStr[i] == 10) || (outStr[i] == 13))
counter++;
}
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();
}
}