Files
hopper_traps/lora_rx/lora_rx.ino
Thaddeus Hughes 9bc81a9ebf init
2025-08-24 07:39:06 -05:00

74 lines
2.5 KiB
C++

void setup() {
Serial.begin(115200); // Serial Monitor for debugging
Serial1.begin(115200); // Serial1 for RYLR998 at 9600 baud
delay(2000); // Wait for RYLR998 to stabilize
Serial.println("Starting RYLR998 receiver configuration...");
// Configure RYLR998 with AT commands and verify responses
sendATCommand("AT+FACTORY", "+OK"); // Reset to factory settings
delay(1000);
sendATCommand("AT+BAND=915000000", "+OK"); // Set frequency to 915MHz
delay(1000);
// Retry setting address up to 3 times
for (int i = 0; i < 3; i++) {
sendATCommand("AT+ADDRESS=2", "+OK"); // Set receiver address to 2
delay(1000);
String address = sendATCommand("AT+ADDRESS?", "+ADDRESS"); // Verify
if (address.indexOf("+ADDRESS=2") != -1) {
Serial.println("Address set successfully to 2");
break;
} else {
Serial.println("Retry " + String(i + 1) + ": Failed to set address");
}
}
sendATCommand("AT+NETWORKID=6", "+OK"); // Set network ID
delay(1000);
Serial.println("Receiver initialized");
// Verify configuration
sendATCommand("AT+BAND?", "+BAND"); // Check frequency
sendATCommand("AT+ADDRESS?", "+ADDRESS"); // Check address
sendATCommand("AT+NETWORKID?", "+NETWORKID"); // Check network ID
sendATCommand("AT+UART?", "+UART"); // Check baud rate
sendATCommand("AT+VER?", "+VER"); // Check firmware version
}
void loop() {
// Check for incoming serial data from RYLR998
if (Serial1.available()) {
String received = Serial1.readStringUntil('\n'); // Read incoming message
Serial.print("Received: ");
Serial.println(received); // Print all data for debugging
}
}
// Helper function to send AT command and print response
String sendATCommand(String command, String expected) {
Serial1.println(command);
Serial.print("Sent AT: ");
Serial.println(command);
String response = waitForResponse(2000); // Wait up to 2 seconds
Serial.print("Response: ");
Serial.println(response);
if (!response.startsWith(expected)) {
Serial.println("Warning: Unexpected response for " + command);
}
return response;
}
// Helper function to read response from RYLR998
String waitForResponse(unsigned long timeout) {
unsigned long start = millis();
String response = "";
while (millis() - start < timeout) {
if (Serial1.available()) {
response += Serial1.readStringUntil('\n');
if (response.length() > 0) return response; // Return on first complete response
}
delay(10); // Small delay to prevent tight loop
}
return response; // Return empty or partial response if timeout
}