jgromes / RadioLib

Universal wireless communication library for embedded devices
https://jgromes.github.io/RadioLib/
MIT License
1.48k stars 373 forks source link

ESP32 SX1262 no OTAA answer #907

Closed derGraph closed 7 months ago

derGraph commented 8 months ago

Sketch that is causing the module fail

#include <RadioLib.h>
#include <ss_oled.h>

static uint8_t ucBackBuffer[1024];

#define SDA_PIN 17
#define SCL_PIN 18
// Set this to -1 to disable or the GPIO pin number connected to the reset
// line of your display if it requires an external reset
#define RESET_PIN 21
// let ss_oled figure out the display address
#define OLED_ADDR -1
// don't rotate the display
#define FLIP180 0
// don't invert the display
#define INVERT 0
// Bit-Bang the I2C bus
#define USE_HW_I2C 0

// Change these if you're using a different OLED display
#define MY_OLED OLED_128x64
#define OLED_WIDTH 128
#define OLED_HEIGHT 64

// SX1262 has the following connections:
// NSS pin:   8
// DIO0 pin:  13
// RESET pin: 12
// DIO1 pin:  14
SX1262 radio = new Module(8, 13, 12, 14);

// create the node instance on the EU-868 band
// using the radio module and the encryption key
// make sure you are using the correct band
// based on your geographical location!
LoRaWANNode node(&radio, &EU868);
SSOLED ssoled;
String monitor[8] = {"1","2","3","4","5","6","7","8"};

void Oled_println(String);
void Oled_print(String);

void setup() {
  Serial.begin(9600);

  int rc;
  rc = oledInit(&ssoled, MY_OLED, OLED_ADDR, FLIP180, INVERT, USE_HW_I2C, SDA_PIN, SCL_PIN, RESET_PIN, 400000L); // use standard I2C bus at 400Khz

  // initialize SX1278 with default settings
  Oled_println("[SX1262] Initializing ... ");
  int state = radio.begin(868.0, 125.0, 12, 7, RADIOLIB_SX126X_SYNC_WORD_PRIVATE, 17, 8, 1.6, false);

  if(state == RADIOLIB_ERR_NONE) {
    Oled_println("success!");
  } else {
    Oled_print("failed, code ");
    Oled_println(String(state));
    while(true);
  }

  // application identifier - pre-LoRaWAN 1.1.0, this was called appEUI
  // when adding new end device in TTN, you will have to enter this number
  // you can pick any number you want, but it has to be unique
  uint64_t joinEUI = 0x0000000000000000;

  // device identifier - this number can be anything
  // when adding new end device in TTN, you can generate this number,
  // or you can set any value you want, provided it is also unique
  uint64_t devEUI = 0x70B3D57ED0063A79;

  // select some encryption keys which will be used to secure the communication
  // there are two of them - network key and application key
  // because LoRaWAN uses AES-128, the key MUST be 16 bytes (or characters) long

  // network key is the ASCII string "topSecretKey1234"
  uint8_t nwkKey[] = { 0x02, 0x44, 0x49, 0x2d, 0x3d, 0xfa, 0xd5, 0x81, 0xeb, 0x97, 0x09, 0x4c, 0x45, 0xd6, 0xa9, 0xef};

  // application key is the ASCII string "aDifferentKeyABC"
  uint8_t appKey[] = { 0x4e, 0x40, 0x12, 0x07, 0x09, 0xa4, 0x1a, 0xec, 0xe7, 0xc9, 0x91, 0x4d, 0x39, 0xb4, 0x5a, 0x8c};

  // prior to LoRaWAN 1.1.0, only a single "nwkKey" is used
  // when connecting to LoRaWAN 1.0 network, "appKey" will be disregarded
  // and can be set to NULL

  // some frequency bands only use a subset of the available channels
  // you can select the specific band or set the first channel and last channel
  // for example, either of the following corresponds to US915 FSB2 in TTN
  /*  
    node.selectSubband(2);
    node.selectSubband(8, 15);
  */

  // now we can start the activation
  // this can take up to 10 seconds, and requires a LoRaWAN gateway in range
  // a specific starting-datarate can be selected in dynamic bands (e.g. EU868):
  /* 
    uint8_t joinDr = 4;
    state = node.beginOTAA(joinEUI, devEUI, nwkKey, appKey, joinDr);
  */
  Oled_println("[LoRaWAN] Attempting over-the-air activation ... ");
  node.wipe();
  state = node.beginOTAA(joinEUI, devEUI, nwkKey, appKey);
  Oled_println("TEST");
  if(state == RADIOLIB_ERR_NONE) {
    Oled_println("success!");
  } else {
    Oled_print("failed, code ");
    Oled_println(String(state));
    while(true);
  }

}

