hello everyone, so what im trying to do here is sending data to a hosted dashboard website using JSON POST, the ESP32 is done sending the data (because http code is showing 200 which mean the data is successfully sended) but the issue is the website didn't receive the data properly, below are part of my ESP32 code and php code i can provide
..........................................................
//esp32
//HTTP server (websocket)
if (useWebsocket) {
server.on("/flow", handleFlowData);
server.begin();
}
}
void loop() {
if (useWebsocket) {
server.handleClient();
}
flowRate = random(1,70) / 10.0;
totalWater += flowRate / 60.0 * 2.0;
sendToDashboard();
delay(2000);
}
void sendToDashboard() {
if (WiFi.status() != WL_CONNECTED) {
Serial.println("Wifi Disconnected");
return;
}
DynamicJsonDocument doc(128);
doc["flow"] = flowRate;
doc["total"] = totalWater;
String jsonPayLoad;
serializeJson(doc, jsonPayLoad);
if (!useWebsocket) {
HTTPClient http;
http.begin(serverUrl);
http.addHeader("Content-type", "application/json");
Serial.println("Sending: " + jsonPayLoad);
int httpCode = http.POST(jsonPayLoad);
if (httpCode > 0) {
Serial.printf("HTTP Sent: %d\n", httpCode);
} else {
Serial.printf("HTTP Error: %d\n", http.errorToString(httpCode).c_str());
}
http.end();
}
else {
Serial.println("Websocket Data: " + jsonPayLoad);
}
}
void handleFlowData(){
DynamicJsonDocument doc(128);
doc["flow"] = flowRate;
doc["total"] = totalWater;
String jsonPayLoad;
serializeJson(doc, jsonPayLoad);
server.send(200, "application/json", jsonPayLoad);
}
//api php code for the website
<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json");
$data = json_decode(file_get_contents('php://input'), true);
if (!isset($data['flow']) || !isset($data['total'])) {
http_response_code(400);
die(json_encode(["error" => "Invalid data"]));
}
file_put_contents('sensor_data.json', json_encode($data));
echo json_encode(["status" => "success"]);
?>
"$data = json_decode(file_get_contents('php://input'), true); "is this part of my php code wrong? or do i miss something?