version using String (not recommended, but it makes simpler to understand the following C-string version)
#define LED 2
const char* a = "abcd";
void setup() {
Serial.begin(115200);
pinMode(LED, OUTPUT);
}
void loop() {
if (Serial.available()) {
String s = Serial.readStringUntil('\n');
s.trim();
if (s == a) {
digitalWrite(LED, HIGH);
} else {
digitalWrite(LED, LOW);
}
}
}
the version with C-string:
#define LED 2
const char* a = "abcd";
char buffer[32];
void setup() {
Serial.begin(115200);
pinMode(LED, OUTPUT);
}
void loop() {
if (Serial.available()) {
size_t l = Serial.readBytesUntil('\n', buffer, sizeof(buffer - 1));
if (buffer[l - 1] == '\r') {
l--;
}
buffer[l] = 0; // the terminating zero
Serial.println(buffer);
if (strcmp(buffer, a) == 0) {
digitalWrite(LED, HIGH);
} else {
digitalWrite(LED, LOW);
}
}
}