LoRaWAN: How to set spreading factor, output power, bandwidth? #1170
-
Following discussion #1169, I noticed something else that confused me regarding the radio controls in RadioLib. Summarizing that other discussion, I was able to connect my device to the things network and upload data. Right now I would really like to test the performance under different radio settings and see how often I can transmit under those settings according to law and FUP. I have the following program: #include <Arduino.h>
#include "utilities.h"
#include "RadioLib.h"
#include "LoRaBoards.h"
#include "SparkFun_Ublox_Arduino_Library.h"
SFE_UBLOX_GPS myGPS;
// !!!!! APP EUI
#define RADIOLIB_LORAWAN_JOIN_EUI [redacted]
// !!!!! DEV EUI
#define RADIOLIB_LORAWAN_DEV_EUI [redacted]
// !!!!! APP EUI
#define RADIOLIB_LORAWAN_APP_KEY [redacted]
// !!! APP KEY
#define RADIOLIB_LORAWAN_NWK_KEY [redacted]
uint64_t joinEUI = RADIOLIB_LORAWAN_JOIN_EUI;
uint64_t devEUI = RADIOLIB_LORAWAN_DEV_EUI;
uint8_t appKey[] = {RADIOLIB_LORAWAN_APP_KEY};
uint8_t nwkKey[] = {RADIOLIB_LORAWAN_NWK_KEY};
const uint32_t uplinkIntervalSeconds = 5UL * 60UL; // minutes x seconds
const LoRaWANBand_t Region = EU868;
const uint8_t subBand = 0; // For US915, change this to 2, otherwise leave on 0
SX1276 radio = new Module(RADIO_CS_PIN, RADIO_DIO0_PIN, RADIO_RST_PIN, RADIO_DIO1_PIN);
LoRaWANNode node(&radio, &Region, subBand);
void debug(bool isFail, const __FlashStringHelper *message, int state, bool Freeze)
{
if (isFail)
{
Serial.print(message);
Serial.print("(");
Serial.print(state);
Serial.println(")");
while (Freeze)
;
}
}
void encodeCoordinates(byte *buffer, long latitude, long longitude)
{
// Store first long (4 bytes)
buffer[0] = (latitude >> 24) & 0xFF;
buffer[1] = (latitude >> 16) & 0xFF;
buffer[2] = (latitude >> 8) & 0xFF;
buffer[3] = latitude & 0xFF;
// Store second long (4 bytes)
buffer[4] = (longitude >> 24) & 0xFF;
buffer[5] = (longitude >> 16) & 0xFF;
buffer[6] = (longitude >> 8) & 0xFF;
buffer[7] = longitude & 0xFF;
}
void setup()
{
setupBoards();
Serial.begin(9600);
while (!Serial.available())
delay(1000);
Serial.println(F("Starting up"));
Serial1.begin(GPS_BAUD_RATE, SERIAL_8N1, GPS_RX_PIN, GPS_TX_PIN);
if (myGPS.begin(Serial1) == false) // Connect to the Ublox module using serial port 1
{
Serial.println(F("Ublox GPS could not start."));
while (1)
delay(1000);
}
int state = radio.begin();
debug(state != RADIOLIB_ERR_NONE, F("Initialise radio failed"), state, true);
node.setADR(false);
if (radio.setSpreadingFactor(8) != RADIOLIB_ERR_NONE)
{
Serial.println(F("Error setting spreading factor"));
}
while (1)
{
Serial.println(F("joining TTN"));
node.beginOTAA(joinEUI, devEUI, nwkKey, appKey);
state = node.activateOTAA();
if (state == RADIOLIB_ERR_NONE || state == RADIOLIB_LORAWAN_NEW_SESSION)
{
Serial.println(F("Join successful"));
break;
}
debug(state != RADIOLIB_ERR_NONE, F("Join failed"), state, false);
delay(10000);
Serial.print("Retry ");
}
}
void loop()
{
// get a GPS fix
long latitude = myGPS.getLatitude();
long longitude = myGPS.getLongitude();
Serial.printf("%d %d\n", latitude, longitude);
Serial.println("Sending uplink");
byte payload[8];
encodeCoordinates(payload, latitude, longitude);
// Perform an uplink
int state = node.sendReceive(payload, sizeof(payload));
debug((state != RADIOLIB_LORAWAN_NO_DOWNLINK) && (state != RADIOLIB_ERR_NONE), F("Error in sendReceive"), state, false);
Serial.printf("Uplink complete, next in %d seconds\n", uplinkIntervalSeconds);
Serial.printf("Allowed in: %d\n", node.timeUntilUplink());
Serial.printf("Last uplink: %d\n", node.getLastToA());
// Wait until next uplink - observing legal & TTN FUP constraints
delay(uplinkIntervalSeconds * 1000UL); // delay needs milli-seconds
} As you can see, I am using "settings": {
"data_rate": {
"lora": {
"bandwidth": 125000,
"spreading_factor": 10,
"coding_rate": "4/5"
}
},
"frequency": "867900000",
"timestamp": 3218191924,
"time": "2024-07-25T15:58:27.557965040Z"
}, The spreading factor observed by TTN seems to still be 10. This is also confirmed by the duration of my uplink as reported by RadioLib: I have also tried an SF of 7, with a high bandwidth of 250 as specified by that airtime calculator, but I get the same result in the TTN dashboard. How do I change these settings? It doesn't seem like this is working right now, judging by the values reported by TTN and even RadioLib itself. Moderator note: Some values changed after discussions had started. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Your code breaches legal power output and the TTN FUP and if it ever gets to SF12, duty cycle. We can't assist those that deliberately break the law &/or cause unnecessary interference to the shared ISM band &/or breach FUP on the shared community resource that others rely on. |
Beta Was this translation helpful? Give feedback.
Unfortunately that won't cut it, here nor in university. You say you know how it works, but are not demonstrating your knowledge in the actual examples you provide. You can't make an exam saying "suppose we can construct a binary tree in linear time" and afterwards tell the examinator "I know how it works, it was just easier this way".
Or, if you are working on an assignment, using some odd value for easy debugging thinking "I'll change this when I hand it in" - I've heard many peers and students go "Oh balls, I handed it in with the wrong value".
Thirdly: