user71845: I'm trying to POST the data of total, but it returns 400. but when I try a sample data like
int httpResponseCode = http.POST("{\"amount\":\"total\"}"); (400)
into
int httpResponseCode = http.POST("{\"amount\":\"15\"}"); it works (201).
How to make it work? Thanks!
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>

const byte interruptPin = 4;
const int interval = 500;
volatile unsigned long tiptime = millis();
volatile float rainrate = 0.3;
volatile float total;
void ICACHE_RAM_ATTR count();

void loop() {
  if ((millis() - lastTime) > timerDelay) {
    //Check WiFi connection status
    if (WiFi.status()== WL_CONNECTED) {
      HTTPClient http;
      
      // Your Domain name with URL path or IP address with path
      http.begin(serverName);

      // Specify content-type header
      http.addHeader("Content-Type", "application/json");

      // Data to send with HTTP POST
      int httpResponseCode = http.POST("{\"amount\":\"total\"}");

      Serial.print("HTTP Response code: ");
      Serial.println(httpResponseCode);
        
      // Free resources
      http.end();
    }
    else {
      Serial.println("WiFi Disconnected");
    }
    lastTime = millis();
  }
}

void count() {
  // Grab the current ms count for common calculations
  unsigned long curtime = millis();
  
  // Make sure we don't record bounces
  if ((curtime - tiptime) < interval) {
    return;
  }

  // How long since the last tip?
  unsigned long tipcount = curtime - tiptime;
  tiptime = curtime;

  total += rainrate;
  Serial.println(total);
}


user4143: Your line
int httpResponseCode = http.POST("{\"amount\":\"total\"}");

is not doing what you think it's doing. You're not placing the contents of the variable called total in the string, you're placing the word total in the string.
Instead you need to build up the string from components before sending. I'd use something like:
char temp[32];
snprintf(temp, 32, "{\"amount\":\"%.2f\"}", total);
int httpResponseCode = http.POST(temp);

That is assuming the http.POST() function will take a char * parameter. If not you may need to resort to a String:
String temp = "{\"amount\":\"";
temp += String(total, 2);
temp += "\"}";

However it is best to avoid String as it can cause heap fragmentation causing your code to become unstable over time.

71845: <p>I'm trying to POST the data of <code>total</code>, but it returns 400. but when I try a sample data like</p>
<p><code>int httpResponseCode = http.POST(&quot;{\&quot;amount\&quot;:\&quot;total\&quot;}&quot;);</code> (400)</p>
<p>into</p>
<p><code>int httpResponseCode = http.POST(&quot;{\&quot;amount\&quot;:\&quot;15\&quot;}&quot;);</code> it works (201).</p>
<p>How to make it work? Thanks!</p>
<pre class="lang-cpp prettyprint-override"><code>#include &lt;ESP8266WiFi.h&gt;
#include &lt;ESP8266HTTPClient.h&gt;
#include &lt;WiFiClient.h&gt;

const byte interruptPin = 4;
const int interval = 500;
volatile unsigned long tiptime = millis();
volatile float rainrate = 0.3;
volatile float total;
void ICACHE_RAM_ATTR count();

void loop() {
  if ((millis() - lastTime) &gt; timerDelay) {
    //Check WiFi connection status
    if (WiFi.status()== WL_CONNECTED) {
      HTTPClient http;
      
      // Your Domain name with URL path or IP address with path
      http.begin(serverName);

      // Specify content-type header
      http.addHeader(&quot;Content-Type&quot;, &quot;application/json&quot;);

      // Data to send with HTTP POST
      int httpResponseCode = http.POST(&quot;{\&quot;amount\&quot;:\&quot;total\&quot;}&quot;);

      Serial.print(&quot;HTTP Response code: &quot;);
      Serial.println(httpResponseCode);
        
      // Free resources
      http.end();
    }
    else {
      Serial.println(&quot;WiFi Disconnected&quot;);
    }
    lastTime = millis();
  }
}

void count() {
  // Grab the current ms count for common calculations
  unsigned long curtime = millis();
  
  // Make sure we don't record bounces
  if ((curtime - tiptime) &lt; interval) {
    return;
  }

  // How long since the last tip?
  unsigned long tipcount = curtime - tiptime;
  tiptime = curtime;

  total += rainrate;
  Serial.println(total);
}
</code></pre>
 4143: <p>Your line</p>
<pre class="lang-cpp prettyprint-override"><code>int httpResponseCode = http.POST(&quot;{\&quot;amount\&quot;:\&quot;total\&quot;}&quot;);
</code></pre>
<p>is not doing what you think it's doing. You're not placing the contents of the variable called <code>total</code> in the string, you're placing the <em>word</em> <code>total</code> in the string.</p>
<p>Instead you need to build up the string from components before sending. I'd use something like:</p>
<pre class="lang-cpp prettyprint-override"><code>char temp[32];
snprintf(temp, 32, &quot;{\&quot;amount\&quot;:\&quot;%.2f\&quot;}&quot;, total);
int httpResponseCode = http.POST(temp);
</code></pre>
<p>That is assuming the <code>http.POST()</code> function will take a <code>char *</code> parameter. If not you may need to resort to a <code>String</code>:</p>
<pre class="lang-cpp prettyprint-override"><code>String temp = &quot;{\&quot;amount\&quot;:\&quot;&quot;;
temp += String(total, 2);
temp += &quot;\&quot;}&quot;;
</code></pre>
<p>However it is best to avoid <code>String</code> as it can cause heap fragmentation causing your code to become unstable over time.</p>
