Skip to content

InfluxDB V1 - read timeout #237

@DieterWelter

Description

@DieterWelter

Steps to reproduce:
Based on the Basic Write example, i add some additional values to the influxDB Version 1 (5 tags and 14 field values)
Example of one InfluxDB data point: Measurement,tag1..tag5 field1..field14
esp32-B12FA8,device=esp32-B12FA8,sensor=SHT45,sensor2=DHT22,sensor3=DS18B20,sensor4=BME280 sht45Temperatur=24.98,sht45Feuchte=51.21,dht22Temperatur=22.21,dht22Feuchte=55.92,ds18b20Temperatur=18.16,bme280Temperatur=20.92,bme280Feuchte=55.06,bme280Luftdruck=1007.02,esp32RSSI=-45i,esp32WiFiQ=110i,currentUpTime=4i,diffTimeSlowLoop=0i,noOfWiFiRestarts=0i,noOfInfluxDBRestarts=0i

In the loop() function I write this data point every second.
The time for influxClientV1.writePoint() is more or less stable, approx. ~65-90ms
But after a certain time (~10-11min), the time for influxClientV1.writePoint() increase and writePoint() failed
The error code is: read Timeout

Remark: There is no WiFi problem. The WiFi.RSSI is all the time ~-45dBm (~108%).

Expected behavior:

Actual behavior:
I try to reconnect the InfluxDB connection, but only a restart helps. After ~10-11min the same error comes back again and again ...

Client Specifications:

  • Board/ESP chip: ESP32 NodeMCU DevKitC Development Board mit CP2102 und USB-C / ESP32 wroom 32
  • InfluxDbClient Version 3.13.2 (latest Version)
  • Visual Studio Code Version 1.96.0 (latest Version)
  • PlatformIO with Arduino framework Core 6.1.16 Home 3.4.4 (latest Version)
  • platform: espressif32@6.5.0

Server Specifications:

  • Raspberry Pi 5 with 8GB
  • Installed software:
  • OS Bookworm 64bit latest Version
  • InfluxDB Version 1.8.10
  • Grafana 11.4.0

#include <Arduino.h>
#include <time.h>

#include <WiFi.h>
#define DEVICE "esp32-B12FA8"

#include <InfluxDbClient.h>

#include "....\INFLUXDBcredentials.h" // InfluxDBV1 Zugangsdaten
#include "....\WIFIcredentials.h" // WIFI Zugangsdaten

/*
// ~~~~~~~~~~~ Enter your Wi-Fi SSID and PASSWORD ~~~~~~
const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_PASSWORD";

// Enter your URL USERNAME and PASSWORD for INFLUXDB V1.8.x
const char *influxdb_URL = "YOUR_INFLUXDB_URL";
const char *influxdb_username = "YOUR_USERNAME";
const char *influxdb_password = "YOUR_PASSWORD";
*/

#define SOFTWARE_VERSION "ESP32 InfluxDB V1 BasicWrite V.1.00 12.12.24 22:25"

// Set timezone string according to https://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html
// Examples:
// Europe/Berlin TZ CET-1CEST,M3.5.0,M10.5.0/3
#define TZ_INFO "CET-1CEST,M3.5.0,M10.5.0/3"

// WiFi AP SSID
#define WIFI_SSID ssid
// WiFi password
#define WIFI_PASSWORD password

// InfluxDB server url. Don't use localhost, always server name or ip address.
// E.g. http://192.168.1.48:8086 (In InfluxDB 2 UI -> Load Data -> Client Libraries),
#define INFLUXDB_URL influxdb_URL
#define INFLUXDB_DB_NAME "sensor_data_direct"
#define INFLUXDB_USER influxdb_username
#define INFLUXDB_PASSWORD influxdb_password

// InfluxDB client instance for InfluxDB 1
InfluxDBClient influxClientV1(INFLUXDB_URL, INFLUXDB_DB_NAME);

Point sensor(DEVICE);

boolean firstRun = true;

String esp32Hostname = "";
String esp32IPAdresse = "";
String esp32MACAdresse = "";
int esp32RSSI = 0;
int esp32WiFiQ = 0;

// ~~~~~~~~ Timer for main loop ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#define LOOP_TIMER_SLOW 1000

#define MAXLEN_LOGTEXT 256
char logText[MAXLEN_LOGTEXT];
char logTextDateTime[MAXLEN_LOGTEXT+24];

unsigned long lastTimeSlowLoop = 0;

unsigned long startUpTime = 0;
unsigned long currentUpTime = 0;
unsigned long startTimeSlowLoop = 0;
unsigned long diffTimeSlowLoop = 0;
unsigned int noOfWiFiRestarts = 0;

// ~~~~~~~~ Floatvalues for BME280 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
float bme280Temperatur = 0.0;
float bme280Feuchte = 0.0;
float bme280Luftdruck = 0.0;

