Ok guys, my friend has helped me and made this little code:
#include <stdio.h>
#define LED 13
void setup() {
pinMode(LED, OUTPUT);
Serial.begin(9600);
}
void loop() {
if (Serial.available() == 4) {
char command[5];
for (int i = 0; i < 4; i++) command[i] = Serial.read();
command[4] = '\0';
Serial.println(command);
if (strcmp(command, "AAAA") == 0) {
digitalWrite(LED, HIGH);
Serial.println("LED13 is ON");
} else if (strcmp(command, "BBBB") == 0) {
digitalWrite(LED, LOW);
Serial.println("LED13 is OFF");
}
} else {
Serial.flush();
}
}
Everything works, except for one little and very important thing:
If I send AAAA, it will print AAAA and do the action. However If I send AAAAA, it will make everything the same way and print only AAAA. But when I send BBBB (for example), it will print ABBB. In my words, It is "saving" a character. And this must not happen since the router will be sending "random" stuff trought serial that must be ignored and must not mess with the commands that I send.
Anyone knows how to solve this? Thanks!