I am trying to build a home automation system and i am unable to get data from a website using the arduino sketch, however i am able to get data from the website when i enter the AT commands into the serial monitor.Below is my sketch:

#include <SoftwareSerial.h>

SoftwareSerial esp8266(4,5);

void setup(){
  Serial.begin(9600);
  esp8266.begin(115200);
  esp8266.println("AT+CIPMUX=1\r\n");
  delay(2000);
  esp8266.println("AT+CIPSTART=0,\"TCP\",\"www.homeautomationserver.com\",80 \r\n");
  delay(2000);
  esp8266.println("AT+CIPSEND=0,75");
  esp8266.println("GET /f/pw9e1acqx7yb0gl.txt HTTP/1.1\r\n");
  esp8266.println("Host:www.homeautomationserver.com\r\n\r\n");
}

void loop(){

  if (esp8266.available()){
    Serial.write(esp8266.read());
  }
  if (Serial.available()){
    esp8266.write(Serial.read());
  }
}

I get a 501 not Implemented error from the server.

Please Help.

Thanks

share|improve this question
    
computerhope.com/issues/ch001020.htm – Len Sep 20 '16 at 7:43
    
You should really include Connection: close in your headers as well, so the server doesn't try and keep the connection open longer than needed. – Majenko Sep 20 '16 at 9:34
    
Content-length: ??? might help too. (??? should be the message size) – Matt Sep 20 '16 at 10:05
    
@Matt Content-length would be 0 - there is no body there to have a length. It's not strictly needed, especially if the sending pipe is manually closed by the client. – Majenko Sep 20 '16 at 10:07
    
Maybe you should replace esp8266.println() with esp8266.print(), because you already append "\r\n" in your strings. – jfpoilpret Sep 20 '16 at 21:18
up vote 0 down vote accepted

Can you use you favourite web browser and retrieve the page http://www.homeautomationserver.com/f/pw9e1acqx7yb0gl.txt ? If you can then the problem is with your client, if not its with the address you are requesting or the server.

The fact you are getting a 501 error indicates that GET is not supported for this resource. So check that you are using the correct method?

Get yourself a REST console (there is an add-on for chrome and plenty of others) try typing the command in manually to see if you can identify the fault.

Look at Wikipedia for a list of error codes, it might give you a clue as to what is wrong.

And don't you need a AT+CIPSEND after you have printed the http packet?

share|improve this answer
    
And don't you need a AT+CIPSEND after you have printed the http packet? - no that comes beforehand. The numbers indicate how many bytes you are about to send so it knows how many to expect. – Majenko Sep 20 '16 at 9:32
    
@Majenko - OK, thanks for the correction, I just saw an example that had the send after the header, maybe it was sending an empty body(?). – Matt Sep 20 '16 at 10:03
1  
If the send is after any form of data (including the header) the data will be interpreted as ESP8266 AT commands and will fail. The example you found must be horribly broken. – Majenko Sep 20 '16 at 10:04
    
Thanks for your reply , I have tried using your code and this time i am not receiving any reply from the server. i have also noticed that using my code i sometimes receive a 400 error -bad request as well followed by the 501 error after a few tries .i am so confused with the error codes generated by the server. – Ashviel Sep 21 '16 at 0:53
    
Sorry Matt i was trying to reply to Majenko and thank you for your reply as well, I know the issue is with my sketch but cannot figure out what exactly is the problem. – Ashviel Sep 21 '16 at 1:02

Your problem lies in these lines:

esp8266.println("AT+CIPSEND=0,75");
esp8266.println("GET /f/pw9e1acqx7yb0gl.txt HTTP/1.1\r\n");
esp8266.println("Host:www.homeautomationserver.com\r\n\r\n");

You are mistakenly using println() instead of print() when sending the data. That imposes its own <CR><LF> sequence on the end of the lines, yet you are adding the <CR><LF> manually. You are also incorrectly specifying 75 characters to send, so the result of all that is:

GET /f/pw9e1acqx7yb0gl.txt HTTP/1.1\r\n              37
\r\n                                                  2
Host:www.homeautomationserver.com\r\n                35
\r                                                    1
                                                   ----
                                                     75

I.e., your request is garbled. You need to change the println() to print() for the data printing and make sure the character count is correct:

esp8266.println("AT+CIPSEND=0,74");
esp8266.print("GET /f/pw9e1acqx7yb0gl.txt HTTP/1.1\r\n");
esp8266.print("Host:www.homeautomationserver.com\r\n\r\n");

GET /f/pw9e1acqx7yb0gl.txt HTTP/1.1\r\n              37
Host:www.homeautomationserver.com\r\n                35
\r\n                                                  2
                                                   ----
                                                     74

You should also include more detail and commands in your headers. Things like the length of the body (0 bytes) and the fact that the connection should be closed, and there really should be a space after the colon on the Host: header:

esp8266.println("AT+CIPSEND=0,113");
esp8266.print("GET /f/pw9e1acqx7yb0gl.txt HTTP/1.1\r\n");
esp8266.print("Connection: close\r\n");
esp8266.print("Content-Length: 0\r\n");
esp8266.print("Host: www.homeautomationserver.com\r\n\r\n");

GET /f/pw9e1acqx7yb0gl.txt HTTP/1.1\r\n              37
Connection: close\r\n                                19
Content-Length: 0\r\n                                19
Host: www.homeautomationserver.com\r\n               36
\r\n                                                  2
                                                  -----
                                                    113
share|improve this answer
    
Thanks for your reply , I have tried using your code and this time i am not receiving any reply from the server. i have also noticed that using my code i sometimes receive a 400 error -bad request as well followed by the 501 error after a few tries .i am so confused with the error codes generated by the server. – Ashviel Sep 21 '16 at 1: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.