chandrawi / LoRaRF-Arduino

Arduino library for basic transmitting and receiving data using LoRa and FSK modulation
MIT License
53 stars 15 forks source link

Ebyte Lora E220-400M22S Facing problem in Building communication Between Transmitter and Receiver #23

Open krishank652 opened 5 months ago

krishank652 commented 5 months ago

Greetings,

I'm currently facing a challenge with establishing communication between a Transmitter and Receiver Device. Specifically, I'm utilizing the Ebyte LoRa E220-400M22S alongside the MCU ATTINY804 IC. Below, I've provided the code snippet I'm employing:

#include <SX126x.h>

SX126x LoRa;

// Message to transmit
char message[] = "HeLoRa World!";
uint8_t nBytes = sizeof(message);
uint8_t counter = 0;

void setup() {

  // Begin serial communication
  Serial.begin(9600);

  // Uncomment below to use non default SPI port
  //SPIClass SPI_2(PB15, PB14, PB13);
  //LoRa.setSPI(SPI_2, 16000000);

  // Begin LoRa radio and set NSS, reset, busy, IRQ, txen, and rxen pin with connected arduino pins
  // IRQ pin not used in this example (set to -1). Set txen and rxen pin to -1 if RF module doesn't have one
  Serial.println("Begin LoRa radio");
  int8_t nssPin = 0, resetPin = 1, busyPin = 2, irqPin = -1, txenPin = -1, rxenPin = -1;
  if (!LoRa.begin(nssPin, resetPin, busyPin, irqPin, txenPin, rxenPin)){
    Serial.println("Something wrong, can't begin LoRa radio");
    while(1);
  }

  // Optionally configure TCXO or XTAL used in RF module
  // Different RF module can have different clock, so make sure clock source is configured correctly
  // uncomment code below to use TCXO
  //Serial.println("Set RF module to use TCXO as clock reference");
  //uint8_t dio3Voltage = SX126X_DIO3_OUTPUT_1_8;
  //uint32_t tcxoDelay = SX126X_TCXO_DELAY_10;
  //LoRa.setDio3TcxoCtrl(dio3Voltage, tcxoDelay);
  // uncomment code below to use XTAL
  //uint8_t xtalA = 0x12;
  //uint8_t xtalB = 0x12;
  //Serial.println("Set RF module to use XTAL as clock reference");
  //LoRa.setXtalCap(xtalA, xtalB);

  // Optionally configure DIO2 as RF switch control
  // This is usually used for a LoRa module without TXEN and RXEN pins
  //LoRa.setDio2RfSwitch(true);

  // Set frequency to 433 Mhz
  Serial.println("Set frequency to 433 Mhz");
  LoRa.setFrequency(433000000);

  // Set TX power, default power for SX1262 and SX1268 are +22 dBm and for SX1261 is +14 dBm
  // This function will set PA config with optimal setting for requested TX power
  Serial.println("Set TX power to +17 dBm");
  LoRa.setTxPower(17, SX126X_TX_POWER_SX1262);                        // TX power +17 dBm for SX1262
delay(200);
  // Configure modulation parameter including spreading factor (SF), bandwidth (BW), and coding rate (CR)
  // Receiver must have same SF and BW setting with transmitter to be able to receive LoRa packet
  Serial.println("Set modulation parameters:\n\tSpreading factor = 7\n\tBandwidth = 125 kHz\n\tCoding rate = 4/5");
  uint8_t sf = 7;                                                     // LoRa spreading factor: 7
  uint32_t bw = 125000;                                               // Bandwidth: 125 kHz
  uint8_t cr = 5;                                                     // Coding rate: 4/5
  LoRa.setLoRaModulation(sf, bw, cr);

delay(200);
  // Configure packet parameter including header type, preamble length, payload length, and CRC type
  // The explicit packet includes header contain CR, number of byte, and CRC type
  // Receiver can receive packet with different CR and packet parameters in explicit header mode
  Serial.println("Set packet parameters:\n\tExplicit header type\n\tPreamble length = 12\n\tPayload Length = 15\n\tCRC on");
  uint8_t headerType = SX126X_HEADER_EXPLICIT;                        // Explicit header mode
  uint16_t preambleLength = 12;                                       // Set preamble length to 12
  uint8_t payloadLength = 15;                                         // Initialize payloadLength to 15
  bool crcType = true;                                                // Set CRC enable
  LoRa.setLoRaPacket(headerType, preambleLength, payloadLength, crcType);

delay(200);
  // Set syncronize word for public network (0x3444)
  Serial.println("Set syncronize word to 0x3444");
  LoRa.setSyncWord(0x3444);

delay(200);
  Serial.println("\n-- LORA TRANSMITTER --\n");

}

