// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
I am going to just post a clip, with a little tweak I made:
// We now create a URI for the request
String url = "/input/";
url += streamId;
url += "?private_key=";
url += privateKey;
url += "&value=";
url += value;
url = "/";
Serial.print("Requesting URL: ");
Serial.println(url);
// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
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()){
String line = client.readStringUntil('\r');
Serial.print(line);
}
All this does is dump the HTTP response out to serial.
It connects fine when host is NOT an IP address.
But when I give it an IP address in the host string it does not connect.
Daenerys:
All this does is dump the HTTP response out to serial.
So the ESP8266 is a client, you have a separate webserver, what is the ESP8266 supposed to do? It's getting the response, so what makes you say it isn't working?
I think they are saying.. "All this does is dump the HTTP response out to serial".,..... WHEN USING A HOST NAME.... but dumps nothing when an IP ADDRESS is used.
WifiClient.connect() returns False if I try to connect to a LAN address (e.g. "192.168.1.123"), but returns True for a WAN address (e.g. "arduino.cc").
Like @Daenerys, I have tried using WiFiClient::connect(IPAddress ip, uint16_t port) instead of WiFiClient::connect(const char* host, uint16_t port) (in order to bypass the call to WiFi.hostByName()), but it still returns False.
I have verified that my web server running on 192.168.1.123 is responding when contacted via a browser.
Can anyone advise why WifiClient.connect() can't connect to a local address?
Can you connect to a local hostname, especially the one that resolves to this local IP address? You're sort of comparing apples to oranges by using an internet hostname and a local IP address. Plus, did you open up port 80 on this local IP address on your router so it can receive requests from local devices? Also, is there a web server on the local machine that uses this address? You can't just "connect" to a computer. It has to have something on it capable of understanding and responding to web requests, e.g. a TCP connection.
Try set "Debug Port: Serial" and "Debug Level: WiFi" in Arduino Tools menu, and add Serial.setDebugOutput(true); line after Serial.begin(115200);. This will print some info from WiFi stack.