beegee-tokyo / SX126x-Arduino

Arduino library to use Semtech SX126x LoRa chips and modules to communicate
MIT License
229 stars 64 forks source link

Chirpstack ABP configuration #64

Closed andrelmbraga closed 2 years ago

andrelmbraga commented 2 years ago

Hello,

For us915 i would like to know these informations. I didnt find it on the lib.

Rx1 Delay, Rx1 DR offset, RX2 DR, RX2 Frequency, Factory-preset Frequecies

ABP is working but not OTA, so in ABP I need to have these values to use downlinks on chirpstack.

Also I would like to know where can I change the uplink to be confirmed.

Thanks in advance!

beegee-tokyo commented 2 years ago

Rx1 Delay, Rx1 DR offset, RX2 DR, RX2 Frequency, Factory-preset Frequencies are defined by the LoRaWAN regional parameters and the library has no function to change them. I am testing with ABP and there is no need to change them manually.

If you want to mess with them, you have to look into the RegionAU915.h

andrelmbraga commented 2 years ago

@beegee-tokyo thanks for your answer. In fact I dont wanna change the configuration on the RegionAU915.h file. I wanna get the parameters from there.

For us915:

Rx1 Delay : 1000 or 1 ? ( chirpstack say that expected values are between 0 and 15)

/*!

Rx1 DR offset : 0

/*!

RX2 DR : 8

/*!

RX2 Frequency: 923300000

/*!

Factory-preset Frequecies: i really dont know what they mean. ??

If you can help I ll really appreciate, thanks for your time!

beegee-tokyo commented 2 years ago

For the delays, Chirpstack talks in seconds, the library talks in milliseconds, so 1000 == 1 For the frequencies, US915 has 72 channels pre-defined. But as most gateways are only capable of 8 channels, they are put into 8 groups with 8 TX and 1 RX channel: image

You have to set Chirpstack, the gateway and your node to the same group. In the library you select the group with lmh_setSubBandChannels(subband) with subband counting from 1 to 9. On the gateway, it depends on the brand. On a RAK Edge gateway (picture above) you can select the group, Dragino gateways are the same. And in Chirpstack you have to select US915 in the chirpstack-network-server.toml. Usually there is a prepared example file for Chirpstack. I found it in /etc/chirpstack-network-server and it defined the region like this:

# Network-server settings.
[network_server]
# Network identifier (NetID, 3 bytes) encoded as HEX (e.g. 010203)
net_id="000000"

  # LoRaWAN regional band configuration.
  #
  # Note that you might want to consult the LoRaWAN Regional Parameters
  # specification for valid values that apply to your region.
  # See: https://www.lora-alliance.org/lorawan-for-developers
  [network_server.band]
  name="US915"
andrelmbraga commented 2 years ago

Thanks for your answer. It works really well. With uplink confirmed and also donwlink confirmed. I'm now in a task in order to identify how can I know that my uplink was confirmed, like returning something after lmh_send, just the error status is not enough. Another point is be able to know if the the process of a class A for example was completed, like sending process and windows to receive were finished.

Looking for the RegionUS915.h file I can find:

US915_MAX_RX_WINDOW 3000 US915_RECEIVE_DELAY1 1000 US915_RECEIVE_DELAY2 2000

That is a bit weird because following the image attached RX2_DELAY was suposed to be bigger than RX1 delay + RX1 Window.

image

In this case I can at least thinking that I ll not have a gap between so the total time to the process after sending will be:

1000 + 3000 + 1000( final of the rx2 windows) = 5000 ms total.

I saw one function called lmh_send_blocking() when I'm sending unconfirmed messages it takes around 5262 ms, I suppose that 262 ms is the time to send the message (SF 9 - 4 bytes) and 5000 ms are the process for the receive windows.

When I use confirmed uplinks it takes just 1262 ms that I suppose is 262 ms to send it and around 1000 ms to receive the confirmation.

Is that correct? At least I could use this value on the timeout of the lmh_send_blocking() function. With a big timeout I could avoid problems in cases that I lost a confirmed uplink that could be a problem as I ll take more than 5000 seconds to complete the process because I'm going to send more than one message.

Could you please clarify any point of those and if I'm in the correct path.

Thanks to much for the support. I thanks for this nice library.

beegee-tokyo commented 2 years ago

Instead of using the blocking send function, why do you not use the normal send function and register the callbacks. There are several of them:

// LoRaWAN event handlers
/** LoRaWAN callback when join network finished */
static void lpwan_joined_handler(void);
/** LoRaWAN callback when join network failed */
static void lpwan_join_fail_handler(void);
/** LoRaWAN callback when data arrived */
static void lpwan_rx_handler(lmh_app_data_t *app_data);
/** LoRaWAN callback after class change request finished */
static void lpwan_class_confirm_handler(DeviceClass_t Class);
/** LoRaWAN callback after class change request finished */
static void lpwan_unconfirm_tx_finished(void);
/** LoRaWAN callback after class change request finished */
static void lpwan_confirm_tx_finished(bool result);
/** Structure containing LoRaWan callback functions, needed for lmh_init() */
static lmh_callback_t lora_callbacks = {get_lora_batt, BoardGetUniqueId, BoardGetRandomSeed, lpwan_rx_handler,
                                        lpwan_joined_handler, lpwan_class_confirm_handler, lpwan_join_fail_handler,
                                        lpwan_unconfirm_tx_finished, lpwan_confirm_tx_finished};

