Thursday, March 6, 2014

Access Raspberry Pi via SSH, VNC and netatalk from your Linux PC

Working on a Raspberry Pi is realtivly slow. Because of that I set up a cross compiler on my Linux Laptop (Part 1, Part 2). But also if you want to move files to your RPi or try running a program, it is sometimes a pain. I for example don't have a HDMI monitor or a USB keyboard. I only have my laptop. But luckily I can do everything I want on my RPi over the network.

To all of this you need the IP of your RPi in your network. For this I connected my RPi to my TV and borrowd a USB keyboard from a friend. If even this is not possible to you, here's a solution on how you can search for devices connected to your network.

On the RPi type in the terminal:
 ifconfig  
You should get something like:
 inet addr:192.168.0.101  
Note this!

Connecting over SSH

To connect to your RPi open the terminal on your Linux computer and type this with the IP adress of your RPi:
 ssh pi@192.168.0.101  
Answer the fingerprint question with yes and type in your password (default: raspberry).

Finally in your terminal you should see:

Now every command you type here the RPi executes - not your computer.

Connecting over VNC

Sometimes this is not enough and you want to see the GUI of the RPi. You can also do this over the network with a VNC server.

On your RPi:
First connect to your RPi over SSH and install tightVNC.
 sudo apt-get update  
 sudo apt-get upgrade  
 sudo apt-get install tightvncserver  
To start the VNC Server use this command:
 vncserver :1 -geometry 1024x600 -depth 16 -pixelformat rgb565  
You must specify a password (8 characters) that you need to connect later. Answer no to the view only question.

On your computer:
Open another terminal window and install a VNC viewer:
 sudo apt-get update  
 sudo apt-get upgrade  
 sudo apt-get install xtightvncviewer  
Now you can connect to the VNC server on the RPi via (edit the IP!):
 vncviewer 192.168.0.101:5901  
You should see the Desktop of the RPi in a window on your computer.

To stop the viewer just close the window and to stop the server type this on the RPi:
 vncserver -kill :1  

File transfer with netatalk

And how can I put these newly cross compiled programs on my RPi? Sure, with a USB stick. But since I already do everything over the network, I want to transfer files like this as well. The answer is netatalk.

On your computer and RPi install netatalk:
 sudo apt-get install netatalk  
Then just reboot both. After that give it some time to fully boot and then you should see the RPi in your file explorer under network:

Double click it and it asks for a username and password. These are:
Username: pi
Password: whatever you chose, default is raspberry

After that you can browse your files on the RPi from your computer and copy easily new files over

Program versions:
Ubuntu 12.04 LTS
Raspian wheezy 2014-01-07

How to cross compile for Raspberry Pi using Code::Blocks on Linux

In my privious post I described how I managed to build a cross compiler in Ubuntu for a Raspberry Pi. To compile I used a terminal command. But I want to use a IDE. On Linux I use Code::Blocks for now.

So we have to add a custom compiler to Code::Blocks. Go to
Settings --> Compiler ... --> Global compiler settings
Select the GNU CCC Compiler and click Copy. Give your new cross compiler a meaningful name.


Select your created compiler and go to Toolchain executables. Specify under Compiler's installation directory the path where you keep you cross compiler. For me the path looks like this:
/home/christian/Programming/x-tools/arm-unknown-linux-gnueabi

IMPORTANT: Don't specify the path to the bin directory like in the add-to-$PATH tutorial !!!

After that change the compilers and linkers so it looks like this:


Hit OK and create a new project. Select Console application.


Select your language and your directory where you want to save the project ...


Select your just created compiler...


After that you see already a Hello-World-Program. Try to build it. If it works yout get this notification.


Now we want to check if the cross compiler worked. Copy from your project directory the created file in /bin/debug in your home directory on your RPi. On your RPi (I used ssh) open the terminal and type
 ./ FILENAME  
If you see a "Hello World!" then your cross compiler works !

Program versions:
OS: Ubuntu 12.04 LTS
crosstool-ng 1.19.0
Code::Blocks 10.05

How to build a cross compiler for Raspberry Pi using crosstool-ng

Normally I'm working with a Mac and OpenCV. But I want to run OpenCV on a Raspberry Pi for a small preject. Since I don't want to compile on the Raspberry and lose a lot of time, I was looking for a cross compiler. Looks like this is easier from a Linux machine. I had an old Windows laptop left and so I installed Ubuntu 12.04 LTS along with Windows 7 on it.

I found some really good tutorials about this so I won't repeat them and just give you some links:

1. Very clear tutorial. Lightweight for a good overview:
http://www.bootc.net/archives/2012/05/26/how-to-build-a-cross-compiler-for-your-raspberry-pi/
2. More detailed with screenshots:
http://www.kitware.com/blog/home/post/426

I really like the first tutorial but it's missing information about packages you have to install before you can build crosstool-ng. On Ubuntu you do this with this command in the terminal:
 sudo apt-get install PACKAGE_NAME  
The second link already names some packages, but not all of them. I got errors when I tried to build the toolchain, for example subversion is needed to download a package during the build process. So here is a list of what I installed (replace PACKAGE_NAME with the name below to install):
  • libssl-dev 
  • openssh-server 
  • git-core 
  • pkg-config 
  • build-essential 
  • curl
  • gcc 
  • g++
  • bison 
  • flex 
  • gperf 
  • libtool 
  • texinfo 
  • gawk 
  • automake 
  • libncurses5-dev
  • subversion
Lot's of stuff, but finally I got the toolchain built.

The tutorial says that we have to add the compiler to our $PATH. This is done with this command (don't forget editing the path to your compiler):
 export PATH=$PATH:/PATH/TO/x-tools/arm-unknown-linux-gnueabi/bin  
Remember that this is only temporal! The next time you open your terminal you have to add this to your $PATH again

After this we can compile a "Hello World!" program with this command (source code):
 arm-unknown-linux-gnueabi-c++ hello.cpp -o hello  

Program versions:
OS: Ubuntu 12.04 LTS
crosstool-ng 1.19.0
gcc-linaro 4.8-2013.06-1