im trying to make influxdb into my database for my android.
but first i need to connect my input (4 sensor data in 1 device) which is in arduino?
went i see the tutorial the cloud (influxdbcloud) give me went to add an input i’m confused at what is what
below is the example and tutorial it gave me:
initialize client
#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>
// WiFi AP SSID
#define WIFI_SSID "YOUR_WIFI_SSID"
// WiFi password
#define WIFI_PASSWORD "YOUR_WIFI_PASSWORD"
#define INFLUXDB_URL "https://us-east-1-1.aws.cloud2.influxdata.com"
#define INFLUXDB_TOKEN "nBx1X1q9OAgStTOCQ73WH3nHKa3oaMqDttOnE_kQ94UJAp3ykCEr5nKR5F85RY_TTxY54B3zgKxBafqSWGhmag=="
#define INFLUXDB_ORG "63aee332ce56a036"
#define INFLUXDB_BUCKET "Data_Sensor"
// Time zone info
#define TZ_INFO "UTC7"
// Declare InfluxDB client instance with preconfigured InfluxCloud certificate
InfluxDBClient client(INFLUXDB_URL, INFLUXDB_ORG, INFLUXDB_BUCKET, INFLUXDB_TOKEN, InfluxDbCloud2CACert);
// Declare Data point
Point sensor("wifi_status");
void setup() {
Serial.begin(115200);
// Setup wifi
WiFi.mode(WIFI_STA);
wifiMulti.addAP(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting to wifi");
while (wifiMulti.run() != WL_CONNECTED) {
Serial.print(".");
delay(100);
}
Serial.println();
// Accurate time is necessary for certificate validation and writing in batches
// We use the NTP servers in your area as provided by: https://www.pool.ntp.org/zone/
// Syncing progress and the time will be printed to Serial.
timeSync(TZ_INFO, "pool.ntp.org", "time.nis.gov");
// Check server connection
if (client.validateConnection()) {
Serial.print("Connected to InfluxDB: ");
Serial.println(client.getServerUrl());
} else {
Serial.print("InfluxDB connection failed: ");
Serial.println(client.getLastErrorMessage());
}
}
void loop() {}
Write Data
void setup() {
// ... code in setup() from Initialize Client
// Add tags to the data point
sensor.addTag("device", DEVICE);
sensor.addTag("SSID", WiFi.SSID());
}
void loop() {
// Clear fields for reusing the point. Tags will remain the same as set above.
sensor.clearFields();
// Store measured value into point
// Report RSSI of currently connected network
sensor.addField("rssi", WiFi.RSSI());
// Print what are we exactly writing
Serial.print("Writing: ");
Serial.println(sensor.toLineProtocol());
// Check WiFi connection and reconnect if needed
if (wifiMulti.run() != WL_CONNECTED) {
Serial.println("Wifi connection lost");
}
// Write point
if (!client.writePoint(sensor)) {
Serial.print("InfluxDB write failed: ");
Serial.println(client.getLastErrorMessage());
}
Serial.println("Waiting 1 second");
delay(1000);
}
so i already assimilate the initialize client to my code know i need to assimilate the writing data part which im confused about :
BTW below is my code:
#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
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[] = "";
char 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"
InfluxDBClient influxclient(INFLUXDB_URL, INFLUXDB_ORG, INFLUXDB_BUCKET, INFLUXDB_TOKEN);
#define TZ_Info "WIB-7"
Point sensor("wifi_status");
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.getLastErrorMessage());
}
}
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);
return;
}
if ((mark == 2) && (mmhg < 50)) {
lcd.clear();
delay(1000);
mark = 0;
sistolex = sistole;
diastolex = random(60, 90);
digitalWrite(solenoid, LOW);
return;
}
delay(1);
mulai();
}