1

Short description:

I want to send commands via Bluetooth to an EV3 brick from a Linux computer. I use python to send bytecodes via Bluetooth and not any EV3-specific software Pairing the device was successful. If i send a "beep" command directly after establishing a connection, i can hear a beep from the brick. However, if i wait longer before i send the command, e.g. 2 seconds, then i can hear no beep. Any ideas how i could fix this behavior and be able to send commands many seconds after i established a connection?

Longer description:

Assuming the devices are already paired and Bluetooth enabled on the EV3 brick, i use the following setup:

To create /dev/rfcomm0, run

sudo rfcomm bind 0 <MAC-adress of the EV3>

Then, execute the following python script as root:

import time
beep_code=[0x0f,0x00,0x00,0x00,0x80,0x00,0x00,0x94,0x01,0x81,0x1,0x82,0x0b,0x02,0x82,0xf4,0x01]
# beep with 1% at 500 hz for .5 seconds
b=bytearray(beepcode)
f=open("/dev/rfcomm0","w+b",0)
f.write(b)
f.flush()
time.sleep(1.7)
f.write(b)
f.flush()

I got the bytecode for the command from here. The result is that i can hear only one beep, instead of the expected two beeps. If i make the waiting time shorter, e.g. 1 second, i can hear two beeps.

I use a computer running Kubuntu (Linux kernel 4.4). The firmware version of the brick is V1.09H, the hardware version is V0.60

Maybe i did something wrong with my Bluetooth setup on Linux, but the Bluetooth connection was successful for

Some comments and observations:

  • very similar behavior can be observed when connecting using the Matlab support package for EV3 and Bluetooth
  • similar behavior can be observed when using a motor (using Matlab)
  • sometimes it works (i.e. i hear two beeps) with a waiting time of 1.7 seconds, sometimes it fails for a shorter time (e.g. 1.5 seconds), so it is not completely deterministic
  • starting the connection with open() takes a few seconds, so it would not be possible to re-open /dev/rfcomm0 for every command
  • i cannot circumvent the problem by sending a command every 0.5 seconds. In this case, the brick still does not produce beep tones after roughly 1.7 seconds (starting from when the fopen() command finished). So it is not just a simple timeout if there is no data.
  • on the EV3 brick the Bluetooth connection is marked as "connected" until f.close() is executed or the python script has completed. The same connection status can be observed using bluetoothctl on Linux.
1
  • 1
    I know the problem is already solved, but there is a new firmware version which I think is 1.10E. Updating could also help. Apr 28, 2020 at 0:26

1 Answer 1

1

rfcomm is an old and deprecated tool, so I'm not going to try to figure out what exactly is going on. I suspect that it has to do with the fact that it treats the connection as a TTY and it is sending extra characters to the EV3 that messes up the communication protocol. I tried adding:

import tty

...

tty.setraw(f.fileno())

to the program to fix this, but it does not seem to work.

Instead, I would recommend just using Bluetooth sockets in your python program instead of using the rfcomm command line tool. Here is a working example:

#!/usr/bin/env python3

import time

import bluetooth

# change to *your* EV3 Bluetooth address
BDADDR = "00:16:53:00:00:00"
PORT = 1

# beep with 1% volume at 500 hz for .5 seconds
BEEP_CODE = b'\x0f\x00\x00\x00\x80\x00\x00\x94\x01\x81\x01\x82\x0b\x02\x82\xf4\x01'

sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
sock.connect((BDADDR, PORT))
sock.send(BEEP_CODE)
time.sleep(1.7)
sock.send(BEEP_CODE)
sock.close()
1
  • This worked for me. However, i am still looking for a solution which makes it possible to use the matlab packages, which seem to require serial communication. Thanks!
    – harfe
    May 8, 2018 at 11:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.