httparduinoesp8266nodemcuarduino-esp8266

Why can't ESP8266 HTTP client receive data?


When I type the serverName into the browser, it returns a json object. But here, it Serial.print(payload) returns -1. How can I fix this?

void loop() {
   if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status
     
      HTTPClient http;  //Declare an object of class HTTPClient
     
      http.begin(serverName);  //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(10000);    //Send a request every 30 seconds
}

Solution

  • Use HTTP instead of HTTPS.

    If you want to use HTTPS, you will have to specify the server's certificate's SHA1 fingerprint. How to do that is easily googled.

    Some libraries will allow something like client->setInsecure();, but I wouldn't go there because, well, it's insecure.