Help me For THESIS PURPOSES

How to post the readings from the sensors to the API because it returns http code 400

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>

const char* ssid = "";
const char* password = "";
//Your Domain name with URL path or IP address with path
const char* serverName = "";
const byte interruptPin = 4;
const int interval = 500;
volatile unsigned long tiptime = micros();
static float rainrate;
//float totalrainrate = 0.3;
void ICACHE_RAM_ATTR count();

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastTime = 0;
// Timer set to 10 minutes (600000)
//unsigned long timerDelay = 600000;
// Set timer to 5 seconds (5000)
unsigned long timerDelay = 15000;

void setup() {
  Serial.begin(115200);

  WiFi.begin(ssid, password);
  Serial.println("Connecting");
  while(WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to WiFi network with IP Address: ");
  Serial.println(WiFi.localIP());
 
  Serial.println("Timer set to 5 seconds (timerDelay variable), it will take 5 seconds before publishing the first reading.");

    // Set up our digital pin as an interrupt
  pinMode(interruptPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(interruptPin), count, RISING);
}

void loop() {

  wifi();
  rain();
}
void rain() {
unsigned long curtime = micros();
  
  // 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;
  
  // Calculate mm/hr from period between cup tips

 Serial.print("Rain rate: ");
 Serial.print((float) rainrate * 0.1);
 //Serial.println("mm/hr");
 delay (1000);
}

void count() {
 
  rainrate++;
 
 
}

void wifi() {
  //Send an HTTP POST request every 10 minutes
  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\":  \" rainrate\"  }" );
      
   
     
      Serial.print("HTTP Response code: ");
      Serial.println(httpResponseCode);
      
      
        
      // Free resources
      http.end();
    }
    else {
      Serial.println("WiFi Disconnected");
    }
    lastTime = millis();
  }
}

What do you think the error of this code ?

thesispurposes:
What do you think the error of this code ?

What do you think about the 'error' that you have kept secret ?

You are incrementing rainrate in your interrupt. You need to use the volatile keyword when declaring rainrate. Also, since rainrate is a multibyte variable, you need to disable interrupts when you manipulate it outside of an interrupt, otherwise you will get corrupted values.

I'm not sure about the http code 400 but I would suggest verifying the rest of the code using the serial console before trying to send it in http.

 int httpResponseCode = http.POST( "{\"amount\" rainrate }" );

I think this is where the problem is comming from what do you think? is my JSON format is correct ?

Have you tried running some simple example sketches to confirm that you have basic connectivity to the server?

int httpResponseCode = http.POST( "{\"amount\" \"15\"}" );

yes if i replace it to sample data it successfully POST a request

Oh, yeah. Strings inside "" are constants ("character string literals") in C. You can't just throw in a variable. You have to use some conversion function to express the float variable as a character string.

What kind of variable can i use ? im only beginer

@thesispurposes

Other post/duplicate DELETED
Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.

Continued cross posting could result in a time out from the forum.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

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