Tuesday, September 22, 2015

Compile and Install R-3.1.2 (32-bit) in Raspberry Pi Model B/B+

Hey guys,

It's been a while since my last post. I've been playing with RPi for some chart plotting features recently and i found out that Raspberry Pi can in fact install pretty recent version of R. In this case, R-3.1.2. I've been using R for my work since few years ago and still loving it. Therefore i tried to install it into my RPi to see how it perform. The available version of R in apt-get repo is quite old, so in order to get more recent version of R, I have to compile it in RPi instead and install it in the system. I'll describe how to do it step by step in the next section.

** Note: Since we are compiling R, i do not recommend to run the compiling over SSH. Instead please start a desktop session through tightvnc. If you don't want to install tightvnc, you can connect your RPi to a monitor, keyboard and mouse during compiling.

When your desktop session is ready, please double click LXTerminal icon on the desktop and you will see a terminal window show up.

  1. We are going to need some extra packages installed in RPi in order to compile R. In the terminal window, type in "sudo apt-get install gfortran libreadline6-dev libx11-dev libxt-dev libpng-dev libjpeg-dev libcairo2-dev xvfb" (without the quote) and follow instruction from the apt-get prompt to install the necessary packages.
  2. Once complete step 1, type in "mkdir R_HOME && cd R_HOME", this command will create a folder name "R_HOME" under "/home/pi/" and then enter the new directory "R_HOME"
  3. Next, type in "wget http://cran.rstudio.com/src/base/R-3/R-3.1.2.tar.gz && tar zxvf R-3.1.2.tar.gz"  to download R-3.1.2 source archive file into the folder and extract it into "R_HOME" folder.
  4. Next type in "ls" command to ensure you have one directory "R-3.1.2" and the archive file "R-3.1.2.tar.gz" in "R_HOME" folder.
  5. Next type type in "cd R-3.1.2" to enter the extracted directory.
  6. Finally type in "./configure --with-cairo --with-jpeglib && make && sudo make install" to start source code compilation and installation. Take note that this process will take several hours to complete due to RPi's slow processor speed. You will see a lot of messages pop up in the window, just leave it until the process complete. After that you will see "pi@yourhostname ~/R_HOME/R-3.1.2 $" popup in window where yourhostname is the hostname you set for your RPi. Once you see that line that means the compilation and installation is complete.
  7. To verify whether R installation is complete, simply type "R" and type enter in the terminal window. You will see R console start up like figure below if everything goes well.

Well, that's it for now. I'll share some example of installing R packages like ggplot2 and running R to plot charts in the next post when I'm available.

Thanks for reading =)

Edit 20th Nov 2014: 
1. Add in additional libraries required at step 1 to enable additional plot output format capabilities (jpeg and png) on R during configuration.
2. Add in ./configure parameters at step 6 to ensure R compile with jpeg and png capabilities.


Reference:
1. http://stackoverflow.com/questions/28309891/install-r-3-1-2-on-wheezy-7-8-raspbian-fails
2. http://r.789695.n4.nabble.com/Compiling-R-3-1-0-on-debian-with-libpng-libjpeg-td4693026.html

Tuesday, February 3, 2015

Remote Control Your Raspberry Pi With SSH

One thing good about Raspberry Pi is after you set it up, it only require a power supply and a network to work. You don't need to always keep it around your TV to work with your Raspberry Pi. As long as your RPi is connected to your home network (WiFi or LAN), You can always remote control your RPi through 3rd party application via your home network.

I'm going to cover on how to setup SSH connection between you RPi and PC/Android Smartphone/Android Tablet.

Here is what you need to install on your client devices before proceed to configure your RPi. I assume you will use PC, android tablet or android smartphone as your client device to connect to your RPi and control it remotely.

(i) PC
  • Putty (Go to Intel x86 section and download Putty.exe, not require installation)
(ii) Android Tablet/Smartphone
  • JuiceSSH (Go to Google Play or click on link to install JuiceSSH app)

SSH (Command Line Interface)

This is the simplest way of remote control your Pi. The SSH server is available out of the box for Raspbian, all you need to do is just enable it through Raspi-config and you will be able to control your RPi through command line interface.

To enable your SSH server, if you are in command line interface after boot up, simply type"sudo raspi-config" and press enter. You will see the command line interface pop up as shown in Figure1. You can do so in desktop session as well by enter the same command through Terminal.

Figure1: Bring up raspi-config Command Line Interface

Using keyboard to navigate to "8 Advanced Options" and press enter. You should see second command line interface like Figure2.

Figure2: Advance Options Command Line Interface

Using keyboard to navigate to "A4 SSH" and press enter. You should see a message like Figure3 asking whether you like to enable SSH server or not. Select "Enable" and press enter.

Figure3: Raspi-config Enable SSH Message

Once you complete step above, your RPi's SSH server is enabled and ready to receive remote command. You can now connect to you RPi using client devices below:

PC

There are a wide range of SSH client available for PC, i will recommend to use Putty since it is pretty user friendly and it doesn't require installation. In earlier section i require you to download Putty. Now go to the location of Putty and double click it. You should see a interface like Figure4 below:

Figure4: Putty's Interface

Once you see Figure4, you just need to type in your RPi's IP address and click "Open" button to start a SSH session with your RPi. Putty will prompt you whether you would like to remember your RPi's keys, just click "Yes" and continue. You should see Figure5 below.

Figure5: Login to Your RPi

If you see Figure5, you need to type in your RPi's user name and password. Default user name for RPi is "pi" and password "raspberry". You won't be able to see your password when you type it in for security purpose. Once you key in your user name and password correctly, you should see system message appearing and look like Figure6 below.

Figure6: RPi is Ready to Receive Remote Command



Android Smartphone / Tablet

