Device’s Connection

Upon the initialization of RPI, the RPI will listen for connection from each device in order:

PC (Algorithm) – Using IP Socket

def init_pc_comm(self):
        #Initiate PC connection over TCP

        # Create a TCP/IP socket
        self.signalObject.signalling()
        self.signalObject.signalTime(100)
        #wait for 5 seconds before timeout

        self.conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.conn.setsockopt(socket.IPPROTO_TCP,socket.TCP_NODELAY,1)
        self.conn.bind((self.ip_address, self.port))
        self.conn.listen(1)

        #Listen for incoming connections
        print ("Listening for incoming connections from PC...")
        self.client, self.addr = self.conn.accept()
        print ("Connected! Connection address: ", self.addr)
        self.pc_is_connect = True

Bluetooth(Android) – using RFCOMM

def connect_bluetooth(self):
        # Creating the server socket and bind to port
        btport = 1

        self.signalObject.signalling()
        self.signalObject.signalTime(100)    #wait for 5 seconds before timeout
        self.server_socket = BluetoothSocket(RFCOMM)
        self.server_socket.bind(("", btport))
        self.server_socket.listen(1)    

        # Listen for requests
        self.port = self.server_socket.getsockname()[1]
        uuid = "00001101-0000-1000-8000-00805f9b34fb"

        advertise_service( self.server_socket, "BluetoothServer",
                           service_id = uuid,
                           service_classes = [ uuid, SERIAL_PORT_CLASS ],
                           profiles = [ SERIAL_PORT_PROFILE ], )

         print ("listening for requests...")
         print ("Waiting for connection on RFCOMM channel %d" % self.port)

         # Accept requests
         self.client_socket, client_address = self.server_socket.accept()
         print ("Accepted connection from ", client_address)
         self.bt_is_connected = True

Serial(Arduino) – USB COM port

def connect_serial(self):
        try:
                self.signalObject.signalling()
                self.signalObject.signalTime(100)       #wait for 5 seconds before timeout

                self.ser = serial.Serial(self.port, self.baud_rate)
                print ("Serial link connected")

        except Exception, e:
                # print "Error (Serial): %s " % str(e)
                print ("Error: Serial connection not established. Try reconnecting the serial cable and/or restart the pi")