Tuesday, February 19, 2013

Wifi autoconnect RaspberryPi

I wanted my RaspberryPi to autoconnect through wifi on a spcified Access Point (AP).
To do this, first I copied /etc/wpa_supplicant/wpa_supplicant.conf to /etc/ .
You need to tell RaspberryPi to connect to your wifi hotspot (for example, your wifi router).
You can than output the wpa_passphrase to /etc/wpa_supplicant.conf by issuing the following command:
sudo wpa_passphrase myssid "this is test" >> /etc/wpa_supplicant.conf 
myssid could be your wifi's ssid and "this is test" could be your wifi's password or code.

In /etc/wpa_supplicant.conf I added the following lines after the psk line:
proto=RSN
key_mgmt=WPA-PSK
pairwise=CCMP
auth_alg=OPEN


...so it will look something like:
network={
        ssid="myssid"
        #psk="this is test"
        psk=fa5d92f2b069636fd3fe65e2376f2061b37467f8908ca373bbf10f7c92cd37b9

        proto=RSN
        key_mgmt=WPA-PSK
        pairwise=CCMP
        auth_alg=OPEN
}


So that's the part of wpa_supplicant.conf part, now we have to modify /etc/network/interfaces.
We add the following lines:
allow-hotplug wlan0
iface wlan0 inet dhcp
pre-up wpa_supplicant -Dwext -i wlan0 -c /etc/wpa_supplicant.conf -B


So, this is it. I have an Android phone, which has a wifi hotspot feature. If you turn a wifi hotspot on (or your own wifi router at home), your RaspberryPi should automatically connect. Make sure you rebooted your Raspberry and make sure you used the right SSID and passphrase of your wifi AP.

A small problem I encountered is that I had no connection on the tcp/ip layer when turning off and on my AP while RasPi was running. There was a physical connection though, but somehow the upper layers was dead. It turns out that wpa_supplicant wasn't cleaned up properly (not killed) and there somehow went wrong. After googling around for solutions, I could only find selfmade scripts which I don't really like (like checking for connection status every 60 sec. or something). I also didn't try netcfg, because I was afraid it will conflict with other default network managers (I'm not sure about that).
The solution I found is as follows:
  • Copy /etc/wpa_supplicant/ifupdown.sh to /etc/ifplugd/action.d/ifupdown (make sure you make a copy of /etc/ifplugd/action.d/ifupdown, before you overwrite it).
  •  sudo reboot
That's it! I do see some error messages in /var/log/syslog, but besides that, it works. Now I can turn off and on AP, my RasPi will automatically reconnect with a working the tcp/ip layer.

Crontab

With cron you can execute scripts or commands based on a schedule.
For example, if I want to run a script every 5 minutes, I have to do as follows: 

crontab -e
This will bring you in the edit mode, than we specify that a script must be executed every 5 min.
*/5 * * * * /home/geek/myscript.sh

From now on, every 5 min. the script myscript.sh will be executed.
Crontab works as follows, you have to tell it according to a certain format what time interval to execute a command.
The format is as follows:
minutes (0-59)    hour (0-23 0=midnight)    day (1-31)    month (1-12)    weekday (0-6 0=Sunday)
So the following will run at 01:15 every Sunday.
15 1 * * 0 /usr/bin/date

Another one is to run a script every 30 min. on every day.
*/30 * * * * movelogfiles.sh
or
0,30 * * * * movelogfiles.sh


To remove the 'cronjob' we issue the following command:
crontab -r

To list the cronjobs (so you can see what jobs it has):
crontab -l

Webcam on Linux

Camera commandline tools on Linux
  • fswebcam
  • streamer
  • uvccapture
  • motion

Example to use fswebcam:


fswebcam -d /dev/video0 -r 320x240 mypic.jpg
 

This will use webcam video0 as a camera device with a resolution of 320x240 pixels and outputs it to the file mypic.jpg.

Another nice feature is to use fbi (on Ubuntu) to display the picture in a terminal.
If you don't have fbi, install it with:
sudo apt-get install fbi
Than show an image as follows:
fbi mypic.jpg
or all available pictures:
fbi *.jpg
With  PgDn or PdUp you can go to the next or previous picture.
If it doesn't work for you, you should google for it, it has something to do with vga setting.

Another very useful tool is ImageMagick for processing images. I'll cover that next time.

Thursday, February 14, 2013

Crosscompiling for Raspberry Pi


Linux crosscompile for the Raspberry Pi

I was looking for a crosscompiler to compile executables for the RPi.
On google I found links about how to build your own toolchain, but I rather have a ready made toolchain instead of cooking my own. So I found the following link:
https://github.com/raspberrypi/tools

According to http://qt-project.org/wiki/RaspberryPi_Beginners_guide, it's based on 32 bits machines, so you also need
to install ia32-libs if you work on a x64 based machine.

...and I assume you use a Linux system like Ubuntu or some other flavor.

If you want to cook your own toolchain, this might be a good link for you:
http://www.kitware.com/blog/home/post/426

Wednesday, January 23, 2013

Pathogen, the Vim plugins manager

Pathogen
After installing a few plugins I started to get a little bit annoyed by copying the plugin files to the .vim folder after downloading. It's not a big problem though, but after some plugins, it starts to get messy. You can't see directly what plugins you've installed or what could cause problems and so on...

So to manage the plugins in a proper way, I use Pathogen to do the job. What you have to do is download the plugin from:
https://github.com/tpope/vim-pathogen

Copy the 'autoload' folder to your .vim folder. Actually, the first time you use Vim you should  install pathogen. Create another folder in .vim and call it 'bundle'. The bundle folder is where  you put all your future Vim plugins:
/.vim
   |___ autoload/
   |___ bundle/
              |___  snipmate/
              |___  taglist/
              |___  .../

