Chat freely about anything...

User avatar
By jmmontan
#76152 Hi, I am new with the ESP8266 and I am working in some projects where I need to read the response from a web site, I achieve connect the esp8266 to the WiFi and if I use this host = "example.com", it works but if I use this one host: "example.com/configuration/execution"; it doesnt work I mean it doesnt achieve the connection to the host, Does someone know why I cant connect to that host? if it is not possible or if I am doing something wrong?

const char* hostinfo = "example.com/configuration/execution";

Serial.print("connecting to ");
Serial.println(host);

// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
User avatar
By martinayotte
#76237 WiFiClient.connect() funtion is only taking hostname, not full URL !
The rest of URL should be transmitted using proper HTML code sent via client.write().

If you don't wish to handle all this HTML code/header, don't use WiFiClient, but use HTTPClient instead ...
User avatar
By jmmontan
#76275
martinayotte wrote:WiFiClient.connect() funtion is only taking hostname, not full URL !
The rest of URL should be transmitted using proper HTML code sent via client.write().

If you don't wish to handle all this HTML code/header, don't use WiFiClient, but use HTTPClient instead ...


thanks, I already fixed it, I changed a little bit the code:

Code: Select all#include <ESP8266WiFi.h>

const char* ssid = "MEGACABLE-BA92";
const char* password = "Sp29fy33";

const char* host = "reduxled.sositi.com";
const char* path = "/configuracion/ejecucion";

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

  Serial.printf("Connecting to %s ", ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println(" connected");
}


void loop()
{
  Serial.println("GetConfigInfo");
  Serial.print("connecting to ");
  Serial.println(host);
 
  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, 80)) {
    Serial.println("connection failed");
    return;
  }
 
  String data = "mac=";
  data += "{\"mac\":\"";
  data += WiFi.macAddress();
  data += "\"}";

  Serial.println(WiFi.macAddress());
  Serial.println(data);

  client.println("POST /configuracion/ejecucion HTTP/1.1");
  client.println("Host: reduxled.sositi.com");
  client.println("Cache-Control: no-cache");
  client.println("Content-Type: application/x-www-form-urlencoded;");
  client.print("Content-Length: ");
  client.println(data.length());
  client.println();
  client.println(data);
 
  unsigned long timeout = millis();
  while (client.available() == 0) {
    if (millis() - timeout > 5000) {
      Serial.println(">>> Client Timeout !");
      client.stop();
      return;
    }
  }
 
  // Read all the lines of the reply from server and print them to Serial
  while(client.available()){
    if (client.find("{")){
      String line = client.readStringUntil('\r');
      Serial.println("ClIENT AVAILABLE");
      Serial.println(line);
      //GuardarCredenciales (line);
    }
  }
  Serial.println();
  Serial.println("closing connection");

  delay(5000);
}