Sunday, March 24, 2013

Send image from Raspberry Pi via Bluetooth

Our next Bluetooth step is to send files between the Raspberry Pi and my Smartphone.

Raspberry Pi to HTC
We have to check which services are available on the Phone:
sdptool browse <MAC of the Phone>
Search for "OBEX Object Push" and remember the Channel. In my case it is: 12.

Next step is to install obexftp:
sudo apt-get install obexftp
Now we can send a file from Raspberry Pi to the Phone. In my case I have downloaded a picture of tux in my home-directory as described here: http://www.acmesystems.it/bluetooth

Enter the following command (don´t forget to use the right channel) and a Bluetooth file receive request should come up on your Phone:





Yeah, tux is on my phone now!

Raspbian Bluetooth USB

Last week I ordered some stuff from amazon:
  • A cheap USB Bluetooth dongle
  • USB Webcam
  • Very small USB Stick 32GB
First I want to try to establish a BT connection between my Raspberry Pi and my Smartphone (HTC Wildfire). To test my new BT dongle, I inserted it in my Laptop (Win7 x64). There I got an device error (Code: 10 cannot start device) - so far so bad.
On my Raspberry Pi, I had to install the necessary software first:
sudo apt-get install bluez
sudo apt-get install bluez-utils
Then we can check if the device is there and scan for devices (Don´t forget to make your Phone etc. visible in your Bluetooth settings). To test if the connection is working, we can use l2ping <MAC of Phone>

That seems to work - nice.
I found most of the infos needed here: http://www.dreamgreenhouse.com/

Wednesday, February 20, 2013

Website accessible via dynamic DNS

To allow access to my website hosted on the raspberry pi, I need a domain name. Also because I am behind a router I will have no fixed public IP. To solve this dilemma we can use a so called dynamic DNS service, which will update our DNS entry with the current public IP of the router. http://en.wikipedia.org/wiki/Dynamic_DNS
Luckily there are several free available dynic DNS providers: http://dnslookup.me/dynamic-dns/
I decided to use no-ip.com because DynDNS is not free any more.

First you need to go to http://www.noip.com/ and register your domain name. Afterwards you need to install the no-ip client software on your raspberry pi. Usually all that it does,is to check your public ip in a specific interval and send this information to no-ip, so they can update the DNS entry.

But because I am using a FritzBox as router, I can configure directly that I want to use a Dynamic DNS provider and it takes care of everything for me (so no need to install anything on my precious pi).
All you have to do, is go to the webinterface of the FritzBox and enter your dynDNS provider, username & password.
It should look something like this:


Save everything, maybe restart your router and wait some minutes. Now you should be able to reach your website under your selected domain name from everywhere in the worl.

Note: After subscribing to no-ip.com it took some hours until my domain name was populated & working - so be a little patient!

Phyton-CGI with lighttpd

Since static html pages are boring and for little girls only, we now want to create our first website with phyton-cgi calls.

Add/change the following in your lighttpd configuration file (/etc/lighttpd/lighttpd.conf):
$HTTP["url"] =~ "^/cgi-bin/" {
        cgi.assign = ( ".py" => "/usr/bin/python" )
}
Create a directoy "/var/www/cgi-bin/" for your phyton scripts, and also make sure that phyton is installed and the binary is "/usr/bin/phyton" - otherwise change according to your setup.
Now we change or index.html file and add a simple form to it:
<form method="GET" action="/cgi-bin/test.cgi">
  <input type="submit" value="Start" name="Start"/>
</form> 
Then we create our phyton file "/var/www/cgi-bin/test.py" with following content:
#!/usr/bin/python
import cgi
import cgitb 
cgitb.enable()  # for troubleshooting
cgi.test()      # cgi test output
 
Now we can go to our website and click on the "Start"-Button, which will call our python-cgi script and outputs a page with all kinds of environment variables etc. It worked!

If you have problems try restarting the lighttpd service and check if you have the rights to read/write/execute stuff in your /var/www/ directory.

More information about phyton & cgi can be found here: http://docs.python.org/2/library/cgi.html

Raspberry Pi as webserver

Now I want to setup a small webserver on my raspberry pi. First we have to choose an appropriate lightweight webserver, because a standard Apache installation seems to big & overkill.
Two small webserver, which seem to be very good and also widely used are:
I heard a lot of good things about nginx, mainly its speed and rewriting abilities, but I decided to use lighttpd just because I found good instructions for it first. Installing:
 sudo apt-get install lighttpd
Configuration:
sudo nano /etc/lighttpd/lighttpd.conf
Check that the following modules are enabled:
server.modules = (
            "mod_access",
            "mod_alias",
            "mod_accesslog",
            "mod_auth",
            "mod_ssi",
            "mod_cgi",
            "mod_compress",
            "mod_fastcgi",
            "mod_rewrite",
            "mod_magnet",
)
The web root dir should be "/var/www/" and the user which runs lighttpd is "www-data" - no need to change anything here.
Lighttpd should be already running. You can start/stop/restart the service with:
sudo /etc/init.d/lighttpd start
sudo /etc/init.d/lighttpd stop
sudo /etc/init.d/lighttpd restart
Now create a file "/var/www/index.html" with some html code, open a Browser and go to "http://<yourIP>/index.html". VoilĂ .

Saturday, January 26, 2013

First python program

I am new to python, so I first want to create the traditional "hello world" program in python and see if it works.
On my raspberry pi, python was already installed and added to the path.
There are different Version installed (standard 2.7 and the newer 3.2). Check your version:
python --version
Open an editor and type the following program:
#!/usr/bin/python
print "hello world"
Save as "test.py". It can be executed either by calling the python binary or making the file executable (asuming you are in the same directory as the python script file):
python test.py
chmod +x test.py   //sets the executable flag
./test.py
Or if you want to run it as phyton 3.2:
python3.2 test.py
This will output an error, because in the 3.x version of python the "print" command is a function and so we have to use it with parenthesis like this:
print("Hello world")
 Now it should work and print "Hello world" to stdout. Nice.

Thursday, January 24, 2013

Text to speech engine

Now we make the raspberry speak. I tried two different text-two-speak engines:
In the end I think espeak is the better choice, because it works more stable and the voice sounds better.
Installing is quite simple:
sudo apt-get install espeak
sudo apt-get install festival
Now for testing:
espeak "hello world"
espeak -f readFile

echo "hello world" | festival --tts
festival --tts readFile
The standard voice is english and male with a speed of 175. You can change that - for example:
espeak -s 150 -v german "hallo welt"
This changes the speed of the voice and sets the language to German.
If you have problems with the audio output, try running the command with sudo.
To check if it is a problem with your audio configuration or just with espeak, try playing a test audio file:
sudo aplay /usr/share/sounds/alsa/Front_Center.wav
Still no audio? Check your mixer settings - you can force an output channel:
sudo amixer cset numid=3 <n>
n=0 auto, n=1 analog, n=2 hdmi



To get the sound over HDMI working with my TV (LG 32LH2000) I also had to change the config.txt file in the boot directory. Add/uncomment the following line and restart the raspberry pi:
hdmi_drive=2
Still not working for some reason hmpf? Try loading the sound driver:
sudo modprobe snd_bcm2835

Removing a package
Since the festival package is not needed anymore, we remove it:
sudo apt-get remove --purge festival
sudo apt-get clean