我正在使用 Raspberry Pi Zero W。在 Pi 启动时,我已成功将 Pi 连接到我的 Android 设备。然后我打开互联网共享以确保我的 Pi 有互联网连接。如果可能的话,我想制作一个可以从 Android 设备接收数据并基于它运行预先存在的脚本而不使用 ssh 的应用程序。
我通常在我的安卓手机上使用 Juice SSH 在 Pi 上运行脚本,但这涉及手动工作,例如查找和执行我不希望我的用户执行的脚本。
我要运行的脚本是 Google Directions Python 脚本。我已经准备好脚本,它只需要用户输入 Origin 和 Destination。之后,它获取方向响应并开始在连接到 Pi 的屏幕上显示指令。
TLDR:我想知道一种从通过蓝牙连接的 Android 设备在 Raspberry Pi 上启动 python 脚本的方法。我需要做一个服务器吗?是否可以使用 Firebase?
最佳答案
不久前我实际上安装了一些非常相似的东西。您可以通过各种方式解决问题,但我认为无论如何都需要某种服务器
。
看看at my public repository in github !
git clone https://github.com/NanoSpicer/XpressShutdown
然后您可以像这样修改我的 index.js
文件:
#!/usr/bin/node
const command = 'python yourscript.py';
const proc = require('child_process');
const express = require('express');
const app = new express();
const router = express.Router();
router.get('/customComand', (request, response) => {
// you could even catch parameters with this, edit your command string and pass them into the script
proc.exec(command, (err, stdout, stderr) => {
response.json({output: stdout});
});
});
app.use('/raspi', router);
app.listen(80);
console.log('Server is running');
启动该服务器并作为后台进程运行:
chmod +x index.js
./index.js & # 你可以这样做是因为 shebang
使
HTTP
请求像http://{your-raspi-IP-address}/raspi/customComand
现在,如果您可以向您的 raspi 执行 http
请求,您就可以在世界任何地方运行您的命令!
关于android - 如何通过蓝牙连接从 Android 应用程序在 Raspberry Pi 上运行脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54176463/