Convert InfluxDB Cloud curl command to be Implemented on Arduino Code

0

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".

you need to do HTTPS for that board. See MKR ETH shield and TLS as a discussion

Thank you so much for your response. I just realized that I ever installed and tried that library. At that time, it was not successful for me because I didn't pay attention to details how to use it.
Important Note : we should update the "trust_anchors.h" based on our project web server to be connected.

Now, it is fully working. I can push the data to InfluxDB Cloud and the data can be seen from Grafana Cloud. Some left things to do : add Unix Time to each data measurement.

Glad it helped

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.