void loop() {

  // Transmit message and counter
  // write() method must be placed between beginPacket() and endPacket()
  LoRa.beginPacket();
  LoRa.write(message, nBytes);
  LoRa.write(counter);
  LoRa.endPacket();

  // Print message and counter in serial
  Serial.print(message);
  Serial.print("  ");
  Serial.println(counter++);

  // Wait until modulation process for transmitting packet finish
  LoRa.wait();

  // Print transmit time
  Serial.print("Transmit time: ");
  Serial.print(LoRa.transmitTime());
  Serial.println(" ms");
  Serial.println();

  // Don't load RF module with continous transmit
  delay(5000);

}

Upon execution, I encounter certain issues which I'll detail further. Here are the pertinent details:

Transmitter Serial Monitor Result:

22:29:59.131 -> Begin LoRa radio
22:29:59.177 -> Set frequency to 433 Mhz
22:29:59.177 -> Set TX power to +17
22:29:59.224 -> 
22:29:59.224 -> 
22:30:00.130 -> Set packet parameters:
22:30:00.177 ->     Explicit header type
22:30:00.177 ->     Preamble length =
22:30:00.224 -> 
22:30:00.224 -> 
22:30:05.148 -> HeLoRa World!  1
22:30:05.195 -> Transmit time: 52 ms
22:30:05.195 -> 
22:30:05.195 -> 
22:30:05.195 -> 
22:30:05.195 -> 
22:30:10.166 -> HeLoRa World!  2
22:30:10.166 -> Transmit time: 52 ms
22:30:10.212 -> 
22:30:10.212 -> 
22:30:10.212 -> 
22:30:10.212 -> 
22:30:15.186 -> HeLoRa World!  3
22:30:15.186 -> Transmit time: 52 ms
22:30:15.233 -> 
22:30:15.233 -> 
22:30:15.233 -> 
22:30:15.233 -> 
22:30:20.204 -> HeLoRa World!  4
22:30:20.204 -> Transmit time: 52 ms
22:30:20.252 -> 
22:30:20.252 -> 
22:30:20.252 -> 
22:30:20.252 ->

Receiver Code:

#include <SX126x.h>

SX126x LoRa;

void setup() {

  // Begin serial communication
  Serial.begin(9600);

  // Begin LoRa radio and set NSS, reset, busy, txen, and rxen pin with connected arduino pins
  Serial.println("Begin LoRa radio");
  int8_t nssPin = 0, resetPin = 1, busyPin = 2, irqPin = -1, txenPin = -1, rxenPin = -1;
  if (!LoRa.begin(nssPin, resetPin, busyPin, irqPin, txenPin, rxenPin)){
    Serial.println("Something wrong, can't begin LoRa radio");
    while(1);
  }

  // Configure TCXO or XTAL used in RF module
  //Serial.println("Set RF module to use TCXO as clock reference");
  //uint8_t dio3Voltage = SX126X_DIO3_OUTPUT_1_8;
  //uint32_t tcxoDelay = SX126X_TCXO_DELAY_10;
  //LoRa.setDio3TcxoCtrl(dio3Voltage, tcxoDelay);

  // Set frequency to 433 Mhz
  Serial.println("Set frequency to 433 Mhz");
  LoRa.setFrequency(433000000);

  // Set RX gain to boosted gain
  Serial.println("Set RX gain to boosted gain");
  delay(1000);
  LoRa.setRxGain(SX126X_RX_GAIN_BOOSTED);

  // Configure modulation parameter including spreading factor (SF), bandwidth (BW), and coding rate (CR)
  Serial.println("Set modulation parameters:\n\tSpreading factor = 7\n\tBandwidth = 125 kHz\n\tCoding rate = 4/5");
  delay(1000);

  uint8_t sf = 7;
  uint32_t bw = 125000;
  uint8_t cr = 5;
  LoRa.setLoRaModulation(sf, bw, cr);

  // Configure packet parameter including header type, preamble length, payload length, and CRC type
  Serial.println("Set packet parameters:\n\tExplicit header type\n\tPreamble length = 12\n\tPayload Length = 15\n\tCRC on");
  delay(1000);
  uint8_t headerType = SX126X_HEADER_EXPLICIT;
  uint16_t preambleLength = 12;
  uint8_t payloadLength = 15;
  bool crcType = true;
  LoRa.setLoRaPacket(headerType, preambleLength, payloadLength, crcType);

  // Set syncronize word for public network (0x3444)
  Serial.println("Set syncronize word to 0x3444");
  LoRa.setSyncWord(0x3444);

  Serial.println("\n-- LORA RECEIVER CONTINUOUS --\n");

  // Request for receiving new LoRa packet in RX continuous mode
  LoRa.request(SX126X_RX_CONTINUOUS);

}

