InfluxDB write failed: connection refused

Hi,

I’ve setup a test account using the cloud option (Google) and copied the example Arduino code to an ESP32, but get the below error.

INFLUXDB_URL, INFLUXDB_TOKEN, INFLUXDB_ORG and INFLUXDB_BUCKET appear correct.

INFLUXDB_URL = “https://europe-west1-1.gcp.cloud2.influxdata.com
INFLUXDB_ORG = InfluxDB UI → User → About → Common Ids → Organization ID.
INFLUXDB_BUCKET = “Test”

Connection with debug on:

Writing: wifi_status,device=ESP32,SSID=***** rssi=-54i
[599750][D][HTTPClient.cpp:298] beginInternal(): protocol: https, host: europe-west1-1.gcp.cloud2.influxdata.com port: 443 url: /api/v2/write?org=***********&bucket=TEST
[599760][D][HTTPClient.cpp:595] sendRequest(): request type: 'POST' redirCount: 0

[599767][E][WiFiClientSecure.cpp:135] connect(): start_ssl_client: -1
[599773][D][HTTPClient.cpp:1134] connect(): failed connect to europe-west1-1.gcp.cloud2.influxdata.com:443
[599782][W][HTTPClient.cpp:1437] returnError(): error(-1): connection refused
[599789][D][HTTPClient.cpp:405] disconnect(): tcp is closed

InfluxDB write failed: connection refused
Wait 10s


Any idea what I’ve done wrong?

@madlan, just to be sure, do you pass InfluxDbCloud2CACert to the InfluxDBClient constructor, and do you have time synchronization before writing data?

@VlastaHajek Thanks for replying, yes, time is synced correct:

Syncing time...
Synchronized time: Mon Mar 21 11:51:27 2022

Constructor is:

InfluxDBClient client(INFLUXDB_URL, INFLUXDB_ORG, INFLUXDB_BUCKET, INFLUXDB_TOKEN, InfluxDbCloud2CACert);

Here is the full code:

#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 "*********"
// WiFi password
#define WIFI_PASSWORD "*********"
// InfluxDB v2 server url, e.g. https://eu-central-1-1.aws.cloud2.influxdata.com (Use: InfluxDB UI -> Load Data -> Client Libraries)
#define INFLUXDB_URL "https://europe-west1-1.gcp.cloud2.influxdata.com"
// InfluxDB v2 server or cloud API token (Use: InfluxDB UI -> Data -> API Tokens -> Generate API Token)
#define INFLUXDB_TOKEN "**************************************"
// InfluxDB v2 organization id (Use: InfluxDB UI -> User -> About -> Common Ids )
#define INFLUXDB_ORG "******************"
// InfluxDB v2 bucket name (Use: InfluxDB UI ->  Data -> Buckets)
#define INFLUXDB_BUCKET "TEST"

// Set timezone string according to https://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html
// Examples:
//  Pacific Time: "PST8PDT"
//  Eastern: "EST5EDT"
//  Japanesse: "JST-9"
//  Central Europe: "CET-1CEST,M3.5.0,M10.5.0/3"
#define TZ_INFO "GMT+0BST-1,M3.5.0/01:00:00,M10.5.0/02:00:00"

// InfluxDB client instance with preconfigured InfluxCloud certificate
InfluxDBClient client(INFLUXDB_URL, INFLUXDB_ORG, INFLUXDB_BUCKET, INFLUXDB_TOKEN, InfluxDbCloud2CACert);

// 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();

  // Add tags
  sensor.addTag("device", DEVICE);
  sensor.addTag("SSID", WiFi.SSID());

  // Accurate time is necessary for certificate validation and writing in batches
  // For the fastest time sync find NTP servers in your area: 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() {
  // Clear fields for reusing the point. Tags will remain untouched
  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("Wait 10s");
  delay(10000);
}

Thanks a lot for the info.
Did the validation connection finish correctly?
What is your ESP32 Arduino Core version?

I’m using Arduino IDE 1.8.19 and 2.0.2 ESP32 Core (I’ve just done a clean install of both):

image

Here is the full output, seems connected ok?

