[英]How to run a script on Raspberry Pi from Android App over Bluetooth Connection
I am using a Raspberry Pi Zero W. I have succeeded in connecting the Pi to my Android Device on startup of Pi. 我正在使用Raspberry Pi ZeroW。在启动Pi时,我已成功将Pi连接到我的Android设备。 Then I turn on Internet Sharing to make sure my Pi has an internet connection. 然后,打开Internet共享以确保我的Pi可以连接互联网。 I want to make an application which can receive data from Android Device and run preexisting scripts based on it without using ssh, if possible. 我想制作一个可以从Android设备接收数据并基于它运行预先存在的脚本而不使用ssh的应用程序(如果可能)。
I normally use Juice SSH on my android phone to run scripts on the Pi but that involves manual work like finding and executing the script which I do not want my user to do. 我通常在Android手机上使用Juice SSH在Pi上运行脚本,但是这涉及诸如查找和执行脚本的手动工作,而我不希望用户这样做。
The script I want to run is a Google Directions Python Script. 我要运行的脚本是Google Directions Python脚本。 I have the script ready, it just takes input of Origin and Destination from the user. 我已经准备好脚本,它只需要用户输入Origin和Destination。 After that it fetches the Direction Response and starts showing instructions on a screen connected to the Pi. 之后,它将获取方向响应并开始在连接到Pi的屏幕上显示指令。
TLDR: I would like to know a way to initiate a python script on a Raspberry Pi from an Android Device connected via Bluetooth. TLDR:我想知道一种通过蓝牙连接的Android设备在Raspberry Pi上启动python脚本的方法。 Do I need to make a server? 我需要做一个服务器吗? Is it possible using Firebase? 可以使用Firebase吗?
I actually mounted something very similar not too long ago. 实际上,不久前我安装了非常类似的东西。 You may be able to get around various ways but I think some sort of server
is going to be needed any way. 您也许可以解决各种问题,但是我认为无论哪种方式都需要某种server
。
Take a look at my public repository in github ! 看看我在github中的公共存储库 !
git clone https://github.com/NanoSpicer/XpressShutdown
You could then modify my index.js
file like so: 然后,您可以像这样修改我的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');
Get that server up and running as a background process with: 使用以下命令使该服务器启动并作为后台进程运行:
chmod +x index.js
./index.js & # you can do this because of the shebang
Make the HTTP
request like http://{your-raspi-IP-address}/raspi/customComand
发出HTTP
请求,例如http://{your-raspi-IP-address}/raspi/customComand
And now you could run your command wherever in the world if you can perform an http
request to your raspi! 现在,如果您可以对raspi执行http
请求,则可以在世界任何地方运行命令!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.