61 lines
1.8 KiB
C++
Executable File
61 lines
1.8 KiB
C++
Executable File
#include <HardwareSerial.h>
|
|
|
|
#define LORA_UART Serial2
|
|
#define LORA_TX_PIN 17 // GPIO17 for TX
|
|
#define LORA_RX_PIN 16 // GPIO16 for RX
|
|
|
|
#define RELAY_PIN 13 // GPIO pin to control the relay
|
|
|
|
void setup() {
|
|
Serial.begin(115200); // Main serial for debugging
|
|
LORA_UART.begin(9600, SERIAL_8N1, LORA_RX_PIN, LORA_TX_PIN); // Initialize UART for LoRa
|
|
|
|
pinMode(RELAY_PIN, OUTPUT); // Set the relay pin as output
|
|
digitalWrite(RELAY_PIN, LOW); // Ensure the relay is off at startup
|
|
|
|
if (LORA_UART) {
|
|
Serial.println("LoRa Receiver Initialized");
|
|
} else {
|
|
Serial.println("LoRa Receiver Initialization Failed");
|
|
}
|
|
}
|
|
|
|
void loop() {
|
|
// Check for incoming LoRa messages via UART
|
|
if (LORA_UART.available()) {
|
|
String incoming = "";
|
|
while (LORA_UART.available()) {
|
|
char receivedChar = (char)LORA_UART.read();
|
|
Serial.print(receivedChar); // Print each received character
|
|
incoming += receivedChar;
|
|
}
|
|
Serial.println("\nReceived via LoRa: " + incoming);
|
|
|
|
// Handle relay commands
|
|
if (incoming == "turn_on_front_sprinkler") {
|
|
digitalWrite(RELAY_PIN, HIGH); // Turn on the relay
|
|
Serial.println("Relay turned on");
|
|
} else if (incoming == "turn_off_front_sprinkler") {
|
|
digitalWrite(RELAY_PIN, LOW); // Turn off the relay
|
|
Serial.println("Relay turned off");
|
|
}
|
|
}
|
|
|
|
// Check for Serial Monitor command
|
|
if (Serial.available()) {
|
|
String command = Serial.readStringUntil('\n');
|
|
command.trim(); // Remove any extra whitespace
|
|
|
|
if (command == "restart") {
|
|
Serial.println("Restarting Receiver...");
|
|
delay(1000); // Give time for message to print
|
|
ESP.restart();
|
|
} else if (command == "test") {
|
|
LORA_UART.print("Test message from Receiver");
|
|
Serial.println("Sent test message via LoRa");
|
|
}
|
|
}
|
|
|
|
delay(1000); // Add a delay to avoid watchdog timer reset
|
|
}
|