void loop() {

  Serial.println("-- LOOP START --");

  // Check for incoming LoRa packet
  const uint8_t msgLen = LoRa.available();
  if (msgLen) {

    // Put received packet to message and counter variable
    char message[msgLen-1];
    uint8_t counter;
    uint8_t i=0;
    while (LoRa.available() > 1){
      message[i++] = LoRa.read();
    }
    counter = LoRa.read();

    // Print received message and counter in serial
    Serial.print(message);
    Serial.print("  ");
    Serial.println(counter);

    // Print packet/signal status including package RSSI and SNR
    Serial.print("Packet status: RSSI = ");
    Serial.print(LoRa.packetRssi());
    Serial.print(" dBm | SNR = ");
    Serial.print(LoRa.snr());
    Serial.println(" dB");

    // Show received status in case CRC or header error occur
    uint8_t status = LoRa.status();
    if (status == SX126X_STATUS_CRC_ERR) Serial.println("CRC error");
    else if (status == SX126X_STATUS_HEADER_ERR) Serial.println("Packet header error");
    Serial.println();

  }
  delay(100);
  Serial.println("-- LOOP END --");
  delay(100);
}

On serial Monitor I got:

22:43:03.157 -> ⸮Begin LoRa radio
22:43:03.204 -> Set frequency to 433 Mhz
22:43:03.204 -> Set RX gain to boo
22:43:03.251 -> 
22:43:03.251 -> 
22:43:04.183 -> Set modulation parameters:
22:43:04.183 ->     Spreading factor = 7
22:43:04.229 ->     Bandwidth = 1
22:43:04.229 -> 
22:43:04.229 -> 
22:43:05.167 -> Set packet parameters:
22:43:05.214 ->     Explicit header type
22:43:05.214 ->     Preamble length =
22:43:05.261 -> 
22:43:05.261 -> 
22:43:06.198 -> Set syncronize word to 0x3444
22:43:06.198 -> 
22:43:06.198 -> -- LORA RECEIVER CONTINUOUS --
22:43:06.245 -> 
22:43:06.245 -> 
22:43:06.245 -> 
22:43:07.182 -> -- LOOP START --
22:43:07.182 -> -- LOOP END --
22:43:07.229 -> -- LOOP START --
22:43:07.229 -> -- LOOP END
22:43:07.229 -> 
22:43:07.229 -> 
22:43:08.171 -> -- LOOP START --
22:43:08.218 -> -- LOOP END --
22:43:08.218 -> -- LOOP START --
22:43:08.218 -> -- LOOP END
22:43:08.264 -> 
22:43:08.264 -> 
22:43:09.202 -> -- LOOP START --
22:43:09.202 -> -- LOOP END --
22:43:09.202 -> -- LOOP START --
22:43:09.249 -> -- LOOP END
22:43:09.249 -> 
22:43:09.249 -> 
22:43:10.187 -> -- LOOP START --
22:43:10.187 -> -- LOOP END --
22:43:10.234 -> -- LOOP START --
22:43:10.234 -> -- LOOP END
22:43:10.280 -> 
22:43:10.280 -> 
22:43:11.211 -> -- LOOP START --
22:43:11.211 -> -- LOOP END --
22:43:11.211 -> -- LOOP START --
22:43:11.258 -> -- LOOP END
22:43:11.258 -> 
22:43:11.258 -> 
22:43:12.196 -> -- LOOP START --
22:43:12.196 -> -- LOOP END --
22:43:12.244 -> -- LOOP START --
22:43:12.244 -> -- LOOP END
22:43:12.244 -> 
22:43:12.244 -> 

Connections:

Note: TXEN, RXEN, DI01 are not connected.

Power Supply: I'm utilizing the AMS1117 3.3V Power Supply Module, drawing input from Arduino 5V and GND, then supplying it to the circuit via the AMS1117 3.3V Power supply.

Programming: For ATTINY804 programming, I'm leveraging the megaTinyCore Library and referring to the ATTINY804 pinout details.

I'm seeking assistance from the library developer and the community to resolve this issue. Any guidance or suggestions would be greatly appreciated. Thank you in advance.