#include <LoRa.h>
// LoRa module pins
#define LORA_CS 10
#define LORA_RST 9
#define LORA_IRQ 2
// Define SIMULATE_LORA for Wokwi simulation
#define LoRA_simulation Copy
void setup() {
// Initialize Serial Monitor
Serial.begin(9600);
while (!Serial);
#ifndef LoRA_simulation Copy
// Initialize LoRa for actual hardware
Serial.println("Initializing LoRa receiver...");
LoRa.setPins(LORA_CS, LORA_RST, LORA_IRQ); // Set LoRa module pins
if (!LoRa.begin(433E6)) { // Ensure the frequency matches the transmitter
Serial.println("LoRa initialization failed!");
while (1);
}
Serial.println("LoRa receiver initialized!");
#else
// Bypass LoRa initialization in simulation mode
Serial.println("Simulating LoRa receiver in Wokwi!");
#endif
}
void loop() {
// Check for incoming packets (only if LoRa is initialized in real hardware)
#ifndef LoRA_simulation Copy
int packetSize = LoRa.parsePacket();
if (packetSize) {
// Read packet
String receivedData = "";
while (LoRa.available()) {
receivedData += (char)LoRa.read();
}
// Print received data to Serial Monitor
Serial.println("Received: " + receivedData);
}
#else
// Simulate receiving data in Wokwi
String receivedData = "Accel X: 1.23 m/s^2, Accel Y: 0.98 m/s^2, Accel Z: 9.81 m/s^2";
Serial.println("Simulated Received: " + receivedData);
#endif
delay(1000); // Small delay to prevent excessive processing
}