Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to open the webpage in ESP8266 via Arduino Mega. I have successfully opened the webpage if I use the AT command in the Serial Monitor

The output is perfect

+IPD,382:HTTP/1.1 200 OK
Server: nginx
Date: Sun, 08 May 2016 06:44:47 GMT
Content-Type: text/html
Connection: close
Vary: Accept-Encoding
X-Powered-By: PHP/5.5.26
Set-Cookie: PHPSESSID=rnsmo0b4kq07c5mi3mdsu280g4; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache

S|R190|W5|O31.0|T0|A24|CLOSED

As you can see the output is fine. Now If I issue commands using Arduino Serial1.print() and read the output using following code:

 while( Serial1.available() )
 {
     inChar = Serial1.read();
     Serial.write(inChar);
     //delay(1);         //could play around with this value if buffer overflows are occuring    
 } // while

The output skips character randomly

+IPD,386:HTTP/1.1 200 OK
Server: nginx
Date: Tue, 17 May wed-y: HP/.5.26
SetCooie:PHPESSD=7kameos5kd7aqcn4e54; pth=
Epirs: hu, 19Nov198 0852:0 GT
ach-Cotro: n-stre,no-ach, mst-evaidate,pos-chck=, pe-ceck0
raga: o-cche
F~SR19|W5O310|T90|ENDLOSD

I have tried different values of delay() but of no help. My baudrate is 9600 for both arduino Serial and ESP8266 Serial1

Can you please tell me what can be the reason. Thanks

share|improve this question
up vote 0 down vote accepted

First things first: never use a delay when reading from serial.

You are just blindly trying to read data and send it out to another port. That's not going to work unless you know how much data you need to read.

And that information is handed to you on a plate - you just need to interpret it.

You are required to read the serial data as it comes in and decide what it means, then do different things depending on that data. For instance, when you read the characters +IPD you know you have an IP Data response to be interpreted. Just after that comes a comma, and following that a number. That number is how many bytes you then have to read to get your data. So you keep looking and reading until you have received all the data you should be receiving.

share|improve this answer
    
If you notice the output does contain END This is the flag that shows that entire response has been read. The problem is that data is being skipped in middle randomly. Cache-Control is read like ach-Cotro with skipped characters. – Sallu May 17 at 9:29
    
My guess is it's something happening outside your while loop. That loop won't read the whole of the output - it will just read snippets as they become available, and other things in your program that may consume serial bytes are getting in the way. Follow my instructions and read the output properly. – Majenko May 17 at 9:32
    
I changed the code like you said and it is working fine. I think Serial.print() is coming in way of reading Serial1.read() I first read output in array and then print and it shows fine. – Sallu May 17 at 10:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.