Unable to connect to a local webserver

Hello everyone!

I'm trying to use and ESP8266 module with an Arduino to turn on/off a Philips hue light bulb. I've already set the Philips account and it work perfectly with the browser. Now I've tried to do the same thing with that HW configuration but I always fail to connect to the bridge. So I've used a NodeMCU board to simulate a webserver to see if the problem is the bridge but again with browser works and with the board I cannot connect to the server. At the moment I have switched the role so I'm running the web server on the Arduino+ESP and the client on my NodeMCU in order to use the ESP8266HTTPClient.h and ESP8266WiFi.h libraries that are more powerful than WiFiEsp.h to do http request.
Ah, if I tried the example to connect to the Arduino page to get the logo in ASCII the sketch works perfectly so I think that the problem is to establish the connection locally.

About code, I avoid to write the webserver because I have change anything.
Instead the important part about the client is this:

....

WiFiClient client;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  WiFi.begin(ssid, password); 
  Serial.println("Connecting to WiFi");
  Serial.println(WiFi.localIP());
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }
  Serial.println("Connected to the WiFi network");  

.....


if(client.connect("http://192.168.1.128", 80)){
     client.print("GET ");
     client.println(" HTTP/1.1");
     client.stop();
  } else {
    Serial.println("Unable to connect to server");
  }

I always get Unable to connect to server. I've also tried to use just the IP without http://.

Any help?

Thanks in advance!

Instead the important part about the client is this:

In most cases we see this sentence the error is in the part of the code that is hidden from us.

if(client.connect("http://192.168.1.128", 80)){

The "http://" is completely wrong here, the IP address as a string too because that implies that the ESP has access to a DNS server as it will always try to resolve the hostname you supplied. Have you tried to supply an actual IPAddress to that function:

IPAddress server(192, 168, 1, 128);
if (client.connect(server, 80) {

I guess 192.168.1.128 is your other ESP. Did you check that your router doesn't have the option "device isolation" active? Many of today's routers have that enabled by default (often even without the option to deactivate it) and the manufacturer call that a security feature...

Your PC (the one with the "browser" you mentioned) was connected by WiFi too? You need to tell us much more about your local network to enable us to help you. This doesn't look like a programming problem but rather a network problem on your side.

Thanks for your reply and yes I agree. I think that is a network problem.

I’ve tried without http:// as said but it still doesn’t work. Then after some research I have used

IPAddress server(192, 168, 1, 128);
 if(client.connect(server, 80));

And it works!

This IP is the server IP that are running on the Arduino+ESP.
The IP of the NodeMCU that acts like a client is 192.168.1.207

Your PC (the one with the “browser” you mentioned) was connected by WiFi too? You need to tell us much more about your local network to enable us to help you. This doesn’t look like a programming problem but rather a network problem on your side.

Boards and PC are connected to the same wifi.

The problem now is that I’ve not the Philips bridge here so I’m using an emulator that run a server on the localhost. So If i use Postman, Browser it works but with the NodeMCU I’m not able to connect to that server.
Any help? Maybe I’ve to use the PC IP address?

About the code that I’ve avoided, here is the complete sketch. Don’t care about the get request because the problem at the moment is the client.connect function:

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

// Ambient light sensor
#include <Wire.h>


// Wifi network SSID
const char* ssid = "MYSSID";

// Wifi network password
const char* password = "MYPSW";

WiFiClient client;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  WiFi.begin(ssid, password); 
  Serial.println("Connecting to WiFi");
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }
  Serial.println("Connected to the WiFi network");  
  Serial.println(WiFi.localIP());
  switchLight(1, true);
}

void loop() {
 
}

void switchLight(byte room, bool current_state){
  const IPAddress server1(127,0,0,1);//(127,0,0,1);//(172,30,244,29);//(192,168,1,128);
  if(client.connect(server1, 80)){
   Serial.println("Sending HTTP REQUEST");
   client.print("GET");
   client.println(" HTTP/1.1");
   client.stop();
  } else {
    Serial.println("Unable to connect to server");
  }
}

Ok, I've tried to put the IP of my PC and if I stop the server on the localhost, the client is unable to connect to the server. So I think that It works. I'm testing with the correct GET request

Localhost i.e. 127.0.0.1 is an address on the local host, you cannot connect to that from a different host, if you are connecting to a remote host you need to use the 192.168.1.x address (assuming your local network is 192.168.1).

Yes. Sorry for this newbie error but sometimes only after write or discuss with someone, the idea comes.

If I can ask, I have another question. I have to attach a body to my request. In particular query should be this:

https://bridge_ip/api/newdeveloper/lights/1/state

with this body
{"on":true}

The developer guide says that I have to use a PUT request. So how I can attach this body to my request?

I've to use the Arduino+ESP so I can't use ESP8266HTTPClient.h and I'm using this library:

But if you know other library that are easier to handle I can use it!

At the moment I'm trying this:

HttpClient http(client); 
  char req_string[] = "/api/newdeveloper/lights/1/state";
  Serial.println(req_string);
  int err = 0;
  err = http.get("192.168.1.202", req_string);

  char put_string[] = "{\"on\":false)}";
  http.put("192.168.1.202",put_string);

But doesn't work.
Already test with postman and works.

Thanks again for your help!

Are you sure the library supports https ?

Might make it easier for everyone if you post the whole sketch, Can you print out the value of err rather than just ignore it.

What does happen, as just saying it does not work gives very little help with diagnosing the problem.

I've to use the Arduino+ESP so I can't use ESP8266HTTPClient.h and I'm using this library:

What other restrictions did you forget to tell us? What does Arduino+ESP mean? What exact board are you using?

The linked library doesn't support HTTPS and it's designed for an Ethernet library interface.

So start over and describe your project in detail. Post a wiring diagram for the complete setup!

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