Send macro commands to serial, get a programmed response

See attached for my current sketch.

I'm having no luck with the following:

I would like to be able to send a command via serial to initiate an action for the software serial.

i.e.

I send "SENDPOSIT" via serial, the board recognizes the specific String, then encodes and sends it's GPS lat/long/elevation information to my software serial port to be transmitted via the attached LoRa module.

I have been successful at getting it to send a location, but I've consistently run into two issues.

  1. It reacts to ANY incoming String, regardless of the content, not just the intended "SENDPOSIT"
  2. It is only sending lat/long to the second one-hundredths (DD.DD), which is not accurate enough.

Thanks!

LoRaCOM-WiFi-Confirmed.ino (12.6 KB)

'SENDPOSIT' does not exist in the sketch you posted.

I'm aware of that. I was just using it as an example.

So, let me rephrase #1, and you tell me if it's correct:

  1. It reacts to ANY incoming String, regardless of the content. How can I add code so that it only responds to "SENDPOSIT"?

Question: Does the display library support 6 decimal digit printing of a float? Check the library documentation. Is it at the screen edge? Maybe you wrote off the end.

Yes, exactly that.

As for the DD.MM issue, it is displaying properly on my OLED display using:

display.println (gps.location.lat(), 6);
display.println (gps.location.lng(), 6);

Which will display DD.DDDDDD, DD.DDDDDD

However, if I try to use the same (), 6) in the portion below, I receive "-1" as the output.

Here's an example of what I was using that was giving the proper result, but reacting to ANY incoming String:

while(Serial.available()){

if(Serial.readString() == "SENDPOSIT"){
gps.encode(gpss.read());
latSend = gps.location.lat();
lngSend = gps.location.lng();
eleSend = gps.altitude.feet();
locSend = callSign+latSend+lngSend+eleSend;
locSize = locSend.length();
Serial.println("Sending your position...");
display.clearDisplay();
display.setCursor(0,16);
display.println("Sending your position...");
display.display();
loras.println("AT+SEND=0,"+locSize+","+locSend);
delay(500);
}
}

Yeah, the nasty String class converts floats to Strings with only 2 decimal place fractional digits. What board are you using?

I'm new... I'm gonna need you to break that one down for me.

I'm using an ESP8266, Fire Beetle Node MCU.

Is there a correct way to accomplish what I'm trying to do? Would sending a predetermined HEX string be better than ASCII?

Okay, you missed my edit - my bad. What board are you using?

void setup() {
 Serial.begin(115200);
float pie = 3.151942;
String pieString = "string:";
pieString = pieString + pie;
Serial.println(pie, 6);
Serial.println(pieString);
}

void loop() {}

prints

3.151942
string:3.15

ESP8266, Fire Beetle Node MCU.

I guess I don't understand why I can adjust how many decimal places print to my display, but not in the other portion of the sketch.

snake4377:
ESP8266, Fire Beetle Node MCU.

I guess I don't understand why I can adjust how many decimal places print to my display, but not in the other portion of the sketch.

The String class does that by default when it converts a float to a string. The print class does not. Edit - well it does actually. But the String class has a different mechanism for specifying digits.

Gotcha. That makes sense, unfortunately.

Thanks for your help.

Any ideas on getting the macro command to function properly?

Yes:

void setup() {
  // put your setup code here, to run once:
Serial.begin(115200);
float pie = 3.151942;
String pieString = "string:";
String pieString2 = "string2:";
pieString = pieString + pie;
pieString2 = pieString2 + String(pie, 6);
Serial.println(pie, 6);
Serial.println(pieString);
Serial.println(pieString2);
}

void loop() {
  // put your main code here, to run repeatedly:

}

prints:

3.151942
string:3.15
string2:3.151942

What macro command?

I mean, getting it to recognize and react only to the correct serial input.

While you are busy fixing the String problem, someone will comment on that. Hint:

Serial.readString() reads characters from the serial buffer into a String. The function terminates if it times out

You checked with available(), but not how many characters are there. What if there aren't enough?

That makes sense... I'll try that out. Thanks again for all your help.

NP :slight_smile:

Or you could use a parser library and just tell it to look for your different command strings..

-jim lee

aarg:

float pie = 3.151942;

?????? Since when? :slight_smile:

I said pie, not pi. :slight_smile: Pies don't have an exact diameter, each one is slightly different.

Can you please provide a complete minimal working example which demonstrates the "reacts to any String" issue.

Write an MVCE, minimal reproducible example.

You will need more than this.

Here's an example of what I was using that was giving the proper result, but reacting to ANY incoming String:

 while(Serial.available()){

      if(Serial.readString() == "SENDPOSIT"){
          gps.encode(gpss.read());
          latSend  = gps.location.lat();
          lngSend = gps.location.lng();
          eleSend = gps.altitude.feet();
          locSend = callSign+latSend+lngSend+eleSend;
          locSize = locSend.length();
          Serial.println("Sending your position...");
          display.clearDisplay();
          display.setCursor(0,16);
          display.println("Sending your position...");
          display.display();
          loras.println("AT+SEND=0,"+locSize+","+locSend);
          delay(500);
      }
      }