PlatformIO Community

If (!client.connect(ServerHost1,80)) work on 8266 but not on esp32

I seem to be doing something wrong again!!
What is happening is I have a 8266-01 that just needs to turn a relay on or off, but sometimes the 8266 will be in sleep mode and obviously not available to make a connection using the code below.

If I use a 8266 all is working well, but if I use the ESP32 it just hangs until the watchdog resets maximum time on watchdog is 4 seconds.

I thought obviously wrongly that the line:

if (!client.connect(ServerHost1,80)) {
    Serial.println("Server Host1 not open");
    return;
  }

Would basically say host not available go away and do something else. If anyone has any thoughts on what I’m doing wrong please shout back.
Much appreciated

void Heating_On(){
  if (!client.connect(ServerHost1,80)) {
    Serial.println("Server Host1 not open");
    return;
  }
  if(dht_tv >=26.00){
    UploadData="ROFF";
    client.println("GET /"+UploadData+" HTTP/1.1\r\nConnection: close\r\n\r\n");
    client.println("Host: "+String(ServerHost1));
    delay(250); // Essential delay for ESP32 500
    Serial.print("...Information to be uploaded: "+String(ServerHost1));
    Serial.println(UploadData);
    Serial.println("Tern Relay Off");
    client.println("Connection: close"); 
    client.stop();
  }
  else{
    if(dht_tv <=25.00){
      UploadData="RON";
      client.println("GET /"+UploadData+" HTTP/1.1\r\nConnection: close\r\n\r\n");
      client.println("Host: "+String(ServerHost1));
      delay(250); // Essential delay for ESP32 500
      Serial.print("...Information to be uploaded: "+String(ServerHost1));
      Serial.println(UploadData);
      Serial.println("Tern Relay On");
      client.println("Connection: close"); 
      client.stop();
    }
  }  
}

Platformio.ini

[env:esp12e]
platform = espressif8266
board = esp12e
framework = arduino
;upload_protocol = espota
;upload_port=192.168.1.206
upload_port = COM3
upload_speed=921600
monitor_speed = 115200
lib_deps=
    Adafruit INA219
    TaskScheduler
    DHT sensor library for ESPx
    Adafruit BMP280 Library
    ThingSpeak@1.4.3

The function also has an overload which reads

So if the ESP32 hangs you should call it with a timeout of, say 2 seconds, to initiate the TCP connection as per

if (!client.connect(ServerHost1,80, 2000)) {
    Serial.println("Server Host1 not open");
    return;
  }

the value is in milliseconds.

Because as you can see in the implementation

Calling connect() without a timeout will result in an infinite timeout (-1).

The ESP8266 is different as you can see in the code

With _timeout being initialized to

5 seconds.

Ah, actually a nice way to make the correct code compile for the ESP32 and ESP8266 is to use a #ifdef check to check whether we are compiling for the ESP32 or ESP8266. E.g. with


#ifdef ARDUINO_ARCH_ESP8266
bool connectSuccess = client.connect(ServerHost1,80);
#elif defined(ARDUINO_ARCH_ESP32)
bool connectSuccess = client.connect(ServerHost1,80, 2000);
#else
#error "Need platform specific code!!"
#endif

if (!connectSuccess) {
    Serial.println("Server Host1 not open");
    return;
}
//...

well Maxgerhardt
You did it again answered my question easily and most importantly understandably at least by me.

I don’t really know how you do it, how you have such a depth of knowledge, you must have worked on this sort of stuff for a long time.

And who knows one day I might be able to buy you a pint.

Thanks again for all your help