// counter to keep track of transmitted packets
int count = 0;

void loop() {
  // send uplink to port 10
  Oled_println("[LoRaWAN] Sending uplink packet ... ");
  String strUp = "Hello World! #" + String(count++);
  String strDown;
  int state = node.sendReceive(strUp, 10, strDown);
  if(state == RADIOLIB_ERR_NONE) {
    Oled_println("received a downlink!");

    // print data of the packet (if there are any)
    Oled_println("[LoRaWAN] Data:\t\t");
    if(strDown.length() > 0) {
      Oled_println(strDown);
    } else {
      Oled_println("<MAC commands only>");
    }

    // print RSSI (Received Signal Strength Indicator)
    Oled_print("[LoRaWAN] RSSI:");
    Oled_print(String(radio.getRSSI()));
    Oled_println(" dBm");

    // print SNR (Signal-to-Noise Ratio)
    Oled_print("[LoRaWAN] SNR:");
    Oled_print(String(radio.getSNR()));
    Oled_println(" dB");

    // print frequency error
    Oled_print("[LoRaWAN] Frequency error:");
    Oled_print(String(radio.getFrequencyError()));
    Oled_println(" Hz");

  } else if(state == RADIOLIB_ERR_RX_TIMEOUT) {
    Oled_println("no downlink!");

  } else {
    Oled_print("failed, code ");
    Oled_println(String(state));
  }

  // wait before sending another packet
  delay(30000);
}

void Oled_println(String new_msg){
  Serial.println(new_msg);
  oledFill(&ssoled, 0x0, 1);

  for(int i=0; i<7; i++){
    monitor[i]=monitor[i+1];
  }

  monitor[7]=new_msg;

  for(int i=0; i<8; i++){
    char char_array[monitor[i].length() + 1];
    monitor[i].toCharArray(char_array, monitor[i].length() + 1);
    oledWriteString(&ssoled, 0,0,i,(char *)char_array, FONT_SMALL, 0, 1);
  }
}

void Oled_print(String new_msg){
  Serial.print(new_msg);
  monitor[7]+=new_msg;
  char char_array[monitor[7].length() + 1];
  monitor[7].toCharArray(char_array, monitor[7].length() + 1);
  oledWriteString(&ssoled, 0,0,7,(char *)char_array, FONT_SMALL, 0, 1);
}

Hardware setup ESP32 Heltec Lora V3 (SX1262) Debug mode output Initializing ...

RadioLib Debug Info
Version:  6.3.0.0
Platform: ESP32
Compiled: Dec 19 2023 20:22:18

CMDW    80
SI      0
SO      FF
CMDW    80
SI      0
SO      AA
CMDW    80
SI      0
SO      A2
CMDR    C0
SI      0
SO      22
CMDR    1D      3       20
SI      0       0       0       0       0       0       0       0       0       0       0       0       0       0       0       0       0
SO      A2      53      58      31      32      36      31      20      56      32      44      20      32      44      30      32      0
Found SX126x: RADIOLIB_SX126X_REG_VERSION_STRING:
0000320 53 58 31 32 36 31 20 56 32 44 20 32 44 30 32 00 | SX1261 V2D 2D02.

