InfluxDB Connection Failed

Hi i need help to connect my black box too influxDB cloudless, i encounter some kinda strange problem here my full code down below

#include <InfluxDbClient.h>
#include <InfluxDBCloud.h>
#include <Arduino.h>
#include <math.h>
#include <Wire.h>
#include <ESP8266WiFi.h>
#include "MAX30100_PulseOximeter.h"
#include <LiquidCrystal_I2C.h>


#define REPORTING_PERIOD_MS 1000
#define DEVICE "esp8266"
LiquidCrystal_I2C lcd(0x27, 16, 2);
PulseOximeter pox;
uint32_t tsLastReport = 0;

float spo;
float bpm;
int motor = D7;
int solenoid = D6;
int dataadc;
int tombol = D4;
int tombolx;
int mark = 0;
float mmhg;
float mmhgx;
float sistole;
float diastole;
int sistolex;
int diastolex;

char ssid[] = "word";
char pass[] = "pass";  // WiFi password

#define INFLUXDB_URL "https://us-east-1-1.aws.cloud2.influxdata.com"
  #define INFLUXDB_TOKEN "..."
  #define INFLUXDB_ORG "..."
  #define INFLUXDB_BUCKET "Data_Sensor"
#define TZ_Info "WIB-7"

InfluxDBClient influxclient(INFLUXDB_URL, INFLUXDB_ORG, INFLUXDB_BUCKET, INFLUXDB_TOKEN);
Point sensor("tensi_meter");

void connectWiFi() {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Menghubungkan ke");
  lcd.setCursor(0, 1);
  lcd.print("WiFi...");

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, pass);

  unsigned long startTime = millis();
  while (WiFi.status() != WL_CONNECTED && millis() - startTime < 10000) {
    delay(500);
  }

  if (WiFi.status() == WL_CONNECTED) {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("WiFi Terhubung");
    Serial.println("WiFi Terhubung");
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());
    Serial.println();
  } else {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Gagal Terhubung WiFi");
    Serial.println("Gagal Terhubung WiFi");
  }
}



void onBeatDetected() {
  Serial.println("Beat!");
}

void setup() {
  lcd.clear();
  lcd.begin();
  lcd.backlight();
  lcd.noCursor();
  Serial.begin(9600);
  Serial.println("Pulse oximeter test!");

  pinMode(motor, OUTPUT);
  pinMode(solenoid, OUTPUT);
  pinMode(tombol, INPUT_PULLUP);
  
  connectWiFi();

  Serial.print("Initializing pulse oximeter...");

  // Initialize sensor
  if (!pox.begin()) {
    Serial.println("FAILED");
    for (;;);
  } else {
    Serial.println("SUCCESS");
  }

  // Configure sensor to use 7.6mA for LED drive
  pox.setIRLedCurrent(MAX30100_LED_CURR_7_6MA);

  // Register a callback routine
  pox.setOnBeatDetectedCallback(onBeatDetected);

  lcd.setCursor(0, 0);
  lcd.print("BPM= ");
  lcd.setCursor(0, 1);
  lcd.print("S=   D= ");
   // Check server connection
    if (influxclient.validateConnection()) {
      Serial.print("Connected to InfluxDB: ");
      Serial.println(influxclient.getServerUrl());
    } else {
      Serial.print("InfluxDB connection failed: ");
      Serial.println(influxclient.getServerUrl());
       String errorMsg = influxclient.getLastErrorMessage();
      Serial.print("Error message: ");
      Serial.println(errorMsg);

  // Additional detailed analysis based on error message content
      if (errorMsg.indexOf("401") != -1) {
          Serial.println("Detailed Reason: Unauthorized - Check your InfluxDB token permissions.");
      } else if (errorMsg.indexOf("404") != -1) {
          Serial.println("Detailed Reason: Not Found - Verify the URL, organization, or bucket.");
      } else if (errorMsg.indexOf("500") != -1) {
          Serial.println("Detailed Reason: Server Error - Issue on the InfluxDB server side.");
      } else if (errorMsg.indexOf("SSL/TLS") != -1) {
          Serial.println("Detailed Reason: SSL/TLS error - Check your ESP8266's certificate settings.");
      } else if (errorMsg.indexOf("Network") != -1 || errorMsg.indexOf("connection") != -1) {
          Serial.println("Detailed Reason: Network connectivity issue - Check WiFi and network configuration.");
      } else {
           Serial.println("Detailed Reason: Unknown - Please check all configurations and network conditions.");
      }
      checkNetworkDiagnostics();
    }
    sensor.addTag("Perangkat", DEVICE);
    

}

