The raspberry pi (rpi) is perfect to drive home built hardware. The lego mindstorms ev3 set with its LabVIEW based software is perfect to create simple controlling programs. And this software allows to send and receive messages by bluetooth. This lead to the idea to attach a bluetooth dongle to the raspberry pi and to add some software to receive and send ev3 bluetooth messages.
Reference 1 is the LEGO® MINDSTORMS® EV3 Communication Developer Kit mentioned in this blog post (download second (smaller) file). Reference 2 is a C# project for bluetooth communication from windows pc to ev3 brick. Reference 3 is a python script to receive ev3 bluetooth messages on the raspberry pi. Reference 4 is a blog post giving a summary of the format of the ev3 bluetooth messages (which is also found in pdf of reference 1 on page 21; comment in capitals is result from sniffing; official documentation is not correct here):
bb bb mm mm tt ss ll aa aa … LL LL pp pp … with
bb bb = bytes in the message (excluding the two size bytes), little endian (2 bytes long)
mm mm = message counter
tt = 0×81 (type of message: reply not required)
ss = 0x9E (system command: write to mailbox)
ll = mailbox name length INCLUDING the \0 terminator
aa aa … = mailbox name, should be terminated with a \0
LL LL = payload length INCLUDING the , little endian
pp pp … = payload: either logical: 1 byte; or number: 4 byte, single precision float; or text: should be terminated with the \0
This tutorial shows how to manually pair bluetooth devices using the linux command line (some additional details can be found here). On the ev3 brick side the labVIEW based software has ‘bricks’ to switch bluetooth on or off and to create or delete a bluetooth connection to a named device; create and delete should not be used! See below. (If your pi has hostname mypi then it’s default bluetooth name is mypi-0). To pair use first the menu on the ev3 brick: Turn bluetooth on; (no need to turn the iPhone/iPad variant of bluetooth on); set bluetooth to visible. Then run on the raspberry pi:
bluetooth-agent 1234 &; hcitool scan; rfcomm connect hci0 aa:bb:cc:dd:ee:ff (using address displayed by hcitool scan).
The rfcomm connect command should trigger a pairing key popup on the ev3 brick display, where the number given to bluetooth-agent has to be entered. (Key 1234 should be used because time to enter the key is limited and 1234 is the default key in the ev3 brick menu.) After successful pairing visibility can be switched off again.
On the rpi /var/lib/bluetooth/…./ contains the settings for the bluetooth dongle. File config defines the bluetooth device name.
For a simple test run the program shown below on the ev3 brick and use receive.py on rpi.
(hostname of my rpi is rpico; so bluetooth-name is rpico-0; Hello … is the message and EV3Test is the ‘mailbox name’). First activate bluetooth on ev3. Then (re)start bluetooth on rpi (/etc/init.d/bluetooth restart). Now run:
receive.py -a aa:bb:cc:dd:ee:ff test1.bin (receive.py can be edited to set the bluetooth address of the ev3 brick as default)
This writes the message to test1.bin; (use mc to view the file):
To send the message test1.bin back to the ev3 brick: first run the program shown below; on rpi then run send.py:
send.py -a aa:bb:cc:dd:ee:ff test1.bin (send.py can be edited to set the bluetooth address of the ev3 brick as default)
This displays Hello world! on the screen of the ev3 brick.
Important: Bluetooth only works if connections are always initiated from rpi and never from ev3 (brick or software: do not use the bluetooth ‘software brick’ of the ev3 software to connect or disconnect!) This is the result of current testing (firmware 1.03H or unofficial firmware 1.04H with support for edimax ew-7811un wifi dongle). – Initiating the connection from ev3 brick seems to change something in the bluetooth configuration of the ev3 brick which causes errors. A possibility to get rid of these errors is to delete the directories /var/lib/bluetooth/AA:BB:CC:DD:EE:FF on both devices. (Either needs shell access to ev3 brick; possible with wifi adapter and telnet: user root with empty password. Or use the ev3 software on your computer, open the memory browser of ev3 brick and download reset_bluetooth.rbf (source: reset_bluetooth.lms) to ev3 brick and use the ev3 brick menu to run reset_bluetooth.rbf) Then reboot the ev3 brick, restart bluetooth on rpi and do again the pairing process described above. Afterwards reboot the ev3 brick again.
Instead of sending messages to the ev3 brick it is possible to use send.py to send commands (as described in reference 1 mentioned above).
send.py led9.bin; sleep 20; send.py led0.bin (the ledN.bin files, N=0,1,2…9, can be found here)
As documented here the LED on the ev3 brick is controlled by a byte pattern. For instance the file led9.bin containing the hex values
08 00 00 00 80 00 00 82 1B 09
makes the LED pulse orange and led0.bin (with 0 instead of 9 as last byte) switches the LED off; (byte code reference 1: search for LED 2 : search for opUI_WRITE, 3: page 96 of firmware development pdf).
It is even possible to transfer command sequences of byte codes with send.py. First the program has to be written in “ev3 byte code assembler language” (helloworld.lms). This has to be compiled (helloworld.rbf). Second the trailing 0x0A byte has to be removed and the header (28 bytes) has to be replaced (nn NN 00 00 80 00 00; nn = lower byte and NN = higher byte of L-2; L = size of file): helloworld.bin. A good reference for the first step is here. All files required to create the example program hello world are here (sources 1, 2, byte codes.h). The second program in this archive which allows to run a linux program from sd card has its origin here. Examples for the second step are on pages 26-31 of the LEGO® MINDSTORMS® EV3 Communication Developer Kit (mentioned in this blog post;download second (smaller) file)