简体   繁体   English

将 json 从 raspberry Pi4 发送到 Laravel 并查看

[英]Send json from raspberry Pi4 to Laravel and view it

I need to send a JSON file from a raspberry (programmed in python) to Laravel.我需要将一个 JSON 文件从树莓派(用 Python 编程)发送到 Laravel。 Looking on the internet I found this function for python:在互联网上我找到了python的这个函数:

https://docs.python-requests.org/en/latest/ https://docs.python-requests.org/en/latest/

but when I go to view the JSON file on Laravel nothing appears.但是当我在 Laravel 上查看 JSON 文件时,什么也没有出现。

The code in python is this: python中的代码是这样的:

def readFile():
    with open("/home/pi/Desktop/Progetti SIoTD/Bluetooth/device.txt", "r") as file:
        for i in file:
            line, *lines = i.split()
            if line in mac_dict:
                mac_dict[line] += lines
            else:
                mac_dict[line] = lines
    print(mac_dict)
    print("\n")

    json_obj = json.dumps(mac_dict, indent=4)
    with open("/home/pi/Desktop/Progetti SIoTD/Bluetooth/mac_addr.json", "w") as json_file:
        json_file.write(json_obj)

    r = requests.get(ip, data=json_obj, headers=headers)
    print(r.text)

I use this function to read a txt file (containing the various Bluetooth MAC addresses and their respective RSSI values) and then transform it into JSON.我使用这个函数读取一个 txt 文件(包含各种蓝牙 MAC 地址及其各自的 RSSI 值),然后将其转换为 JSON。 Now I wanted to understand how to send it to Laravel and display something现在我想了解如何将它发送到 Laravel 并显示一些内容

The function in Laravel is this: Laravel 中的函数是这样的:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class DictionaryController extends Controller
{
    public function index()
    {
        return view('backend.auth.user.dictionary');
    }

    public function store(Request $request)
    {
        return $request;
    }

    public function show($id)
    {
        //
    }

    public function update(Request $request, $id)
    {
        //
    }

    public function destroy($id)
    {
        //
    }
}

The route in laravel is in the api.php: laravel 中的路由在 api.php 中:

Route::get('dictionary', [DictionaryController::class, 'store'])->name('dictionary');

I hope to have explained well and that someone can help me, I am stuck for several days on this.我希望已经解释得很好并且有人可以帮助我,我在这方面被困了好几天。 I also need to know how to use php/blade on Laravel to display the JSON file (if it is possible)我还需要知道如何在 Laravel 上使用 php/blade 来显示 JSON 文件(如果可能的话)

Thanks in advance提前致谢

You could use curl library to send data or files to any type of server.您可以使用 curl 库将数据或文件发送到任何类型的服务器。 curl on linux server is like a light client side without showing user interface but It is enough to send or receive data whatever is the target. linux 服务器上的 curl 就像一个轻客户端,没有显示用户界面,但是无论目标是什么,它都足以发送或接收数据。 Her is a url example about how to upload a file from your local device to server.她是一个关于如何将文件从本地设备上传到服务器的 url 示例。 https://medium.com/@petehouston/upload-files-with-curl-93064dcccc76#:~:text=To%20upload%20files%20with%20CURL,%2Ddata%E2%80%9D%20to%20the%20request . https://medium.com/@petehouston/upload-files-with-curl-93064dcccc76#:~:text=To%20upload%20files%20with%20CURL,%2Ddata%E2%80%9D%20to%20the%20request .

I use this function to read a txt file (containing the various Bluetooth MAC addresses and their respective RSSI values) and then transform it into JSON.我使用这个函数读取一个 txt 文件(包含各种蓝牙 MAC 地址及其各自的 RSSI 值),然后将其转换为 JSON。 Now I wanted to understand how to send it to Laravel and display something现在我想了解如何将它发送到 Laravel 并显示一些内容

I assume that your writing JSON via Python has worked.我假设您通过 Python 编写 JSON 已经奏效。 So, what you're asking is how Laravel can see JSON file.所以,您要问的是 Laravel 如何查看 JSON 文件。

class DictionaryController extends Controller
{
    public function index()
    {
        $json = file_get_contents('/home/pi/Desktop/Progetti SIoTD/Bluetooth/mac_addr.json');
        return response()->json($json);
    }

    ...

Route :路线 :

Route::get('dictionary', [DictionaryController::class, 'index'])->name('dictionary.index');

Or if you want to work with template, you can use:或者,如果您想使用模板,可以使用:

class DictionaryController extends Controller
{
    public function index()
    {
        $json = '/home/pi/Desktop/Progetti SIoTD/Bluetooth/mac_addr.json';
        $data['macs'] = json_encode($json, true);

        return view('backend.auth.user.dictionary', $data);
    }

    ...

And then you can process it however you want on the Blade.然后你可以在 Blade 上随意处理它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM