[SOLVED] WiFiClient can't connect to a local server

Hello, I am going fo a very common project of weather station to learn few things, (especially ESP01 and solar panel usage...) but guess what, I have some trouble.

I just try things step by steps before integrating all, so currently I am just trying to send some data to a web server (intranet) from the ESP. At the moment I don't even try to send the data from the sensor, just a value, to check the HTTP GET and wifi part.

I have first tried to use the ESP as a webserver and it was working find, accessible by any browser on the local network.

Now, I want to work the other way, having the ESP contacting a local server.
I first have the WIFI management, (similar to the previous code), then the client part, fully different. The wifi connection is handled with static IP. It helps me for other things. There is no address conflict.

We I start the ESP, It connects to the Wifi, but fail to connect to the local server. When I try to connect to the server from a browser on the local network, it works fine.

Here is my code

/* This sketch sends data via HTTP GET requests to local server from the ESP */ 
#include <ESP8266WiFi.h> 
#include <WiFiClient.h>
const char* ssid = "SSID"; //replace with your own wifi ssid 
const char* password = "PASSWORD"; //replace with your own wifi ssid password 
const int host = (192,168,1,10);  // could be a char with the url, or array with the ip address
const char* host_s = "192.168.1.10";
const int sensor_id=2; // sensor id in the database for records


// Static IP address configuration
IPAddress staticIP(192, 168, 1, 105); //ESP static IP address
IPAddress gateway(192, 168, 1, 1); //IP Address of your WiFi Router (Gateway)
IPAddress subnet(255, 255, 255, 0); //Subnet mask
IPAddress dns(116,228,111,118); //DNS (could be 8,8,8,8 for google dns or any other DNS
const char* deviceName = "TEST_ESP";

void setup() { 
  Serial.begin(115200); delay(10); // We start by connecting to a WiFi network Serial.println();
  Serial.println(); Serial.print("Connecting to ");
  Serial.println(ssid);
  
/* Explicitly set the ESP8266 to be a WiFi-client, and with correct mask */

WiFi.begin(ssid, password);
WiFi.disconnect(); //Prevent connecting to wifi based on previous configuration
WiFi.hostname(deviceName); // DHCP Hostname
WiFi.config(staticIP, subnet, gateway, dns);
WiFi.begin(ssid, password);

WiFi.mode(WIFI_STA); //WiFi mode station (connect to wifi router only) **** Is it here I am making a mistake? ****

while (WiFi.status() != WL_CONNECTED)   {
  delay(500);
  Serial.print(".");
  }
Serial.println("");
Serial.println("WiFi connected"); 
Serial.println("IP address: "); 
Serial.println(WiFi.localIP()); 
} 

int value = 0; 

void loop() { 
  
  delay(5000); ++value; Serial.print("connecting to ");
  Serial.println(host_s); 
 
  WiFiClient client;// Use WiFiClient class to create TCP connections
  
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {  // **** This is where It fails ****
    Serial.println("connection failed");
    return;
  }

// We now create a URI for the request
//this url contains the informtation we want to send to the server
//if esp8266 only requests the website, the url is empty
String url = "/meteo/create_line.php";
 url += "?sensor="; // ?sensor = name of the GET variable
url += sensor_id; // here the value, in this case the 
/* url += "?param2="; //other variable name (normally temperature, or pressure ...
url += param2; //other variable value (normally temperature value
*/
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);
  }
Serial.println();
Serial.println("closing connection"); }

I have added comments with **** which are specific to my problems, while other comments are more global to the code.

I don't see how to investigate the issue. I can't get access to logs, or things like that to know what is actually sent and received. It would be very nice if you could tell me or give me some clues on where to look.

Thanks

what is const int host = (192,168,1,10);?
use IPAddress or string.

what is that desperate begin and disconnect? you wear out the flash memory with it.
turn of connection persistence
https://arduino-esp8266.readthedocs.io/en/latest/esp8266wifi/generic-class.html#persistent

Things that can be overlooked: Did you enter your SSID and PASSWORD instead?? :wink:

Yes, no worries on this one. Just changed it to that to avoid to give my password.
Thanks

Hello Juraj,
Thanks for your advices.
The connect disconnet was to "kind of force" the connection to use static IP. But I can get rid of it at the moment, and if really needed, I'll force the IP on the router.

As for the persistent, I have added it, not very clear on the purpose

as for the IP_address being a string, actually they don't have the same name... I tried with the ip (array of integer) instead of the string to tried if it was one cause of the problem, but kept the string as memory and for debug purpose, but named host_s instead of host

const int host = (192,168,1,101);  // could be a char with the url, or array with the ip address
const char* host_s = "192.168.1.101";

I have tried implementing your comments:

WiFi.begin(ssid, password);
//WiFi.disconnect(); //Prevent connecting to wifi based on previous configuration
WiFi.hostname(deviceName); // DHCP Hostname
WiFi.config(staticIP, subnet, gateway, dns);
//WiFi.begin(ssid, password);

