jped
jped

Reputation: 486

Bluetooth with raspberry pi

I am trying to get my ardunio to communicate with my raspberry pi through bluetooth. As of now I have gotten them to communicate using Minicomm, but I have not been successful with pyserial. I have tried countless things but I can not get it to work; A few things I know for sure (from minicomm and other stuff):

  1. There is a connection
  2. The serial port it /dev/rfcomm0
  3. The code on the arduino is working
  4. The BPS is 9600

Here is the code I have on my pi

    import serial
    import time
    port="/dev/rfcomm0"
    print('hello world')
    bluetooth= serial.Serial(port,9600)
    print ('hello world 2')
    bluetooth.flushInput()
    print ('hello world 3')
    for i in range(100):
        print("we are in the for loop",i)
        inputs=bluetooth.readline()
        print("we are in the inputs for loop",i)
        inputasinteger= int(inputs)
        if inputs:
                print('we have inputs')
                fileb= open("blue.txt",'wU')
                fileb.write(inputasInteger*10)
        time.sleep(.1)
        print('sleeping')
    fileb.close()
    print('file has been closse')
    exit()

You could assume that the indents are correct... I am not to sure how to fix them here But my code runs till the line inputs=bluetooth.readline(); then it just hangs Does anyone have any experience with this? Any solutions? Know of any other modules I could use?

Upvotes: 0

Views: 4358

Answers (1)

Sam Barani
Sam Barani

Reputation: 291

Are you sure that the Arduino side is sending a newline ("\n") character? The code will hang unless it recieves a newline character. See the pySerial documentation for readline(): pySerial API

Read a line which is terminated with end-of-line (eol) character (\n by default) or until timeout.

If you are sure the Arduino is sending a newline, post the code from the Arduino here.

Upvotes: 2

Related Questions