Tuesday, February 26, 2013

WiringPi: IO control for Raspberry Pi

WiringPi
Is a C library to control the IO pins, like the GPIO, I2C, SPI and so on...
WiringPi also has a commandline tool to control the GPIO from a shell script, which is cool and makes
writing small simple scripts very easy to automate certain stuff.
I'll handle here only the WiringPi utility, as that's all I need for now.
For more information about WiringPi, take a look at this link:
https://projects.drogon.net/raspberry-pi/wiringpi/

Installation:
First we need git tool to download the sources:
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install git-core


(clone the project)
git clone git://git.drogon.net/wiringPi

(got the wiringPi folder and build the project)
cd wiringPi
./build


To install the executable in i.e. /usr/bin/ you need sudo for that.
Try the following command to see if all went well:
gpio -v
gpio readall


GPIO Utility:
Now, if we want to use GPIO pin 1 to be the output, we do as follows:
gpio mode 1 out
Here we say, make GPIO pin 1 as output.

To make pin 1 HIGH:
gpio write 1 1
This is translated as write pin 1 as 1.

The GPIO has also an internal pull-up/down resistor, this can be controlled too:
gpio mode 1 up
gpio mode 1 down
gpio mode 1 tri

Streaming webcam with Raspberry Pi

I'll write soon my own experience with streaming video from a webcam with RaspberryPi, but until than, here are some links that can help you to do the same thing. The result depends on the support of the webcam by the RaspberryPi.
http://wolfpaulus.com/journal/embedded/raspberrypi_webcam
http://www.palebluedot.nl/jml/index.php/computer-stuff/33-webcam-streaming-with-raspberry-pi
http://blog.ianrenton.com/raspberry-tank-day-14-video-streaming/
http://blog.davidsingleton.org/raspberry-pi-webcam-a-gentle-intro-to-crontab/
http://sirlagz.net/category/raspberry-pi/
http://www.thisismyrobot.com/2012/08/getting-logitech-c270-webcam-working-on.html
http://www.slblabs.com/2012/09/26/rpi-webcam-stream/

ssh, scp, rsync, ftp, ...

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

NSNotification example