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 sucessful. 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 behaviour 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 behaviour can be observed when connecting using the matlab support package for EV3 and bluetooth
  • similar behaviour 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

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()
  • 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 '18 at 11:35

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

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