Here is my function
host and fingerprint are defined globally
void sendUsage(String url) {
WiFiClientSecure client;
Serial.print("connecting to: ");
Serial.println(host);
//Serial.printf("Using fingerprint '%s'\n", fingerprint);
client.setFingerprint(fingerprint);
if (!client.connect(host, httpsPort)) {
Serial.println("connection failed");
return;
}
Serial.print("Accessing URL: ");
Serial.print(host);
Serial.println(url);
String body = "data=sampleData";
client.print(String("POST ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"User-Agent: ESP8266" + "\r\n" +
"Content-Type: application/x-www-form-urlencoded\r\n" +
"Content-Length: " + body.length() + "\r\n" +
"Connection: close\r\n" + "\r\n"+ body);
Serial.println("request sent");
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
Serial.println("headers received: ");
Serial.println(line);
break;
}
}
if (check if status is 200) {
Serial.println("Server received data successfully");
} else {
Serial.print("Error sending data to server: Status is ");
Serial.println(client.status());
}
Serial.println("closing connection");
delay(200);
}