Integrating Laravel with Node Red

Created on March 14, 2023 at 12:03 pm

Integrating Laravel with Node-RED

The first step in integrating Laravel with Node-RED is to set up a RESTful API in Laravel. This API will allow Node-RED to communicate with Laravel and retrieve data from the sensor. Here’s an example of how to set up a simple RESTful API in Laravel:

  1. Create a new Laravel project using the following command:
composer create-project --prefer-dist laravel/laravel my-project
  1. Create a new controller using the following command:
php artisan make:controller SensorController
  1. Edit the SensorController file to include a method for retrieving data from the sensor:
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class SensorController extends Controller
{
    public function getData()
    {
        $temperature = // code to retrieve temperature from sensor
        $humidity = // code to retrieve humidity from sensor

        return response()->json([
            'temperature' => $temperature,
            'humidity' => $humidity,
        ]);
    }
}
  1. Set up a route in your web.php file to point to the getData method:
cssCopy codeRoute::get('/data', '[email protected]');

Once you have set up the RESTful API in Laravel, you can move on to integrating it with Node-RED.

  1. Open Node-RED and drag in an HTTP request node.
  2. Double-click the HTTP request node and configure it to point to the /data route in your Laravel project.
  3. Drag in a JSON node and connect it to the output of the HTTP request node.
  4. Double-click the JSON node and configure it to parse the JSON data.
  5. Finally, drag in a dashboard node and configure it to display the temperature and humidity data from the sensor.

Here is some sample code to get you started:

[{"id":"373f54e4.4c50e4","type":"http request","z":"ce0edc1e.98c49","name":"","method":"GET","ret":"txt","paytoqs":false,"url":"http://localhost:8000/data","tls":"","persist":false,"proxy":"","authType":"","x":260,"y":120,"wires":[["5b6d4812.b2f70c"]]},{"id":"5b6d4812.b2f70c","type":"json","z":"ce0edc1e.98c49","name":"","property":"payload","action":"","pretty":false,"x":430,"y":120,"wires":[["f94c9297.44122"]]},{"id":"f94c9297.44122","type":"ui_gauge","z":"ce0edc1e.98c49","name":"","group":"1a7a143b.2f2349","order":0,"width":0,"height":0,"gtype":"gage","title":"Temperature","label":"°C","format":"{{value}}","min":"0","max":"40","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":590,"y":120,"wires":[]},{"id":"b372cfb5.d78a8","type":"ui_gauge","z":"ce0edc1e"}]



Connecting to lzomedia.com... Connected... Page load complete