Need to query RYLR998 LoRa device for its UID in my Arduino code

In a serial terminal, when connected directly to the RYLR998, I can get the UID using:
AT+UID?

I need to query for this UID from within my Arduino application, and assign it to a variable, so that the UID can be forwarded to a receiving LoRa radio for unique identification. Any ideas on how to format this code?

A link to the RYLR998 datasheet/manual might help forum.

Item

Datasheet

Thanks for pointing that out!

The variable would be a character array. A good place to start is the Serial Input Basics tutorial.

You will also find the RYLR998 AT Command Guide handy.

Thanks for replying. Maybe I could be somewhat more specific in my question. Here is my current code...

String deviceUID = "";
String RX_address = "1";
int uidLength = "";

void setup() {
  Lora.begin(115200);

  // OBTAIN RADIO DEVICE UID
  Lora.println("AT+ID=DevAddr");
  delay(500);
  while (Lora.available()) {
    char c = Lora.read();
    deviceUID += c;
  }
deviceUID.trim();
uidLength = deviceUID.length();
}

void loop() {
  int messageLength = uidLength + 22;  //add length of known string to length of uid
  Lora.println("AT+SEND=" + RX_address + "," + String(messageLength) + ",UID:" + deviceUID + ",No water detected");
}

When I query the device, nothing is returned into the variable 'deviceUID'. I have tried using both AT+UID? and AT+ID=DevAddr. I am seeking the correct syntax to query for the device's UID.

When you state that the "variable would be a character array," are you implying that the RYLR998 returns an array of all available AT data, and that I need to specify which element in the array I want?

Avoid using String objects. They add absolutely nothing, and on smaller Arduinos, the poor memory management associated with their usage leads to program crashes and/or malfunctions.

Thank you for the good advice. Code altered as advised (and could still be improved)

Original question remains open. Still looking for the specific syntax for querying the device from my code, instead of from a serial monitor.

char lora_RX_address[] = "1";
char deviceUID[50];
int uidLength = "";

 void setup() {
  loraSerial.begin(115200);

  // OBTAIN RADIO DEVICE UID
  loraSerial.println("AT+UID?");
  delay(500);
  size_t index = 0;
  while (loraSerial.available() && index < sizeof(deviceUID) - 1) {
    char c = loraSerial.read();
    deviceUID[index++] = c;
  }
  deviceUID[index] = '\0';
  size_t uidLength = strlen(deviceUID);
 }
 
 void loop() {
       int messageLength = uidLength + 22;
       loraSerial.println("AT+SEND=" + String(lora_RX_address) + "," + String(messageLength) + ",UID:" + deviceUID + ",No water detected");
 }

You have a rough idea of how to proceed, but it looks like you have not fully absorbed the lessons in the Serial Input Basics tutorial, and you definitely need more study of C/C++ basic concepts.

For example, this won't do what you think.

int uidLength = "";

Code altered as advised

Not completely. To avoid crashes caused by String object usage, replace constructs like this:

loraSerial.println("AT+SEND=" + String(lora_RX_address) + "," + String(messageLength) + ",UID:" + deviceUID + ",No water detected");

with statements like these:

loraSerial.print("AT+SEND=");
loraSerial.print(lora_RX_address);
loraSerial.print(",");
loraSerial.print(messageLength);
loraSerial.print(",UID:");
loraSerial.print(deviceUID);
loraSerial.println(",No water detected");

Thanks! You are, without a doubt, correct. I am jumping into this with many years of web app development experience, but literally have never written anything in C++. The above is the result of literally my first hour of working with C++ datatyping. I understand your revision perfectly, though.

Any idea of why my request for the output of AT+UID? might not be giving me back the UID I am seeking from the device, given that this works perfectly when called from the serial monitor?

I’d just send the "AT+UID?" command to the module using your Arduino’s serial communication and then read the response into a string variable. Make sure to give it a small delay after the command so the module has time to respond. I’ve done something similar with other LoRa modules, and sometimes you need to tweak the baud rate or add a bit of error checking if the response isn’t clean. It’s pretty straightforward once you get the timing right.

Thanks everyone. So, the following lines otherwise appear to be correct to you, and you think it's just a baud rate issue? I'll certainly try dropping the rate down and/or raising the wait delay time.

loraSerial.println("AT+UID?");
delay(500);

Weirdly, I can not see the returned value of deviceUID (the variable appears empty when I print it to the serial monitor), but when I print the value of strlen(deviceUID), it returns a length of 584 characters, so something must be coming back. If I find the resolution, I'll make certain to post it here. Thanks!

when I print the value of strlen(deviceUID), it returns a length of 584 characters

Do you see the problem here?

Thanks to everyone who replied. Finally worked this out, and sharing for anyone else on the same learning path. Here's the code that finally worked. I'm sure it's not perfectly formed, but it successfully pulls the UID from a Lora device, counts the character length, adds this count to the count of the message being sent, and forms the send instruction in the expected format, with the transmitting UID in the message, so that the v device can be identified.

// GLOBAL LORA VARIABLES
char lora_RX_address[] = "1";
const byte numChars = 128;
char deviceUID[numChars];
int uidLength = "";
boolean newData = false;
  
void setup() {
	// GET UID FROM LORA DEVICE
	receiveLoraData();
	uidLength = strlen(deviceUID);
}

void loop() {
	char message = "Insert message to send here."
	int messageLength = uidLength + strlen(message);
	Serial1.print("AT+SEND=");
	Serial1.print(lora_RX_address);
	Serial1.print(",");
	Serial1.print(messageLength);
	Serial1.print(",SRC: ");
	Serial1.print(deviceUID);
	Serial1.print(", ");
	Serial1.println(message);
}


void receiveLoraData() {
	static byte ndx = 0;
	char endMarker = '\n';
	char rc;
	
	while (newData == false) {
		Serial1.println("AT+UID?");
		delay(500);
		rc = Serial1.read();
		
		/* TO VIEW EACH CHARACTER AS IT'S BEING RETURNED
		 SUPER HELFUL FOR DDEBUGGING THE DATA BEING RETURNED
		Serial.print("Char: '");
		Serial.print(rc);
		Serial.print("' ASCII: ");
		Serial.println((int)rc);
		*/
		
		if (rc != endMarker) {
			deviceUID[ndx] = rc;
			ndx++;
				if (ndx >= numChars) {
				  ndx = numChars - 1;
				}
		}
		else {
		deviceUID[ndx] = '\0';
		ndx = 0;
		newData = true;
		}
	}
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.