0

I have 3 scripts and I want them to be listening to a bluetooth command. When the Raspberry receive a command from the bluetooth, one of the three scripts must run. For example if the Raspb recieve the command "0", the script "0" will run. Anyone could help me please to do this.

closed as off-topic by joan, Milliways, Ghanima Jun 4 at 17:56

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question does not appear to be specific to the Raspberry Pi within the scope defined in the help center." – joan, Milliways
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    The Pi seems irrelevant to any answer to this question. The question seems to be how to write software. – joan May 22 at 8:21
  • The scripts already done, just i want to run them on the Pi simultaneously. – Go Deeper May 22 at 8:25
  • Hello. Which part is giving you trouble? Listening for commands on the bluetooth interface; parsing the received commands; launching the relevant scripts? Be more specific and maybe share some details/code you've already tried. Also clarify why this is a Raspberry Pi problem and not a general linux, python or bluetooth problem: you'll get better answers that way. – Roger Jones May 22 at 12:18
  • 1
    I am not very sure this is a Raspberry Pi problem. I am working on my Raspberry to implement this. So I need to listen for commands on the bluetooth and then I must launch the relevant script. To be more specific , I am new to Raspberry environment Thats why i cannot be extremely specific in the way of asking. I apologize about that. So could you please help me implement this. – Go Deeper May 22 at 13:12
  • This question does not appear to be specific to the Raspberry Pi within the scope defined in the help center. – Ghanima Jun 4 at 17:56
2

You can do this by writing an RFCOMM socket (server) and implementing all the options you want. Here's how to proceed:

Write a script implementing an RFCOMM socket, listenning for your commands. see: Example 4-2. rfcomm-server.c. Read the example comments.If you use C programming for example, then you can use: system("bash -c Script0.sh") ... but you can also read about popen too.

Note: You can send your custom command by writing an RFCOMM client, see Example 4-3. rfcomm-client.c

If you prefer python programming, you can see for examples here: bluez-intro

pseudo code and schema:

 ----------------------------
|      BT RFCOMM SERVER      |
| if recvbuffer == command:0 |
|             popen -------------> script0.sh
 -----------------------------
               ^
               |
           command:0
               |
     -----------------------
    |   BT RFCOMM CLIENT    |
    | sendbuffer(command:0) |
     -----------------------

From the python MIT example: Example 3-2. rfcomm-server.py (with example for your question)

import bluetooth
from subprocess import Popen, PIPE

server_sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM )

port = 1
server_sock.bind(("",port))
server_sock.listen(1)

client_sock,address = server_sock.accept()
print "Accepted connection from ",address

data = client_sock.recv(1024)    
print "received [%s]" % data

# HERE MODIFICATIONS FOR PERSONNAL SYSTEM COMMANDS (not tested,
written on the moment)

fileToExec = None
if str(data).startswith("command:0"):
  fileToExec = "/tmp/script0.sh"
elif str(data).startswith("command:1"):
  fileToExec = "/tmp/script1.sh"    

if fileToExec is not None:
   process = Popen("bash %s && echo success||echo error;" % fileToExec, Shell=True, stdout=PIPE, stderr=PIPE)
   out, error = process.communicate()
   # Please read about subprocess
   print(out, error)

client_sock.close()
server_sock.close()
  • 1
    Excuse me I just saw the tag for python but I leave the answer as it is. – Ephemeral May 22 at 11:36

Not the answer you're looking for? Browse other questions tagged or ask your own question.