“[HTTP] GET… failed, error: connection refused” when using Static IP ESP32

Hello there!

I’m trying to log data from ESP32 to Google Sheets via Google App Script. If I only use wifi.begin() via DHCP IP then there is no problem. But when I set static IP for ESP32 for uploading the code to ESP32, then use wifi.begin() to send data to Sheets, I get the problem of connection failure. I guess the problem maybe that I set the static IP wrong. Below is my code:

//Include required libraries
#include "WiFi.h"
#include <HTTPClient.h>
#include "time.h"
#include <WiFiClient.h>
#include <Update.h>
const char* ntpServer = "pool.ntp.org";
const long  gmtOffset_sec = 19800;
const int   daylightOffset_sec = 0;
IPAddress local_ip(192, 168, 1, 4);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);

// WiFi credentials
const char* ssid = "";         // change SSID
const char* password = "";    // change password
// Google script ID and required credentials
String GOOGLE_SCRIPT_ID = "";    // change Gscript ID
int count = 0;
void setup() {
  delay(1000);
  Serial.begin(115200);
  delay(1000);
  // connect to WiFi
  Serial.println();
  Serial.print("Connecting to wifi: ");
  Serial.println(ssid);
  Serial.flush();
  WiFi.config(local_ip, gateway, subnet);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  // Init and get the time
  configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
}
void loop() {
   if (WiFi.status() == WL_CONNECTED) {
    static bool flag = false;
    struct tm timeinfo;
    if (!getLocalTime(&timeinfo)) {
      Serial.println("Failed to obtain time");
      return;
    }
    char timeStringBuff[50]; //50 chars should be enough
    strftime(timeStringBuff, sizeof(timeStringBuff), "%A, %B %d %Y %H:%M:%S", &timeinfo);
    String asString(timeStringBuff);
    asString.replace(" ", "-");
    Serial.print("Time:");
    Serial.println(asString);
    String urlFinal = "https://script.google.com/macros/s/"+GOOGLE_SCRIPT_ID+"/exec?"+"date=" + asString + "&lan_1=" + "100"
  +"&lan_2=" + "200"+"&lan_3=" + "300"+"&trung_binh=" + "250";
    Serial.print("POST data to spreadsheet:");
    Serial.println(urlFinal);
    HTTPClient http;
    http.begin(urlFinal.c_str());
    http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
    int httpCode = http.GET(); 
    Serial.print("HTTP Status Code: ");
    Serial.println(httpCode);
    //---------------------------------------------------------------------
    
    //getting response from google sheet
    String payload;
    if (httpCode > 0) {
        payload = http.getString();
        Serial.println("Payload: "+payload);    
    }
    //---------------------------------------------------------------------
    
    http.end();
  }
  count++;
  delay(1000);
} 

I have read this post and tried but it did not work.
What do you think the problem is?

Well the first step would be to enable verbose logs to see what the Arduino core has to say about this. Can you do that per documentation and post the log?