M       SX126x
CMDW    80
SI      0
SO      0
CMDW    80
SI      0
SO      AA
CMDW    80
SI      0
SO      A2
CMDR    C0
SI      0
SO      22
CMDW    80
SI      0
SO      A2
CMDR    C0
SI      0
SO      22
CMDW    80
SI      0
SO      A2
CMDR    C0
SI      0
SO      22
CMDR    17
SI      0       0       0
SO      A2      0       20
CMDR    C0
SI      0
SO      22
CMDW    7
SI      0       0
SO      A2      A2
CMDR    C0
SI      0
SO      22
CMDW    97
SI      0       0       1       40
SO      A2      A2      A2      A2
CMDR    C0
SI      0
SO      22
CMDW    8F
SI      0       0
SO      A2      A2
CMDR    C0
SI      0
SO      22
CMDW    8A
SI      1
SO      A2
CMDR    C0
SI      0
SO      22
CMDW    93
SI      20
SO      A2
CMDR    C0
SI      0
SO      22
CMDW    88
SI      3       16      A       0       0       0       0
SO      A2      A2      A2      A2      A2      A2      A2
CMDR    C0
SI      0
SO      22
CMDW    2
SI      43      FF
SO      A2      A2
CMDR    C0
SI      0
SO      22
CMDW    8
SI      0       0       0       0       0       0       0       0
SO      A2      A2      A2      A2      A2      A2      A2      A2
CMDR    C0
SI      0
SO      22
CMDW    89
SI      7F
SO      A2
CMDR    C0
SI      0
SO      22
CMDR    11
SI      0       0
SO      A2      1
CMDR    C0
SI      0
SO      22
CMDW    8B
SI      9       6       3       0
SO      A2      A2      A2      A2
CMDR    C0
SI      0
SO      22
CMDR    11
SI      0       0
SO      A2      1
CMDR    C0
SI      0
SO      22
CMDW    D       7       40
SI      14      24
SO      A2      A2
CMDR    11
SI      0       0
SO      A2      1
CMDR    C0
SI      0
SO      22
CMDR    1D      7       36
SI      0       0
SO      A2      D
CMDR    C0
SI      0
SO      22
CMDW    D       7       36
SI      D
SO      A2
CMDW    8C
SI      0       8       0       FF      1       0
SO      A2      A2      A2      A2      A2      A2
CMDR    C0
SI      0
SO      22
CMDW    96
SI      1
SO      A2
CMDR    C0
SI      0
SO      22
CMDW    D       8       E7
SI      18
SO      A2
CMDW    9D
SI      1
SO      A2
CMDR    C0
SI      0
SO      22
CMDR    11
SI      0       0
SO      A2      1
CMDR    C0
SI      0
SO      22
CMDR    1D      7       36
SI      0       0
SO      A2      D
CMDR    C0
SI      0
SO      22
CMDW    D       7       36
SI      D
SO      A2
CMDW    8C
SI      0       8       0       FF      1       0
SO      A2      A2      A2      A2      A2      A2
CMDR    C0
SI      0
SO      22
CMDR    11
SI      0       0
SO      A2      1
CMDR    C0
SI      0
SO      22
CMDR    1D      7       36
SI      0       0
SO      A2      D
CMDR    C0
SI      0
SO      22
CMDW    D       7       36
SI      D
SO      A2
CMDW    8C
SI      0       8       0       FF      1       0
SO      A2      A2      A2      A2      A2      A2
CMDR    C0
SI      0
SO      22
CMDR    11
SI      0       0
SO      A2      1
CMDR    C0
SI      0
SO      22
CMDW    8B
SI      C       6       3       0
SO      A2      A2      A2      A2
CMDR    C0
SI      0
SO      22
CMDR    11
SI      0       0
SO      A2      1
CMDR    C0
SI      0
SO      22
CMDW    8B
SI      C       4       3       1
SO      A2      A2      A2      A2
CMDR    C0
SI      0
SO      22
CMDW    98
SI      D7      DB
SO      A2      A2
CMDR    C0
SI      0
SO      22
CMDW    86
SI      36      40      0       0
SO      A2      A2      A2      A2
CMDR    C0
SI      0
SO      22
CMDR    1D      8       D8
SI      0       0
SO      A2      C8
CMDR    C0
SI      0
SO      22
CMDW    D       8       D8
SI      DE
SO      A2
CMDR    1D      8       E7
SI      0       0
SO      A2      18
CMDR    C0
SI      0
SO      22
CMDW    95
SI      4       7       0       1
SO      A2      A2      A2      A2
CMDR    C0
SI      0
SO      22
CMDW    8E
SI      11      4
SO      A2      A2
CMDR    C0
SI      0
SO      22
CMDW    D       8       E7
SI      18
SO      A2
success!
[LoRaWAN] Attempting over-the-air activation ... 
CMDR    1D      8       E7
SI      0       0
SO      A2      18
CMDR    C0
SI      0
SO      22
CMDW    95
SI      4       7       0       1
SO      A2      A2      A2      A2
CMDR    C0
SI      0
SO      22
CMDW    8E
SI      10      4
SO      A2      A2
CMDR    C0
SI      0
SO      22
CMDW    D       8       E7
SI      18
SO      A2
CMDR    11
SI      0       0
SO      A2      1
CMDR    C0
SI      0
SO      22
CMDR    11
SI      0       0
SO      A2      1
CMDR    C0
SI      0
SO      22
CMDW    D       7       40
SI      34      44
SO      A2      A2
CMDR    11
SI      0       0
SO      A2      1
CMDR    C0
SI      0
SO      22
CMDR    1D      7       36
SI      0       0
SO      A2      D
CMDR    C0
SI      0
SO      22
CMDW    D       7       36
SI      D
SO      A2
CMDW    8C
SI      0       8       0       FF      1       0
SO      A2      A2      A2      A2      A2      A2
CMDR    C0
SI      0
SO      22

