ESP8266 HTTPClient issue

Good Afternoon

I am encountering an issue with a project that i am building, whereby i need the Controller to simply access a web address under a condition, say Pin 5 = HIGH, but cannot even get the basic connection to the webpage to work.

#include <ESP8266WiFi.h>

const char* ssid = "SSID";
const char* password = "PSWD";

const char* host = "SERVER IP";

//:7999/3/config/set?emulate_motion=on


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()
{
  WiFiClient client;

  Serial.printf("\n[Connecting to %s ... ", host);
  if (client.connect(host,7999))
  {
    Serial.println("connected]");

    client.println("PUT /3/config/set?emulate_motion=on");
delay(10000);
    client.println("PUT /3/config/set?emulate_motion=off");
delay(10000);
    Serial.println("[Response:]");
    while (client.connected() || client.available())
    {
      if (client.available())
      {
        String line = client.readStringUntil('\n');
        Serial.println(line);
      }
    }
    client.stop();
    Serial.println("\n[Disconnected]");
  }
  else
  {
    Serial.println("connection failed!]");
    client.stop();
  }
  delay(5000);
}

when using a web browser, all i need to do is navigate to http://IPADDRESS:7999/3/config/set?emulate_motion=on to achieve my goal, but cant get this to work with the ESP8266. I have tried GET, POST, PUT but am fairly certain that it wouldn’t work with any of those options, as the usage is incorrect. Does anybody have asuggestion that i can use to get the ESP8266 to simply load a webpage like a browser would? The target behaviour is observed in Linux “wget” command.

Apologies for the hectic code, its still a WIP, but a GET request on the HTTP headers works, so the code is functional.

a proper GET would be sent this way

if (client.connect(hostIP,7999)) {
    client.println("GET /3/config/set?emulate_motion=on HTTP/1.0");
    client.println(); // IMPORTANT EMPTY LINE
}

———
side note:

I prefer not to use the IP cString (char representation) but an array of bytes as there is a convenient type for thisIPAddress hostIP(aaa, bbb, ccc, ddd);

You would keep the cString representation if you have the domain name like const char* hostName = "www.myserver.com";
then you would do

if (client.connect(hostName,7999)) {
    client.println("GET /3/config/set?emulate_motion=on HTTP/1.0");
    client.println(); // IMPORTANT EMPTY LINE
}

I don't have an answer for your specific question, but am surprised a GET would not work. The following is the format of an IFTTT trigger when typed into a browser:

https://maker.ifttt.com/trigger/MailboxOpened/with/key/blX5hxxxxmZ_h6j8D5wsGL

and it also works with a GET command from an ESP8266.

Is there a chance that a secure connection is required, and your browser is automatically switching to HTTPS?

Anyway, in case it might be helpful, this is a mailbox notifier that works fine using the same library, but it's HTTPS:

Thank you both for the responses, It may be that i was incorrectly formatting the GET request, so will have another attempt at this over the coming days and report back.

if (client.connect(hostIP,7999)) {
    client.println("GET /3/config/set?emulate_motion=on HTTP/1.0");
    client.println(); // IMPORTANT EMPTY LINE
}

This worked like a charm! evidently a GET would work, had i formatted it properly. Thank you very much!

Out of interest, what is the importance of the empty line?

the generic message consist of a start-line, zero or more header fields (also known as "headers"), an empty line (i.e., a line with nothing preceding the CRLF) indicating the end of the header fields, and possibly a message-body.

If you miss that line then the server side does not know it has finished receiving the header and thus keeps waiting until it times out

Basically it’s part of the RFC specification

By the way, a "GET" request should not modify state. Use the "POST" method to change the state of something.

From HTTP/1.1: Method Definitions

In particular, the convention has been established that the GET and HEAD methods SHOULD NOT have the significance of taking an action other than retrieval. These methods ought to be considered "safe". This allows user agents to represent other methods, such as POST, PUT and DELETE, in a special way, so that the user is made aware of the fact that a possibly unsafe action is being requested.