Error sending POST, code: -1

No funciona POST en Nodecu ESP8622, en el siguiente código:
La respuesta de response_code siempre es -1
Alguien puede ayudar para saber a qué se debe?


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

#ifndef STASSID
#define STASSID "your-ssid"
#define STAPSK "your-password"
#endif


const char* ssid = "VI";
const char* password = "A33";

// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80);


// Define Static IP Setting

IPAddress ip(100,100,1,215);
IPAddress gateway(100,100,1,1);
IPAddress subnet(255,255,0,0);


//Variables used in the code
unsigned int Actual_Millis, Previous_Millis;
int refresh_time = 10000;               //Refresh rate of connection to website (recommended more than 1s)

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

  // prepare LED
  pinMode(2, OUTPUT);
  digitalWrite(2, 0);

  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print(F("Connecting to "));
  Serial.println(ssid);

  WiFi.begin(ssid, password);
  WiFi.mode(WIFI_STA);
  WiFi.config(ip,gateway,subnet);

  while (WiFi.status() != WL_CONNECTED) {
    delay(10000);
    Serial.print(F("."));
  }
  Serial.println();
  Serial.println(F("WiFi connected"));

  // Start the server
  server.begin();
  Serial.println(F("Server started"));

  // Print the IP address
  Serial.println(WiFi.localIP());
}



void loop() {  
  //We make the refresh loop using millis() so we don't have to sue delay();
  Actual_Millis = millis();
  if(Actual_Millis - Previous_Millis > refresh_time){
    Previous_Millis = Actual_Millis;  
    if(WiFi.status()== WL_CONNECTED){                   //Check WiFi connection status 

  Serial.println();
  Serial.println(F("WiFi connected"));

WiFiClient client;
HTTPClient http; 
String dato = "hola" ;
            
      http.begin(client,"http://www.httpbin.org/get");                 //Indicate the destination webpage 
      http.addHeader("Content-Type", "application/x-www-form-urlencoded");
           
      Serial.print("Dato a enviar : ");
      Serial.println(dato);
      int response_code = http.POST(dato);                                //Send the POST. This will giveg us a response code
      String msgsrv = http.getString();
   
      Serial.print("Respuesta POST : ");   
      Serial.println(response_code);
      Serial.print("Respuesta del SRV : ");
      Serial.println(msgsrv);
      
      //If the code is higher than 0, it means we received a response
      if(response_code > 0){
        Serial.println("HTTP code " + String(response_code));                     //Print return code
  
        if(response_code == 200){                                                 //If code is 200, we received a good response and we can read the echo data
          String response_body = http.getString();                                //Save the data comming from the website
          Serial.print("Server reply: ");                                         //Print data to the monitor for debug
          Serial.println(response_body);
     
          }  
        }//End of response_code = 200
      }//END of response_code > 0
      
      else{
       Serial.print("Error sending POST, code: ");
       Serial.println(response_code);
      }
      http.end();                                                                 //End the connection
    }//END of WIFI connected
    else{
      Serial.println("WIFI connection error");
    }
  }
}




This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.