[2-hal-cpu.c:211] setCpuFrequencyMhz(): PLL: 480 / 2 = 240 Mhz, APB: 80000000 Hz
[    37][D][WiFiGeneric.cpp:831] _eventCallback(): Arduino Event: 0 - WIFI_READY
[   129][D][WiFiGeneric.cpp:831] _eventCallback(): Arduino Event: 2 - STA_START
[   129][I][WiFiMulti.cpp:84] addAP(): [WIFI][APlistAdd] add SSID: ********
Connecting to wifi[  2769][D][WiFiGeneric.cpp:831] _eventCallback(): Arduino Event: 1 - SCAN_DONE
[  2770][I][WiFiMulti.cpp:114] run(): [WIFI] scan done
[  2770][I][WiFiMulti.cpp:119] run(): [WIFI] 10 networks found
[  2799][D][WiFiMulti.cpp:149] run():  --->   3: [6][**:**:**:**:**:**] ******** (-58) *
[  2855][I][WiFiMulti.cpp:160] run(): [WIFI] Connecting BSSID: **:**:**:**:**:** SSID: ******** Channel: 6 (-58)
[  3032][D][WiFiGeneric.cpp:831] _eventCallback(): Arduino Event: 4 - STA_CONNECTED
[  3555][D][WiFiGeneric.cpp:831] _eventCallback(): Arduino Event: 7 - STA_GOT_IP
[  3555][D][WiFiGeneric.cpp:880] _eventCallback(): STA IP: 192.168.86.137, MASK: 255.255.255.0, GW: 192.168.86.1
[  3563][I][WiFiMulti.cpp:174] run(): [WIFI] Connecting done.
[  3566][D][WiFiMulti.cpp:175] run(): [WIFI] SSID: ********
[  3572][D][WiFiMulti.cpp:176] run(): [WIFI] IP: 192.168.86.137
[  3578][D][WiFiMulti.cpp:177] run(): [WIFI] MAC: **:**:**:**:**:**
[  3584][D][WiFiMulti.cpp:178] run(): [WIFI] Channel: 6

Syncing time........................................
Synchronized time: Thu Jan  1 00:00:23 1970

[ 23601][D][HTTPClient.cpp:298] beginInternal(): protocol: https, host: europe-west1-1.gcp.cloud2.influxdata.com port: 443 url: /health
[ 23605][D][HTTPClient.cpp:595] sendRequest(): request type: 'GET' redirCount: 0

[ 24720][D][HTTPClient.cpp:1141] connect():  connected to europe-west1-1.gcp.cloud2.influxdata.com:443
[ 24758][D][HTTPClient.cpp:1275] handleHeaderResponse(): code: 200
[ 24758][D][HTTPClient.cpp:1278] handleHeaderResponse(): size: 1224
[ 24759][D][HTTPClient.cpp:619] sendRequest(): sendRequest code=200

[ 24765][D][HTTPClient.cpp:383] disconnect(): still data in buffer (1224), clean up.

[ 24778][D][HTTPClient.cpp:392] disconnect(): tcp stop
Connected to InfluxDB: https://europe-west1-1.gcp.cloud2.influxdata.com
Writing: wifi_status,device=ESP32,SSID=******** rssi=-57i
[ 24790][D][HTTPClient.cpp:298] beginInternal(): protocol: https, host: europe-west1-1.gcp.cloud2.influxdata.com port: 443 url: /api/v2/write?org=****************&bucket=TEST
[ 24805][D][HTTPClient.cpp:595] sendRequest(): request type: 'POST' redirCount: 0

[ 24910][E][WiFiClientSecure.cpp:135] connect(): start_ssl_client: -1
[ 24912][D][HTTPClient.cpp:1134] connect(): failed connect to europe-west1-1.gcp.cloud2.influxdata.com:443
[ 24915][W][HTTPClient.cpp:1437] returnError(): error(-1): connection refused
[ 24921][D][HTTPClient.cpp:405] disconnect(): tcp is closed

InfluxDB write failed: connection refused
Wait 10s

Here’s the connection attempt with Verbose:

Syncing time.
Synchronized time: Mon Mar 21 14:33:49 2022

[  3327][V][HTTPClient.cpp:247] beginInternal(): url: https://europe-west1-1.gcp.cloud2.influxdata.com/health
[  3329][D][HTTPClient.cpp:298] beginInternal(): protocol: https, host: europe-west1-1.gcp.cloud2.influxdata.com port: 443 url: /health
[  3341][D][HTTPClient.cpp:595] sendRequest(): request type: 'GET' redirCount: 0