There are several SSH client available in Google Play, i choose JuiceSSH because it is quite user friendly and it can manage your SSH connection quite well. Go to Google Play, search for JuiceSSH and install it.

Figure7: JuiceSSH, Android SSH Client

Tap the app icon once you install it. You will see the app GUI like in Figure8. Tap on connection button and then you will see a "+" yellow button on bottom right of your screen. Tap on it to create a new connection.

Figure8: Create New Connection in JuiceSSH

Next you will see a page with some field require to fill up for your SSH connection. Please refer to figure9. You will need to fill up "Address" field with you RPi's IP address, tap "Identity" field to create a new identity "pi", you can also change your identity to your other RPi user name. Lastly key in "22" into "Port" field as we will be using default SSH port. Tap tick button on top right of GUI to finish setup your connection.

Figure9: Fill up Necessary Info for SSH Connection

Once complete, you will go back to connection GUI and you should see a new connection appear. Refer to Figure10. Tap on that connection you will see some connection message and then app will prompt you to enter password, enter your RPi password (default is "raspberry") and you should be able to see similar terminal command line for RPi. If you already setup your password during last session, it will auto connect to your Pi with configured password.

Figure10: Connecting to Your RPi Using JuiceSSH

There you go! Now your RPi is ready to receive remote command from your PC or Android! You can pretty much do everything just like working with Terminal in desktop session.


Reference:
http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html

Thursday, January 1, 2015

Audio Streaming to Bluetooth Speaker Using Raspberry Pi B+

Edit 2016Jan3: This guide was based on old Raspbian Wheezy distro, some packages were no longer included in latest Raspbian Jessie distro. I'll have a look on how to make the old setting works on Raspbian Jessie.

Edit 2017May14: Finally found a way to make it work on Raspbian Jessie distro. Please refer to this link for instructions to setup bluetooth speaker.

Hi~ Here is a small project for my new RPi B+. So i got some bluetooth peripherals lying around the house, so i thought i can make a small music box using RPi. Did some research and got it working!

So here are what you need for the hardware:
  1. RPi Model B+ with Raspbian Installed (Should work with model B or A)
  2. Bluetooth Speaker
  3. USB Bluetooth dongle
  4. USB Wifi dongle / LAN cable for internet connection
  5. 5V 1A/2A Power Supply
Figure6: Hardware Setup
 If you have everything ready for the hardware. You need to install some packages to your RPi before you can connect to your bluetooth speaker. Boot up your RPi, but do not plug in your bluetooth dongle yet. Open up terminal and enter commands below for installation of the required packages.
  1. "sudo apt-get update && sudo apt-get upgrade" - upgrade your Raspbian packages list to latest version. Wait for program to finish upgrade before next command.
  2. "sudo apt-get install bluetooth bluez bluez-utils bluez-alsa blueman" - install five packages for bluetooth dongle and bluetooth audio. Wait for installation to complete before next command.
  3. "sudo usermod -a -G lp pi" - this command will add "lp" group under account "pi". You account need to have access to this group for this to work. If you are using other account, just replace the "pi" in command with your account name.
  4. "sudo nano /etc/bluetooth/audio.conf" - you need to add some command in the audio.conf file. This command will open nano text editor and allow you to edit the file. You need to add in two line in the file. "Enable=Source,Sink,Socket" and "Disable=Media". Refer to Figure1 and Figure2 below. After edit file, press Ctrl+O and Enter to save the file.
    Figure1: Editing audio.conf file
    Figure2: Add in highlighted two lines in audio.conf file
  5. Once save the file, enter command "sudo shutdown -h -P now" to safe shutdown the Pi. Once you see green light blip for 10 times. You can turn off the power supply.
  6. Now plug-in your bluetooth dongle to the USB and start up your RPi.
  7. Wait for boot complete, go to terminal and enter command "lsusb". You should see your bluetooth dongle available as shown in Figure3. You can refer HERE for list of working bluetooth adapter for RPi in case your bluetooth dongle is not detectable.
    Figure3: Bluetooth Dongle Detected in USB list
  8. If you able to find your bluetooth dongle in USB list, you should see a Bluetooth icon at your RPi tray. If you did not boot up your Pi in GUI and you are in command line interface, enter "startx" to start your GUI session. Refer to Figure4 below to setup your bluetooth connection with your bluetooth speaker with 3 simple steps.
    Figure4: Setup Bluetooth Connection with Your Bluetooth Speaker
  9. Follow up on step 8. You can also click "Trusted" on the device as shown in Figure4 if you want to let the speaker auto connect to RPi in the future. Mine is showing "Untrust" because i've already "Trusted" this device.
  10. Open up terminal again to edit one last file. Enter "sudo nano ~/.asoundrc" and enter the script as shown in Figure5. For line start with "device 00:", that the place where you put your bluetooth speaker's MAC address. You can find you MAC address in Figure4. Copy and paste in to the script. Once done save the file and you are done setting up the bluetooth portion for RPi.
    Figure5: Script to Connect Bluetooth to Alsa
  11. If you completed the steps up to 10, you are almost ready. You just need to install one more package so that you RPi can play music. In this case i recommend Mplayer because it is a terminal player that can support wide range of audio format including m4a. Go to terminal and enter "sudo apt-get install mplayer" and wait for installation complete.
  12. Once you complete installation of mplayer. Navigate to your music folder using terminal and then enter command "mplayer -volume 10 -ao alsa:device=bluetooth youmusic.mp3" to play music.
  13. You should be able to listen to your music from bluetooth speaker from now. 
  14. Here is the video showing the working bluetooth speaker using RPi.

Hope you like this tutorial. Thanks for your time!

Reference:
http://elinux.org/RPi_USB_Bluetooth_adapters
http://www.correderajorge.es/bluetooth-on-raspberry-audio-streaming/