sony8943
Posts: 5
Joined: Mon Apr 16, 2018 12:30 pm

bluetooth proximity car

Mon Apr 16, 2018 4:38 pm

Code: Select all

#!/usr/bin/python

import bluetooth
import time
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
RELAY = 17
GPIO.setup(RELAY, GPIO.OUT)

while True:
    print "Checking " + time.strftime("%a, %d %b %Y %H:%M:%S", 

time.gmtime())
    result = bluetooth.lookup_name('78:7F:70:38:51:1B', timeout=5)
    if (result != None):
        print "User present"
        GPIO.output(RELAY,1)
    else:
        print "User out of range"
        GPIO.output(RELAY,0)

    time.sleep(10)


Hello world i find this code on internet github and i its working fine the led is turn on when you have bluetooth on and the led is turn off when bluetooth is off

this project i want to instal in my car i have a switch for lock and unlock the doors : the problem is i have to give signal for 1 or 2 sec ( pin 17 led stay on for 2 sec when bluetooth is on) and (pin 18 led stay on for 2 sec when bluetooth is off , of course i have to put another pin gpio 18 or another pin

how to insert in code this pin 17 to stay 2 sec when bluetooth is on and
pin 18 to stay 2 sec when bluetooth is off

thankyou

pcmanbob
Posts: 3535
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: bluetooth proximity car

Tue Apr 17, 2018 7:07 am

Hi.

Adding the extra relay operation is as simple as specifying a new output and then adding the 2 outputs and a sleep command to the if and else parts of the code, like this

Code: Select all

#!/usr/bin/python

import bluetooth
import time
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
locked = 17
unlocked = 18
GPIO.setup(locked, GPIO.OUT)
GPIO.setup(unlocked, GPIO.OUT)

while True:
    print "Checking " + time.strftime("%a, %d %b %Y %H:%M:%S", 

time.gmtime())
    result = bluetooth.lookup_name('78:7F:70:38:51:1B', timeout=5)
    if (result != None):
        print "User present"
        GPIO.output(unlocked,1)
        time.sleep(2)
        GPIO.output(unlocked,0)
        
    else:
        print "User out of range"
        GPIO.output(locked,1)
        time.sleep(2)
        GPIO.output(locked,0)
        
    time.sleep(10)
    
    
untested code there may be errors

you have not said if your relay is active high or active low, so I assumed active high but you can change this if need be.
also as the code stands the relays will continue to be activated every 10 seconds on each execution of the while loop, to prevent this you will need to add some logic code so your relays only operate once for locking and unlocking..
Please only ask questions in the forum I will not answer questions sent to me directly via PM
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

sony8943
Posts: 5
Joined: Mon Apr 16, 2018 12:30 pm

Re: bluetooth proximity car

Tue Apr 17, 2018 3:53 pm

thanks pcmanbob i check i connect 2 led on in pin 17 one in pin 18 its working very good ;but is stay 2 sec open after 10 sec off just i need once to give 2 sec and stay off forever; so i need to find a timer push button to pres continuu ( or switch) and give 2 sec signal to another gpio pin

pcmanbob
Posts: 3535
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: bluetooth proximity car

Tue Apr 17, 2018 4:08 pm

What you need is to add some logic to the python program so that once a relay has been activated it will not activate again until the other has been activated.

some thing like this

Code: Select all

#!/usr/bin/python

import bluetooth
import time
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
locked = 17
unlocked = 18
GPIO.setup(locked, GPIO.OUT)
GPIO.setup(unlocked, GPIO.OUT)
rls = 0
ruls = 0

while True:
    print "Checking " + time.strftime("%a, %d %b %Y %H:%M:%S", 

time.gmtime())
    result = bluetooth.lookup_name('78:7F:70:38:51:1B', timeout=5)
    if (result != None):
        print "User present"
        if ruls = 0:
            GPIO.output(unlocked,1)
            time.sleep(2)
            GPIO.output(unlocked,0)
            ruls = 1
            rls = 0
        
    else:
        print "User out of range"
        if rls = 0
            GPIO.output(locked,1)
            time.sleep(2)
            GPIO.output(locked,0)
            rls = 1
            ruls = 0
        
    time.sleep(10)
untested code
Please only ask questions in the forum I will not answer questions sent to me directly via PM
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

sony8943
Posts: 5
Joined: Mon Apr 16, 2018 12:30 pm

Re: bluetooth proximity car

Tue Apr 17, 2018 4:43 pm

i try but is not working
Attachments
proximity  bluetooth car.jpg
proximity bluetooth car.jpg (224.6 KiB) Viewed 177 times

User avatar
DougieLawson
Posts: 32058
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: bluetooth proximity car

Tue Apr 17, 2018 7:11 pm

Change

Code: Select all

        if ruls = 0:
to

Code: Select all

        if ruls == 0:
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3B*3, 3B+

Please post ALL technical questions on the forum. Do not send private messages.

pcmanbob
Posts: 3535
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: bluetooth proximity car

Tue Apr 17, 2018 7:56 pm

Yes there were several errors in my code but I did say it was untested.

so here is the corrected code

Code: Select all

#!/usr/bin/python

import bluetooth
import time
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
locked = 17
unlocked = 18
GPIO.setup(locked, GPIO.OUT)
GPIO.setup(unlocked, GPIO.OUT)
rls = 0
ruls = 0

while True:
    print "Checking " + time.strftime("%a, %d %b %Y %H:%M:%S", 

time.gmtime())
    result = bluetooth.lookup_name('78:7F:70:38:51:1B', timeout=5)
    if (result != None):
        print "User present"
        if ruls == 0:
            GPIO.output(unlocked,1)
            time.sleep(2)
            GPIO.output(unlocked,0)
            ruls = 1
            rls = 0
        
    else:
        print "User out of range"
        if rls == 0:
            GPIO.output(locked,1)
            time.sleep(2)
            GPIO.output(locked,0)
            rls = 1
            ruls = 0
        
    time.sleep(10)
Please only ask questions in the forum I will not answer questions sent to me directly via PM
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

sony8943
Posts: 5
Joined: Mon Apr 16, 2018 12:30 pm

Re: bluetooth proximity car

Wed Apr 18, 2018 5:38 am

thank you a lot its working fine
i am not connect relay, only 2 pnp transistor bd 138 and i try to find this python script to run automatic when i open raspberry pi with schedule program ( 22:00 to 4:00 the program will shout off) and day is (4:10 to 21:50)

sony8943
Posts: 5
Joined: Mon Apr 16, 2018 12:30 pm

Re: bluetooth proximity car

Wed Apr 18, 2018 8:11 am

and how to add extra address bluetooth

pcmanbob
Posts: 3535
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: bluetooth proximity car

Wed Apr 18, 2018 2:15 pm

sony8943 wrote:
Wed Apr 18, 2018 5:38 am
thank you a lot its working fine
i am not connect relay, only 2 pnp transistor bd 138 and i try to find this python script to run automatic when i open raspberry pi with schedule program ( 22:00 to 4:00 the program will shout off) and day is (4:10 to 21:50)
if your pi is running 24/7 your can start the program using cron so that it will start at 4:10 , to stop it you will need to do that within the program, say after a set amount of time has passed.

what have you tired to add a second Bluetooth address.
Please only ask questions in the forum I will not answer questions sent to me directly via PM
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

Return to “Automation, sensing and robotics”

Who is online

Users browsing this forum: No registered users and 8 guests