I am trying to make repeated POST requests (sending sensor values) with Arduino UNO Wifi rev 2. GET requests seem to be working fine. POST requests on the other hand, sometimes freeze while reading the response (buffer overflow or something?) Intermittently, I am also unable to connect to servers (sometimes it works perfectly fine). I guess there is something wrong with my connect logic.
void sendWeatherReport(){
if (status == WL_CONNECTED) {
Serial.println(F("Connecting to server..."));
if (client.connect(server, 443)) {
Serial.println(F("Connected to server."));
Serial.println(F("Getting sensor values."));
DynamicJsonDocument sensorValues = getSensorValuesAsJsonDocument();
Serial.println(F("Making POST request."));
client.println("POST /XkrWg3XRX9CGBBm3Jabj HTTP/1.1");
client.print("Host: ");
client.println(server);
client.println("Connection: close");
client.print("Content-Length: ");
client.println(measureJson(sensorValues));
client.println("Content-Type: application/json; charset=UTF-8");
client.println();
serializeJson(sensorValues, client);
Serial.println(F("Disconnecting..."));
client.stop();
}
} else {
Serial.println(F("Not connected to WiFi, unable to send weather report"));
}
}
void loop() {
// Based on the Blink without Delay example, sends weather report every 10 secs.
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
sendWeatherReport();
}
}
Would appreciate any help!