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.