Just a question of effeciency -
This code works totally fine:
for (int c = 0; c < dataPoints; c++) {
if (data[c].indexOf(",") > 0) {
if (fromPC) { Serial.println(data[c]); }
channel = data[c].substring(0,data[c].indexOf(","));
value = data[c].substring(data[c].indexOf(",")+1);
String postData = channel + ":" + value;
postData = pad(postData,12);
postData.toCharArray(cv, 16);
if (fromPC) { Serial.println(cv); }
LcdString(cv);
const int httpPort = 80;
const char* host = "my.server.domain";
String url = "/emoncms/input/post.json?node=1&json={" + channel + ":" + value + "}&apikey=abcd1234";
WiFiClient client;
yield();
if (!client.connect(host, httpPort)) {
if (fromPC) { Serial.println("Server connection failed"); }
return;
}
client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");
yield();
delay(500);
while(client.available()) {
value = client.readStringUntil('\r');
if (fromPC) { Serial.println(value); }
} //while
} //if
} //for
What I'm curious about is whether there's a way to do multiple GETs or POSTs from the same connection, rather than having to close and reopen the connection each time?
I tried leaving the connection creation and "connection: close" stuff outside of the loop, but it would only execute the first URL, not any of the subsequent ones. So I was just curious if there was a way to make this more efficient from a connection perspective?