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

80 lines
2.7 KiB
C++

#include <SoftwareSerial.h>
// Initialize SoftwareSerial on pins D4 (RX) and D5 (TX)
SoftwareSerial rySerial(4, 5); // RX, TX
void setup() {
Serial.begin(115200); // Serial Monitor for debugging
rySerial.begin(115200); // RYLR998 at 9600 baud
delay(2000); // Wait for RYLR998 to stabilize
Serial.println("Starting RYLR998 transmitter 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=1", "+OK"); // Set transmitter address to 1
delay(1000);
String address = sendATCommand("AT+ADDRESS?", "+ADDRESS"); // Verify
if (address.indexOf("+ADDRESS=1") != -1) {
Serial.println("Address set successfully to 1");
break;
} else {
Serial.println("Retry " + String(i + 1) + ": Failed to set address");
}
}
sendATCommand("AT+NETWORKID=6", "+OK"); // Set network ID
delay(1000);
Serial.println("Transmitter 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() {
// Send a message to address 2 (receiver)
String message = "Hello, RYLR998!";
String command = "AT+SEND=2," + String(message.length()) + "," + message;
sendATCommand(command, "+OK"); // Send and verify
Serial.println("Sent: " + command);
delay(500); // Wait 2 seconds
}
// Helper function to send AT command and print response
String sendATCommand(String command, String expected) {
rySerial.println(command);
Serial.print("Sent AT: ");
Serial.println(command);
rySerial.flush(); // Wait for command to be sent
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 (rySerial.available()) {
response += rySerial.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
}