ESP8266: HTTP POST Requests

Introduction

The objective of this post is to explain how to do POST requests from an ESP8266, using the Arduino IDE and the ESP8266 libraries. All the tests shown here were performed on a NodeMCU board, which you can find at Ebay for less than 5 euros.

If you prefer a video tutorial, please check my YouTube Channel below.

The setup

First, we need to include some libraries, which should be available after the installation of the ESP8266 support for the Arduino IDE.

We will need the ESP8266WiFi.h, so we can connect the ESP8266 to a WiFi network, and the ESP8266HTTPClient.h, which makes available the methods needed to perform the POST request.

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

In the Arduino setup function, we will start by initializing a serial connection, so we can output the results of our program.

Serial.begin(115200);

After that, we will take care of connecting the ESP8266 to a WiFi network, so later we can send the HTTP request. To do so, we only need to call the begin method on the WiFi extern variable, passing as first input the name of the network (SSID) and as second the password. Note that, in the snippet below, I’m using placeholder strings that you should replace by your network credentials.

For a detailed tutorial on how to connect a ESP8266 to a WiFi network, please check here.

WiFi.begin("yourSSID", "yourPASS"); 

To finish the setup function, we will poll the status of the WiFi connection until it is established. We are using a polling approach here for simplicity, since we can only start doing the HTTP POST requests after the connection to the WiFi network is established. Naturally, in a real application, we should take in consideration that there might be some issue trying to connect to the network and that polling might not be the best approach for the requirements.

The complete setup function can be seen below.

void setup() {

  Serial.begin(115200);                 //Serial connection
  WiFi.begin("yourSSID", "yourPASS");   //WiFi connection

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

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

  }

}

The main code

In this section, we will analyze the code needed in the Arduino main loop function to perform the HTTP POST request. We will break the code in parts and analyze them step by step, but the final result is summarized at the end of this section.

First of all, we need to define an object of class HTTPClient, from which we will call various methods to prepare the headers and content of the request, send it and check for the result. We will call this object simply “http“.

HTTPClient http;

After that, we will call the begin method on the http object and pass the URL that we want to connect to and make the HTTP POST request. In this case, I’m sending the post request to an application running on my local network, which is why I’m sending it the format seen bellow (Host_IP:Port/Path).

http.begin("http://192.168.1.88:9999/hello");

Nevertheless, we can send the request to a website by specifying it’s domain name, as seen bellow (the destination website used in the code snippet implements a dummy REST API for testing and prototyping).

http.begin("http://jsonplaceholder.typicode.com/users");

Next, we can define headers for the request with the addHeader method. In this case, we are specifying the content-type as “text/plain“, since we will just send a simple string in the body.

http.addHeader("Content-Type", "text/plain");

The body of the request is specified as a parameter when calling the POST method on the HTTPClient object. In this case, we will simply send a string saying “Message from ESP8266”. The return value of this method corresponds to the HTTP response code and thus it is important to check for error handling.

Take in consideration that, if the value is greater than 0, it corresponds to a standard HTTP code. If this value is lesser than 0, it corresponds to a ESP8266 error, related with the connection. You can check the list of possible errors here.

int httpCode = http.POST("Message from ESP8266");

We can now get the payload by calling the getString method, which will return the response payload as a String.

String payload = http.getString();

We will print both the received payload and the HTTP code.

Serial.println(httpCode);
Serial.println(payload);

To finish, we need to call the end method on the http object to guarantee that the TCP connection is closed. This is very important to free the resources.

http.end();

The final code is shown bellow. To handle any possible WiFi connection errors, we will include a validation of the connection status before making the request. We will also add a 30 seconds delay between each iteration of the Arduino loop, to avoid a constant polling of the server.

Note that, to keep the code simpler and focus on the main subject of sending the HTTP POST request, we did not check if the httpCode variable is less than zero, which indicates an error in the connection. Nevertheless, we should do so in the final code of an application.

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