Channel frequency UL = 868.099976 MHz
CMDW    98
SI      D7      DB
SO      A2      A2
CMDR    C0
SI      0
SO      22
CMDW    86
SI      36      41      99      80
SO      A2      A2      A2      A2
CMDR    C0
SI      0
SO      22
DR 72: LORA (SF: 10, BW: 125.000000, CR: 5)
CMDR    11
SI      0       0
SO      A2      1
CMDR    C0
SI      0
SO      22
CMDR    11
SI      0       0
SO      A2      1
CMDR    C0
SI      0
SO      22
CMDW    8B
SI      A       4       3       0
SO      A2      A2      A2      A2
CMDR    C0
SI      0
SO      22
CMDR    11
SI      0       0
SO      A2      1
CMDR    C0
SI      0
SO      22
CMDW    8B
SI      A       4       3       0
SO      A2      A2      A2      A2
CMDR    C0
SI      0
SO      22
CMDR    11
SI      0       0
SO      A2      1
CMDR    C0
SI      0
SO      22
CMDW    8B
SI      A       4       1       0
SO      A2      A2      A2      A2
CMDR    C0
SI      0
SO      22
CMDW    80
SI      0
SO      A2
CMDR    C0
SI      0
SO      22
CMDR    11
SI      0       0
SO      A2      1
CMDR    C0
SI      0
SO      22
CMDR    11
SI      0       0
SO      A2      1
CMDR    C0
SI      0
SO      22
Timeout in 556032 us
CMDR    11
SI      0       0
SO      A2      1
CMDR    C0
SI      0
SO      22
CMDR    1D      7       36
SI      0       0
SO      A2      D
CMDR    C0
SI      0
SO      22
CMDW    D       7       36
SI      D
SO      A2
CMDW    8C
SI      0       8       0       17      1       0
SO      A2      A2      A2      A2      A2      A2
CMDR    C0
SI      0
SO      22
CMDW    8
SI      2       1       0       1       0       0       0       0
SO      A2      A2      A2      A2      A2      A2      A2      A2
CMDR    C0
SI      0
SO      22
CMDW    8F
SI      0       0
SO      A2      A2
CMDR    C0
SI      0
SO      22
CMDW    E       0
SI      0       0       0       0       0       0       0       0       0       79      3A      6       D0      7E      D5      B3      70      1       0       38      1E      3D      25
SO      A2      A2      A2      A2      A2      A2      A2      A2      A2      A2      A2      A2      A2      A2      A2      A2      A2      A2      A2      A2      A2      A2      A2
CMDR    C0
SI      0
SO      22
CMDW    2
SI      43      FF
SO      A2      A2
CMDR    C0
SI      0
SO      22
CMDR    1D      8       89
SI      0       0
SO      A2      4
CMDR    C0
SI      0
SO      22
CMDR    11
SI      0       0
SO      A2      1
CMDR    C0
SI      0
SO      22
CMDW    D       8       89
SI      4
SO      A2
CMDW    83
SI      0       0       0
SO      A2      A2      A2
CMDR    C0
SI      0
SO      62
CMDW    2
SI      43      FF
SO      E2      E2
GPIO pre-transfer timeout, is it connected?
GPIO pre-transfer timeout, is it connected?
Join-request sent <-- Rx Delay start
TEST
failed, code -705

Additional info (please complete):

Error -705 is not occuring when RADIOLIB_VERBOSE is not defined. Is the setup correct? Debug output without RADIOLIB_VERBOSE:

RadioLib Debug Info
Version:  6.3.0.0
Platform: ESP32
Compiled: Dec 19 2023 20:25:26

Found SX126x: RADIOLIB_SX126X_REG_VERSION_STRING:
0000320 53 58 31 32 36 31 20 56 32 44 20 32 44 30 32 00 | SX1261 V2D 2D02.

M       SX126x
success!
[LoRaWAN] Attempting over-the-air activation ... 

