Trying to Upload Data to Server Using ESP32

nnnayeem
Posts: 2
Joined: Sun Aug 05, 2018 4:48 am

Trying to Upload Data to Server Using ESP32

Postby nnnayeem » Sun Aug 05, 2018 4:59 am

Hello Everyone. I am trying to send post request to a specific url using esp32 arduino core. But every time I run the code It returns 200.

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
 
}

In the backend I am using laravel 5.5. And my code:

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;

    }


ESP_Sprite
Posts: 9725
Joined: Thu Nov 26, 2015 4:08 am

Re: Trying to Upload Data to Server Using ESP32

Postby ESP_Sprite » Sun Aug 05, 2018 5:51 am

Moved to the Arduino subforum.

Who is online

Users browsing this forum: No registered users and 59 guests