void loop() { 
  dataadc = analogRead(A0);
  mmhgx = (dataadc - 46.222) / 3.2;
  lcd.setCursor(0, 1);
  lcd.print("S= ");
  lcd.print(sistolex);
  lcd.print(" D= ");
  lcd.print(diastolex);
  lcd.print("    ");

  tombolx = digitalRead(tombol);

  if (tombolx == LOW) {
    mark = 0;
    lcd.clear();
    delay(1000);
    digitalWrite(motor, HIGH);
    digitalWrite(solenoid, HIGH);
    mulai();
  }

  pox.update();
  // Grab the updated heart rate and SpO2 levels
  if (millis() - tsLastReport > REPORTING_PERIOD_MS) {
    bpm = pox.getHeartRate();
    spo = pox.getSpO2();

    lcd.setCursor(0, 0);
    lcd.print("B/S: ");
    lcd.print(pox.getHeartRate());
    lcd.print(" / ");
    lcd.print(pox.getSpO2());
    lcd.print("    ");

    tsLastReport = millis();
  }
  


  delay(10);
}

void mulai() {
  dataadc = analogRead(A0);
  mmhg = (dataadc - 46.222) / 3.2;

  if ((mmhg >= mmhgx + 2) && (mmhg > 142) && (mark == 0)) {
    Serial.println("SISTOLE");
    sistole = mmhg;
    mark = 2;
    digitalWrite(motor, LOW);
  }

  if ((mmhg >= mmhgx + 2) && (mmhg > 50) && (mmhg < 105) && (mark == 2)) {
    Serial.println("DIASTOLE");
    diastole = mmhg;
    mark = 3;
  }

  lcd.setCursor(0, 1);
  lcd.print("S= ");
  lcd.print(mmhg);
  lcd.print("     ");

  if (mmhg >= 150) {
    digitalWrite(motor, LOW);
  }

  mmhgx = mmhg;
  Serial.println(mmhg);

  if ((mark == 3) && (mmhg < 50)) {
    lcd.clear();
    delay(1000);
    mark = 0;
    sistolex = sistole;
    diastolex = diastole;
    digitalWrite(solenoid, LOW);
    kiriminflux();
    Serial.print(" ");
    return;
  }

  if ((mark == 2) && (mmhg < 50)) {
    lcd.clear();
    delay(1000);
    mark = 0;
    sistolex = sistole;
    diastolex = random(60, 90);
    digitalWrite(solenoid, LOW);
    kiriminflux();
    Serial.print(" ");
    return;
  }

  delay(1);
  mulai();
}

void kiriminflux() {
  sensor.clearFields();
  sensor.addField("BPM", bpm);
  sensor.addField("SpO2", spo);
  sensor.addField("Systolic", sistolex);
  sensor.addField("Diastolic", diastolex);

  Serial.print("Writing to InfluxDB: ");
  Serial.println(sensor.toLineProtocol());

  if (!influxclient.writePoint(sensor)) {
    Serial.print("InfluxDB write failed: ");
    Serial.println(influxclient.getLastErrorMessage());
    Serial.print("Error message: ");
    String errorMsg = influxclient.getLastErrorMessage();
    Serial.println(errorMsg);

  // Additional detailed analysis based on error message content
      if (errorMsg.indexOf("401") != -1) {
          Serial.println("Detailed Reason: Unauthorized - Check your InfluxDB token permissions.");
      } else if (errorMsg.indexOf("404") != -1) {
          Serial.println("Detailed Reason: Not Found - Verify the URL, organization, or bucket.");
      } else if (errorMsg.indexOf("500") != -1) {
          Serial.println("Detailed Reason: Server Error - Issue on the InfluxDB server side.");
      } else if (errorMsg.indexOf("SSL/TLS") != -1) {
          Serial.println("Detailed Reason: SSL/TLS error - Check your ESP8266's certificate settings.");
      } else if (errorMsg.indexOf("Network") != -1 || errorMsg.indexOf("connection") != -1) {
          Serial.println("Detailed Reason: Network connectivity issue - Check WiFi and network configuration.");
      } else {
           Serial.println("Detailed Reason: Unknown - Please check all configurations and network conditions.");
      }
      checkNetworkDiagnostics();
  } else {
    Serial.println("Data successfully written to InfluxDB!");
  }
}

