Page 73 - Computing Book 8
P. 73

Programming Robots and Single-Board Computers                                        Class 8


               Bumble Pi Programming:

               Enter the following code in Python IDLE and save the file with the name of SRC:

               from Bluetooth import *
                                                Including the libraries!!
               import RPi.GPIO as GPIO

               GPIO.setmode(GPIO.BCM)
               GPIO.setwarnings(False)          Addressing the GPIO Pins as per
               GPIO.setup(16,GPIO.OUT)          Pi Shield configuration

               MotorL1=21
               MotorL2=18
               enA=20                             Creating variables for motor
                                                  functionality
               MotorR1=26
               MotorR2=12
               enB=19

               GPIO.setup(MotorL1,GPIO.OUT)
               GPIO.setup(MotorL2,GPIO.OUT)
               GPIO.setup(enA,GPIO.OUT)

               GPIO.setup(MotorR1,GPIO.OUT)
               GPIO.setup(MotorR2,GPIO.OUT)
               GPIO.setup(enB,GPIO.OUT)            Initializing the output pins

               GPIO.output(MotorL1,False)
               GPIO.output(MotorL2,False)
               GPIO.output(MotorR1,False)
               GPIO.output(MotorR2,False)

               p=GPIO.PWM(enA,1000)
               p.start(75)
                                                   Setting Up the speed of the Motors
               q=GPIO.PWM(enB,1000)
               q.start(75)

               server_sock=BluetoothSocket(RFCOMM)
               port =1
               server_sock.bind(("",port))
               server_sock.listen(1)
               port=server_sock.getsockname()[1]

               GPIO.output(16,True)
               uuid ="94f39d29-7d6d-437d-973b-fba39e49d4ee"
               advertise_service(server_sock,"SampleServe",          Activating the on-board Bluetooth
                                 service_id=uuid,
                                 service_classes=[uuid,SERIAL_PORT_CLASS],
                                 profiles=[SERIAL_PORT_PROFILE],
                                 )
               client_sock,client_info=server_sock.accept()
               print("Accept connection",client_info)




               The City School /Academics/Computing Curriculum/Class 8/2020-2021                Page 72 of 75
   68   69   70   71   72   73   74   75   76   77   78