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();
}
}
}
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;
}
//...