WiFi.mode(WIFI_STA); //WiFi mode station (connect to wifi router only)
WiFi.persistent(true);

Unfortunately, I still have the same output:

...
WiFi connected
IP address: 
192.168.1.170
connecting to 192.168.1.101
connection failed
connecting to 192.168.1.101
connection failed
connecting to 192.168.1.101
connection failed

As you can see, I am not connected with the ip address (105) I set. But shouldn't be a problem for the webserver 192.168.1.101 as it is opened and no firewall limiting IP entry

Hello Juraj,
I was disturbed by you pointing out the question of the string, for the IP address. At first I was using string, it was not working, and I suspected it could be because of being a local IP and not a domain name, so I suspected, if string, it is sent to the dns, which can't find the ip, as it is just a local IP. So I shifted to IP, as an array of string as it is normally possible to do so.
But, to be sure, I tried with the string back and it works fine. :grinning:

So, problem solved.
Thanks

first call config, then begin.
you want WiFi.persistent(false);
why use string for IP address? IPAddress host(192,168,1,10); is better

Because the ip brings a compile error. I don't know why, I haven't investigated


In file included from /home/nous/.arduino15/packages/esp8266/hardware/esp8266/3.0.2/cores/esp8266/Arduino.h:286,
                 from sketch/ESP_HTTP_GET-DHT11.ino.cpp:1:
/home/nous/.arduino15/packages/esp8266/hardware/esp8266/3.0.2/cores/esp8266/WString.h: In instantiation of 'String& String::operator+=(const T&) [with T = IPAddress]':
/home/nous/.arduino15/packages/esp8266/hardware/esp8266/3.0.2/cores/esp8266/WString.h:351:9:   required from 'String operator+(String&&, const T&) [with T = IPAddress; <template-parameter-1-2> = void]'
/home/nous/Arduino/ESP_HTTP_GET-DHT11/ESP_HTTP_GET-DHT11.ino:93:66:   required from here
/home/nous/.arduino15/packages/esp8266/hardware/esp8266/3.0.2/cores/esp8266/WString.h:134:19: error: call of overloaded 'concat(const IPAddress&)' is ambiguous
  134 |             concat(rhs);
      |             ~~~~~~^~~~~
/home/nous/.arduino15/packages/esp8266/hardware/esp8266/3.0.2/cores/esp8266/WString.h:117:14: note: candidate: 'bool String::concat(char)'
  117 |         bool concat(char c);
      |              ^~~~~~
/home/nous/.arduino15/packages/esp8266/hardware/esp8266/3.0.2/cores/esp8266/WString.h:118:14: note: candidate: 'bool String::concat(unsigned char)'
  118 |         bool concat(unsigned char c);
      |              ^~~~~~
/home/nous/.arduino15/packages/esp8266/hardware/esp8266/3.0.2/cores/esp8266/WString.h:119:14: note: candidate: 'bool String::concat(int)'
  119 |         bool concat(int num);
      |              ^~~~~~
/home/nous/.arduino15/packages/esp8266/hardware/esp8266/3.0.2/cores/esp8266/WString.h:120:14: note: candidate: 'bool String::concat(unsigned int)'
  120 |         bool concat(unsigned int num);
      |              ^~~~~~
/home/nous/.arduino15/packages/esp8266/hardware/esp8266/3.0.2/cores/esp8266/WString.h:121:14: note: candidate: 'bool String::concat(long int)'
  121 |         bool concat(long num);
      |              ^~~~~~
/home/nous/.arduino15/packages/esp8266/hardware/esp8266/3.0.2/cores/esp8266/WString.h:122:14: note: candidate: 'bool String::concat(long unsigned int)'
  122 |         bool concat(unsigned long num);
      |              ^~~~~~
/home/nous/.arduino15/packages/esp8266/hardware/esp8266/3.0.2/cores/esp8266/WString.h:123:14: note: candidate: 'bool String::concat(long long int)'
  123 |         bool concat(long long num);
      |              ^~~~~~
/home/nous/.arduino15/packages/esp8266/hardware/esp8266/3.0.2/cores/esp8266/WString.h:124:14: note: candidate: 'bool String::concat(long long unsigned int)'
  124 |         bool concat(unsigned long long num);
      |              ^~~~~~
/home/nous/.arduino15/packages/esp8266/hardware/esp8266/3.0.2/cores/esp8266/WString.h:125:14: note: candidate: 'bool String::concat(float)'
  125 |         bool concat(float num);
      |              ^~~~~~
/home/nous/.arduino15/packages/esp8266/hardware/esp8266/3.0.2/cores/esp8266/WString.h:126:14: note: candidate: 'bool String::concat(double)'
  126 |         bool concat(double num);
      |              ^~~~~~
exit status 1
Erreur de compilation pour la carte Generic ESP8266 Module

After some more investigation, it seems it was also partially due to a network configuration problem. One of the router should behave as slave, but tend to act as master from time to time... Hence esp and the server are not on the same local network anymore.
Nothing to do with the ESP code... :face_with_hand_over_mouth:

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