If you use confirmed packages, lpwan_confirm_tx_finished(bool result); tells you with the parameter result if the packet was acknowledged by the server.

You can find a code sample how to use the callbacks here: https://github.com/beegee-tokyo/WisBlock-API/blob/main/src/lorawan.cpp

andrelmbraga commented 2 years ago

Really nice, I ll try on my side in a few hours. Thank you very much.

andrelmbraga commented 2 years ago

It worked really well thanks. Now I'm wondering if is possible to synchronize my RTC with the lorawan network server using your lorawan mac version V1.0.2. Is that possible? Thanks! ps: do not need much precision on the time.

beegee-tokyo commented 2 years ago

Unfortunately it is not possible in an easy way. You could send a downlink packet from your LoRaWAN server to the node, but the transmission delays might be in the range of 6-10 seconds, so the time would not be accurate anymore when it arrives at the node.

andrelmbraga commented 2 years ago

Its ok, in fact I mad a real nice process in order to work. Basically I`m a class A device when a send command uplink to get timestamp from the server my device goes to C and my network server active the device again o c profile and send a downlink. The error is less than 2 seconds. For my application works like a charm.

Now I get another point, is it possible to restart the lora chip? If yes can you suggest the function? Sometimes after a while my device just keep getting error on the sending process.

beegee-tokyo commented 2 years ago

There are two options, a) restart only LoRaMAC state machine with lmh_init() calling a second time. If using ABP, it should work just fine. If using OTAA it will start a join cycle. b) reset the SX1262 by going through the complete initialization again

beegee-tokyo commented 2 years ago

But the restart should not be required. I have nodes running since months without reset without problems.

On US915, I would try to disable ADR and disable Dutycycle with LORAWAN_ADR_OFF and LORAWAN_DUTYCYCLE_OFF

static lmh_param_t lora_param_init = {LORAWAN_ADR_OFF, LORAWAN_DEFAULT_DATARATE, LORAWAN_PUBLIC_NETWORK, JOINREQ_NBTRIALS, LORAWAN_DEFAULT_TX_POWER, LORAWAN_DUTYCYCLE_OFF};

Another thing, how frequently are you sending packets? In my experience, everything faster than 20 seconds can lead to congestion somewhere in the LoRaMAC stack (not my code, that is from Semtech). I usually use 30 seconds as lowest value between two packets.