-1

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);
}
1
  • For debugging purposes, would you please try to serial output the exact strings that are sent in both cases to maybe find what's the important difference between them? You can post the results as edit to your question. Good luck!
    – Offer
    Commented Jan 22, 2021 at 10:50

1 Answer 1

5

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.

5
  • Note that his total is a float, so the format should have %f or %g. Commented Jan 22, 2021 at 17:06
  • @EdgarBonet So it is...
    – Majenko
    Commented Jan 22, 2021 at 17:10
  • If you're going to go the sprintf route, it would be better to use snprintf: char temp[32]; snprintf(temp, sizeof temp, "{. Reasons to use the old non-n version are few and far between.
    – timemage
    Commented Jan 22, 2021 at 17:41
  • I was going to but couldn't remember off hand the order of the first two arguments.
    – Majenko
    Commented Jan 22, 2021 at 18:08
  • Thanks! it worked :)
    – Giddsec
    Commented Jan 24, 2021 at 4:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.