esp8266 AT commands embedded code

hi to all ,

i am trying to write the embedded c code for esp8266 via AT commands .

flashed the AT commands firmware in the esp8266 . connected esp8266 via rx and tx with arduino mega pins 18-19.

Now first for testing purpose i am just sending the AT command and expecting the response from esp8266 as OK.

#include <uarts.h>
#include <avr/delay.h>
int main(void)
{ char buff[100];
//UART_Init0();
UART_Init3(); //for printing on serial monitor
UART_Init1(); ///for esp bauddrate is selected 115200
printString3("*hellow");
printString3("\n");

while(1)
{
printString3("type a command \n");
printString1("AT"); //sending command to esp8266
printString3("AT \n"); //printing command to terminal screen
printString3("received response \n");
// rxstring1(buff); //blue colour
for(int i=0;i<2;i++) //green colour
{
buff*=UART_RxChar1();*
UART_TxChar3(buff*);
delay_ms(1000);
_
}*

* //printString3(buff);*
* printString3("\n");*
}}[/quote]
this is code which i have written and sending the AT command via serial UART now but in response i am not getting the OK command i am getting output like this.
https://drive.google.com/file/d/... (green output when sending code coloured in green and blue when sending clue cloured code)
Now I have 2 questions
1) why i am not getting OK in response and instead getting the command which i am sending
2) does the response of AT commands had anything to with CR and NL ...

for reference below is my git repo for above uart functions ..
_* https://github.com/gkunalupta/uart-/blob/master/code*_

AT commands needs carriage return + newline.

Well okay i have used now carriage return + newline .But problem is now that in response i am only getting AT .....

When i send command AT to esp8266 in return as a response i only get the AT ( echo enabled) ....as i have echo enabled so it should send response
AT

OK

But it is only sending the AT.

So i tried something

i debug my code by printing the bit value of UCSRnA each time before data is received and after data is received ...so that i can get the live status of RXCn bit.

and here is the output.

output

Now interesting thing which to see is

  1. that as i have send the AT command to esp8266 with \r\n ....so in return esp8266 will send the response . So RXCn bit of UCSRnA register will be 1 till esp8266 is sending the response . But here after sending 2 or 3 characters this bit is cleared . as you can see in pic. So why is this happening??????

My esp8266 is receiving the command that is sure but at the time of printing response , it is not doing that. why??????

  1. and 5th bit of UCSRnA ..that is DORn bit is set to 1 after receiving two characters. What can be reason for this , can this be the reason for clearing of my RXCn bit.

You can see the register details of UCSRnA here

Here is the revised code

[color=green]include <uarts.h>

#include <avr/delay.h>
int main(void)
{ char buff[100];
char str[30];
//UART_Init0();
UART_Init3(); //for printing on serial monitor
UART_Init1(); ///for esp bauddrate is selected 115200
//printString3("*hellow");
printString3("\n");

while(1)
{
 printString3("type a command to send \n");
 printString1("ATE1\r\n"); //sending command to esp8266
 printString3("ATE1 \n"); //printing command to terminal screen
 printString3("receiving response \n");

//UART_TxChar3(rxstring3(buff));
	 				for(int i=0;i<20;i++)
	{ printString3("\nWaiting to receive data \n");
							 buff[i]=UART_RxChar1();
							printString3(" received data \n");
UART_TxChar3(buff[i]);
_delay_ms(1000);

}
}}

/* function definations of uart functions which i used
void printint3(int myint[])

{

uint16_t i = 0;
while (myint[i])
{
	UART_TxChar3(myint[i]);
	i++;
}

}

void UART_TxChar3(char data)
{
while((UCSR3A & (1<<UDRE3))==0);
UDR3 = data;

}

void printString1( char *myString) /////to print any string
{
char i = 0;
while (myString[i])
{
UART_TxChar1(myString[i]);
i++;
}
}

int UART_RxChar1()
{
printString3(" UCSR1A value before receiving data \n");
bit3(UCSR1A);
printString3(" \n");

while( !(UCSR1A & (1<<RXC1)));

printString3(" UCSR1A value after receiving data \n");
bit3(UCSR1A);
printString3("\n");
return UDR1;

}[/color]