I have some arduino code where I am trying to pull information from four different locations. Three are working great and the fourth I am not getting any response.
These two strings work perfect in a browser and I get the proper data response. http ://10.196.1.163:8086/query?db=Garden&q=SELECT * FROM "Station" WHERE station = 61 ORDER BY DESC LIMIT 1 Or http://10.197.1.57:8086/query?db=Garden&q=SELECT%20*%20FROM%20%22Station%22%20WHERE%20station%20=%2091%20ORDER%20BY%20DESC%20LIMIT%201
{"results":[{"statement_id":0,"series":[{"name":"Station","columns":["time","altitude","humid","moist","press","rainfall","soiltemp","station","temp","type","vbat","winddir","windspeed"],"values":[["2022年01月08日T21:13:58Z",1269,29.2,0,28.5,0,0,91,73.4,9,4.32,0,0]]}]}]}
The code below is what I am trying to use. It appears to make the connection but brings nothing back.
What I see in the console is:
19:55:42.024 -> Starting connection to server: 10.196.1.163 InfluxDB Garden DB...
19:55:42.024 -> connected to server
19:55:42.024 ->
The next line is starting to connect to the next server which brings back the JSON data fine. I have been assuming that I was having issues because of the alternate port 8086 I was connecting to, but I am not sure. I have been looking and finding examples similar to what I am doing.
I must be just missing something, but I am not sure what. I have been working off and on for this issue for a couple of months.
void getLocalDataQuery2() {
Serial.println("\nStarting connection to server: 10.196.1.163 InfluxDB Garden DB...");
if (client.connect(server2, 8086)) {
Serial.println("connected to server");
client.print("GET /query?");
client.print("db=Garden");
client.println("&q=SELECT%20*%20FROM%20%22Station%22%20WHERE%20station%20=%2091%20ORDER%20BY%20DESC%20LIMIT%201");
client.println("Host: 10.196.1.163");
client.println("Connection: close");
client.println();
} else {
Serial.println("unable to connect");
while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
// unsuccessful, retry in 4 seconds
Serial.print("failed ... ");
delay(4000);
Serial.print("retrying ... ");
}
}
String line = "";
while(client.available())
{
line = client.readStringUntil('\n');
Serial.print("Line : ");
Serial.println(line);
}
}
2 Answers 2
My solution to this was to setup a webserver running php code in a Docker container. The php code ran the influxdb query and sends the results
A client doesn't receive data immediately after sending a request. So client.available() returns 0 at first and your while
loop is skipped.
The readStringUntil
is one of the functions which wait for the next byte until timeout (1 second by default). So you can call readStringUntil first and it will wait for the data to arrive.
One way would be to replace the while
loop with a do while
loop, but the do while
loop is strange and even experienced programmers don't like to use it.
In real application the first line of a HTTP response is important, because it contains the response status. The rest of the response header lines can be skipped.
So why not add reading of the first line before the while
loop? This way readStringUntil
will wait for the response and read the first line.
String line = client.readStringUntil('\n');
line.trim();
Serial.print("Status line : ");
Serial.println(line);
while (client.available())
{
line = client.readStringUntil('\n');
line.trim();
Serial.print("Line : ");
Serial.println(line);
}
-
I tried this suggestion and still get no data back. I see the following: 17:04:36.537 -> Starting connection to server: 10.196.1.163 InfluxDB Garden DB... 17:04:36.582 -> connected to server 17:04:37.558 -> Status line : 17:04:37.558 ->rlgjr562– rlgjr5622022年01月15日 23:46:32 +00:00Commented Jan 15, 2022 at 23:46