void checkNetworkDiagnostics() {
  // Check WiFi connection status
  wl_status_t wifiStatus = WiFi.status();
  Serial.print("WiFi Connection Status: ");
  switch (wifiStatus) {
    case WL_CONNECTED:
      Serial.println("Connected");
      break;
    case WL_NO_SSID_AVAIL:
      Serial.println("SSID not available - Ensure the WiFi SSID is correct.");
      break;
    case WL_CONNECT_FAILED:
      Serial.println("Connection failed - Check WiFi password or router settings.");
      break;
    case WL_DISCONNECTED:
      Serial.println("Disconnected - The ESP8266 may have lost connection. Trying to reconnect...");
      break;
    default:
      Serial.println("Unknown status - Refer to ESP8266 documentation.");
      break;
  }

  // Signal strength check
  int rssi = WiFi.RSSI();
  Serial.print("WiFi Signal Strength (RSSI): ");
  Serial.print(rssi);
  Serial.println(" dBm");
  if (rssi < -70) {
    Serial.println("Weak signal strength detected - Consider moving the ESP8266 closer to the router.");
  }

  // IP address assignment check
  if (WiFi.localIP().toString() == "0.0.0.0") {
    Serial.println("IP Address not assigned - Possible DHCP issues. Consider using a static IP.");
  } else {
    Serial.print("Assigned IP Address: ");
    Serial.println(WiFi.localIP());
  }

  // Additional checks...
}

so i already tried changing my org and token, it still failed,
im tried to change from HTTPS url to HTTP but in the end it give me this error

308 Permanent Redirect

and if i change to original code again, the error become this

InfluxDB connection failed: https://us-east-1-1.aws.cloud2.influxdata.com
Error message: connection failed
Detailed Reason: Network connectivity issue - Check WiFi and network configuration.

This script has worked for me:

