-1
#include <ESP8266WiFi.h>

const char* ssid     =  "Tonosh";
const char* password =  "a43056043";

const char* host     =  "192.168.8.107";


void setup() 
{
  Serial.begin(115200);
  delay(10);

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid,password);

  while(WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP Address: ");
  Serial.println(WiFi.localIP());


}

int value = 0;

void loop() 
{
  delay(2000);
  ++value;

  Serial.print("Connecting to ");
  Serial.println(host);


  WiFiClient client;
  const int httpPort = 80;
  if(!client.connect(host,httpPort))
  {
    Serial.println("Connection failed");
    return;
  }


  String url = "http://192.168.8.107/prueba.php";

  String data = "serie=777&temp=33";


  Serial.print("Requesting URL:  ");
  Serial.println(url);


  client.print(String("POST ") + url + "HTTP/1.0 \r\n" +
               "Host: " + host + "\r\n" +
               "Accept: *" + "/" + "*\r\n" +
               "Content-Length: " + data.length() + "\r\n" +
               "Content-Type: aplication/x-www-form-urlencoded\r\n" +
               "\r\n" + data
              );
  delay(10);           


  Serial.println("Respond:");

  while(client.available())
  {
    String line = client.readStringUntil('\r');
    Serial.print(line);


    Serial.println("Closing connection");
  }
}

Any idea? I tried different post here and nothing works.

  • 2
    in POST line the url should be without protocol and host. "POST /prueba.php HTTP/1.0 – Juraj Jun 27 '18 at 8:21
  • Thanks a lot this simple trick to print it to the console already helped. – appi Mar 7 at 0:24
3

HTTP status code 400 means that your request syntax is invalid.

Here's a good way to debug it: store the request in a string, print that, and then transmit it. You'd see very quickly at least one of the problems your code has.

String request = String("POST ") + url + "HTTP/1.0 \r\n" +
                 "Host: " + host + "\r\n" +
                 "Accept: *" + "/" + "*\r\n" +
                 "Content-Length: " + data.length() + "\r\n" +
                 "Content-Type: aplication/x-www-form-urlencoded\r\n" +
                 "\r\n" + data;
Serial.print(request);
client.print(request);

The request in the POST line needs to be just the pathname and any arguments. You also need a space before HTTP/1.0.

So right now you're transmitting:

POST http://192.168.8.107/prueba.phpHTTP/1.0

when you need to transmit

POST /prueba.php HTTP/1.0
| improve this answer | |

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