manuelbl / ttn-esp32

The Things Network device library for ESP32 (ESP-IDF) and SX127x based devices
MIT License
303 stars 64 forks source link

Add lmic controls #27

Closed Doom4535 closed 3 years ago

Doom4535 commented 4 years ago

This resolves an issue where the library hid the underlying LMIC controls from the end user, and defaulted to enabling all the LoRaWAN channels. This is an issue as not all gateways support the full LoRaWAN band plan, and this will cause a large packet loss when communicating with such gateways (effectively (total_region_channels - supported_channels) / total_region_channels). This is very likely to be encounter during normal use, as the library indicates it is targeting The Things Network (TTN), which explicitly does not use the full LoRaWAN allocated spectrum.

LoRaWAN bandplan: https://lora-alliance.org/sites/default/files/2018-04/lorawantm_regional_parameters_v1.1rb_-_final.pdf#page=15 TTN bandplan: https://www.thethingsnetwork.org/docs/lorawan/frequency-plans.html.

Doom4535 commented 4 years ago

Referencing issue: https://github.com/manuelbl/ttn-esp32/issues/28

I'm leaving this as a draft right now as I'd like to consider having it detect the configured region and default to a TTN sub-band in that region (since the naming indicates that it targets TTN).

manuelbl commented 4 years ago

Thanks for the contribution. It would be nice if it worked out of the box for the US.

I'll have a look at it. As I'm busy with other project, it might take a while. I hope to catch up soon.

manuelbl commented 3 years ago

Can you provide a code example how you would use this in a US network? Are you working with TTN or some other network? Are you using ADR? Shouldn't the join message configure the subband / channels?

Doom4535 commented 3 years ago

Intended Usage

@manuelbl The intent with this was to use something like the following after instantiating the ttn object:

ttn.setAdrMode( true );
ttn.selectSubBand( 1 );

Current Setup and Issue