Channel frequency UL = 868.099976 MHz
DR 72: LORA (SF: 10, BW: 125.000000, CR: 5)
Timeout in 556032 us
Join-request sent <-- Rx Delay start

Channel frequency DL = 868.099976 MHz
DR 72: LORA (SF: 10, BW: 125.000000, CR: 5)
Opening Rx1 window (226848 us timeout)... <-- Rx Delay end 
closing

Is the problem that I am not in range (I have tested previously 10m to a Gateway from TTN-Mapper) or that something isn't working.

Thanks!

jgromes commented 8 months ago

GPIO pre-transfer timeout, is it connected? GPIO pre-transfer timeout, is it connected?

This suggests there could be some hardware issue, though the module itself seems to be communicating (mostly) fine. Other than that, you seem to always be wipign the persistent storage. That will lead to reuse of previous nonces, which your application server might be rejecting (just a guess though). From RadioLib end, everything seems to be correct. The only suspicious thing is:

// SX1262 has the following connections:
// NSS pin:   8
// DIO0 pin:  13
// RESET pin: 12
// DIO1 pin:  14
SX1262 radio = new Module(8, 13, 12, 14);

Is the pinout actually correct for SX1262? The comment seems to be copied from some SX127x module, since SX126x does not have DIO0 pin.

derGraph commented 8 months ago

I just edited the comments to match my config. DIO0=Busy And how do I change the SF in the otaa sequence?

jgromes commented 8 months ago

DIO0=Busy

So your constructor actually looks like this?

SX1262 radio = new Module(NSS, BUSY, RESET, DIO1);

If that's the case, then you have BUSY and DIO1 swapped. Since DIO1 is used to detected received packets, that could explain why you don't get a reply.

And how do I change the SF in the otaa sequence?

It's stated in the example:

  // a specific starting-datarate can be selected in dynamic bands (e.g. EU868):
  /* 
    uint8_t joinDr = 4;
    state = node.beginOTAA(joinEUI, devEUI, nwkKey, appKey, joinDr);
  */

You will need to consult the LoRaWAN regional specification to find out which SF corresponds to a specific data rate value.

derGraph commented 8 months ago

I tried changing the pins and have removed the node.wipe() now getting error code -6, will try near a lora gateway tomorrow...

derGraph commented 8 months ago

Ok, so I got a connection to a gateway of a friend of mine, it looks like this: image but i dont recieve an answer. How do i have to Format the nwkKey and the appKey (LSB, MSB ...) from the TTN Console for my device. Thanks

jgromes commented 8 months ago

The gateway just forwards this to the TTN servers, so the more interesting information will be found there. If it's dropping the join request for some reason, it should report it (you might have to enable some verbose mode in the node console).

How do i have to Format the nwkKey and the appKey (LSB, MSB ...) from the TTN Console for my device.

It has to be the exact same as what you set in TTN.

derGraph commented 8 months ago

Can you please tell me if i have this translated correctly (I will reset the device after): image image In the console i recieved this WhatsApp Bild 2023-12-31 um 12 07 14_592dd545

jgromes commented 8 months ago

I fail to understand why you swapped the order of the bytes in the key. Like I said above, it has to be exactly the same.

Also JoinEUI 000... is pretty weird, though I guess not strictly forbidden.

derGraph commented 8 months ago

I tried putting the byte in the other order but it didn't work.

jgromes commented 8 months ago

I would suggest trying to find out whgat is the cause for this behavior, instead of guessing. The byte order in keys really isn't the culprit here.

In the console i recieved this

Judging by the gateway status message, I'm guessing that's the gateway console. You probably won't be able to tell why the end device is not joining from there, since the gateway just forwards messages between the TTN servers and your end devices. What does the end device console say? If you enable verbose stream it should tell you e.g. if some nonces need to be reset.

derGraph commented 8 months ago

You have correctly assumed the console of the gateway, in the console of the device i cant find anything. I also created a new device with Join EUI of 0x6969696969696969.

StevenCellist commented 8 months ago

@derGraph really make sure that your keys match. On the pictures you shared, the devEUIs differ between the TTN console and your code. You can use the formatting and copy-buttons on the TTN console to just copy-paste the keys, and in that case you cannot make any typos. If the join-request appears in the gateway console, also look at the device console (probably with verbose mode enabled), to see if there is any problem reported there. But it won't end up at your device console if the devEUI is incorrect, because then it can't match the device which is based on the devEUI...

jgromes commented 7 months ago

Closing due to inactivity.