// ~~~~~~~~ Floatvalues for SHT45 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
float sht45Temperatur = 0.0;
float sht45Feuchte = 0.0;

// ~~~~~~~~ Floatvalues for DHT22 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
float dht22Temperatur = 0.0;
float dht22Feuchte = 0.0;

// ~~~~~~~~ Floatvalues for DS18B20 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
float ds18b20Temperatur = 0.0;

// ~~~~~~~~~~~ ESP32_NTP_ESPRESSIF ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
const char* ntpServer = "pool.ntp.org";
const char* nistServer = "time.nis.gov";

boolean isInfluxConnectionValid = false;
unsigned int noOfInfluxDBRestarts = 0;

// Function declaration
String get_wifi_status(int status);
void setup_wifi();
char* getLocalTimeCharString();
void getSHT45Values();
void getDHT22Values();
void getDS18B20Values();
void getBME280Values();
unsigned long PrintDiffTime(unsigned long startTime, const char *text);
void SerialDebugDateTime(String txt2Log, boolean isSerialPrint);

void setup() {
String txt2Log = "";

startUpTime = millis();
Serial.begin(115200);

// ~~~~~~ Handle WiFi Connection ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
setup_wifi();

// Set InfluxDB 1 authentication params
influxClientV1.setConnectionParamsV1(INFLUXDB_URL, INFLUXDB_DB_NAME, INFLUXDB_USER, INFLUXDB_PASSWORD);

// Store measured value into point
sensor.addTag("device", DEVICE);

sensor.addTag("sensor", "SHT45");
sensor.addTag("sensor2", "DHT22");
sensor.addTag("sensor3", "DS18B20");
sensor.addTag("sensor4", "BME280");

timeSync(TZ_INFO, ntpServer, nistServer);

// Check server connection
if (influxClientV1.validateConnection()) {
txt2Log = "Connected to InfluxDB: " + influxClientV1.getServerUrl();
SerialDebugDateTime(txt2Log, true);
isInfluxConnectionValid = true;
} else {
txt2Log = "InfluxDB connection failed: " + influxClientV1.getLastErrorMessage();
SerialDebugDateTime(txt2Log, true);
isInfluxConnectionValid = false;
}
}

void loop() {

unsigned long now=0, startTime = 0;
String txt2Log = "";

// ~~~~~~~~~~~~~~~~~~~ Beginn Slow loop ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
now = startTimeSlowLoop = millis();
if (firstRun || (now - lastTimeSlowLoop) > LOOP_TIMER_SLOW) {
lastTimeSlowLoop = now;

startTime = millis();
getSHT45Values();
PrintDiffTime(startTime, "getSHT45Values():");

startTime = millis();
getDHT22Values();
PrintDiffTime(startTime, "getDHT22Values():");

startTime = millis();
getDS18B20Values();
PrintDiffTime(startTime, "getDS18B20Values():");

startTime = millis();
getBME280Values();
PrintDiffTime(startTime, "getBM280Values():");

if (isInfluxConnectionValid == true) {
  startTime = millis();
  //~~~~~~~~~~~~~~~~~ InfluxDB writePoint ~~~~~~~~~~~~~~~~~~~~~~~~~~
  // Clear fields for reusing the point. Tags will remain untouched
  //
  sensor.clearFields();

  sensor.addField("sht45Temperatur",sht45Temperatur);
  sensor.addField("sht45Feuchte",sht45Feuchte);
  sensor.addField("dht22Temperatur",dht22Temperatur);
  sensor.addField("dht22Feuchte",dht22Feuchte);
  sensor.addField("ds18b20Temperatur",ds18b20Temperatur);
  sensor.addField("bme280Temperatur",bme280Temperatur);
  sensor.addField("bme280Feuchte",bme280Feuchte);
  sensor.addField("bme280Luftdruck",bme280Luftdruck);
  esp32RSSI = WiFi.RSSI();
  esp32WiFiQ = 2 * (WiFi.RSSI() + 100);
  sensor.addField("esp32RSSI", esp32RSSI);
  sensor.addField("esp32WiFiQ", esp32WiFiQ);

  currentUpTime = (millis() - startUpTime)/1000;
  sensor.addField("currentUpTime",currentUpTime);
  sensor.addField("diffTimeSlowLoop", diffTimeSlowLoop);
  sensor.addField("noOfWiFiRestarts", noOfWiFiRestarts);
  sensor.addField("noOfInfluxDBRestarts", noOfInfluxDBRestarts);
}

if (isInfluxConnectionValid == true) {
  // Print to locale influxDB what are we exactly writing
  txt2Log = "InfluxDB writing: " + sensor.toLineProtocol();
  SerialDebugDateTime(txt2Log, true);

  // Write point to local server RPi 5
  if (!influxClientV1.writePoint(sensor)) {
    noOfInfluxDBRestarts++;
    txt2Log = "InfluxDBV1 write failed: " + influxClientV1.getLastErrorMessage();
    SerialDebugDateTime(txt2Log, true);

    txt2Log = "InfluxDB restart in 1 second";
    SerialDebugDateTime(txt2Log, true);

    txt2Log = "InfluxDB ESP.restart()";
    SerialDebugDateTime(txt2Log, true);

    txt2Log = "InfluxDB WiFi.disconnect(true)";
    SerialDebugDateTime(txt2Log, true);
    WiFi.disconnect(true);
    delay(1000);
    ESP.restart();
  }
}
PrintDiffTime(startTime, "influxClientV1.writePoint():");

// ~~~~~~~~~~~~~~~ Gesamtzeit für Slow Loop ~~~~~~~~~~~~~~~
diffTimeSlowLoop = PrintDiffTime(startTimeSlowLoop, "DiffTimeSlowLoop:");
firstRun = false;

} // ~~~~~~~~~~~~~~~~~~~ Ende Slow loop ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
}

