ESP8266: HTTP GET Requests

Introduction

The objective of this post is to explain how to perform HTTP GET Requests using the ESP8266. If you prefer a video explanation, you can check my YouTube channel below:

The tests from this tutorial were performed using a NodeMCU board, a very cheap and easy to use ESP8266 board.

The setup

First, we need do include some libraries. Naturally, we need the ESP8266WiFi library, which provides to us the methods to connect to a WiFi network.

Then, we need the ESP8266HTTPClient library, which provides the methods to send HTTP requests. The header file for the ESP8266HTTPClient library can be seen here.

#include <esp8266wifi.h>
#include <esp8266httpclient.h>

To be able to connect to the WiFi network, we will need its credentials. So, we will define two global variables containing the network name (SSID) and password. Note that you should replace the placeholders in the code snippet below by the credentials of your network.

const char* ssid = "yourNetworkName";
const char* password = "yourNetworkPassword";

On the setup function, we will connect to the WiFi Network. This is done with a call to the begin method on the WiFi extern variable and providing the previously defined network credentials. More details about how to connect to a WiFi network using the ESP8266 are explained in this previous post.

We will also start a Serial connection to print the results of our program.

The complete Arduino setup function code can be seen below.

void setup () {

  Serial.begin(115200);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {

    delay(1000);
    Serial.print("Connecting..");

  }

}

The main code

The code for the request will be specified in the Arduino main loop function. First, we declare an object of class HTTPClient, which we will simply call http. This class provides the methods to create and send the HTTP request.

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 GET request. The destination website specified here implements a dummy REST API for testing and prototyping.

Note that the particular endpoint we are reaching will return a JSON payload as response, simulating a possible “user” data structure. You can directly access the URL and check the result in a web browser. Later, when testing the ESP8266 code, we should receive the same response.

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

Then, we send the request by calling the GET method on the http object. This method will return the status of the operation, which is important to store for error handling. If the value is greater than 0, then it is a standard HTTP code. If the value is lesser than 0, then it is a client error, related with the connection. All available error codes for this method are listed here.

int httpCode = http.GET();

So, if the code is greater than 0, we can get the response payload by calling the getString method on the http object. The response is returned as a String, which we can directly print to the serial port.

String payload = http.getString();
Serial.println(payload);

Finally, we will call the end method. This is very important to close the TCP connection and thus free the resources.

http.end();

The final complete code is shown bellow. Note that we have added a small delay of 30 seconds between each iteration of the Arduino main loop, to avoid constantly polling the server.

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

const char* ssid = "yourNetworkName";
const char* password = "yourNetworkPassword";

void setup () {

  Serial.begin(115200);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {

    delay(1000);
    Serial.print("Connecting..");

  }

}

void loop() {

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

    HTTPClient http;  //Declare an object of class HTTPClient

    http.begin("http://jsonplaceholder.typicode.com/users/1");  //Specify request destination
    int httpCode = http.GET();                                  //Send the request

    if (httpCode > 0) { //Check the returning code

      String payload = http.getString();   //Get the request response payload
      Serial.println(payload);             //Print the response payload

    }

    http.end();   //Close connection

  }

  delay(30000);    //Send a request every 30 seconds
}

Testing the code

To test the code, simply compile it and upload it to your device using the Arduino IDE.

After the upload procedure finishes, open the IDE serial monitor and wait for the WiFi connection to be established. Once this procedure is done, the ESP8266 should start sending the HTTP GET requests to the server periodically.

The expected output of the program is shown in figure 1. The response printed to the serial monitor corresponds to the JSON payload returned by the server. The ESP8266 should keep doing a HTTP GET request every 30 seconds.

Output of the ESP8266 GET Request program, on the Arduino IDE serial monitor
Figure 1 – Output of the GET Request.

Finally, it’s important to take in consideration that the microcontroller has a limited amount of resources and thus it is not able to handle very large results. So, it is not expected that it will be used to make and process requests to sites that return a lot of information, as a typical browser would do.

Related Posts

Technical details

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

