A weird is happening which i can't figure out why. First I had issue communicating with SIM800L. So i tried checking the USART Hardware to see what UNO sends to SIM800 using small piece attached below. The sketch is to compares what i send from serial monitor with strings "Hello", "heLLo" and "Hello" expecting to get Found .... or No match found. each line string i send, the serial monitor prints twice WHY?, The first says not found and the second say Received == ........ WHY?
**See the output of serial terminal when i typed "Hello" with quotes.
12:03:46.536 -> Buffer Received= " No match found
12:03:46.569 ->
12:03:46.569 -> Buffer Received= Hello"
12:03:46.603 -> Found Hello
when i typed "HELLO" with quotes.
12:03:55.855 -> Buffer Received= " No match found
12:03:55.888 ->
12:03:55.888 -> Buffer Received= HELLO"
12:03:55.921 -> Found HELLO**
****when i typed "heLLo" with quotes.
12:04:04.016 -> Buffer Received= " No match found
12:04:04.049 ->
12:04:04.049 -> Buffer Received= heLLo"
12:04:04.082 -> Found heLLo
12:04:04.115 ->****
When i typed without quotes, all i get No match found
char buffer[100];
uint8_t index=0;
void setup()
{
Serial.begin(9600);
_delay_ms(4000);
}
void loop()
{
if(Serial.available()>0)
{
index=0;
while(Serial.available())
{
buffer[index++]=Serial.read();
}
buffer[index]='\0';
Serial.println(' ');
Serial.print("Buffer Received");
Serial.print(buffer);
Serial.print('\t');
if(strstr(buffer, "Hello")!=NULL) Serial.println("Found Hello");
else if(strstr(buffer, "HELLO")!=NULL) Serial.println("Found HELLO");
else if(strstr(buffer, "heLLo")!=NULL) Serial.println("Found heLLo");
else Serial.println("No match found");
Serial.flush();
}
}
Note: Suspecting my PC hardware and serial driver,
so I tried it in my Ubuntu 20.04 and window 10 installed with Arduino IDE 1.8.13
suspecting it may be crystal, i tried two different boards and used even Atmega328P core.
I also suspected my keyboard "ENTER" key, so i tried the serial monitor "send" button.
with no difference.
I seems to run short is ideas so i am posting to the community. Let me have your advice and experience too.
Thanks.
delay(10);
after the firstSerial.available()> 0
is an ugly but doable alternative in this case.