3

I have an ESP8266, which I'm programming in Arduino. I need make a POST request. I tried this code:

#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>

void setup() {

  Serial.begin(115200);                                  //Serial connection
  WiFi.begin("ssid", "wifi_password");   //WiFi connection

  while (WiFi.status() != WL_CONNECTED) {  //Wait for the WiFI connection completion

    delay(500);
    Serial.println("Waiting for connection");

  }
}

void loop() {

  if(WiFi.status()== WL_CONNECTED){   //Check WiFi connection status

    HTTPClient http;    //Declare object of class HTTPClient

    http.begin("server_name");      //Specify request destination
    http.header("POST / HTTP/1.1");
    http.header("Host: server_name");
    http.header("Accept: */*");
    http.header("Content-Type: application/x-www-form-urlencoded");
    int httpCode = http.POST("Message from ESP8266");   //Send the request
    String payload = http.getString();                                        
    Serial.println(httpCode);   //Print HTTP return code
    Serial.println(payload);    //Print request response payload
    http.end();  //Close connection

  }else{

    Serial.println("Error in WiFi connection");   

  }

  delay(5000);  //Send a request every 30 seconds

}

But on the web page I did not receive any response.

Undo
  • 25,519
  • 37
  • 106
  • 129
Matej
  • 782
  • 2
  • 6
  • 19
  • "But on the web page i did not receive any response" — What web page? Why would the response appear **on** a web page? – Quentin Dec 28 '16 at 23:50
  • `http.header("Content-Type: application/x-www-form-urlencoded"); ` and `http.POST("Message from ESP8266");` — Those don't match. – Quentin Dec 28 '16 at 23:51
  • On the web page is mysql database. And what is correct? – Matej Dec 29 '16 at 09:32
  • "On the web page is mysql database" — You can't have a MySQL database "on a webpage". You can create a web page using content from a MySQL database. You can do that dynamically (with server side code) in response to a request for a web page. – Quentin Dec 29 '16 at 10:16
  • "And what is correct?" — Either encoding the data as x-www-form-urlencoded or changing the content-type to match the data (which looks like `text/plain` – Quentin Dec 29 '16 at 10:17
  • "But on the web page i did not receive any response" — What web page? Why would the response appear on a web page? – Quentin Dec 29 '16 at 10:18
  • Overwriting the header each time. – cagdas Dec 30 '16 at 05:22

1 Answers1

10

I found this code and it works!

#include <ESP8266WiFi.h>
const char* ssid     = "ssid";
const char* password = "password";

const char* host = "server_name";

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

  // We start by connecting to a WiFi network
  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()); 
}

void loop() {

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


 String data = "pst=temperature>" + String(random(0,100)) +"||humidity>" + String(random(0,100)) + "||data>text";

   Serial.print("Requesting POST: ");
   // Send request to the server:
   client.println("POST / HTTP/1.1");
   client.println("Host: server_name");
   client.println("Accept: */*");
   client.println("Content-Type: application/x-www-form-urlencoded");
   client.print("Content-Length: ");
   client.println(data.length());
   client.println();
   client.print(data);

   delay(500); // Can be changed
  if (client.connected()) { 
    client.stop();  // DISCONNECT FROM THE SERVER
  }
  Serial.println();
  Serial.println("closing connection");
  delay(5000);
}

Thank you very much for your help.

Matej
  • 782
  • 2
  • 6
  • 19