2

Anybody had worked with ESP8266 to post data to RESTful web service? I was trying but it's not posting anything. Tried with a test server:

http://www.posttestserver.com/

Serial.println("\nSetting AT+CIPSTART");
String client_conn = "AT+CIPSTART=\"TCP\",\"";
client_conn += SERVER;
client_conn += "\",";
client_conn += PORT;
Serial.println(client_conn);
Serial2.println(client_conn);
delay(3000);
while (Serial2.available() > 0) {
    c = Serial2.read();
    Serial.print(c);
    if(c=='\n\r' || c == '\r\n')
    Serial.println('\n\r'); 
}

It showed me that it got connected Now I created POST string

String data = "{JSONBODY}";
String post = "POST /post.php HTTP/1.0\r\n";
post += "Host: posttestserver.com\r\n";
post += "Content-Length: ";
post += data.length();
post += "\r\n";
post += data;

Transmitting data

Serial2.print("AT+CIPSEND=");
Serial2.println(post.length());
delay(1000);
if(Serial2.find(">")) {
    Serial2.println(post);
    while (Serial2.available() > 0) {
        c = Serial2.read();
        Serial.print(c);
        if(c=='\n\r' || c == '\r\n')
            Serial.println('\n\r'); 
    }
    delay(5000);
    Serial2.flush();
    delay(5000);
}

Returned nothing Closing connection

delay(1000);
Serial2.println("AT+CIPCLOSE");
while (Serial2.available() > 0) {
    c = Serial2.read();
    Serial.print(c);
    if(c=='\n\r' || c == '\r\n')
        Serial.println('\n\r'); 
}
delay(5000);
Serial2.flush();

Connection closed

But It returned nothing. posttestserver returns data path where it stored.

3
  • 1
    1) There must be two CRLFs between the headers and the data, thus replace post += "\r\n"; by post += "\r\n\r\n";. 2) c == '\r\n' does not make sense, as \r and \n are two different characters. Commented Jun 15, 2015 at 8:22
  • Thanks man, I will try it and let you know @EdgarBonet :) Commented Jun 15, 2015 at 8:40
  • @EdgarBonet Thanks man. Another thing I had to do is, I had to add a 1000uF capacitor across VCC and GND as due to data transmission it fall short of power. Commented Jun 16, 2015 at 5:51

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.