user71845: I set my code to if it executes HTTP.POST, the count will reset / back to zero, but I noticed that even it returns other http code than 201 (-1 or 500), the count still resets.
How to put a condition here that if httpResponseCode = 201 (successful), then reset (total = 0;) else http.end().
Thanks!
void loop () {
  if ((millis() - lastTime) > timerDelay) {
    if (WiFi.status() == WL_CONNECTED) {
      HTTPClient http;
      http.begin(serverName);

      http.addHeader("Content-Type", "application/json");
      char temp[32];
      snprintf(temp, 32, "{\"amount\":\"%.2f\"}", total);
      int httpResponseCode = http.POST(temp);
     
      Serial.print("HTTP Response code: ");
      Serial.println(httpResponseCode);
      Serial.println(total);
      total = 0;
        
      http.end();
    }

void count () {
  total += rainrate;
}


user2881: You mean something like this?
if( httpResponseCode==201 )
  total = 0;

Your entire code would then become:
void loop () {

  if ((millis() - lastTime) > timerDelay) {

    if(WiFi.status()== WL_CONNECTED){
      HTTPClient http;
      
      http.begin(serverName);

      http.addHeader("Content-Type", "application/json");

      char temp[32];
      snprintf(temp, 32, "{\"amount\":\"%.2f\"}", total);
      int httpResponseCode = http.POST(temp);

     
      Serial.print("HTTP Response code: ");
      Serial.println(httpResponseCode);
      Serial.println(total);

      if( httpResponseCode==201 )
        total = 0;
        
      http.end();
    }

void count () {
 total += rainrate;
}



71845: <p>I set my code to if it executes HTTP.POST, the count will reset / back to zero, but I noticed that even it returns other http code than 201 (-1 or 500), the count still resets.</p>
<p>How to put a condition here that <code>if httpResponseCode = 201 (successful), then reset (total = 0;) else http.end().</code></p>
<p>Thanks!</p>
<pre class="lang-cpp prettyprint-override"><code>void loop () {
  if ((millis() - lastTime) &gt; timerDelay) {
    if (WiFi.status() == WL_CONNECTED) {
      HTTPClient http;
      http.begin(serverName);

      http.addHeader(&quot;Content-Type&quot;, &quot;application/json&quot;);
      char temp[32];
      snprintf(temp, 32, &quot;{\&quot;amount\&quot;:\&quot;%.2f\&quot;}&quot;, total);
      int httpResponseCode = http.POST(temp);
     
      Serial.print(&quot;HTTP Response code: &quot;);
      Serial.println(httpResponseCode);
      Serial.println(total);
      total = 0;
        
      http.end();
    }

void count () {
  total += rainrate;
}
</code></pre>
 2881: <p>You mean something like this?</p>
<pre><code>if( httpResponseCode==201 )
  total = 0;
</code></pre>
<p>Your entire code would then become:</p>
<pre class="lang-cpp prettyprint-override"><code>void loop () {

  if ((millis() - lastTime) &gt; timerDelay) {

    if(WiFi.status()== WL_CONNECTED){
      HTTPClient http;
      
      http.begin(serverName);

      http.addHeader(&quot;Content-Type&quot;, &quot;application/json&quot;);

      char temp[32];
      snprintf(temp, 32, &quot;{\&quot;amount\&quot;:\&quot;%.2f\&quot;}&quot;, total);
      int httpResponseCode = http.POST(temp);

     
      Serial.print(&quot;HTTP Response code: &quot;);
      Serial.println(httpResponseCode);
      Serial.println(total);

      if( httpResponseCode==201 )
        total = 0;
        
      http.end();
    }

void count () {
 total += rainrate;
}

</code></pre>
