post data esp8266 to laravel

693 Views Asked by At

my problem is that when i tap rfid to scanner it appears on serial monitor Valet - Not found.

this is a picture when i tap the rfid card and an error appears on the arduino ide monitor serial : Valet Not Found InSerial Monitor Arduino IDE

that's my code in arduino ide :

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

#define SS_PIN D2
#define RST_PIN D1

MFRC522 mfrc522(SS_PIN, RST_PIN);

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

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;
 
  String ValueSend, postData;
  ValueSend = String(content);
 
  //Post Data
  postData = "uid=" + ValueSend;
  
  http.begin("http://that's my IP:80/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");
 
  int httpCode = http.POST(postData);
  String payload = http.getString();

  //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
}

That's my code in 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->get("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)
    {
        //
    }
}

That's my Model in 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;
}

sorry if my english is bad.

1

There are 1 best solutions below

0
On

tas déjà établie un lien api pour ce controller ? comme ceci :

Route::post('/', [App\Http\Controllers\Controller::class, 'update']);