简体   繁体   English

如何通过蓝牙连接从Android App在Raspberry Pi上运行脚本

[英]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中的公共存储库

  1. 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');
  1. 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

  2. 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请求,则可以在世界任何地方运行命令!

I solved this problem by using the Jsch Library for Android. 我通过使用Android的Jsch库解决了这个问题。 It is quite simple and well documented. 它非常简单并且有据可查。 It allows me to start a SSH connection with a set command that I want to execute on the Server. 它允许我使用要在服务器上执行的set命令启动SSH连接。

暂无
暂无

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

相关问题 通过蓝牙将树莓派2的字符串变量发送到android应用 - send string variable from raspberry pi 2 to android app over bluetooth 如何使用 Android 应用程序通过蓝牙连接到 Raspberry pi - How to connect to Raspberry pi with an android app over bluetooth 我如何通过蓝牙在 ANDROID 应用程序和 Raspberry Pi 之间发送/接收数据? - How can i send/receive data over bluetooth between ANDROID app and Raspberry Pi? 如何构建Android蓝牙服务器应用程序以处理使用pybluez从Raspberry Pi发送的数据 - How to build Android Bluetooth server app handling data sent from Raspberry Pi using pybluez 通过Wifi将数据从Android App发送到Raspberry Pi - Sending data over Wifi from Android App to Raspberry Pi 通过蓝牙从Raspberry Pi(使用python)发送字节到java android应用程序 - sending bytes from Raspberry Pi (using python) to a java android application over bluetooth Raspberry Pi3和android app蓝牙发送消息但不接收 - Raspberry Pi3 and android app bluetooth sending messages but not receiving 如何通过蓝牙将Android应用程序连接到Raspberry Pi,以发送数字文本文件? - How can I connect an Android app to Raspberry Pi, via Bluetooth, in order to send a text file of numbers? 如何在树莓派和Android之间建立连接? - How to establish connection between raspberry pi and android? 通过蓝牙从Android中的Raspberry Pi接收数据 - Receiving data from raspberry pi in android via bluetooth
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM