Jump to content

POST Data Using ESP8266 To Framework Laravel

Irham

hi all, i have a problem that is when i tap the rfid card to the scanner the display on the arduino serial monitor ide Error on sending POST: -1, can you all help me?

 

This is my code in arduino ide using esp8266 :

 

#include <SPI.h>
#include <MFRC522.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>

#define SS_PIN D2  //--> SDA / SS is connected to pinout D2
#define RST_PIN D1  //--> RST is connected to pinout D1

MFRC522 mfrc522(SS_PIN, RST_PIN);

const char* ssid = "My WiFi";
const char* password = "MyPassword";

String content;

void setup() {
  Serial.begin(115200);
  SPI.begin();
  mfrc522.PCD_Init();
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting..");
  }
  Serial.println("");
  Serial.print("Successfully connected to : ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  Serial.println("Please tag a card or keychain to see the UID !");
  Serial.println("");
}

void loop () {
  if (WiFi.status() == WL_CONNECTED) {
    if ( ! mfrc522.PICC_IsNewCardPresent())
    {
      return;
    }
    if ( ! mfrc522.PICC_ReadCardSerial())
    {
      return;
    }
    Serial.println();
    Serial.print("UID tag :");
    content = "";
    byte letter;

    for (byte i = 0; i <mfrc522.uid.size; i++)
    {
      Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? "0" : "");
      Serial.print(mfrc522.uid.uidByte[i], HEX);
      content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? "0" : ""));
      content.concat(String(mfrc522.uid.uidByte[i], HEX));
    }
    content.toUpperCase();
    Serial.println();
    kirim();
  } else {
    Serial.println("Error in WiFi connection");
  }
}

void kirim()
{
  HTTPClient http;    //Declare object of class HTTPClient
 
  String ValueSend, postData; //Read Analog value of LDR
  ValueSend = String(content);   //String to interger conversionapp
 
  //Post Data
  postData = "uid=" + ValueSend;

  http.begin("http://MyIP:8000/perpustakaan-smk-bhakti-anindya/routes/web.php/UIDresult");  //:80 is port laravel, UIDresult is the url I created to point to the controller

  http.addHeader("Content-Type", "application/x-www-form-urlencoded");    //Specify content-type header
 
  int httpCode = http.POST(postData);   //Send the request
  String payload = http.getString();    //Get the response payload

  //Serial.println("uid=" + ValueSend);
  if (httpCode > 0)
  {
    Serial.println(payload);
  } else
  {
    Serial.print("Error on sending POST: ");
    Serial.println(httpCode);
  }
  delay(2000);
  http.end();  //Close connection
}

 

And this my controller laravel :

 

<?php

namespace App\Http\Controllers;

use App\Models\UidEntry;

use App\Http\Requests\StoreUidEntryRequest;

use App\Http\Requests\UpdateUidEntryRequest;

use Illuminate\Support\Facades\DB;

class UidEntryController extends Controller

{

    /**

     * Display a listing of the resource.

     *

     * @return \Illuminate\Http\Response

     */

    public function index()

    {

        return view('GetUID/datauid', [

            "uidentry" => UidEntry::all()

        ]);

    }

    /**

     * Show the form for creating a new resource.

     *

     * @return \Illuminate\Http\Response

     */

    public function create()

    {

        return view('register/index');

    }

    /**

     * Store a newly created resource in storage.

     *

     * @param  \App\Http\Requests\StoreUidEntryRequest  $request

     * @return \Illuminate\Http\Response

     */

    public function store(StoreUidEntryRequest $request)

    {

        $uid = $request->post('uid');

        $values = array("uid" => $uid);

        DB::table('uid_entries')->insert($values);

        // if (null !== $request->get('uid')) {

        //     $uid = $request->get('uid');

        //     dd($uid);

        //     $values = array('uid' => $uid);

        //     $result = DB::table('uid_entries')->insert($values);

        //     // return response($result);

        // }

        //validate form

        // $this->validate($request, [

        //     'uid'     => 'required'

        // ]);

        // //upload image

        // $entry = str($_POST['uid']);

        // $image = $request->$entry;

        // //create post

        // UidEntry::create([

        //     'uid'     => $image

        // ]);

       

            // $validatedData = $request->validate([

            //     'uid' => 'required'

            // ]);

   

            // UidEntry::create($validatedData);

    }

    /**

     * Display the specified resource.

     *

     * @param  \App\Models\UidEntry  $uidEntry

     * @return \Illuminate\Http\Response

     */

    public function show(UidEntry $uidEntry)

    {

        //

    }

    /**

     * Show the form for editing the specified resource.

     *

     * @param  \App\Models\UidEntry  $uidEntry

     * @return \Illuminate\Http\Response

     */

    public function edit(UidEntry $uidEntry)

    {

        //

    }

    /**

     * Update the specified resource in storage.

     *

     * @param  \App\Http\Requests\UpdateUidEntryRequest  $request

     * @param  \App\Models\UidEntry  $uidEntry

     * @return \Illuminate\Http\Response

     */

    public function update(UpdateUidEntryRequest $request, UidEntry $uidEntry)

    {

        //

    }

    /**

     * Remove the specified resource from storage.

     *

     * @param  \App\Models\UidEntry  $uidEntry

     * @return \Illuminate\Http\Response

     */

    public function destroy(UidEntry $uidEntry)

    {

        //

    }

}

 

and this is my model laravel :

 

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class UidEntry extends Model
{
    use HasFactory;

    // protected $guarded = ['id'];

    protected $fillable = [
        'uid'
    ];

    // bisa insert data di database tanpa colum updated_at
    public $timestamps = false;
}

 

and the last, this is route in web.php laravel :

 

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UidEntryController;

/*

|--------------------------------------------------------------------------

| Web Routes

|--------------------------------------------------------------------------

|

| Here is where you can register web routes for your application. These

| routes are loaded by the RouteServiceProvider within a group which

| contains the "web" middleware group. Now create something great!

|

*/

// Route::get('/', function () {

//     return view('welcome');

// });

Route::resource('UIDresult', UidEntryController::class);

 

dfdf

Link to comment
Share on other sites

Link to post
Share on other sites

I'm not a php programmer, but have you tried doing a post request with some other tool like postman before trying it in your esp?

Make sure that your code works properly before trying to do stuff in the esp.

FX6300 @ 4.2GHz | Gigabyte GA-78LMT-USB3 R2 | Hyper 212x | 3x 8GB + 1x 4GB @ 1600MHz | Gigabyte 2060 Super | Corsair CX650M | LG 43UK6520PSA
ASUS X550LN | i5 4210u | 12GB
Lenovo N23 Yoga

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×