robintibor/python-mindwave-mobile

the program just stopped working

Opened this issue · 11 comments

As I use the api you provided, the program is stopped working after running for a while(15-20 mins.).
I still have no idea how to fix it. As I tried to track the code it shows that it was stopped on this line.
receivedBytes += self.mindwaveMobileSocket.recv(missingBytes)
in _readBytesFromMindwaveMobile of MindwaveMobileRawReader.py
I am using rasberry pi 3 B, the bluetooth is already embeded on the board.

Thank you

Hmm can you somehow get the error message?

There is no error message, the program is not crashing but it seems can not receive or waiting for the bytes from the mindwave. As I tried to set the timed out for receiving, the time out is raised instead.
I don't sure if it was the hardware problem or software problem but I've already tried 2 mindwave, the results still the same. I have not try to change the raspberry pi yet.

Hm in that case I also don't know what to do besides setting timeout and maybe then reconnecting or sth if timeout error is raised.... but no idea if that would help :/

I've already tried to disconnect and re-connect when timed out.
After reconnect, it'll be timed out again in less than a minute.
So I'm thinking if it was a hardware problem.
Which version of RaspberryPi do you use, and did you use an external bluetooth receiver?

I did not use any RasperryPi, I used my normal laptop with a usb bluetooth dongle/adapter.

@robintibor Thanks for your code so much, it helps a lot. @bbbbear We are using RaspberryPi to connect the mindwave mobile, also via the embedded bluetooth. Our previous engineer had a similar disconnecting problem, but he coded with C# with official API and used the usb dongle to connect a desktop. In my memory, he said it's an event loop monitoring, one can add a piece of blocking code or something to keep it stay in.

Before we using Raspberry, we used a STM32 board and a HC05 bluetooth model to connect mindwave mobile, which coded in C, looks like this:
image
And it worked well.

Currently, we can connect the mindwave mobile by using raspberry pi 3, but the attention and meditation data are all zero. I think we might face the disconnecting problem in the future. So, @bbbbear have you ever had this problem about zero attention and meditation?

@macheteluc No, I had never face the problem about the zero attention and meditation problem
It works well as it should except the timed out problem.

@bbbbear hi men can you giveme a hand? im trying to conect the mindwave mobile mw003 to my raspberry pi 3b but i have an error that says
Could not connect:  (52, 'Invalid exchange') ; Retrying in 5s...
Could not connect:  (77, 'File descriptor in bad state') ; Retrying in 5s...
Could not connect:  (77, 'File descriptor in bad state') ; Retrying in 5s...

And when i try to do in terminal
pi@raspberrypi:~ $ sudo rfcomm connect rfcomm0 20:68:9D:91:DA:9D
Can't connect RFCOMM socket: Invalid Exchange

Pls help that i have trying for long time and dont foun the error

@AdairValdivia Umm, it's already long time ago that I can't remember and I don't have raspberry pi now.
Did you try to connect by UI?
As I remember, I can't connect the bluetooth via PyBluez and terminal on UI when it works.
But I can't connect on UI when the terminal and PyBluez works. There might me some conflict that I might accidentally fixed.
I think that I'm familiar with bluetoothctl command when I try to make it works.

@bbbbear Did you solve the problem? I am facing it right now. I was using the same config. as you (mindwave mobile + raspberry pi 3 B). After a while, the console does not refresh as if no data is being streamed. After reconnecting the issue rises in less than a minute (approx. 30 seconds).

@gabe-sr
I did roughly solve the problem by modifying the receiving code in MindwaveMobileRawReader.py.

  • I set a receive timeout
    original
    def _connectToAddress(self, mindwaveMobileAddress): self.mindwaveMobileSocket = bluetooth.BluetoothSocket(bluetooth.RFCOMM) while (not self._isConnected): try: self.mindwaveMobileSocket.connect( (mindwaveMobileAddress, 1)) self._isConnected = True except bluetooth.btcommon.BluetoothError as error: print "Could not connect: ", error, "; Retrying in 5s..." time.sleep(5)
    modified to
    def _connectToAddress(self, mindwaveMobileAddress): err_count = 0 #self.mindwaveMobileSocket = bluetooth.BluetoothSocket(bluetooth.RFCOMM) while (not self._isConnected): try: self.mindwaveMobileSocket = bluetooth.BluetoothSocket(bluetooth.RFCOMM) self.mindwaveMobileSocket.connect( (mindwaveMobileAddress, 1)) self._isConnected = True self.mindwaveMobileSocket.settimeout(1) except bluetooth.btcommon.BluetoothError as error: err_count += 1 if err_count == 10: print ("Attempt",err_count,"of 10: Could not connect:", error, ";") return False else: print ("Attempt",err_count,"of 10: Could not connect:", error, "; Retrying in 5s...") time.sleep(5) return True
  • after the timeout was raised, disconnect and reconnect
    original
    def _readBytesFromMindwaveMobile(self, amountOfBytes): missingBytes = amountOfBytes receivedBytes = "" # Sometimes the socket will not send all the requested bytes # on the first request, therefore a loop is necessary... while(missingBytes > 0): receivedBytes += self.mindwaveMobileSocket.recv(missingBytes) missingBytes = amountOfBytes - len(receivedBytes) return receivedBytes;
    modified to
    def _readBytesFromMindwaveMobile(self, amountOfBytes): missingBytes = amountOfBytes receivedBytes = "" # Sometimes the socket will not send all the requested bytes # on the first request, therefore a loop is necessary... #print('(read)',end='') while(missingBytes > 0): #print('\n(.REQ',missingBytes,')') try: receivedBytes += self.mindwaveMobileSocket.recv(missingBytes) except: print ("\n--- recev timed out! ---",datetime.now()) #receivedBytes += ['\n']*missingBytes print("--- exit time out code ---") print("--- Close connection ---") self.close() time.sleep(1) print("--- Trying to re-connect ---") if (self._connectToAddress(self._mindwaveMobileAddress)): print("--- Connected ---") else: print('Terminating process') os.kill(os.getpid(), signal.SIGKILL) return receivedBytes #time.sleep(10) return receivedBytes #return 170 missingBytes = amountOfBytes - len(receivedBytes) #print('--(GET',len(receivedBytes),',miss',missingBytes,')--',) #print('e-read',end='') return receivedBytes;
    I have no idea how to show you what did I try to change here, so you can read the edited version in the following url.
    My modified version is here https://github.com/bbbbear/Python-Russel-Circumflex/blob/master/mindwavemobile/MindwaveMobileRawReader.py
    I think my code is not good enough but aleast it can roughly overcome the problem, I hope there is a way to write a better code.