How to Connect Laravel with a Raspberry Pi
Web developement Publié le 16 Sep 2025

How to Connect Laravel with a Raspberry Pi

Laravel is one of the most popular PHP frameworks for building modern web applications, while the Raspberry Pi is a versatile, low-cost microcomputer used for IoT and automation projects. Combining them allows you to create powerful dashboards, control devices remotely, or collect sensor data directly from your Pi into your Laravel app.

Why Connect Laravel and Raspberry Pi?

  • IoT Dashboards – Display temperature, humidity, or sensor readings on a web interface.
  • Remote Device Control – Send commands from Laravel to the Pi to control GPIO pins, LEDs, motors, or other peripherals.
  • Automation & Notifications – Trigger alerts or run tasks based on sensor data.

Typical Architecture

You don’t run Laravel directly on the Raspberry Pi for heavy apps (the Pi’s CPU/RAM is limited). Instead:

  1. Laravel App on a Server/Cloud
    • Hosts your main web app, API, and database.
  2. Raspberry Pi on Local Network
    • Runs lightweight scripts (Python, Node.js, or PHP CLI) to communicate with your Laravel app.
  3. REST API / MQTT
    • The Laravel app exposes endpoints; the Pi sends/receives data using HTTP requests or MQTT messages.

Step-by-Step: Connecting Laravel to Raspberry Pi

1. Prepare Your Laravel App

  • Create a REST API in Laravel using routes and controllers (/api/sensors, /api/commands).
  • Use Laravel’s built-in authentication (Sanctum/Passport) to secure your endpoints.
// routes/api.php
Route::post('/sensors', [SensorController::class, 'store']);
Route::post('/commands', [CommandController::class, 'execute']);

2. Set Up Your Raspberry Pi

  • Install Raspbian OS (or Raspberry Pi OS) on your Pi.
  • Make sure it’s connected to Wi-Fi or Ethernet.
  • Install the necessary packages for making HTTP requests (e.g., curl for shell, requests for Python).

3. Send Data from the Pi to Laravel

Use Python or PHP scripts on the Pi to post data to your Laravel API:

import requests

url = "httpss://your-laravel-app.com/api/sensors"
data = {"temperature": 22.5, "humidity": 60}
headers = {"Authorization": "Bearer YOUR_TOKEN"}

response = requests.post(url, json=data, headers=headers)
print(response.status_code)

4. Receive Commands from Laravel

Your Laravel app can store “commands” in a database table (like “turn LED on”). Your Pi polls the endpoint or uses WebSockets/MQTT to get commands and execute them locally (e.g., turning GPIO pins on/off).

5. Display Data in Laravel

Use Laravel Blade or a frontend framework (Vue.js, React) to display sensor data, logs, or control interfaces on a dashboard.

Security Tips

  • Always use HTTPS when exposing your Laravel API.
  • Protect endpoints with API tokens or OAuth.
  • Consider using VPNs or firewalls if your Pi is on a private network.

Conclusion

Connecting Laravel with a Raspberry Pi unlocks a world of IoT possibilities. By building a secure API in Laravel and writing lightweight scripts on the Pi, you can easily collect sensor data, control devices, and create interactive dashboards—all with the flexibility of PHP and the power of Raspberry Pi hardware.