void setup() {

  Serial.begin(115200);                 //Serial connection
  WiFi.begin("yourSSID", "yourPASS");   //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("http://192.168.1.88:8085/hello");      //Specify request destination
    http.addHeader("Content-Type", "text/plain");  //Specify content-type header

    int httpCode = http.POST("Message from ESP8266");   //Send the request
    String payload = http.getString();                  //Get the response payload

    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(30000);  //Send a request every 30 seconds

}

Testing the code

In the context of this tutorial, I was sending the post requests to a MuleSoft application running in a machine on my local network. Just to illustrate the result, figure 1 shows the output of incoming requests on the console of the development environment. As can be seen in the bottom right, it is printing the “Message from ESP8266” string we defined early.

MuleSoft application receiving HTTP POST from ESP8266
Figure 1 – Output of the MuleSoft application that is receiving the POST requests.

Figure 2 shows the HTTP codes and response payloads for the POST requests. In this case, I defined a simple “Message received” string as response to the requests.

Output of the ESP8266 POST Request program, on the Arduino IDE serial monitor
Figure 2 – Output of the POST requests.

To avoid having to setup your own server to test the ESP8266 code, you can simply send the request to the testing API suggested in the previous section.

Final Notes

As an alternative, the begin method used before can be called with other sets of parameters, as can be seen in the specification of the HTTPClient class. For example, we can pass the host IP, port and path as 3 different parameters, instead of a single string, amongst many other options.

The HTTPClient class also has a method to simplify debugging of a response to the request. So, if we want to print the response payload to the serial port, we can just call the writeToStream method and pass as argument a pointer to the Serial port, that we initialized before in the setup function. So, the call bellow is an alternative to the getString method used in the code section:

http.writeToStream(&Serial);

These are just 2 alternative implementation examples. The HTTPClient class has many other useful methods not used in this tutorial. You can check them here.

Technical details

  • ESP8266 libraries: v2.3.0.
  • Operating system: Windows 8.1

Related Posts

