ricaun / LoRaWanPacket

LoRaWanPacket Encode / Decoder
MIT License
10 stars 2 forks source link

Downlink message #1

Closed Samuel-ZDM closed 3 years ago

Samuel-ZDM commented 3 years ago

Hello!

I'm here again kkkk.

My question is: Does this library work for downlink messages?

Thanks in advance.

ricaun commented 3 years ago

Hello!

Yes kinda work! But you need to change and open the RX window with the right frequency and other parameters for the specific LoRaWAN channel.

#include <SPI.h>
#include <LoRa.h>

#include <LoRaWanPacket.h>

const int csPin = 10;
const int resetPin = 9;
const int irqPin = 2;

const char *devAddr = "11111111";
const char *nwkSKey = "11111111111111111111111111111111";
const char *appSKey = "11111111111111111111111111111111";

struct LoRa_config
{
  long Frequency;
  int SpreadingFactor;
  long SignalBandwidth;
  int CodingRate4;
  bool enableCrc;
  bool invertIQ;
  int SyncWord;
  int PreambleLength;
};

// ttn US915
static LoRa_config txLoRa = {903900000, 7, 125000, 5, true, false, 0x34, 8};
static LoRa_config rxLoRa = {923300000, 7, 500000, 5, false, true, 0x34, 8};
static LoRa_config rxLoRa2 = {923300000, 12, 500000, 5, false, true, 0x34, 8};

void LoRa_setConfig(struct LoRa_config config)
{
  LoRa.setFrequency(config.Frequency);
  LoRa.setSpreadingFactor(config.SpreadingFactor);
  LoRa.setSignalBandwidth(config.SignalBandwidth);
  LoRa.setCodingRate4(config.CodingRate4);
  if (config.enableCrc)
    LoRa.enableCrc();
  else
    LoRa.disableCrc();
  if (config.invertIQ)
    LoRa.enableInvertIQ();
  else
    LoRa.disableInvertIQ();
  LoRa.setSyncWord(config.SyncWord);
  LoRa.setPreambleLength(config.PreambleLength);
}

void LoRa_TxMode()
{
  LoRa_setConfig(txLoRa);
  LoRa.idle();
}

void LoRa_RxMode()
{
  LoRa_setConfig(rxLoRa);
  LoRa.receive();
}

void LoRa_RxMode2()
{
  LoRa_setConfig(rxLoRa2);
  LoRa.receive();
}

void setup()
{
  Serial.begin(115200);
  while (!Serial);

  LoRaWanPacket.personalize(devAddr, nwkSKey, appSKey);

  LoRa.setPins(csPin, resetPin, irqPin);

  if (!LoRa.begin(txLoRa.Frequency)) {
    Serial.println("LoRa init failed. Check your connections.");
    while (true);
  }

  Serial.println("LoRa init succeeded.");
  Serial.println();

  LoRa.onReceive(onReceive);
  LoRa.onTxDone(onTxDone);

  LoRa_sendMessage();
}

void loop() {
  if (runEvery(10000)) {

    LoRa_sendMessage();

    Serial.println("Send Message!");
  }
}

void LoRa_sendMessage()
{
  LoRa_TxMode();
  LoRaWanPacket.clear();
  LoRaWanPacket.print("Hello World");
  if (LoRaWanPacket.encode()) 
  {
    LoRa.beginPacket();
    LoRa.write(LoRaWanPacket.buffer(), LoRaWanPacket.length());
    LoRa.endPacket(true);
  }
}

void onReceive(int packetSize) {
  Serial.println("Receive: ");
  LoRaWanPacket.clear();
  while (LoRa.available()) {
    LoRaWanPacket.write(LoRa.read());
  }
  int port = LoRaWanPacket.decode();
  int length = LoRaWanPacket.length();
  switch (port) {
    case 1:
      // LoRaWanPacket.read();
      break;
  }
}

void onTxDone() {
  LoRa_RxMode();
}

boolean runEvery(unsigned long interval)
{
  static unsigned long previousMillis = 0;
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval)
  {
    previousMillis = currentMillis;
    return true;
  }
  return false;
}

This example only sets the window to RX1 frequency.

I need to create a better example hehehe

Samuel-ZDM commented 3 years ago

