In the backend I am trying to get the data from post request sent by esp32. But the value return null for every request. I tested the backend using postman to find out if there is any wrong and I found out that data is uploading successfully using postman.
But the same code is not working for esp32. In arduino ide I wrote the following code:
Code: Select all
#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "xxxxx";
const char* password = "xxxxxxx";
#define LED_BUILTIN 2
void setup() {
Serial.begin(115200);
delay(4000); //Delay needed before calling the WiFi.begin
pinMode(LED_BUILTIN,OUTPUT);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) { //Check for the connection
delay(1000);
Serial.println("Connecting to WiFi..");
}
Serial.println("Connected to the WiFi network");
}
void loop() {
digitalWrite(LED_BUILTIN,HIGH);
delay(1000);
digitalWrite(LED_BUILTIN,LOW);
delay(1000);
if(WiFi.status()== WL_CONNECTED){ //Check WiFi connection status
HTTPClient http;
http.begin("http://lara.nemanufacturer.com/arduino"); //Specify destination for HTTP request
http.addHeader("Content-Type", "application/json");
int httpResponseCode = http.POST("{\"floor\":\"22\",\"switch\":\"7\",\"status\":\"1\"}");
if(httpResponseCode>0){
String response = http.getString(); //Get the response to the request
Serial.println(httpResponseCode); //Print return code
Serial.println(response);
Serial.println(WiFi.localIP());//Print request answer
}else{
Serial.print("Error on sending POST: ");
Serial.println(httpResponseCode);
}
http.end(); //Free resources
}else{
Serial.println("Error in WiFi connection");
}
delay(1000); //Send a request every 10 seconds
}
Code: Select all
public function postData(Request $request){
$data = json_decode(array_first($request->all()),true);
$floor =$data['floor'];
$switch =$data['switch'];
$status =$data['status'];
$p = ['floor'=>$floor,'switch'=>$switch,'status'=>$status];
$p = serialize($p);
DB::insert('insert into arduino (body) values (?)', [$p]);
return $status;
}