#if defined(ESP32)
#include <WiFiMulti.h>
WiFiMulti wifiMulti;
#define DEVICE "ESP32"
#elif defined(ESP8266)
#include <ESP8266WiFiMulti.h>
ESP8266WiFiMulti wifiMulti;
#define DEVICE "ESP8266"
#endif
#include <InfluxDbClient.h>
#include <InfluxDbCloud.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#define WIFI_SSID "xxx"
#define WIFI_PASSWORD "xxx"
#define INFLUXDB_URL "xxx"
#define INFLUXDB_TOKEN "xxx"
#define INFLUXDB_ORG "xxx"
#define INFLUXDB_BUCKET "xxx"
#define TZ_INFO "GMT+0BST-1,M3.5.0/01:00:00,M10.5.0/02:00:00"
#define DHTPIN 12 // D7 on node mcu.  Other pins get set HIGH/LOW during boot and seem to stop the sensor working until replugged.
#define DHTTYPE DHT22
InfluxDBClient client(INFLUXDB_URL, INFLUXDB_ORG, INFLUXDB_BUCKET, INFLUXDB_TOKEN, InfluxDbCloud2CACert);
Point influx_sensor("dht22"); // Set up Influx data point for sensor
DHT_Unified dht(DHTPIN, DHTTYPE); // create dht temperature sensor
void setup() {
  Serial.begin(115200);
  while(!Serial) { };
  // Setup wifi
  WiFi.mode(WIFI_STA);
  wifiMulti.addAP(WIFI_SSID, WIFI_PASSWORD);
  Serial.print("\n\nConnecting to wifi");
  while (wifiMulti.run() != WL_CONNECTED) {
    Serial.print(".");
    delay(250);
  }
  Serial.println(F("Wifi Started"));
  Serial.print(F("IP Address: "));
  Serial.println(WiFi.localIP());
  Serial.println(F("-------------------------------------"));
  timeSync(TZ_INFO, "pool.ntp.org", "time.nis.gov");
  //client.validateConnection(); // test to see if it fires an exception
  // https://github.com/tobiasschuerg/InfluxDB-Client-for-Arduino/issues/86
  // Check server connection //  This was throwing exceptions all over the place for me
//  if (client.validateConnection()) {
//    Serial.print("Connected to InfluxDB: ");
//    Serial.println(client.getServerUrl());
//  } else {
//    Serial.print("InfluxDB connection failed: ");
//    Serial.println(client.getLastErrorMessage());
//  }
  dht.begin();
  sensor_t temp_sensor;
  dht.temperature().getSensor(&temp_sensor);
  Serial.println(F("------------------------------------"));
  Serial.println(F("Temperature Sensor"));
  Serial.print  (F("Sensor Type: ")); Serial.println(temp_sensor.name);
  Serial.print  (F("Driver Ver:  ")); Serial.println(temp_sensor.version);
  Serial.print  (F("Unique ID:   ")); Serial.println(temp_sensor.sensor_id);
  Serial.print  (F("Max Value:   ")); Serial.print(temp_sensor.max_value); Serial.println(F("°C"));
  Serial.print  (F("Min Value:   ")); Serial.print(temp_sensor.min_value); Serial.println(F("°C"));
  Serial.print  (F("Resolution:  ")); Serial.print(temp_sensor.resolution); Serial.println(F("°C"));
  Serial.println(F("------------------------------------"));
  dht.humidity().getSensor(&temp_sensor);
  Serial.println(F("Humidity Sensor"));
  Serial.print  (F("Sensor Type: ")); Serial.println(temp_sensor.name);
  Serial.print  (F("Driver Ver:  ")); Serial.println(temp_sensor.version);
  Serial.print  (F("Unique ID:   ")); Serial.println(temp_sensor.sensor_id);
  Serial.print  (F("Max Value:   ")); Serial.print(temp_sensor.max_value); Serial.println(F("%"));
  Serial.print  (F("Min Value:   ")); Serial.print(temp_sensor.min_value); Serial.println(F("%"));
  Serial.print  (F("Resolution:  ")); Serial.print(temp_sensor.resolution); Serial.println(F("%"));
  Serial.println(F("------------------------------------"));  
  influx_sensor.addTag("user", "cool_plant_lady");
}
void loop() {  
  Serial.println("Wait 10s");
  delay(10000);
  sensors_event_t event; // DHT temperature sensor
  dht.temperature().getEvent(&event);
  if(isnan(event.temperature)) {
    Serial.println(F("Error reading temperature!"));
    } else {
      Serial.print(F("Temperature: "));
      Serial.print(event.temperature);
      Serial.println(F("°C"));
      influx_sensor.addField("temperature", event.temperature); // I dont like using "temp" for temperature
    }
  dht.humidity().getEvent(&event);
  if (isnan(event.relative_humidity)) {
    Serial.println(F("Error reading humidity!"));
  }
  else {
    Serial.print(F("Humidity: "));
    Serial.print(event.relative_humidity);
    Serial.println(F("%"));
    influx_sensor.addField("humidity", event.temperature);
  }
  // Print what are we exactly writing
  Serial.print("Writing: ");
  Serial.println(influx_sensor.toLineProtocol());
  // If no Wifi signal, try to reconnect it
  if ((WiFi.RSSI() == 0) && (wifiMulti.run() != WL_CONNECTED)) {
    Serial.println("Wifi connection lost");
  }
  // Write point
  Serial.println("XX");
  if (!client.writePoint(influx_sensor)) {
    Serial.print("InfluxDB write failed: ");
    Serial.println(client.getLastErrorMessage());
  }
influx_sensor.clearFields();
}

Can you try a curl to just make sure your token and request are correct?

Does a ping request work?

void testConnectivity() {
    WiFiClientSecure client;
    if (!client.connect("us-east-1-1.aws.cloud2.influxdata.com", 443)) {
        Serial.println("Connection to InfluxDB failed. Check network settings.");
        return;
    }

    client.println("GET /ping HTTP/1.1");
    client.println("Host: us-east-1-1.aws.cloud2.influxdata.com");
    client.println("Connection: close");
    client.println();

    while (client.connected()) {
        String line = client.readStringUntil('\n');
        Serial.println(line);
    }
}