sudo apt-get install bluez
sudo apt-get install python-bluez

sudo nano /etc/systemd/system/dbus-org.bluez.service

ExecStart=/usr/lib/bluetooth/bluetoothd -C
ExecStartPost=/usr/bin/sdptool add SP

Robotics and Coding Training

 

from bluetooth import *
import time

 

while True:

        server_sock=BluetoothSocket( RFCOMM )
        server_sock.bind(("",PORT_ANY))
        server_sock.listen(1)
        print " listening "
        try:
                client_sock,address = server_sock.accept()
                print "Accepted connection from ",address

                while 1:
                        try:
                                data = client_sock.recv(1024)
#                               if len(data) == 0: break

                                print "received  %s" % data
                                x, y, z = [int(val) for val in data.split(',')]
                                print "received 1st %s" % x
                                print "received 2nd %s" % y
                                print "received 3rd %s" % z
                        except IOError:
                                print "connection disconnected"
                                break
                        except KeyboardInterrupt:
                                client_sock.close()
                                sys.exit()
        except KeyboardInterrupt:
                server_sock.close()
                sys.exit()

Raspberry PI Control Robotic car using Bluetooth

 

 

 

 

In this article I am explaining how to control robotic car using Bluetooth. Processor board used is Raspberry Pi. All version of Raspberry Pi has inbuilt Bluetooth. You can use Bluetooth dongle incase if you don’t have Bluetooth on board. 

 

Install Bluetooth remote control application from mobile app store (android or iPhone store). I am using Bluetooth Universal Pad+ and MeArm controller

If you are new to Raspberry Pi please click this link to get basic detail. Run these two command to configure Bluetooth python library for Raspberry Pi. 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

In Raspberry Pi make below changes to connect client code in Pi board to Bluetooth mobile app. Without below changes Mobile app will connect to default Bluetooth running on Pi board.

 

 

 

Edit this file

 

 

 

 

 

Basic Bluetooth Client code

This is simple client code (python script) to run on Pi board. This code captures incoming data from Mobile phone and prints on the screen. Using this simple code you will know incoming data from mobile app. According to your project design use this data and create code to handle your requirement.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Run below command to start client code on Raspberry pi in Listening mode.

 

 

 

 

 

Open mobile app(MeArm) and connect it to client code running on Raspberry pi by selecting device or adding Raspberry PI IP. 

Once connected, as you make changes in mobile app, row data will be captured at client code as shown below. 

 

 

 

 

 

 

 

 

 

 

Bluetooth Client Code to control Robot car

 

I have edited above code to create script to control simple robot car with four wheel (Multidirectional). Please click here to know simple four wheel car chassis design for multidirectional movement.  This code is used to control robot car four movements - Move Front, Back, left and right. Please edit this code according to your GPIO PIN used and Motor configuration. Please check this link for Raspberry Pi GPIO configuration and this link for Motor driver configuration on Raspberry PI.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Please let me know if you have any questions and add your comments below. Thank you for reading,

 

Please like us on Facebook.

 

#LeenaBOTcom

 

 

from bluetooth import *
import RPi.GPIO as GPIO
import time

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)


#*****************GPIO setup ***************************
GPIO.setup(3,GPIO.OUT) #Left wheel motor control
GPIO.setup(5,GPIO.OUT) #Left wheel motor control
GPIO.setup(7,GPIO.OUT) #right wheel motor control
GPIO.setup(11,GPIO.OUT) #right wheel motor control

#Motor stop/brake
GPIO.output(3,0)
GPIO.output(5,0)
GPIO.output(7,0)
GPIO.output(11,0)

 

while True:

        server_sock=BluetoothSocket( RFCOMM )
        server_sock.bind(("",PORT_ANY))

        server_sock.listen(1)
        print " listening "

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

                 while True:

                        
                        try:
                                data = client_sock.recv(1024)
                                #print "received  %s" % data
                               

                                c,data1 = [str(val) for val in data.split(':')]
                                x, y, z = [int(val) for val in data1.split(',')]
                                
                                print "c,x,y,z",c,x,y,z
                                if c==0:
                                        if x>0 and y>0:    # Move front - Depends on your Motor assigned
                                                GPIO.output(3,0)
                                                GPIO.output(5,0)
                                                GPIO.output(7,1)
                                                GPIO.output(11,1)
                                        elif x<0 and y<0: # Move back- Depends on your Motor assigned
                                                GPIO.output(3,1)
                                                GPIO.output(5,1)
                                                GPIO.output(7,0)
                                                GPIO.output(11,0)
                                        elif x>0 and y<0: # Move right - Depends on your Motor assigned
                                                GPIO.output(3,0)
                                                GPIO.output(5,1)
                                                GPIO.output(7,1)
                                                GPIO.output(11,0)
                                        elif x<0 and y>0: # Move left - Depends on your Motor assigned
                                                GPIO.output(3,1)
                                                GPIO.output(5,0)

                                                GPIO.output(7,0)
                                                GPIO.output(11,1)
                                        else:                 # Stop 
                                                GPIO.output(3,0)
                                                GPIO.output(5,0)
                                                GPIO.output(7,0)
                                                GPIO.output(11,0)
                                                time.sleep(1)
                                elif c==1:

                        except IOError:
                                print "connection disconnected"
                                break
                        except KeyboardInterrupt:
                                client_sock.close()
                                sys.exit()
        except KeyboardInterrupt:
                server_sock.close()
                sys.exit()

 

loading...
(you must be logged in to Facebook to see comments).