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: 2016年5月08日 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: 1981年11月19日 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
-
Can you share with us the At commands to access a web page ?The Zack– The Zack2018年12月21日 13:43:20 +00:00Commented Dec 21, 2018 at 13:43
1 Answer 1
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.
-
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– Sallu2016年05月17日 09:29:36 +00:00Commented May 17, 2016 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– Majenko2016年05月17日 09:32:51 +00:00Commented May 17, 2016 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– Sallu2016年05月17日 10:01:31 +00:00Commented May 17, 2016 at 10:01