Archive for March, 2014

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)

 Then start the program on the ev3 brick.

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)

Raspbian (current version, 2014-03) on raspberry pi comes preinstalled with oracle jdk. Using a lightweight replacement for Eclipse
java programs for lego mindstorm ev3 with lejos firmware can  be developed on the raspberry pi.

This tutorial was used to get started. More about IDE Geany can be found on www.geany.org.

apt-get install geany

apt-get install ant

apt-get install libjsch-java

cd /usr/share/ant

ln -s /usr/share/java/jsch-0.1.42.jar jsch.jar

 

assuming ev3 classes are installed with eclipse as described in lejos.org wiki:

On raspberry pi create a subdirectory ev3 in your geany projects folder (e.g. /home/pi/Projekte/ev3 in german localized version of geany)

cd into this directory and use scp to copy DBusJava and ev3classes from eclipse ev3 folder on your computer with eclipse:

scp -r your_ip_address:/your_eclipse_ev3/folder/DBusJava .
scp -r your_ip_address:/your_eclipse_ev3/folder/ev3classes .

Open geany and create a new project ev3. Open the file ev3/DBusJava/build.xml. Then open the Terminal tab at the bottom of the geany window.
Use cd to change into your projects folder; then cd into ev3/DBusJava
Run ant jar
If the command did complete successfully use cd to change into ev3classes directory and open the file ev3/ev3classes/build.xml from geany menu.
Run ant jar
Use scp to copy some example project from eclipse to geany. Create a new geany project with the same name.
Use command ant jar in geany Terminal to compile the project. Finally use ant copy to transfer the jar archive to raspberry pi.

Result on iPad with bluetooth keyboard (using the steps described here):

Mathematica comes preinstalled on raspbian and is free for personal use. Adding a vnc or rdp server to a raspberry pi running raspbian allows to display and control Mathematica with an iPad. If the raspberry pi is configured as wlan access point you get a free and rather mobile installation of Mathematica.

Required hardware: raspberry pi model B, 16 GB sd(hc) card (at least 4 GB), ew-7811un wireless usb adapter; device running a vnc or rdp  viewer (e.g. iPad with RD Client app).

  1. Installation of raspbian to sd card:
    download raspbian image http://downloads.raspberrypi.org/raspbian_latest
    unzip the file
    copy it to the sd card (linux: dd if=your_file of=/dev/sdX bs=1M; sdX is the device name of the sd card)
  2. Configuration of raspbian on raspberry pi:
    for the first run plug monitor, keyboard, lan and usb current source into raspberry pi;
    raspi-config gets started automatically on first boot
    resize raspbian partition to full size of sd card (using the menu of raspi-config)
    set hostname, keyboard, language, timezone, password; enable ssh server; boot into console
    login as user pi and become root with sudo su. Run apt-get update and then apt-get upgrade
  3. Add software to raspbian (as root user)
    apt-get install mc (I cannot live without)
    apt-get install xrdp
    apt-get install tightvnc-server
    apt-get install hostapd dnsmasq
  4.  The xrdp server works with the default configuration. Alternatively use tightvnc-server as described here:
    with  VNC_USER=”pi”, HOME=”/home/pi”,FOO_ID=1
    and set as user pi a password for vnc with vncpasswd;
    after reboot the vnc server will listen on port 5901
  5. Configure the wlan router function as described here, but make the following changes:
    the program /usr/bin/hostapd installed by raspian has to be replaced by  a self compiled version (mine is here – having used these instructions); (or you might use this file mentioned in this note)
    add dns server 8.8.8.8 to /etc/dnsmasq.conf: dhcp-option=6,8.8.8.8
    (ios clients seem to be picky about non standard network masks. So configure wlan0 with 255.0.0.0 and adapt dnsmasq.conf.
    dhcp-range=interface:wlan0,10.0.0.2,10.0.0.20,10.254.254.20,255.0.0.0,infinite)
    add modules to /etc/modules (one per line):
    nf-conntrack-ipv4 iptable-nat ipt-MASQUERADE (optional iptable-filter )
    do not create the file /etc/network/ifup.d/router.sh, but add the 2 lines of this file to /etc/rc.local
    ifup wlan0
    iptables –table nat -A POSTROUTING –out-interface eth0 -j MASQUERADE
    #optional: iptables -A FORWARD –in-interface wlan0 -j ACCEPT
    #optional: iptables -I INPUT -i wlan0 -p udp –dport 67:68 –sport 67:68 -j ACCEPT
    (and make sure that the newly added lines are above the line “exit 0”)
    Remark: long iptables options have — which tends to get displayed as unicode long hyphen; (has to be replaced manually when using copy and paste):
    To test hostapd and/or dnsmasq run it on the console: hostapd /etc/hostapd/hostapd.conf
    and/or dnsmasq -u dnsmasq –conf-file=/etc/dnsmasq.conf -d
  6. Plug the mini wlan adapter ew-7811un into the raspberry pi and reboot
    try to join wlan Himmbeerhacks and try to connect with rdp to 10.0.0.1 or open 10.0.0.1:5901 with a vnc viewer (like  RD Client or Remoter VNC on iPad).

Further trial and error shows that with a bluetooth keyboard rdp works better than vnc. Especially square brackets (alt+5, alt+6) do not work well with vnc – at least in my setup with locale swiss german. That is why I started to use xrdp and the free microsoft remote desktop client for iOS.