Arduino Giga and Influxdb cloud2 via Ethernet

Hello,

I have an Arduino Giga WiFi board with a W5500 Ethernet shield, I want to post sensors data to influxdb cloud2. I have tried the code below to post random values for a test but it field. However, with the same code when I use the local server for influx (not cloud) on 192.168.x.x:8086, it works very well. I appreciate your support.

#include <SPI.h>
#include <Ethernet.h>

// Network settings
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };  // Your MAC address
char server[] = "eu-central-1-1.aws.cloud2.influxdata.com";  // InfluxDB Cloud 2 server URL
int port = 443;  // HTTPS port for InfluxDB Cloud 2

// InfluxDB settings for Cloud 2
const char* organization = "MyOrg"; // Organization name
const char* token = "MyToken";
const char* bucket = "MyBucket";

// Ethernet client
EthernetClient client;

void setup() {
  // Initialize serial communication
  Serial.begin(9600);

  // Set the CS pin to 15
  Ethernet.init(15);

  // Initialize the Ethernet shield using DHCP
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    for (;;);
  }

  // Give the Ethernet shield a second to initialize
  delay(1000);

  // Print out the IP address
  Serial.print("IP Address: ");
  Serial.println(Ethernet.localIP());

  // Start connection attempt
  if (connectToInfluxDB()) {
    Serial.println("Connected to InfluxDB Cloud 2");
  } else {
    Serial.println("Connection failed");
  }
}

void loop() {
  // Your main loop code here
  // Example: sendDataToInfluxDB();
  delay(10000);  // Example: Delay between sending data
}

bool connectToInfluxDB() {
  if (client.connect(server, port, "https")) {
    Serial.println("Connected to server");

    // Create the HTTPS POST request for InfluxDB Cloud 2
    String postStr = "POST /api/v2/write?org=" + String(organization) + "&bucket=" + String(bucket) + "&precision=s HTTP/1.1\r\n";
    postStr += "Host: " + String(server) + "\r\n";
    postStr += "Authorization: Token " + String(token) + "\r\n";
    postStr += "Connection: close\r\n";
    postStr += "Content-Type: text/plain; charset=utf-8\r\n";
    // Example: postStr += "Content-Length: " + String(data.length()) + "\r\n\r\n";
    // Example: postStr += data;

    // Send the HTTPS POST request
    client.println(postStr);  // Use println to send the complete request
    Serial.println("Sent data: ");
    Serial.println(postStr);

    // Wait for server response
    while (client.connected()) {
      if (client.available()) {
        String line = client.readStringUntil('\n');
        Serial.println(line); // Print server response for debugging
        if (line == "\r") {
          break;
        }
      }
    }

    // Check HTTPS status
    String status = client.readStringUntil('\n');
    Serial.println("HTTPS status: " + status); // Print HTTPS status for debugging
    if (status.indexOf("204") >= 0) {
      client.stop();
      return true;
    }
  } else {
    Serial.println("Connection failed");
  }
  client.stop();
  return false;
}

This is the serial debug return

IP Address: 192.168.0.62
Connected to server
Sent data: 
POST /api/v2/write?org=MyOrg&bucket=MyBucket&precision=s HTTP/1.1
Host: eu-central-1-1.aws.cloud2.influxdata.com
Authorization: Token MyToken
Connection: close
Content-Type: text/plain; charset=utf-8

HTTP/1.1 400 Bad Request

Date: Mon, 08 Jul 2024 23:57:56 GMT

Content-Type: text/html

Content-Length: 248

Connection: close



HTTPS status: <html>

Connection failed

400 usually indicates a data syntax error, but if you’re successfully writing the same data to a local instance, then it might be a token/authentication problem.
These might help troubleshoot:

@Jason_Stirnaman Thank you for your reply. I double-checked all the data related to token, bucket, and organisation, and they are all correct. It sounds like there is a problem while trying to post into https as the local server is http. Sorry I am not any good with networking terms.

Ah, I missed that. Yes, you might want to Upload SSL root certs for your Arduino.