[  3348][V][ssl_client.cpp:61] start_ssl_client(): Free internal heap before TLS 261636
[  3356][V][ssl_client.cpp:67] start_ssl_client(): Starting socket
[  3428][V][ssl_client.cpp:143] start_ssl_client(): Seeding the random number generator
[  3429][V][ssl_client.cpp:152] start_ssl_client(): Setting up the SSL/TLS structure...
[  3433][V][ssl_client.cpp:175] start_ssl_client(): Loading CA cert
[  3504][V][ssl_client.cpp:244] start_ssl_client(): Setting hostname for TLS session...
[  3504][V][ssl_client.cpp:259] start_ssl_client(): Performing the SSL/TLS handshake...
[  4510][V][ssl_client.cpp:280] start_ssl_client(): Verifying peer X.509 certificate...
[  4511][V][ssl_client.cpp:288] start_ssl_client(): Certificate verified.
[  4514][V][ssl_client.cpp:303] start_ssl_client(): Free internal heap after TLS 218320
[  4521][D][HTTPClient.cpp:1141] connect():  connected to europe-west1-1.gcp.cloud2.influxdata.com:443
[  4531][V][ssl_client.cpp:351] send_ssl_data(): Writing HTTP request with 311 bytes...
[  4584][V][HTTPClient.cpp:1234] handleHeaderResponse(): RX: 'HTTP/1.1 200 OK'
[  4585][V][HTTPClient.cpp:1234] handleHeaderResponse(): RX: 'Date: Mon, 21 Mar 2022 14:33:51 GMT'
[  4589][V][HTTPClient.cpp:1234] handleHeaderResponse(): RX: 'Content-Type: text/html'
[  4597][V][HTTPClient.cpp:1234] handleHeaderResponse(): RX: 'Content-Length: 1224'
[  4604][V][HTTPClient.cpp:1234] handleHeaderResponse(): RX: 'Connection: close'
[  4611][V][HTTPClient.cpp:1234] handleHeaderResponse(): RX: 'last-modified: Mon, 21 Mar 2022 08:32:04 GMT'
[  4621][V][HTTPClient.cpp:1234] handleHeaderResponse(): RX: 'vary: Accept-Encoding'
[  4628][V][HTTPClient.cpp:1234] handleHeaderResponse(): RX: 'etag: "62383804-4c8"'
[  4636][V][HTTPClient.cpp:1234] handleHeaderResponse(): RX: 'accept-ranges: bytes'
[  4643][V][HTTPClient.cpp:1234] handleHeaderResponse(): RX: 'Strict-Transport-Security: max-age=15724800; includeSubDomains'
[  4654][V][HTTPClient.cpp:1234] handleHeaderResponse(): RX: 'X-Influxdb-Request-ID: 36265316492e872102de912d6d2cb790'
[  4664][V][HTTPClient.cpp:1234] handleHeaderResponse(): RX: 'X-Influxdb-Build: Cloud'
[  4672][V][HTTPClient.cpp:1234] handleHeaderResponse(): RX: ''
[  4678][D][HTTPClient.cpp:1275] handleHeaderResponse(): code: 200
[  4683][D][HTTPClient.cpp:1278] handleHeaderResponse(): size: 1224
[  4689][D][HTTPClient.cpp:619] sendRequest(): sendRequest code=200

[  4696][D][HTTPClient.cpp:383] disconnect(): still data in buffer (1224), clean up.

[  4709][V][ssl_client.cpp:311] stop_ssl_socket(): Cleaning SSL connection.
[  4710][D][HTTPClient.cpp:392] disconnect(): tcp stop
[  4715][V][ssl_client.cpp:311] stop_ssl_socket(): Cleaning SSL connection.
Connected to InfluxDB: https://europe-west1-1.gcp.cloud2.influxdata.com
Writing: wifi_status,device=ESP32,SSID=********rssi=-56i
[  4734][V][HTTPClient.cpp:247] beginInternal(): url: https://europe-west1-1.gcp.cloud2.influxdata.com/api/v2/write?org=********&bucket=TEST
[  4746][D][HTTPClient.cpp:298] beginInternal(): protocol: https, host: europe-west1-1.gcp.cloud2.influxdata.com port: 443 url: /api/v2/write?org=********&bucket=TEST
[  4762][D][HTTPClient.cpp:595] sendRequest(): request type: 'POST' redirCount: 0

[  4769][V][ssl_client.cpp:61] start_ssl_client(): Free internal heap before TLS 261000
[  4777][V][ssl_client.cpp:67] start_ssl_client(): Starting socket
[  4814][V][ssl_client.cpp:143] start_ssl_client(): Seeding the random number generator
[  4815][V][ssl_client.cpp:152] start_ssl_client(): Setting up the SSL/TLS structure...
[  4818][V][ssl_client.cpp:175] start_ssl_client(): Loading CA cert
[  4889][V][ssl_client.cpp:244] start_ssl_client(): Setting hostname for TLS session...
[  4890][V][ssl_client.cpp:259] start_ssl_client(): Performing the SSL/TLS handshake...
[  4897][E][WiFiClientSecure.cpp:135] connect(): start_ssl_client: -1
[  4901][V][ssl_client.cpp:311] stop_ssl_socket(): Cleaning SSL connection.
[  4907][D][HTTPClient.cpp:1134] connect(): failed connect to europe-west1-1.gcp.cloud2.influxdata.com:443
[  4916][W][HTTPClient.cpp:1437] returnError(): error(-1): connection refused
[  4923][D][HTTPClient.cpp:405] disconnect(): tcp is closed

InfluxDB write failed: connection refused
Wait 10s

ESP32 Arduino Core 2.0.2 has an issue with repeated TLS connection. This issue is already fixed in the master.
You can either downgrade ESP32 Arduino Core to 2.0.1 or apply the fix in your environment by simply copying the fixed file:

  1. Download ssl_client.cpp
  2. Find location of the ESP32 Arduino Core libraries folder on your system. On Windows it is: C:\Users\<your-user-name>\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.2\Libraries
  3. Copy downloaded ssl_client.cpp to the libraries\WiFiClientSecure\src\ssl_client.cpp
  4. Rebuild the sketch

Great - works perfectly with the latest master.