xanexxxx
Posts: 9
Joined: Tue Aug 08, 2017 6:38 am

Bluetooth (Blueterm app) Help!!

Sun Aug 13, 2017 9:45 am

I found this code online:

Code: Select all

import bluetooth
import RPi.GPIO as GPIO        #calling for header file which helps in using GPIOs of PI
LED=21

GPIO.setmode(GPIO.BCM)     #programming the GPIO by BCM pin numbers. (like PIN40 as GPIO21)
GPIO.setwarnings(False)
GPIO.setup(LED,GPIO.OUT)  #initialize GPIO21 (LED) as an output Pin
GPIO.output(LED,0)

server_socket=bluetooth.BluetoothSocket( bluetooth.RFCOMM )

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

client_socket,address = server_socket.accept()
print "Accepted connection from ",address
while 1:

 data = client_socket.recv(1024)
 print "Received: %s" % data
 if (data == "0"):    #if '0' is sent from the Android App, turn OFF the LED
  print ("GPIO 21 LOW, LED OFF")
  GPIO.output(LED,0)
 if (data == "1"):    #if '1' is sent from the Android App, turn OFF the LED
  print ("GPIO 21 HIGH, LED ON")
  GPIO.output(LED,1)
 if (data == "q"):
  print ("Quit")
  break

client_socket.close()
server_socket.close()
If i type "1" in blueterm app the LED turns on, "0" and it turns off, i was wondering if you know a way for Blueterm to READ code instead? Like:

"If pin 21 input = HIGH then print "stuff" in blueterm"?

Thanks!!

User avatar
LetHopeItsSnowing
Posts: 357
Joined: Sat May 26, 2012 6:40 am
Location: UK
Contact: Website

Re: Bluetooth (Blueterm app) Help!!

Mon Aug 14, 2017 7:27 am

Out of interest - what are you trying to do?

If you want to send data to the client (the blueterm app in this instance) you can use:

Code: Select all

server_socket.send("hi")
It might also be worth putting a new line at the end:

Code: Select all

server_socket.send("hi\n")
Whether the blueterm app is listening and will write data to the screen is a different matter - test it and find out.
"am I getting slower, or is stuff more complicated; either way I now have to write it down - stuffaboutcode.com"

xanexxxx
Posts: 9
Joined: Tue Aug 08, 2017 6:38 am

Re: Bluetooth (Blueterm app) Help!!

Mon Aug 14, 2017 7:32 am

LetHopeItsSnowing wrote:
Mon Aug 14, 2017 7:27 am
Out of interest - what are you trying to do?

If you want to send data to the client (the blueterm app in this instance) you can use:

Code: Select all

server_socket.send("hi")
It might also be worth putting a new line at the end:

Code: Select all

server_socket.send("hi\n")
Whether the blueterm app is listening and will write data to the screen is a different matter - test it and find out.
Thank you i will try that!! And I'm trying to make a wireless motion detector. Here is my other thread if you're interested:

viewtopic.php?f=41&t=190348

Will report back once i tried your tips!

xanexxxx
Posts: 9
Joined: Tue Aug 08, 2017 6:38 am

Re: Bluetooth (Blueterm app) Help!!

Mon Aug 14, 2017 8:06 am

So I'm trying to make a simple script which will spam "motion detected" in blueterm as soon as i connect. Could anyone help me to write a script? Atm i have this:

Code: Select all

import bluetooth
import RPi.GPIO as GPIO        #calling for header file which helps in using GPIOs of PI
#LED=21

GPIO.setmode(GPIO.BCM)     #programming the GPIO by BCM pin numbers. (like PIN40 as GPIO21)
GPIO.setwarnings(False)
GPIO.setup(4,GPIO.OUT)  #initialize GPIO21 (LED) as an output Pin

server_socket=bluetooth.BluetoothSocket( bluetooth.RFCOMM )

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

client_socket,address = server_socket.accept()
print "Accepted connection from ",address
x = 1
while True:
  sleep(1)
  server_socket.send("MOTION DETECTED!")
  sleep(1)
client_socket.close()
server_socket.close()
But that won't really work.. I can't even connect with that haha (Yes I'm very new with Python).

User avatar
LetHopeItsSnowing
Posts: 357
Joined: Sat May 26, 2012 6:40 am
Location: UK
Contact: Website

Re: Bluetooth (Blueterm app) Help!!

Tue Aug 15, 2017 2:48 pm

Have you tested the program you have written?

What should happen is that it should run and wait for a connection.

When something (in this case the BlueTerm app) connects, the program will go into a loop sending the message to the app.

Is this what happens?
"am I getting slower, or is stuff more complicated; either way I now have to write it down - stuffaboutcode.com"

User avatar
LetHopeItsSnowing
Posts: 357
Joined: Sat May 26, 2012 6:40 am
Location: UK
Contact: Website

Re: Bluetooth (Blueterm app) Help!!

Tue Aug 15, 2017 2:50 pm

You will have to pair your Raspberry Pi and phone to allow Blue Term connect.
"am I getting slower, or is stuff more complicated; either way I now have to write it down - stuffaboutcode.com"

User avatar
LetHopeItsSnowing
Posts: 357
Joined: Sat May 26, 2012 6:40 am
Location: UK
Contact: Website

Re: Bluetooth (Blueterm app) Help!!

Tue Aug 15, 2017 2:58 pm

A module I have written "blue dot" http://bluedot.readthedocs.io/en/latest/ has a simple to use bluetooth comms api included in it, which might be easier to use.

You would have to install blue dot http://bluedot.readthedocs.io/en/latest ... arted.html, pair your Pi and your Phone [http://bluedot.readthedocs.io/en/latest ... droid.html], you could then use the btcomm api to create a simple bluetooth server.

The following code should (I havent tested it) wait for a connection and then spam a message back until it disconnects, if data is received, it will print it out to the screen:

Code: Select all

from bluedot.btcomm import BluetoothServer
from time import sleep

def data_received(data):
    print("data received : " + data)

s = BluetoothServer(data_received)

#wait for a client to connect
while s.client_connected == False:
     sleep(0.1)
     
#keep sending the message hi while the client is connected
while s.client_connected == True:
    s.send("hi\n")
    sleep(1)
"am I getting slower, or is stuff more complicated; either way I now have to write it down - stuffaboutcode.com"

Return to “Python”