// Include the required libraries
#include <SPI.h>
#include <LoRa.h>

// Define the LoRa module pins
#define SCK     5    // SPI Clock (SCK)
#define MISO    19   // SPI MISO
#define MOSI    27   // SPI MOSI
#define SS      18   // LoRa Chip Select (NSS)
#define RST     14   // LoRa Reset
#define DIO0    26   // LoRa IRQ (DIO0)

// LoRa frequency
#define LORA_FREQUENCY 915E6 // Set to your frequency (e.g., 433E6, 868E6, 915E6)

void setup() {
  // Start the serial communication
  Serial.begin(9600);
  while (!Serial);
  Serial.println("LoRa ESP32 Interface");

  // Check SPI connection
  SPI.begin(SCK, MISO, MOSI, SS);
  pinMode(SS, OUTPUT);
  digitalWrite(SS, HIGH); // Ensure the SS pin is high

  // Setup LoRa module
  LoRa.setPins(SS, RST, DIO0); // Set LoRa module pins

  if (!LoRa.begin(LORA_FREQUENCY)) {
    Serial.println("Starting LoRa failed! Check wiring and frequency.");
    while (1);
  }
  Serial.println("LoRa started successfully!");
}

void loop() {
  // Send a test message every 5 seconds
  Serial.println("Sending packet...");
  LoRa.beginPacket();
  LoRa.print("Hello, LoRa!");
  LoRa.endPacket();

  Serial.println("Packet sent!");
  delay(5000);
}
