Making looped HTTP POST requests with Arduino UNO

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!

Not having knowledge in this matter I look at the info You provide. Well done posting code using code tags.
However, post the entire code while waiting for the better helpers. Often details You don't think of are important.

Not really an answer, but potentially a clue. A few years ago I too was trying to send repeated http requests (in that case via an Ethernet attachment to an Arduino Nano).

I found similar kinds of unreliability until I changed the code to explicitly read the response returned by the server (which I then discarded; I didn't need it).

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.