unsigned long PrintDiffTime(unsigned long startTime, const char *text) {
unsigned long diffTime = millis() - startTime;
sprintf(logTextDateTime, "%s %s%dms", getLocalTimeCharString(), text, diffTime);
Serial.println(logTextDateTime);
return diffTime;
}

String get_wifi_status(int status) {
switch(status){
case WL_IDLE_STATUS:
return "WL_IDLE_STATUS";
case WL_SCAN_COMPLETED:
return "WL_SCAN_COMPLETED";
case WL_NO_SSID_AVAIL:
return "WL_NO_SSID_AVAIL";
case WL_CONNECT_FAILED:
return "WL_CONNECT_FAILED";
case WL_CONNECTION_LOST:
return "WL_CONNECTION_LOST";
case WL_CONNECTED:
return "WL_CONNECTED";
case WL_DISCONNECTED:
return "WL_DISCONNECTED";
}
return "";
}

void setup_wifi() {

int status = WL_IDLE_STATUS;
delay(100);
// We start by connecting to a WiFi network
Serial.print("Connecting to ");
Serial.println(ssid);

// connecting to a WiFi network
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("");

// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
status = WiFi.status();
Serial.println(get_wifi_status(status));
}

Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
esp32IPAdresse = WiFi.localIP().toString();
Serial.println(esp32IPAdresse);
Serial.print("MAC address: ");
esp32MACAdresse = WiFi.macAddress();
Serial.println(esp32MACAdresse);
esp32RSSI = WiFi.RSSI();
Serial.print("Connected Network Signal Strength (RSSI): ");
Serial.println(esp32RSSI); /Print WiFi signal strength/
}

char* getLocalTimeCharString() {
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
Serial.println("Failed to obtain time");
return nullptr;
}
// Allocate memory for the string char*
char* timeString = (char*)malloc(32);
if (timeString != nullptr) {
strftime(timeString, 64, "%d.%m.%Y %H:%M:%S", &timeinfo);
}
return timeString;
}

// ~~~~~~~~ SHT45 Temperatur und Feuchte ~~~~~~~~~~~~~~~~~~
void getSHT45Values() {
// ~~~~~~~ Simulation Temperatur und Feuchte ~~~~~~~~~~~~
sht45Temperatur = 24.5 + random(1000)/1000.0;
sht45Feuchte = 45 + random(1000)/100.0;
delay(10);
}

// ~~~~~~~~ Simulation DHT22 Temperatur und Feuchte ~~~~~~~
void getDHT22Values() {
dht22Temperatur = 22 + random(1000)/1000.0;
dht22Feuchte = 52 + random(1000)/100.0;
delay(10);
}

// ~~~~~~~~ Simulation DS18B20 Temperatur ~~~~~~~~~~~~~~~~~
void getDS18B20Values() {
ds18b20Temperatur = 18 + random(1000)/1000.0;
delay(10);
}

// ~~~~~~~~ Simulation BME280 Temperatur, Feuchte und Luftdruck ~~~~~~~~~~~~~
void getBME280Values() {
bme280Temperatur = 28 + random(1000)/1000.0;
bme280Feuchte = 48 + random(1000)/100.0;
bme280Luftdruck = 1000 + random(1000)/100.0;
delay(10);
}

// ~~~~~~~~~ Fehlertext mit Datum und Uhrzeit ~~~~~
void SerialDebugDateTime(String txt2Log, boolean isSerialPrint) {
String timestamp = getLocalTimeCharString();
String msg = timestamp + " " + txt2Log;

if (isSerialPrint == true) {
  Serial.println(msg.c_str());
}

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions