bebebeis
bebebeis

Reputation: 1

Convert InfluxDB Cloud API curl Command to be implemented on Arduino Code

I found difficulties to publish data to InfluxDB Cloud. My hardware setup : ESP32 dev board connected to Arduino MKR ETH shield.

Actually, there is library provided by InfluxDB to publish data from ESP32/ESP8266 to InfluxDB cloud. I tried and worked perfectly, but it is based on Wifi Connection. Meanwhile, my actual implementation area has not WiFi connection available. That's why I use Ethernet shield for internet connection.

I tried manually to push data to InfluxDB based on its API documentation. It is worked. Here is the curl command :

curl -i -XPOST "[my server address]/api/v2/write?org=[my ORG_ID]&bucket=[my Bucket_ID]&precision=s" \
--header "Authorization: Token [my Token]" \
--data-raw "[my data to be posted]"

I tried using HTTP request, it replied status code 400. Then I found that InfluxDB Cloud requires HTTPS connection. Now, I tried using BearSSLClient but still couldn't connect to InfluxDB Cloud. Here is my current code approach :

  if (sslClient.connect(server, port)) {
    Serial.println("Connected to server");

    // Construct and send the HTTP POST request
    sslClient.println("POST /api/v2/write?org=[my ORG_ID]&bucket=[my Bucket]&precision=s HTTP/1.1");
    sslClient.println("Host: [my server address(without https://)]");
    sslClient.println("Authorization: Token [my Token]");
    sslClient.println("Content-Length: [my body data length]");
    sslClient.println("Content-Type: text/plain; charset=utf-8");
    sslClient.println();
    sslClient.print("[body data]");

    // Allow time for the server to respond
    delay(500);

    // Read and print the server's response
    while (sslClient.available()) {
      char c = sslClient.read();
      Serial.write(c);
    }

    sslClient.stop();
  } else {
    Serial.println("Connection failed");
  }

In this code, it always goes to "connection failed".

Upvotes: 0

Views: 42

Answers (0)

Related Questions