314 thoughts on “ESP8266: HTTP GET Requests”

  1. antepher thanks for the tutorial! I’m looking for a way to receive (and process) data from the server response line – not the payload. Especial I’m intretsted in the time and date stamp from the server respond.
    Thanks

    1. Hi!

      You’re welcome and thank you very much for the feedback 🙂

      What do you mean by the server response line? Something like the response headers?

      Best regards,
      Nuno Santos

      1. yes exacly, The line that contains time and date info. With this info I like to set the system time An example ho to do this is welcomed 😉
        Thank you!

          1. Hi Nuno,
            thats exacly what I was looking for. Thank you so much!
            If you point me to the documentation of ESP8266HTTPClient documentation next time I’ll first look at before asking stupid questions
            ——————–
            Connecting..
            Connected to WiFi Network
            Sat, 09 Jun 2018 06:31:08 GMT
            cloudflare
            Sat, 09 Jun 2018 06:31:08 GMT
            cloudflare
            ——————–
            Thanks
            Jochen

            1. Hi!

              Awesome, you’re welcome 🙂

              It was not a stupid question at all. In fact I hadn’t yet played with the headers of the response, so it was an interesting exercise to figure it out and I think it will be useful for more people 🙂

              I’m not sure if there is some “official” documentation about the HTTP Client. The only place that has some structured documentation about the libraries that I’m aware of is the following:
              https://arduino-esp8266.readthedocs.io/en/latest/index.html

              Nonetheless, I did not find anything about the HTTPClient :/

              What I usually do to check the API of some class if actually going to the header file and check the available methods. Sometimes they are straightforward to use, but others it requires some digging on wikis, the github issues and the implementation files.

              If you want to take a look, here is the header file of the HTTP Client:
              https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h

              I wish you good luck in your project 🙂

              Best regards,
              Nuno Santos

          2. Hi Nuno,
            I’m facing the problen to send a request to a server that insists of pfs connection set up. As this behavior will become more prominent it’s not just me beeing confronted I believe.
            Thank you

            1. Hi!

              When you mean pfs, are you refering to perfect forward secrecy?

              If so, at the time I was more actively working with the ESP8266, HTTPS functionalities were really limited and I’m not sure if perfect forward secrecy was ever supported.

              To be honest, I’m not sure if things have evolved much since then, but I would recommend you to take a look at the ESP32 for more robust security functionalities.

              The ESP32 is far more powerful and it is able to handle HTTPS very well. But even with the ESP32, I’m not sure if it can support perfect forward secrecy, so you would need to do some investigation or ask around the forums and git hub pages.

              But an easy way to test is to make an HTTPS request to a server that uses PFS and check if everything works.

              If I come across some information or examples regarding pfs support, I will make sure to share 🙂

              Best regards,
              Nuno Santos

              1. Hi Nuno,
                I’ve send this request to Espresif: “As perfect forward secrecy, short pfs, becomes more prominent/importand I wonder if there is a pfs implementation for the famous ESP32”
                Looking forward for an answer …

              2. Hi!

                Please let us know if they give back some answer, I’m also curious about what their answer will be since this is indeed something very useful to have available.

                Hopefully there is support 🙂

                Best regards,
                Nuno Santos

              3. It’s a shame, no answer yet – no positve nor negativ – jusst silenc :-((
                As my english isn’t proper and my knowledge of internet protocols is poor maybe somone can step into this request and forward it to the right community. As I said befor it’s an upcomming demand.
                Thank you for your great support. All the best
                Mhz

              4. Hi,

                I’m sad to hear that you haven’t yet got an answer from Espressif, it would be interesting to know their answer on this.

                Nonetheless, I think it is probable that you get some answers in forums or on the github pages of ESP8266 Projects. Here is a list of interesting places where you can ask around:
                – ESP8266 forum: https://www.esp8266.com/
                – ESP8266 Arduino core github page: https://github.com/esp8266/Arduino/issues

                Nonetheless, I would also ask around the ESP32 forum, since it is more probable that this support for pfs gets to the ESP32:
                – Forum: https://esp32.com/
                – Arduino core Github page: https://github.com/espressif/arduino-esp32
                – IDF Github Page: https://github.com/espressif/esp-idf

                Don’t worry about your English, I’m not a native speaker myself and never worry about that when asking about stuff in technical forums 🙂

                You’re welcome, and thanks for the feedback 🙂

                Best regards,
                Nuno Santos

  2. antepher thanks for the tutorial! I’m looking for a way to receive (and process) data from the server response line – not the payload. Especial I’m intretsted in the time and date stamp from the server respond.
    Thanks

    1. Hi!
      You’re welcome and thank you very much for the feedback 🙂
      What do you mean by the server response line? Something like the response headers?
      Best regards,
      Nuno Santos

      1. yes exacly, The line that contains time and date info. With this info I like to set the system time An example ho to do this is welcomed 😉
        Thank you!

          1. Hi Nuno,
            thats exacly what I was looking for. Thank you so much!
            If you point me to the documentation of ESP8266HTTPClient documentation next time I’ll first look at before asking stupid questions
            ——————–
            Connecting..
            Connected to WiFi Network
            Sat, 09 Jun 2018 06:31:08 GMT
            cloudflare
            Sat, 09 Jun 2018 06:31:08 GMT
            cloudflare
            ——————–
            Thanks
            Jochen

            1. Hi!
              Awesome, you’re welcome 🙂
              It was not a stupid question at all. In fact I hadn’t yet played with the headers of the response, so it was an interesting exercise to figure it out and I think it will be useful for more people 🙂
              I’m not sure if there is some “official” documentation about the HTTP Client. The only place that has some structured documentation about the libraries that I’m aware of is the following:
              https://arduino-esp8266.readthedocs.io/en/latest/index.html
              Nonetheless, I did not find anything about the HTTPClient :/
              What I usually do to check the API of some class if actually going to the header file and check the available methods. Sometimes they are straightforward to use, but others it requires some digging on wikis, the github issues and the implementation files.
              If you want to take a look, here is the header file of the HTTP Client:
              https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h
              I wish you good luck in your project 🙂
              Best regards,
              Nuno Santos

          2. Hi Nuno,
            I’m facing the problen to send a request to a server that insists of pfs connection set up. As this behavior will become more prominent it’s not just me beeing confronted I believe.
            Thank you

            1. Hi!
              When you mean pfs, are you refering to perfect forward secrecy?
              If so, at the time I was more actively working with the ESP8266, HTTPS functionalities were really limited and I’m not sure if perfect forward secrecy was ever supported.
              To be honest, I’m not sure if things have evolved much since then, but I would recommend you to take a look at the ESP32 for more robust security functionalities.
              The ESP32 is far more powerful and it is able to handle HTTPS very well. But even with the ESP32, I’m not sure if it can support perfect forward secrecy, so you would need to do some investigation or ask around the forums and git hub pages.
              But an easy way to test is to make an HTTPS request to a server that uses PFS and check if everything works.
              If I come across some information or examples regarding pfs support, I will make sure to share 🙂
              Best regards,
              Nuno Santos

              1. Hi Nuno,
                I’ve send this request to Espresif: “As perfect forward secrecy, short pfs, becomes more prominent/importand I wonder if there is a pfs implementation for the famous ESP32”
                Looking forward for an answer …

              2. Hi!
                Please let us know if they give back some answer, I’m also curious about what their answer will be since this is indeed something very useful to have available.
                Hopefully there is support 🙂
                Best regards,
                Nuno Santos

              3. It’s a shame, no answer yet – no positve nor negativ – jusst silenc :-((
                As my english isn’t proper and my knowledge of internet protocols is poor maybe somone can step into this request and forward it to the right community. As I said befor it’s an upcomming demand.
                Thank you for your great support. All the best
                Mhz

              4. Hi,
                I’m sad to hear that you haven’t yet got an answer from Espressif, it would be interesting to know their answer on this.
                Nonetheless, I think it is probable that you get some answers in forums or on the github pages of ESP8266 Projects. Here is a list of interesting places where you can ask around:
                – ESP8266 forum: https://www.esp8266.com/
                – ESP8266 Arduino core github page: https://github.com/esp8266/Arduino/issues
                Nonetheless, I would also ask around the ESP32 forum, since it is more probable that this support for pfs gets to the ESP32:
                – Forum: https://esp32.com/
                – Arduino core Github page: https://github.com/espressif/arduino-esp32
                – IDF Github Page: https://github.com/espressif/esp-idf
                Don’t worry about your English, I’m not a native speaker myself and never worry about that when asking about stuff in technical forums 🙂
                You’re welcome, and thanks for the feedback 🙂
                Best regards,
                Nuno Santos

  3. Pingback: ESP8266 Arduino: Getting HTTP Response headers | techtutorialsx

  4. Pingback: ESP8266 Arduino: Getting HTTP Response headers | techtutorialsx

  5. hello sir ,
    iam trying to send the url to the mysql database. but using below sketch not sending ,where i did mistake please help.

    #include
    #include

    void setup() {

    Serial.begin(115200); //Serial connection
    WiFi.begin(“iota_telecom”, “iota160616”); //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://10.132.16.241:8080/HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45”); //Specify request destination
    http.addHeader(“Content-Type”, “text/plain”); //Specify content-type header

    int httpCode = http.POST(“http://10.132.16.241:8080/HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45”); //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

    }

    serial monitor:

    Waiting for connection
    state: 2 -> 0 (2)
    reconnect
    scandone
    state: 0 -> 2 (b0)
    Waiting for connection
    state: 2 -> 3 (0)
    state: 3 -> 5 (10)
    add 0
    aid 2
    cnt
    Waiting for connection
    Waiting for connection
    Waiting for connection

    connected with iota_telecom, channel 6
    dhcp client start…
    Waiting for connection
    ip:10.132.39.81,mask:255.255.0.0,gw:10.132.0.1
    Waiting for connection
    [HTTP-Client][begin] url: http://10.132.16.241:8080/HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45
    [HTTP-Client][begin] host: 10.132.16.241 port: 8080 url: /HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45
    [HTTP-Client] connected to 10.132.16.241:8080
    [HTTP-Client] sending request header
    —–
    POST /HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45 HTTP/1.1
    Host: 10.132.16.241:8080
    User-Agent: ESP8266HTTPClient
    Connection: close
    Accept-Encoding: identity;q=1,chunked;q=0.1,*;q=0
    Content-Type: text/plain
    Content-Length: 136

    —–
    [HTTP-Client][returnError] error(-11): read Timeout
    [HTTP-Client][returnError] tcp stop
    [HTTP-Client][returnError] error(-4): not connected
    -11

    [HTTP-Client][end] tcp is closed
    [HTTP-Client][begin] url: http://10.132.16.241:8080/HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45
    [HTTP-Client][begin] host: 10.132.16.241 port: 8080 url: /HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45
    [HTTP-Client] failed connect to 10.132.16.241:8080
    [HTTP-Client][returnError] error(-1): connection refused
    [HTTP-Client][returnError] error(-4): not connected
    -1

    [HTTP-Client][end] tcp is closed
    [HTTP-Client][begin] url: http://10.132.16.241:8080/HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45
    [HTTP-Client][begin] host: 10.132.16.241 port: 8080 url: /HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45
    [HTTP-Client] failed connect to 10.132.16.241:8080
    [HTTP-Client][returnError] error(-1): connection refused
    [HTTP-Client][returnError] error(-4): not connected
    -1

    I’m using nodeMCU(esp_12E)

  6. hello sir ,
    iam trying to send the url to the mysql database. but using below sketch not sending ,where i did mistake please help.
    #include
    #include
    void setup() {
    Serial.begin(115200); //Serial connection
    WiFi.begin(“iota_telecom”, “iota160616”); //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://10.132.16.241:8080/HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45”); //Specify request destination
    http.addHeader(“Content-Type”, “text/plain”); //Specify content-type header
    int httpCode = http.POST(“http://10.132.16.241:8080/HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45”); //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
    }
    serial monitor:
    Waiting for connection
    state: 2 -> 0 (2)
    reconnect
    scandone
    state: 0 -> 2 (b0)
    Waiting for connection
    state: 2 -> 3 (0)
    state: 3 -> 5 (10)
    add 0
    aid 2
    cnt
    Waiting for connection
    Waiting for connection
    Waiting for connection
    connected with iota_telecom, channel 6
    dhcp client start…
    Waiting for connection
    ip:10.132.39.81,mask:255.255.0.0,gw:10.132.0.1
    Waiting for connection
    [HTTP-Client][begin] url: http://10.132.16.241:8080/HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45
    [HTTP-Client][begin] host: 10.132.16.241 port: 8080 url: /HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45
    [HTTP-Client] connected to 10.132.16.241:8080
    [HTTP-Client] sending request header
    —–
    POST /HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45 HTTP/1.1
    Host: 10.132.16.241:8080
    User-Agent: ESP8266HTTPClient
    Connection: close
    Accept-Encoding: identity;q=1,chunked;q=0.1,*;q=0
    Content-Type: text/plain
    Content-Length: 136
    —–
    [HTTP-Client][returnError] error(-11): read Timeout
    [HTTP-Client][returnError] tcp stop
    [HTTP-Client][returnError] error(-4): not connected
    -11
    [HTTP-Client][end] tcp is closed
    [HTTP-Client][begin] url: http://10.132.16.241:8080/HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45
    [HTTP-Client][begin] host: 10.132.16.241 port: 8080 url: /HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45
    [HTTP-Client] failed connect to 10.132.16.241:8080
    [HTTP-Client][returnError] error(-1): connection refused
    [HTTP-Client][returnError] error(-4): not connected
    -1
    [HTTP-Client][end] tcp is closed
    [HTTP-Client][begin] url: http://10.132.16.241:8080/HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45
    [HTTP-Client][begin] host: 10.132.16.241 port: 8080 url: /HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45
    [HTTP-Client] failed connect to 10.132.16.241:8080
    [HTTP-Client][returnError] error(-1): connection refused
    [HTTP-Client][returnError] error(-4): not connected
    -1
    I’m using nodeMCU(esp_12E)

  7. sorry sir ,my first post was not added libraries ,please help me where i did the mistake.

    #include
    #include

    void setup() {

    Serial.begin(115200); //Serial connection
    WiFi.begin(“iota_telecom”, “iota160616”); //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://10.132.16.241:8080/HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45”); //Specify request destination
    http.addHeader(“Content-Type”, “text/plain”); //Specify content-type header

    int httpCode = http.POST(“http://10.132.16.241:8080/HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45”); //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

    }

    serial monitor:

    Waiting for connection
    state: 2 -> 0 (2)
    reconnect
    scandone
    state: 0 -> 2 (b0)
    Waiting for connection
    state: 2 -> 3 (0)
    state: 3 -> 5 (10)
    add 0
    aid 2
    cnt
    Waiting for connection
    Waiting for connection
    Waiting for connection

    connected with iota_telecom, channel 6
    dhcp client start…
    Waiting for connection
    ip:10.132.39.81,mask:255.255.0.0,gw:10.132.0.1
    Waiting for connection
    [HTTP-Client][begin] url: http://10.132.16.241:8080/HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45
    [HTTP-Client][begin] host: 10.132.16.241 port: 8080 url: /HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45
    [HTTP-Client] connected to 10.132.16.241:8080
    [HTTP-Client] sending request header
    —–
    POST /HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45 HTTP/1.1
    Host: 10.132.16.241:8080
    User-Agent: ESP8266HTTPClient
    Connection: close
    Accept-Encoding: identity;q=1,chunked;q=0.1,*;q=0
    Content-Type: text/plain
    Content-Length: 136

    —–
    [HTTP-Client][returnError] error(-11): read Timeout
    [HTTP-Client][returnError] tcp stop
    [HTTP-Client][returnError] error(-4): not connected
    -11

    [HTTP-Client][end] tcp is closed
    [HTTP-Client][begin] url: http://10.132.16.241:8080/HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45
    [HTTP-Client][begin] host: 10.132.16.241 port: 8080 url: /HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45
    [HTTP-Client] failed connect to 10.132.16.241:8080
    [HTTP-Client][returnError] error(-1): connection refused
    [HTTP-Client][returnError] error(-4): not connected
    -1

    [HTTP-Client][end] tcp is closed
    [HTTP-Client][begin] url: http://10.132.16.241:8080/HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45
    [HTTP-Client][begin] host: 10.132.16.241 port: 8080 url: /HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45
    [HTTP-Client] failed connect to 10.132.16.241:8080
    [HTTP-Client][returnError] error(-1): connection refused
    [HTTP-Client][returnError] error(-4): not connected
    -1
    thank you.

    1. Hi!

      Unfortunately I’m not aware how these connections to the mysql database work, so I cannot be of much assistance.

      Nonetheless, I can recommend a couple of things to try to help you debugging:
      – Can you connect to the database and send the request using another tool, such as Postman? Postman is very easy to use and helps testing HTTP requests. So if it is also not working with postman, then it is not an ESP related problem.
      – Are you sure the database accepts plain HTTP requests? Or does it work over HTTPS (note the s, from secure)?
      – Why are you sending the URL as body of your HTTP request? You should just put the content you want to send to the server in the body of your POST request.

      Hope this helps.

      Best regards,
      Nuno Santos

  8. sorry sir ,my first post was not added libraries ,please help me where i did the mistake.
    #include
    #include
    void setup() {
    Serial.begin(115200); //Serial connection
    WiFi.begin(“iota_telecom”, “iota160616”); //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://10.132.16.241:8080/HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45”); //Specify request destination
    http.addHeader(“Content-Type”, “text/plain”); //Specify content-type header
    int httpCode = http.POST(“http://10.132.16.241:8080/HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45”); //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
    }
    serial monitor:
    Waiting for connection
    state: 2 -> 0 (2)
    reconnect
    scandone
    state: 0 -> 2 (b0)
    Waiting for connection
    state: 2 -> 3 (0)
    state: 3 -> 5 (10)
    add 0
    aid 2
    cnt
    Waiting for connection
    Waiting for connection
    Waiting for connection
    connected with iota_telecom, channel 6
    dhcp client start…
    Waiting for connection
    ip:10.132.39.81,mask:255.255.0.0,gw:10.132.0.1
    Waiting for connection
    [HTTP-Client][begin] url: http://10.132.16.241:8080/HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45
    [HTTP-Client][begin] host: 10.132.16.241 port: 8080 url: /HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45
    [HTTP-Client] connected to 10.132.16.241:8080
    [HTTP-Client] sending request header
    —–
    POST /HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45 HTTP/1.1
    Host: 10.132.16.241:8080
    User-Agent: ESP8266HTTPClient
    Connection: close
    Accept-Encoding: identity;q=1,chunked;q=0.1,*;q=0
    Content-Type: text/plain
    Content-Length: 136
    —–
    [HTTP-Client][returnError] error(-11): read Timeout
    [HTTP-Client][returnError] tcp stop
    [HTTP-Client][returnError] error(-4): not connected
    -11
    [HTTP-Client][end] tcp is closed
    [HTTP-Client][begin] url: http://10.132.16.241:8080/HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45
    [HTTP-Client][begin] host: 10.132.16.241 port: 8080 url: /HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45
    [HTTP-Client] failed connect to 10.132.16.241:8080
    [HTTP-Client][returnError] error(-1): connection refused
    [HTTP-Client][returnError] error(-4): not connected
    -1
    [HTTP-Client][end] tcp is closed
    [HTTP-Client][begin] url: http://10.132.16.241:8080/HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45
    [HTTP-Client][begin] host: 10.132.16.241 port: 8080 url: /HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45
    [HTTP-Client] failed connect to 10.132.16.241:8080
    [HTTP-Client][returnError] error(-1): connection refused
    [HTTP-Client][returnError] error(-4): not connected
    -1
    thank you.

    1. Hi!
      Unfortunately I’m not aware how these connections to the mysql database work, so I cannot be of much assistance.
      Nonetheless, I can recommend a couple of things to try to help you debugging:
      – Can you connect to the database and send the request using another tool, such as Postman? Postman is very easy to use and helps testing HTTP requests. So if it is also not working with postman, then it is not an ESP related problem.
      – Are you sure the database accepts plain HTTP requests? Or does it work over HTTPS (note the s, from secure)?
      – Why are you sending the URL as body of your HTTP request? You should just put the content you want to send to the server in the body of your POST request.
      Hope this helps.
      Best regards,
      Nuno Santos

  9. hello sir how are you. this post is very useful but i have some problems like
    what is meant by http.get()
    and what is meant by http.getstring()
    please resolve this issues

    1. Hi!

      Thanks for the feedback 🙂

      As explained in the tutorial, http.GET() is used to perform the actual request and http.getString() is used to get the response returned by the server.

      What is your concrete doubt about these methods?

      Best regards,
      Nuno Santos

  10. hello sir how are you. this post is very useful but i have some problems like
    what is meant by http.get()
    and what is meant by http.getstring()
    please resolve this issues

    1. Hi!
      Thanks for the feedback 🙂
      As explained in the tutorial, http.GET() is used to perform the actual request and http.getString() is used to get the response returned by the server.
      What is your concrete doubt about these methods?
      Best regards,
      Nuno Santos

Leave a Reply