So every time you download a plugin, you just copy the complete plugin folder in .vim/bundle/
You could also go in the .vim/bundle/ folder and download a plugin using Git, like this:
Assuming you're in the .vim/bundle/ folder.
# git clone https://link.to.vimplugin.git

You just need a small piece of script added in your .vimrc:
execute pathogen#infect()
syntax on
filetype plugin indent on

That's it! Try this out with snipmate plugin, as this plugin doesn't need any other configuration in your .vimrc file. If it works for you, than you can download all your favorite plugins and all are hierarchically structured.

Tuesday, January 22, 2013

Vim plugins for C/C++ development

To use Vim as an editor for C/C++ (and other programming languages as well), we need to extend Vim with some handy plugins. Vim is a powerful editor, it really is, but you need some plugins to make it more powerful to your needs. Vim is not a code editor out of the box, so you can not expect to have cool stuff like Visual Studio's intellisense, or jumping to definition of a function and so on. So to make our coding life easier, we can install Vim plugins to have more fun while coding.

First, here's a list of Vim plugins below I use it for writing code:
  • Surround
  • Matchit
  • Snipmate
  • Clang_complete
  • NERDTree
  • TagList

Matchit
This is a nice plugin too, especially when dealing with html files. It searches for matching tags using the existing % key. In a C file, if you're on one of the brackets like {}, [], () and so on, you'll see those brackets highlighting in pairs. When hitting % the cursor jumps to the matching pair, but normally this won't work for a html tag. Without this plugin this won't work for a html tag.

Put cursor on head tag and enable V mode


Hit % to jump to the matching tag


Link: http://www.vim.org/scripts/script.php?script_id=39

 
SnipMate
Snipmate is really one of my favourite, it's simple but yet so powerful, it's brilliant. You simply download the plugin, copy it to your .vim folder and you're ready to go, just that simple! No, extra additions to .vimrc, no long docs reading, just download, copy and start playing. When you've done that, open a C file and type as follows:
for and than hit the tab key, voilĂ ! A complete for loop snippet is placed and we're not finished yet! You can replace the default words by hitting tab again, change the word in the selected field, than hit tab again, edit the selected field until you're done. This is fantastic!

Typing 'for' before hitting TAB




Result after hitting TAB


Link to download snipmate plugin: https://github.com/garbas/vim-snipmate

Surround
This plugin is a must-have when writing html code, it surrounds a selected block of text/code with div tags (in case you're writing html code) or brackets (in case you're writing C code). It's also very easy to use.
For example, type a simple text and than go back to normal mode and type: yss"

Before typing yss" in normal mode

After typing yss" in normal mode

After typing yss} in normal mode

After typing cs}) in normal mode

When using visual selection (v or V) than use capital S for surrounding, i.e. veS' is the same as ysw'.
The first one is in visual mode and the second one is in normal mode.
The key combinations can be seen as follows:
ysmn surround word m with tag n
csmn change surround with tag m to tag n
dsn delete surround with tag m

For more detailed information go to link: https://github.com/tpope/vim-surround


NERDTree
This plugin works as a file browser, you open it, search the file you want to edit and than hit enter. The file will be displayed and you can edit all along. So, you don't have to get out of Vim to look for files to edit, you simply stay in Vim, open NERDTree and look for the files. But I tell you this, a real good experienced Vim user doesn't even need NERDTree to do such a magic.

I've seen die-hard Vim users loading a whole bunch of files at once and switch buffers as they drink coffee and do crazy stuff! Vim is so powerful, you can't call it just an editor, it's actually an editor that you have a conversation with. When you say, 'dd', it responds with "listen baby, if you want I can also delete multiple lines", than you move 4 lines down and Vim says "baby, just say 4j and I do what you want, but anyway sweety, you want me to delete this line too? Just press . and I'll do it for you". This is Vim and I'm also a beginner, Vim is just incredibly good, you just need to be patient with her and once you have control over her, you can let her do whatever you want ;-)

By adding this line .vimrc, you can toggle NERDTree on or off by pressing on the F4 key:
:noremap   :NERDTreeToggle

Example of NERDTree

Link: https://github.com/scrooloose/nerdtree


TagList
TagList plugin is a sourcecode browser, it shows you a list of classes, defines, global variables, functions, methods etc. for the currently displayed file. This a very useful feature, this way you have an overview of all the functions and you can navigate to a function or a method easily. If you want to jump to a function and edit right away, you simply move to the function name in the taglist and hit enter. If you just want to view the function without jumping to it, you simply press 'p' key (which means preview), this way you stay in the taglist window.
It supports most programming languages, so it's a simple and useful plugin without putting a bunch of tweaks in the vimrc file.

I added these lines in the .vimrc script, to enable and disable TagList with F5 key:
:noremap :TlistToggle
:let Tlist_Show_One_File = 1
:let Tlist_Exit_OnlyWindow = 1
:let Tlist_Use_Right_Window = 1
The last line tells TagList to show on the right side of the window.


TagList example on the right side





Clang_complete
This plugin is important when developing C/C++ code. Clang_complete together with clang and libclang-dev is the best replacement for omnicppcompletion, the old completion plugin which doesn't work well. omnicppcompletion uses ctags to perform completion of code, but it's not perfect like Visual Studio's intellisense or Eclipse. But clang is here to save us, developed by llvm (www.llvm.org). First you  need to install clang, I did the following:
#sudo apt-get install clang
#sudo apt-get install libclang-dev

Than download clang_complete from:
https://github.com/Rip-Rip/clang_complete

Copy the files to your .vim folder and you're done. That's actuall all I did.
I tested with BOOST C++ and it works without tweaking or adding creepy scripts, it just works! See below for an example:



NSNotification example