Thank you! I tested now uplink worked, downlink did not work, but it is not a problem of your code, because I am using an ESP32 as a gateway and it is not being able to send downlink messages.

Very good library. It consumes much less memory space. Thanks a lot for the help.

Note: I tested the frequency of the micro with 8 MHz and 1 MHz, initially it worked correctly.

Samuel-ZDM commented 3 years ago

Hello!

I am trying to send messages via downlink but I am not getting it. I'm using ttn as a network server. If I'm not mistaken, ttn only sends messages in the second window. I tried to modify the code and it still didn't work. Can you help me?

Thanks in advance.

ricaun commented 3 years ago

Hello!

It depends on your LoRaWAN network, usually is rx1 if the network is good.

Here I have another project that uses Rx1 and Rx2.

https://github.com/ricaun/LoRaWan_Energy_Meter

I'm using US915 and works perfectly, for join and downlink.

See yaa.

Samuel-ZDM commented 3 years ago

Thank you so much for your answer.

I'm going to do some tests with your example. Return soon.

See you!

Samuel-ZDM commented 3 years ago

Hello!

I tested it here with the example LoRaWanPacket_LoRa_join and it is giving the same problem that I faced with other libraries. He sends the first one and then to. Strange that with the previous example the Uplink messages worked well, the downlink did not work.

I am very suspicious that there is a problem with the irq pin. I don't know if it can be that. In my case, I'm using pin 9, which is DIO0, as shown in the figure.

image

Thanks in advance.

ricaun commented 3 years ago

Hello,

That's strange... Did you test if these examples work?

You need 2 boards to test.

DIO0 is responsible for the TxDone and the Receive so if is not working the onTxDone never happens.

Add some Serial.println("onTxDone"); on the onTxDone.

void onTxDone() {
  Serial.println("onTxDone");
  LoRa_RxMode();
}

Ohhh that's the problem, DIO0 needs to be an interruption check the LoRa - source code.

You should change to pin 2 or 3.

See yaa!

Samuel-ZDM commented 3 years ago

Thank you very much for your feedback.

I didn't specifically test these examples, but I did communication between two devices using only LoRa without LoRaWAN and it worked. It just doesn't work when I use a LoRaWAN library.

I believe it must be the pin I am using. As you showed it needs interruption to work.

See you.

Samuel-ZDM commented 3 years ago

Thank you so much! It worked!

I switched from to pin 3 and now it's working!

I am now trying to do a duty cycle in which the device goes into sleep mode. I saw that example of you from the Energy Meter and I was left with a doubt. I'll put it here, but if you want me to put it in the other repository I can put it there.

My question is how you save the value of the counter to know what message it is in, because when he sleeps he loses all the values of the variables.

Thank you very much in advance! See you.

ricaun commented 3 years ago

Thank you so much! It worked!

I switched from to pin 3 and now it's working!

Great! 😄

I am now trying to do a duty cycle in which the device goes into sleep mode. I saw that example of you from the Energy Meter and I was left with a doubt. I'll put it here, but if you want me to put it in the other repository I can put it there.

My question is how you save the value of the counter to know what message it is in, because when he sleeps he loses all the values of the variables.

The variables for the counter to uplink and downlink below: https://github.com/ricaun/LoRaWanPacket/blob/bfe2479c9c74dc310d457631522f4484d3e15932/src/LoRaWanPacket.h#L31-L32

The atmega328p when wake up does not lose the values of the variables, only lose if you reset the board.

See Yaa!

Samuel-ZDM commented 3 years ago

Thank you very much!

I am facing some problems now when I get downlink. If you don't want to help me because I'm asking too many questions, that's fine, but I'll open a new issue kkkk.

Samuel-ZDM commented 3 years ago

Hello!

Must the downlink packets received by the end device be in base64 format?

I'm doing some studies to receive a downlink and the end device receives it, but it doesn't decode it, implying that the package is wrong or not for it.

Thanks in advance.

jovanfrandika commented 1 year ago

Hello!

Must the downlink packets received by the end device be in base64 format?

I'm doing some studies to receive a downlink and the end device receives it, but it doesn't decode it, implying that the package is wrong or not for it.

Thanks in advance.

Got the same issue here with downlink. Currently using this frequency plan. https://www.thethingsnetwork.org/community/jakarta/post/indonesia-regional-plan-as923-2