I am not currently using this with TTN, I'm using Chirpstack on a Raspberry Pi with a RAK board. ADR is enabled (and does appear to work). There is a possiblity that I have an error in my Chirpstack config. The issues I'm having occur can be broken into two pieces.

  1. Slow join (because of trying to use all 64 channels with a gateway that only supports 8 channels). This issue cannot be solved with a changed gateway configuration (other than by purchasing a 64 channel gateway).
  2. High packet loss after join (because of trying to use all 64 channels when the gateway only supports 8 channels). This issue could potentially be related to unaddressed issues with my gateway (I'll explore this further later).
Slow Join Behavior Snippet Here is a snippet from the console showing how joins currently behave, notice the tick counts indicating a long time to pair (time till it randomly gets to a channel supported by the 8 channel gateway). ```bash I (451) ttn_prIv: Dev and app EUI and app key saved in NVS storage I (461) ttn_prov: Dev and app EUI and app key saved in NVS storage Joining... I (461) ttn: event EV_JOINING I (461) ttn: event EV_TXSTART I (5801) ttn: event EV_RXSTART I (6801) ttn: event EV_RXSTART I (6921) ttn: event EV_JOIN_TXCOMPLETE ... I (467621) ttn: event EV_JOIN_TXCOMPLETE I (468551) ttn: event EV_TXSTART I (473551) ttn: event EV_RXSTART I (474541) ttn: event EV_RXSTART I (474661) ttn: event EV_JOIN_TXCOMPLETE I (478301) ttn: event EV_TXSTART I (483641) ttn: event EV_RXSTART I (484631) ttn: event EV_RXSTART I (484751) ttn: event EV_JOIN_TXCOMPLETE I (485071) ttn: event EV_TXSTART I (490071) ttn: event EV_RXSTART I (491071) ttn: event EV_RXSTART I (491181) ttn: event EV_JOIN_TXCOMPLETE I (504401) ttn: event EV_TXSTART I (509741) ttn: event EV_RXSTART I (509861) ttn: event EV_JOINED Joined. Sending message... I (509861) ttn: event EV_TXSTART I (511051) ttn: event EV_RXSTART I (512051) ttn: event EV_RXSTART I (512131) ttn: event EV_TXCOMPLETE Message sent. ```

Current Solution

Adding the calls to LMIC_selectSubBand() solved my pairing and packet loss issues; however, I have discovered an issue with the approach used in this pull request, as LMIC_selectSubBand() is not defined in the EU plans, so blindly including it in the ttn object definition causes problems when the code is built for other regions (as it is apparently not a mandatory function). Additionally, I have since become aware of an issue with this where the library does not realize that it it successfully transmitted the message and continues to spam empty message uplinks (more on this below).

Recommended Solution

I believe the approach I laid out with this branch is not the best method (unless someone wants to go all the way and expose something like ttn.lmic; however, I'm not sure how to wrap the LMIC C object as a CPP class that can be exposed easily).

The good news is, I think that there is a simpler method of solving this issue, where a couple of parameters are added to the Kconfig and ttn object initialization. This is a fairly simple modification that allows the code to easily be ported between regions; however, I am having issues with the underlying LMIC_selectSubBand() function call.

SubBand Spamming with LMIC_selectSubBand()

Expected Behavior It is expected that the device join the network and broadcast unconfirmed messages as laid out in routine (this is from the [hello-world](https://github.com/manuelbl/ttn-esp32/tree/master/examples/hello_world) example). ```bash I (451) ttn_prIv: Dev and app EUI and app key saved in NVS storage I (461) ttn_prov: Dev and app EUI and app key saved in NVS storage Joining... I (461) ttn: event EV_JOINING I (461) ttn: event EV_TXSTART I (5801) ttn: event EV_RXSTART I (6801) ttn: event EV_RXSTART I (6921) ttn: event EV_JOIN_TXCOMPLETE ... Joined. Sending message... I (509861) ttn: event EV_TXSTART I (511051) ttn: event EV_RXSTART I (512051) ttn: event EV_RXSTART I (512131) ttn: event EV_TXCOMPLETE Message sent. Sending message... I (542131) ttn: event EV_TXSTART I (543321) ttn: event EV_RXSTART I (544311) ttn: event EV_RXSTART I (544401) ttn: event EV_TXCOMPLETE Message sent. ```
Actual Behavior What actually occurs is the device quickly connects to the network (as desired); but it seems to then do a partial remasking of the LoRaWAN channels/subbands. This then causes some sort of a conflict when the device goes to send a subsequent message. It acts as if the message is stuck in the queue and unable to be sent; causing the LMIC library to spam empty uplink messages to the LoRaWAN gateway (after having actually sent the correct message). ```bash I (434) ttn_prov: Dev and app EUI and app key saved in NVS storage I (444) ttn_prov: Dev and app EUI and app key saved in NVS storage Joining... I (444) ttn: event EV_JOINING I (444) ttn: event EV_TXSTART I (5784) ttn: event EV_RXSTART I (5904) ttn: event EV_JOINED Joined. Sending message... I (5904) ttn: event EV_TXSTART I (7094) ttn: event EV_RXSTART I (7164) ttn: event EV_TXCOMPLETE Message of 0 bytes received on port 0: Message sent. I (7654) ttn: event EV_TXSTART I (8804) ttn: event EV_RXSTART I (8874) ttn: event EV_TXCOMPLETE I (9364) ttn: event EV_TXSTART I (10514) ttn: event EV_RXSTART I (10584) ttn: event EV_TXCOMPLETE I (11074) ttn: event EV_TXSTART I (12224) ttn: event EV_RXSTART I (12294) ttn: event EV_TXCOMPLETE I (12784) ttn: event EV_TXSTART I (13934) ttn: event EV_RXSTART I (14004) ttn: event EV_TXCOMPLETE I (14494) ttn: event EV_TXSTART I (15644) ttn: event EV_RXSTART I (15814) ttn: event EV_TXCOMPLETE I (16204) ttn: event EV_TXSTART I (17354) ttn: event EV_RXSTART I (17524) ttn: event EV_TXCOMPLETE I (17914) ttn: event EV_TXSTART I (19064) ttn: event EV_RXSTART I (19234) ttn: event EV_TXCOMPLETE I (19624) ttn: event EV_TXSTART I (20774) ttn: event EV_RXSTART I (20944) ttn: event EV_TXCOMPLETE I (21334) ttn: event EV_TXSTART I (22484) ttn: event EV_RXSTART I (22654) ttn: event EV_TXCOMPLETE I (23044) ttn: event EV_TXSTART I (24194) ttn: event EV_RXSTART I (24364) ttn: event EV_TXCOMPLETE ```

Current Course of Action

I'm going to close this pull request and leave development tied to issue https://github.com/manuelbl/ttn-esp32/issues/28, as it looks like it may take more development than I had originally thought.