242 thoughts on “ESP8266: HTTP POST Requests”

  1. Is it possible to do this kind of POST request from arduino UNO board (not directly from ESP8266)?

    I need to read a card ID with an RFID reader (attached to an arduino board) and send it to a django server via POST. I have found several examples on how to put the ESP8266 as server, but I don’t want it. I just want to send the data to a django server which has a few REST webservices available.

    Is it possible with ESP8266? if not, how can I do that?

    Thank you!

    1. Do you mean the nodemcu which can be board independent or the esp8266 which gives connectivity to the arduino because putting the if you’re using the esp8266 it’s the arduino that’s the server.

      1. Hi,

        NodeMCU is the name of a generic board that has a ESP8266 inside it.

        So, when you are “programming a NodeMCU”, in fact, you are programming a ESP8266.

        The Arduino is another board. Most Arduino boards don’t have WiFi support.

        So, it is not possible to send a POST request directly from an Arduino board that doesn’t have WiFi connectivity.

        However, you can connect an Arduino board to an ESP8266 board (for example, a nodeMCU), establish a serial connection with it, and send a command to the ESP8266, that then is able to send the HTTP POST request,

        Hope this clarifies. 🙂

        Best regards,
        Nuno Santos

        1. Worked fine..After 2 days, again tried to post request it does not working.Where the NodeMCU serial monitor is showing http payload error “-1” I am unable to understand, what I am doing wrong.

          1. Hi!

            when you mention after two days, did you leave the system working for two days and after that it stopped working? Or did you test the same code again after two days?

            Best regards,
            Nuno Santos

        2. Yes, you can use AT Commands to do so.

          One easy way to do so is using SoftwareSerial Library and connect your ESP8266 with Arduino and Send AT+CIPSEND=”channel Name (generally 0)”,”No. of Characters , you want to send +2″.

          We have used +2 because it also includes CR & NL . Apply this in while loop, So that you don’t have to give commands again and again.

          PS:- If you don’t find code for SoftwareSerial for ESP8266, refer for Bluetooth, it will work with small modifications .

          Hope this clarifies.

          Best Regards,
          Anurag Agarwal

  2. Is it possible to do this kind of POST request from arduino UNO board (not directly from ESP8266)?
    I need to read a card ID with an RFID reader (attached to an arduino board) and send it to a django server via POST. I have found several examples on how to put the ESP8266 as server, but I don’t want it. I just want to send the data to a django server which has a few REST webservices available.
    Is it possible with ESP8266? if not, how can I do that?
    Thank you!

    1. Do you mean the nodemcu which can be board independent or the esp8266 which gives connectivity to the arduino because putting the if you’re using the esp8266 it’s the arduino that’s the server.

      1. Hi,
        NodeMCU is the name of a generic board that has a ESP8266 inside it.
        So, when you are “programming a NodeMCU”, in fact, you are programming a ESP8266.
        The Arduino is another board. Most Arduino boards don’t have WiFi support.
        So, it is not possible to send a POST request directly from an Arduino board that doesn’t have WiFi connectivity.
        However, you can connect an Arduino board to an ESP8266 board (for example, a nodeMCU), establish a serial connection with it, and send a command to the ESP8266, that then is able to send the HTTP POST request,
        Hope this clarifies. 🙂
        Best regards,
        Nuno Santos

        1. Worked fine..After 2 days, again tried to post request it does not working.Where the NodeMCU serial monitor is showing http payload error “-1” I am unable to understand, what I am doing wrong.

          1. Hi!
            when you mention after two days, did you leave the system working for two days and after that it stopped working? Or did you test the same code again after two days?
            Best regards,
            Nuno Santos

        2. Yes, you can use AT Commands to do so.
          One easy way to do so is using SoftwareSerial Library and connect your ESP8266 with Arduino and Send AT+CIPSEND=”channel Name (generally 0)”,”No. of Characters , you want to send +2″.
          We have used +2 because it also includes CR & NL . Apply this in while loop, So that you don’t have to give commands again and again.
          PS:- If you don’t find code for SoftwareSerial for ESP8266, refer for Bluetooth, it will work with small modifications .
          Hope this clarifies.
          Best Regards,
          Anurag Agarwal

  3. In the example of this post, we are using the ESP8266 as a client and not as a server, so you can send data to a Django server (or any other application that can receive HTTP requests). It should work fine with the REST webservices. Just check what type of content the Django server is expecting to receive (it can be, for example, JSON) and specify it in the addHeader function.

    You can’t do this kind of POST request using only an Arduino UNO board, since it can’t connect to the Internet. So, you will always need to use the ESP8266 to send the actual HTTP POST to the server.

    Nevertheless, you can use the Arduino board to read the data from the RFID reader, send that data to the ESP8266, which can then send it to the server. The easiest way to make the Arduino and the ESP8266 talk is by a Serial connection.

    If you follow this approach, you need to take in consideration that the ESP8266 operates at 3.3V and the Arduino UNO at 5V, so you need to do some voltage level conversion in order to not damage the ESP8266.

    The other option, which I would recommend, is to remove the Arduino UNO from the equation and use just the ESP8266. In this case, you use the ESP8266 to read the data from the RFID reader and then send it to the Django Server.

    It will depend on the RFID reader you are using, but most probably there will be a library for the ESP866 to work with it easily. Just be careful because if the RFID reader works at a voltage greater than 3.3V, you will also need some voltage level conversion.

    With this second option, you have less hardware (which is cheaper) and you don’t need to have the time delay of the Arduino sending the data to the ESP8266.

    1. Hi antepher 🙂
      I want send data from my arduino NANO to my website. I have serial connection between arudino and esp. I understand your comment that I must upload code from this article to esp. How can I send data from my arduino to esp.

      1. Hi Lukas 🙂 well, to do so you just need to check the basic Arduino serial communication functions.

        On the Arduino Nano side, you need to send the data, using the Serial.write or the Serial.print functions. Example here:
        https://www.arduino.cc/en/Serial/Write

        On the ESP8266 side, you need to read the data, with Serial.read. Example:
        https://www.arduino.cc/en/Serial/Read

        You can check bellow the available functions for the Arduino side. I don’t know if all of them are implemented in the ESP8266, but the main ones should be.
        https://www.arduino.cc/en/reference/serial

        Important stuff to take in consideration:
        – The ESP8266 works with 3.3V and the Arduino with 5V. You need to do some level conversion to not damage the ESP.
        – The baud rates need to be the same when you start the Serial connection (by calling Serial.Begin). I’ve had some troubles in the past using some baud rates on the ESP8266, such as 9600. A value that works for sure is 115200.

        Also, depending on how robust your application needs to be, you may want to implement a simple serial protocol, to guarantee that you don’t loose or misinterpret data exchanged by the devices. You can check an old post that explains how to do it (parts 1 and 2):
        https://techtutorialsx.com/2015/12/06/serial-communication-data-structuring-pt-2/

        Nevertheless, if you are starting now with serial communication, then I would recommend you to first learn the basics and then worry about this more advanced things.

        1. Thank you antepher for info. I used Serial.write() on my Arduino NANO to send information to my ESP, and Serial.readString() on my ESP to receive incoming data and it work perfect ! 🙂 I have another question. How can I send GET/POST from my website using php and receive data on my ESP ?

          1. I’m glad it worked fine 🙂

            The easiest way to do it is by setting a simple webserver on the ESP8266, to get incoming data. You can check bellow how to do it:
            https://techtutorialsx.com/2016/10/03/esp8266-setting-a-simple-http-webserver/

            In that example, I’m sending the HTTP request from a web browser. I your case, you should send it from the PHP application.

            One important thing to take in consideration is that when you connect to a WiFi network with the ESP, you will get a local IP, which is only valid in that network. So, to be able to send a HTTP request to the ESP, your PHP application should be running on the same network as the ESP8266.

            If not, you will have to configure your router to do portforwarding, which means it will need to be able to receive requests on its public IP and forward them to one of the internal IPs of the network, the one assigned to the ESP8266. Nevertheless, this is a more advanced topic and may subject your network to security issues.

            My recommendation is that you test it locally, with both the ESP8266 and the PHP application in the same network, unless you have prior experience with the procedure.

            The mentioned post is an introductory tutorial, the ESP supports many more functions related to receiving HTTP requests. Check some more tutorials:
            https://techtutorialsx.com/2016/11/19/esp8266-webserver-controlling-a-led-through-wifi/
            https://techtutorialsx.com/2016/10/30/esp8266-webserver-receiving-get-requests-from-python/
            https://techtutorialsx.com/2016/10/22/esp8266-webserver-getting-query-parameters/
            https://techtutorialsx.com/2016/10/15/esp8266-http-server-serving-html-javascript-and-css/
            https://techtutorialsx.com/2017/03/26/esp8266-webserver-accessing-the-body-of-a-http-request/

            Hope it helps.
            Best regards

            1. Good, I will test it locally, and if everything will be good i will have configure my router to do portforwarding, because is important for me to send data from my website which is in other network. I want two options on my one ESP. I want put data to my website and receive data from website to my ESP. I understand that when I want send data to ESP it must be set as client, and when I want receive data from website, I must configure webserwer on my ESP?

              1. Let us know if you succeed 🙂

                Yes, I think the easiest way is by doing what you mention. To receive data from the website you set some URLs on your ESP and receive and treat HTTP requests, so it is working as webserver. When you want to send data from the ESP, you use it as a client by sending HTTP requests.

                Naturally, you have other options. You could have socket communication, which operates at a lower level than HTTP, or use MQTT, which is another communication protocol. But for what you are trying to achieve (communicating with a PHP server) I think HTTP is the easiest way.

              2. I configured my router to do portforwarding, and it works perfect 🙂 I received data on ESP from my website. I used JQuery library in my website to send HTTP GET request to the IP address(ESP). But I have one problem. I used server.arg(i) on ESP to get value of the parametr, and this return value in string. I must send to Arduino NANO value in int. So I did it:
                int value = server.arg(i).toInt();
                Serial.write(value);
                On Arduino NANO i used Serial.read() but it didn’t work correct. I got different value. So my question is how can I send value int from ESP, and receive in Arduino NANO?

              3. Awesome news, I’m happy it all worked fine 🙂

                That function should work as long as the string is a number. Maybe the string also has letters or something? You can try to print the output of server.arg(i) and check if it is only a number or if it has some more content.

                If it has characters or something other than only the number you are trying to convert, you can use the Substring function to get the part you want:
                https://www.arduino.cc/en/Tutorial/StringSubstring

                Hope it helps

          2. Man I need to do pretty much the same thing. Could you share your code to give me some light? Do I need to program the Arduino separately from the esp? Thanks!

  4. In the example of this post, we are using the ESP8266 as a client and not as a server, so you can send data to a Django server (or any other application that can receive HTTP requests). It should work fine with the REST webservices. Just check what type of content the Django server is expecting to receive (it can be, for example, JSON) and specify it in the addHeader function.
    You can’t do this kind of POST request using only an Arduino UNO board, since it can’t connect to the Internet. So, you will always need to use the ESP8266 to send the actual HTTP POST to the server.
    Nevertheless, you can use the Arduino board to read the data from the RFID reader, send that data to the ESP8266, which can then send it to the server. The easiest way to make the Arduino and the ESP8266 talk is by a Serial connection.
    If you follow this approach, you need to take in consideration that the ESP8266 operates at 3.3V and the Arduino UNO at 5V, so you need to do some voltage level conversion in order to not damage the ESP8266.
    The other option, which I would recommend, is to remove the Arduino UNO from the equation and use just the ESP8266. In this case, you use the ESP8266 to read the data from the RFID reader and then send it to the Django Server.
    It will depend on the RFID reader you are using, but most probably there will be a library for the ESP866 to work with it easily. Just be careful because if the RFID reader works at a voltage greater than 3.3V, you will also need some voltage level conversion.
    With this second option, you have less hardware (which is cheaper) and you don’t need to have the time delay of the Arduino sending the data to the ESP8266.

    1. Hi antepher 🙂
      I want send data from my arduino NANO to my website. I have serial connection between arudino and esp. I understand your comment that I must upload code from this article to esp. How can I send data from my arduino to esp.

      1. Hi Lukas 🙂 well, to do so you just need to check the basic Arduino serial communication functions.
        On the Arduino Nano side, you need to send the data, using the Serial.write or the Serial.print functions. Example here:
        https://www.arduino.cc/en/Serial/Write
        On the ESP8266 side, you need to read the data, with Serial.read. Example:
        https://www.arduino.cc/en/Serial/Read
        You can check bellow the available functions for the Arduino side. I don’t know if all of them are implemented in the ESP8266, but the main ones should be.
        https://www.arduino.cc/en/reference/serial
        Important stuff to take in consideration:
        – The ESP8266 works with 3.3V and the Arduino with 5V. You need to do some level conversion to not damage the ESP.
        – The baud rates need to be the same when you start the Serial connection (by calling Serial.Begin). I’ve had some troubles in the past using some baud rates on the ESP8266, such as 9600. A value that works for sure is 115200.
        Also, depending on how robust your application needs to be, you may want to implement a simple serial protocol, to guarantee that you don’t loose or misinterpret data exchanged by the devices. You can check an old post that explains how to do it (parts 1 and 2):
        https://techtutorialsx.com/2015/12/06/serial-communication-data-structuring-pt-2/
        Nevertheless, if you are starting now with serial communication, then I would recommend you to first learn the basics and then worry about this more advanced things.

        1. Thank you antepher for info. I used Serial.write() on my Arduino NANO to send information to my ESP, and Serial.readString() on my ESP to receive incoming data and it work perfect ! 🙂 I have another question. How can I send GET/POST from my website using php and receive data on my ESP ?

          1. I’m glad it worked fine 🙂
            The easiest way to do it is by setting a simple webserver on the ESP8266, to get incoming data. You can check bellow how to do it:
            https://techtutorialsx.com/2016/10/03/esp8266-setting-a-simple-http-webserver/
            In that example, I’m sending the HTTP request from a web browser. I your case, you should send it from the PHP application.
            One important thing to take in consideration is that when you connect to a WiFi network with the ESP, you will get a local IP, which is only valid in that network. So, to be able to send a HTTP request to the ESP, your PHP application should be running on the same network as the ESP8266.
            If not, you will have to configure your router to do portforwarding, which means it will need to be able to receive requests on its public IP and forward them to one of the internal IPs of the network, the one assigned to the ESP8266. Nevertheless, this is a more advanced topic and may subject your network to security issues.
            My recommendation is that you test it locally, with both the ESP8266 and the PHP application in the same network, unless you have prior experience with the procedure.
            The mentioned post is an introductory tutorial, the ESP supports many more functions related to receiving HTTP requests. Check some more tutorials:
            https://techtutorialsx.com/2016/11/19/esp8266-webserver-controlling-a-led-through-wifi/
            https://techtutorialsx.com/2016/10/30/esp8266-webserver-receiving-get-requests-from-python/
            https://techtutorialsx.com/2016/10/22/esp8266-webserver-getting-query-parameters/
            https://techtutorialsx.com/2016/10/15/esp8266-http-server-serving-html-javascript-and-css/
            https://techtutorialsx.com/2017/03/26/esp8266-webserver-accessing-the-body-of-a-http-request/
            Hope it helps.
            Best regards

            1. Good, I will test it locally, and if everything will be good i will have configure my router to do portforwarding, because is important for me to send data from my website which is in other network. I want two options on my one ESP. I want put data to my website and receive data from website to my ESP. I understand that when I want send data to ESP it must be set as client, and when I want receive data from website, I must configure webserwer on my ESP?

              1. Let us know if you succeed 🙂
                Yes, I think the easiest way is by doing what you mention. To receive data from the website you set some URLs on your ESP and receive and treat HTTP requests, so it is working as webserver. When you want to send data from the ESP, you use it as a client by sending HTTP requests.
                Naturally, you have other options. You could have socket communication, which operates at a lower level than HTTP, or use MQTT, which is another communication protocol. But for what you are trying to achieve (communicating with a PHP server) I think HTTP is the easiest way.

              2. I configured my router to do portforwarding, and it works perfect 🙂 I received data on ESP from my website. I used JQuery library in my website to send HTTP GET request to the IP address(ESP). But I have one problem. I used server.arg(i) on ESP to get value of the parametr, and this return value in string. I must send to Arduino NANO value in int. So I did it:
                int value = server.arg(i).toInt();
                Serial.write(value);
                On Arduino NANO i used Serial.read() but it didn’t work correct. I got different value. So my question is how can I send value int from ESP, and receive in Arduino NANO?

              3. Awesome news, I’m happy it all worked fine 🙂
                That function should work as long as the string is a number. Maybe the string also has letters or something? You can try to print the output of server.arg(i) and check if it is only a number or if it has some more content.
                If it has characters or something other than only the number you are trying to convert, you can use the Substring function to get the part you want:
                https://www.arduino.cc/en/Tutorial/StringSubstring
                Hope it helps

          2. Man I need to do pretty much the same thing. Could you share your code to give me some light? Do I need to program the Arduino separately from the esp? Thanks!

  5. Thank your for you detailed explanation!!

    Probably, my problem is the header of the request. All other things (voltages, serial connections, RFID reader, …) are under control 🙂

    I didn’t know that ESP8266 could read from RFID sensors, but I can do taht in this project, because it need to be improved with 2 simultaneous readers and a SD card storage, so the best option will be to centralize everything in the Arduino CPU.

    Thanks again for your response, I will take a look to the headers and check all the code again.

  6. Thank your for you detailed explanation!!
    Probably, my problem is the header of the request. All other things (voltages, serial connections, RFID reader, …) are under control 🙂
    I didn’t know that ESP8266 could read from RFID sensors, but I can do taht in this project, because it need to be improved with 2 simultaneous readers and a SD card storage, so the best option will be to centralize everything in the Arduino CPU.
    Thanks again for your response, I will take a look to the headers and check all the code again.

  7. I keep getting a “-1” returned for httpCode

    Here is my code

    void loop() {
    //
    if(WiFi.status()== WL_CONNECTED){ //Check WiFi connection status
    HTTPClient http; //Declare object of class HTTPClient

    http.begin("http://192.168.43.162:80/iot/post.php?"); //Specify request destination
    Serial.println("Connected to HTTPClient");
    http.addHeader("Content-Type", "text/plain"); //Specify content-type header
    int httpCode=http.POST("id=13&value=46.79"); //Send the request
    String payload = http.getString(); //Get the response payload
    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(30000); //Send a request every 30 seconds
    }

    1. Hi,

      My recommendation is to try to test things separately first if you haven’t done it yet

      You can try to reach the destination address http://192.168.43.162:80/iot/post.php? from another computer in your local network. The firewall of the machine where the webserver is running may be refusing the connection, thus resulting in an error in the ESP8266.

      There may also be some problem with the logic on the webserver that is triggering an internal error and no response is returned to the ESP.

      Just another note, it seems that you are trying to send query parameters id=13&value=46.79 in the body of the post request. Query parameters are passed in the URL, so if you just want to send that information, you should send a GET request in the following format:
      http://192.168.43.162:80/iot/post.php?id=13&value=46.79

      You can check in more detail how to send GET requests from the ESP in another post:
      https://techtutorialsx.wordpress.com/2016/07/17/esp8266-http-get-requests/

      It may also be a problem from the ESP. Were you able to use the code of this tutorial successfully to reach the example website? http://jsonplaceholder.typicode.com/users

      1. Hi! The suggestion remains the same, test your code separately to try to isolate the error.

        First of all, are you trying to reach a HTTPS (with an S at the end) website? If so, it is not possible with this code.

        Can you make a request to your destination website using a tool such as postman?

        Best regards,
        Nuno Santos

    2. if somebody still needs the answer to data question,
      you have to set

      http.addHeader(“Content-Type”, “application/x-www-form-urlencoded”);

      instead of plain/text

      http.addHeader(“Content-Type”, “text/plain”);

  8. I keep getting a “-1” returned for httpCode
    Here is my code

    void loop() {
    //
    if(WiFi.status()== WL_CONNECTED){ //Check WiFi connection status
    HTTPClient http; //Declare object of class HTTPClient
    http.begin("http://192.168.43.162:80/iot/post.php?"); //Specify request destination
    Serial.println("Connected to HTTPClient");
    http.addHeader("Content-Type", "text/plain"); //Specify content-type header
    int httpCode=http.POST("id=13&value=46.79"); //Send the request
    String payload = http.getString(); //Get the response payload
    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(30000); //Send a request every 30 seconds
    }

    1. Hi,
      My recommendation is to try to test things separately first if you haven’t done it yet
      You can try to reach the destination address http://192.168.43.162:80/iot/post.php? from another computer in your local network. The firewall of the machine where the webserver is running may be refusing the connection, thus resulting in an error in the ESP8266.
      There may also be some problem with the logic on the webserver that is triggering an internal error and no response is returned to the ESP.
      Just another note, it seems that you are trying to send query parameters id=13&value=46.79 in the body of the post request. Query parameters are passed in the URL, so if you just want to send that information, you should send a GET request in the following format:
      http://192.168.43.162:80/iot/post.php?id=13&value=46.79
      You can check in more detail how to send GET requests from the ESP in another post:
      https://techtutorialsx.wordpress.com/2016/07/17/esp8266-http-get-requests/
      It may also be a problem from the ESP. Were you able to use the code of this tutorial successfully to reach the example website? http://jsonplaceholder.typicode.com/users

      1. Hi! The suggestion remains the same, test your code separately to try to isolate the error.
        First of all, are you trying to reach a HTTPS (with an S at the end) website? If so, it is not possible with this code.
        Can you make a request to your destination website using a tool such as postman?
        Best regards,
        Nuno Santos

    2. if somebody still needs the answer to data question,
      you have to set
      http.addHeader(“Content-Type”, “application/x-www-form-urlencoded”);
      instead of plain/text
      http.addHeader(“Content-Type”, “text/plain”);

Leave a Reply to antepherCancel reply

Discover more from techtutorialsx

Subscribe now to keep reading and get access to the full archive.

Continue reading