How to retrieve value from InfluxDB Cloud V2.0 using ESP32

Hello, I have a project that requires the Python app to put the data into the database and ESP32 to retrieve it. I’ve got no problems with the first part.
However, when it comes to the second part, I can’t make the ESP32 retrieve the value from the database.

I’ve searched through the GitHub repository (GitHub - tobiasschuerg/InfluxDB-Client-for-Arduino: Simple library for sending measurements to an InfluxDB with a single network request. Supports ESP8266 and ESP32.), tried looking for an answer using Google and Perplexity, but unfortunately didn’t find what I was looking for.

I’m using this code to retrieve the data:

// 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() {
// Define the chunk number
String chunkNumber = “1”;

// Define the query
String query = “from(bucket: "ESLMS_bucket")”;
query += “|> range(start: -24h)”;
query += “|> filter(fn: (r) => r._measurement == "string_data" and r._field == "chunk_content" and r.tag == "chunk_number" and r.tagValue == "” + chunkNumber + “")”;
query += “|> last()”;

// Execute the query
FluxQueryResult result = client.query(query);

if(result.getError() != “”) {
Serial.print("Query result error: ");
Serial.println(result.getError());
return;
}

// Iterate through the result set
while (result.next()) {
// Get the value of the ‘temperature’ field as a string
String temp = result.getValueByName(“temperature”).getString();
Serial.print("Temperature: ");
Serial.println(temp);
}
result.close();

delay(10000); // Wait for 10 seconds before retrieving again
}

The ESP32 connects to the database without any problems but doesn’t retrieve anything, and doesn’t even throw an error.

Can you please help me with this?

May be because you are using Flux query, but connecting to cloud InfluxDB V3.0 that doesn’t support Flux anymore?

Does this query work in WebUI of InfluxDB?

from(bucket: "ESLMS_bucket")
|> range(start: -24h)
|> filter(fn: (r) => r._measurement == "string_data" and r._field == "chunk_content" and r.tag == "chunk_number" and r.tagValue == "1")
|> last()

Thank you, that helped a bit, but didn’t solve the problem. At least now the code prints something into the Serial terminal.

I’ve tried executing the original, autogenerated query that works in browser

SELECT *
FROM “string_data”
WHERE
time >= now() - interval ‘72 hours’
AND
“chunk_number” IN (‘1’)

It causes the code to produce the following error

Query result error: {“code”:“invalid”,“message”:“compilation failed: error @1:60-1:61: invalid statement: '\n\nerror @1:69-1:70: invalid statement: '\n\nerror @1:94-1:97: invalid expression @1:96-1:97: ‘\n\nerror @1:94-1:97: unexpected token for property key: ILLEGAL (’)”}

And then when I replaced ’ with "
String chunk_number = “1”;
String query = “SELECT * FROM "string_data" WHERE time >= now() - interval "72 hours" AND "chunk_number" IN ("” + chunk_number +“")”;

And it started throwing this error
Query result error: {“code”:“invalid”,“message”:“compilation failed: error @1:94-1:97: string literal key 1 must have a value”}

Is there something else I can do to solve the problem?

Probably you should escape '

I.e. use \' instead of just '

I’ve tried using the escape sequence, that didn’t work either.

But here’s the thing I noticed, the query executed in browser was executed while the bucket was opened. At the same time, the example from the GitHub repository suggests that the first thing to do is to open the bucket “from(bucket: "” INFLUXDB_BUCKET “")”

The problem is, I can’t figure out how to do this. When I use the query in the form demonstrated in the GitHub repository, it simply doesn’t work, and there’s no output.
And I don’t know how to open a bucket using the SQL Query. Although the bucket name is passed at the time of initialization.

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

Do you happen to have a working example that enables the ESP32 to retrieve something from Influx DB?