HA6QZ Raspberry Pi Projects


This HowTo is being rebuild.
For the new pages follow
here

Solar Powered iGate with Raspberry Pi and TNC Pi

Options: FTP Server, Web Server, Webcam ...
Special: Remote Solar System Charging Measurements

RASPBERRY PI as FTP Server
Here’s a how to on installing VSFTPD on the Raspberry Pi
Open a terminal
Type:
sudo bash (Press Enter)
sudo apt-get install vsftpd (Press Enter)

Press Y when prompted

Type:
nano /etc/vsftpd.conf (Press Enter)

Edit or uncomment the following lines to secure VSFTPD
Anonymous_enable= NO
Local_enable= YES
Write_enable= YES
Ascii_upload_enable= YES
Ascii_download_enable=Y ES

Press CTRL + O to save


Press CTRL + X to exit

Type:
/etc/init.d/vsftpd restart

You are done, now you can reach the FTP server with user "pi"
Note: you will not be able to FTP into the Pi with user "root" for security reasons. So, first you copy your files inside the user "pi" FTP area and then you can copy the files freely with mc (midnight commander). This is a similar program to Total Commander or Windows Commander in MS Windows.

To install midnight commander:

apt-get install mc

and start midnight commander with:
mc


back

Objective(s):
- Understand the concept of a web server for the Raspberry Pi APRS igate:
It is possible to remotely access the Raspberry Pi which can be done via SSH, FTP, and also you can run a web server on it.
For eg. If you write a PHP code, then you just need to save it with actual library files in a location which is accessible to your web server (normally under /var/www/ ) and then use your remote web browser to access this page and run the code remotely on the Pi and get the result on your local machine.
In case you want to remotely monitor or do telemetry, a web server is usually needed.

I am trying to make a remote DC current measurement with the Raspberry Pi connected to an USB Amp-meter.
This remote Amp-meter will be used to measure the charging current of a battery set powered by a remote solar panel.
I will use the Apache web server running in the Pi, to run the PHP code and serve the http requests for the DC current measurement values.
Basically I will be able to see the DC charging current values of the remote solar system from 1000`s of kms away..
Such remote Amp-meter device would costs hundreds of EURs, but can be build this ways for a portion of that cost.
- Accessing and running commands on the Pi over SSH
- Editing a file with nano
- Publishing a webpage

Pre-requisites:
Static IP address, SSH Access (Pi);
The ability to host and serve webpages as a web server, and the software we’re going to install is called Apache. Our web server will sit on your local network and not (yet) be available to the rest of the world via the Internet, but this is a good way to get started.

APT-GET
Apt-get is another command you’re going to be seeing a lot of. We use it to install applications on our Pi. You can think of it like an App Store, except you have to know the name of the app you want to download first, rather than browse through a list. Don’t worry though, in this tutorial you’ll be shown exactly what it is you need to download and why we’re downloading it.

Before We Get Started
This tutorial is going to take some time. Somewhere between 10-20 minutes depending on your network connection as you’ll be downloading software from the Internet straight on to your Pi. So be prepared to be patient.
Also, you should probably update the existing software on your Pi first before carrying out the rest of the installations. You can do this by typing the following:

sudo apt-get update

This is the equivalent of running Software Update on your Mac or Microsoft Update on Windows, and can also take some time. You can read this command as “Hi there, I’m the superuser here, please do go and get any updates for my pi. Thanks!”


Step 1: SSH

ssh pi@192.168.1.x

(again, where 192.168.1.x will be your own IP address). If you haven’t connected before, you’ll be asked if you want to add your Pi’s address to your list of ‘known hosts‘. Go ahead and press Y to continue. You’ll be asked to put in your password – remember that by default, SSH will not show you your password as you type it in.

Step 2: Apache

sudo apt-get install apache2 php5 libapache2-mod-php5


We’re installing three things here, but doing them all at once: apache2, which is the webserver software; php5, which is a scripting language for developing web sites; andlibapache2-mod-php5, a module that allows the PHP language to talk with the Apache webserver. We could write out three commands like this:

sudo apt-get install apache2
sudo apt-get install php5
sudo apt-get install libapache2-mod-php5

..but putting it all into one command saves us a little bit of time.


Step 3: MySQL

sudo apt-get install mysql-server mysql-client php5-mysql


As with the previous command, we’re rolling three installations into one: mysql-server, which is free database server software that powers an enormous chunk of the web; mysql-client, again part of MySQL – this allows you to interface with your MySQL database; and php5-mysql, which enables the PHP scripting language we installed in step 2 to talk to MySQL.

Step 4: Restart Apache

sudo apachectl restart


You might see this written differently across the web – ‘sudo service apache2 restart’ – for example, But in essence, both commands are the same. We’re telling the apache web server that it needs to restart once we’ve finished installing all this extra functionality over the top of it. Notice that we don’t need to actually restart the Pi – just the Apache service.


Step 5: Creating phpinfo.php
We could test the web server out right now – in fact, it’s pretty much ready to go if all you want to do is serve HTML files – but before that we’re just going to make sure that our installation of PHP went okay. We’re going to do that by creating a file called phpinfo.php and insert some code in there that should display information about our PHP installation when we connect to the web server.

sudo nano /var/www/phpinfo.php

sudo you know. nano is a text editor, similar to TextEdit on a Mac or NotePad in Windows, only when you run it, it won’t open up in a nice graphical window. nano runs inside the Terminal window and looks like this:


back

Instead of a menu system along the top of the screen, you’ll notice that all the commands are at the bottom of the screen and can be used by pressing the Ctrl button (that’s what the ^ means) and the appropriate letter. So ^G brings up the Help menu for example. What you may not see is a ‘Save’ function. In nano you save by exiting first with ^X, then confirm that you want to save your changes by pressing Y. This only works on files that you have permission to edit, which is why we run sudo when we run this command.
The /var/www/ is the path to your web server on the Pi, and where all your files and webpages will live. phpinfo.php is the file that we’re editing. Once of the nice touches about nano is that it actually creates the file for us as we type it in. Prior to typing the above command in, the phpinfo.php file didn’t exist – and now it does!
Enter the following code into your phpinfo.php file:

<?php phpinfo(); ?>

and then exit your file, confirming that you want to save the changes.
The ‘<?php ?>’ part of the code above is an opening and closing tag, meaning that whatever we put inbeween, we want the computer to do for us. the phpinfo(); is a function that outputs information about the PHP configuration on your web server.

Step 6: Testing the Web Server
Here’s the part where you can test out everything works by opening up a web browser and typing in the IP address of your Pi followed by a / and the phpinfo.php filename:

http://192.168.1.x/phpinfo.php

If everything’s gone correctly, you should see a page like this:

and if you do – congratulations! You’ve just installed and set up your very own web server on your Pi.

Step 7: Creating index.html
While PHP might be a little daunting to get started with, you could still easily have a go at creating standard HTML pages. The first file a web browser will look at when pointed at a web server is (usually) called index.html. You can try this by using Terminal and typing the following:
sudo nano /var/www/index.html to create your very first file.

Raspberry PI as a remote IP Web Cam

Note.: The Webcam is using USB 2.0 settings but the Amp-meter needs USB 1.1 settings on the Raspberry Pi, which means either you use remote current measurement or remote video surveillance. So far I have not found the good solution to make both work at the same time. For the current measurement it needs slower and absolute error free packets over the USB port. For webcam it needs fast data stream that USB 2.0 can provide.

The Raspberry PI is perfectly equipped to turn your USB based web cam into a fully functional IP web cam and you can use tools such as Python to make your project more unique.

If you just want to have a web cam that can be access from anywhere with an internet connection, then this is your guide.

You need:

- Raspberry Pi with a Wheezy OS installed
- USB Powered Hub
- USB webcam - I have a Logitech Quickcam Pro 9000 Webcam http://www.logitech.com/en-in/product/5867?crid=34

Usually need a powered USB hub because the camera’s draw more power than the Raspberry Pi can actually provide, you see the same with some keyboards and USB dongles. However my webcam works perfectly from the USB port of the Rasberry Pi.


1. Get the software ready

Ok so I am going to assume that you have a working PI on some kind of LAN with internet access, the next step as always is to update your PI ensuring you have the latest software and drivers installed.

sudo apt-get update
sudo apt-get upgrade

Now we need to install the software, we are going to be using a great little application called Motion, this will do a few things for us including accessing the USB cam, getting the images, and streaming them via a built in web server.

sudo apt-get install motion

2. Plug in your web cam

So now the software is on there it’s time to plug in the web cam and ensure that everything is working, ensuring that you plug it into the powered hub, and then into the PI. Otherwise the webcam will not get enough power to turn on.

When plugged in type the “lsusb” command, you should see a line there with your web cam manufacture, that proves that you have the basic connectivity working.

lsusb
....
Bus 001 Device 002: ID 04ea:1142 Microsoft Corp.
....

3. Configure the software

sudo nano /etc/motion/motion.conf

In here there are a few basic changes that you need to perform:

Daemon = OFF to ON
webcam_localhost = ON to OFF

You can change other settings but it’s recommend you don’t take more than 2 frames, and you been the default frame pixel size, for stability.

4. Start the software
To ensure that the motion service will actually start as a daemon we need to change another configuration setting, so enter the following:

sudo nano /etc/default/motion

Then change the value “start_motion_daemon=no” to “yes”

Finally you can start the motion service to stream the web cam images

sudo service motion start

Then after about 30 seconds browse to the new web interface, which should be at the below URL (where 192.168.0.100 is your Raspberry PI’s IP address)

http://192.168.0.100:8081



5. Final Tweeks

Web Port

You could change the web interface port to 80 (from the default 8081), so that you can just browse to the IP address without having to put :8081 at the end, it’s really simple to do, just:

sudo nano /etc/motion/motion.conf

And then change “webcam_port 8081″ to “webcam_port 80″, save the file, and restart the motion service.

sudo service motion restart


Port Forwarding:
Final suggestion is enable port forwarding on your home broadband router to the Raspberry PI on port 80, that will mean you can access the web cam from anywhere in the world. There are too many routers in the word to explain how to do it here, but I am sure you will find some help on Google.


back

Remote Desktop Connection to the Pi
If you need to reach the Pi via graphical user interface, the easiest way is the following:

If you are running Windows on a PC, install xrdp on your Pi (apt-get install xrdp) and use Window's remote desktop.

http://windows.microsoft.com/en-us/windows-vista/connect-to-another-computer-using-remote-desktop-connection

Please note it may not work over the internet, as the firewalls can block the RDP port, but it works perfectly over LAN, in your home network.


Remote Desktop into the Raspberry Pi Using VNC
Needed:
- linux home server / linux home PC
- Raspberry Pi - wheezy linux

SSH is great if you want to remotely access the RPi from the command line. The Raspbian OS also comes with a Desktop GUI environment called LXDE. If can access the LXDE desktop over HDMI by typing “startx” in the command line window. This will not work over SSH. Luckily we can still remotely access the LXDE Desktop from the Linux PC, using the VNC protocol.

- First we need to install the VNC server software on the the RPi. To do this, SSH into the Raspberry Pi and type “sudo apt-get install tightvncserver“. This will install the VNC server software on the RPi. Then type “tightvncserver” to configure the VNC server for the first time. You will then be asked to enter a password (8 characters only) & confirm it. I used “raspberr”. You will then be prompted to enter an optional “view only password”. This is not needed and you can choose to not set one by saying no. the next step is to type the following into the RPi SSH session to start the VNC server: “vncserver :1 “. This starts a VNC server on display number 1 with a resolution of 1280 by 720 and a color depth of 24. At this point the VNC server should be running on the Raspberry Pi.

- Now open another console window on your Linux PC and install a VNC viewer (client) on the Linux PC with the command: “sudo apt-get install xtightvncviewer “. Then in the same console window (Linux PC) type “xtightvncviewer &“. The ampersand allows us the run the application in the background.

- This will open the VNC viewer on our Linux PC. Type in the VNC server address that you want to connect to i.e. the RPi IP address & display number “10.42.0.73:1″. You will then be prompted to enter your VNC password. Type that in and press enter.

- You now have full control over your Raspberry Pi via both the command line with SSH and the desktop environment over VNC.

Raspberry Pi remote-desktop-conection

Reverse Remote Ssh Tunnel
The problem is that if I connect my Raspberry Pi to the Internet somewhere in the world and leave it there, later I need to have remote connectivity to it. I know my home server`s IP but I don’t know the Pi’s IP when for eg NAT is in use, for example using mobile internet for feeding the Raspberry Pi with Internet. Usually most of the incoming ports are blocked by the mobile internet operators, but the outgoing ports are usually open, so we are initiating a tunnel from the Raspberry Pi that opens the door to us to reach the Pi remotely.

Solution:
I can configure the Raspberry Pi to "call home" by creating a tunnel, on which tunnel I can reach the Raspberry Pi from my remote server.
Here is the configuration:

Pre-configuration
Before beginning you need to have the following:
- Raspberry Pi with a Wheezy linux on it
- Raspberry Pi is configured to receive a DHCP address
- Raspberry Pi is configured to receive/create ssh connections
- A separate home linux server (Ubuntu is recommended) that is reachable by ssh from the internet

It should be easy enough to figure out how to complete these on your own. With the above, you should be able ssh to the Pi remotely if you know its IP. Well you don’t know its IP and even if you did it is probably behind a firewall which makes it impossible to get to. Or so you think.
What you’ll need is your home linux server that having a dynamic domain name or static IP address. The Raspberry will ssh into it from anywhere in the world. Suppose it’s IP is 25.25.25.25.
Initially all I’ve done is made a port forwarded on port 22 through the firewall of my home router to towards my linux home server to make this work.

Setting up the ssh certificate on the Pi
From the Raspberry Pi you should be able to ssh to this linux server. Test it to be sure. So now you want to set it up so the authentication is certificate based (this way the Pi can be set up to auto ssh into the server without being prompted for a password). From the Pi do the following to create a certificate based authentication.

cd ~/.ssh
ssh-keygen -t rsa

Choose no passphrase when asked and accept the default filename of id_rsa
scp id_rsa.pub <user>@<yourhomeserver>:.ssh/authorized_keys
Provide your password when asked and thats the last time you’ll have to do it!

Creating the Reverse SSH tunnel
Now comes the fun part. Create a reverse remote ssh tunnel to that host to forward connections back to the Pi. Confused? Here look at this command you would issue from the Pi:

ssh -f -N -R 2222:localhost:22 serverUser@25.25.25.25

The Pi is ssh’ing to the server at 25.25.25.25 using the username “serverUser”. Upon doing so, it’s telling that server to open port 2222. Any connections coming into the server on port 2222 forward them over the tunnel back to the Pi on port 22. The -N tag is saying we don’t need to actually send any ssh commands once connected.

Once the Pi sets up that remote tunnel; from the server you can reach the Raspberry Pi, by doing this:

ssh -l piUser -p 2222 localhost

Now you should be ssh’d into the Pi using port 2222 on the linux server! Awesome!
Why did this work? The linux home server is listening on port 2222 for incoming ssh connections. If it receives one, it will forward all traffic it receives into the previous ssh connection that was established already. That is essentially what the remote tunnel does.

Please note, this SSH tunnel will not last forever, usually if you don't use it, it disconnects in about 20-30 min.
But you can make a script that triggers to build up a new SSH tunnel when it disappears, or another script for example every day after a restart it will make a tunnel at a specific time, so you can gain access to the Raspberry Pi.



Important:
On the server normally there are no other ports than 22 in the LISTEN list, but once the raspy initiate the tunnel it will add port 2222 which is used to call the Raspberry Pi from the server

zsolt@server:~$ ps -A | grep sshd
558 ? 00:00:00 sshd
1611 ? 00:00:00 sshd
1691 ? 00:00:00 sshd
zsolt@server:~$ sudo ss -lnp | grep sshd
[sudo] password for zsolt:
LISTEN 0 128 :::22 :::* users:(("sshd",558,4))
LISTEN 0 128 *:22 *:* users:(("sshd",558,3))
zsolt@server:~$ ssh -p 2222 pi@localhost

pi@localhost's password:
Linux raspberrypi 3.6.11+ #474 PREEMPT Thu Jun 13 17:14:42 BST 2013 armv6l

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Sat Aug 24 20:53:36 2013 from localhost
pi@raspberrypi ~ $ exit
logout
Connection to localhost closed.

zsolt@server:~$ sudo ss -lnp | grep sshd
LISTEN 0 128 :::22 :::* users:(("sshd",558,4))
LISTEN 0 128 *:22 *:* users:(("sshd",558,3))
LISTEN 0 128 127.0.0.1:2222 *:* users:(("sshd",2008,10))
LISTEN 0 128 ::1:2222 :::* users:(("sshd",2008,9))
zsolt@server:~$


When you log out from the Raspberry Pi, the port 2222 still in use, so if you have trouble to reach the Pi, you have to release the port on the server by executing:

sudo killall sshd

after this the Raspberry Pi can freely initiate a new connection for port 2222.

Most of the linux servers are breaking the SSH / tunnel connection if there is no communication on it.
If you want to keep your tunnel alive, you have to add the followings into your ssh_config file in your Raspberry Pi.

sudo nano /etc/ssh/ssh_config

and paste these two lines into it.

ServerAliveCountMax 20
ServerAliveInterval 20


________________________________________________________________________

Making the Remote Reverse SSH Tunnel persistent (always on)

Next is to make this a persistent thing. You want the Pi to keep trying to build this ssh tunnel always and if it goes down try to bring it back. We’ll do this using a bash script and cron job.
Create a file on the Pi called ~/create_ssh_tunnel.sh and put this in it:

#!/bin/bash
createTunnel() {
/usr/bin/ssh -f -N -R 2222:localhost:22 serverUser@25.25.25.25
if [[ $? -eq 0 ]]; then
echo Tunnel to jumpbox created successfully
else
echo An error occurred creating a tunnel to jumpbox. RC was $?
fi
}
/bin/pidof ssh
if [[ $? -ne 0 ]]; then
echo Creating new tunnel connection
createTunnel
fi

What this program is doing is checking to see if there’s a process running called ‘ssh’. If there isn’t then start the ssh tunnel.
Next make it executable by doing the following:
chmod 700 ~/create_ssh_tunnel.sh
Now start the crontab.

crontab -e

Place this in as your cron job (every minute check if the ssh connection is up, if not, attempt to bring it up)

*/1 * * * * ~/create_ssh_tunnel.sh > tunnel.log 2>&1

To troubleshoot any problems in this you can view the tunnel.log file.
When the Raspberri Pi is on, it will check every minute to see if an ssh connection to your linux server exists. If it doesn’t it will create one. The tunnel it creates is really a reverse remote tunnel. Once the tunnel is up, anyone who ssh’s into port 2222 of the linux server will then be redirected to the Pi. Incredible!

Download the qemu.zip from here.:

http://sourceforge.net/projects/rpiqemuwindows/files/

and just start the "run.bat" and do the installation as you normally do.

Enabling Telnet
Installing Telnet Server and Client on Raspberry Pi

sudo apt-get install telnetd
sudo /etc/init.d/openbsd-inetd restart
sudo apt-get install telnet

Now you can telnet into your Raspberry Pi and also telnet from your Raspberry Pi

Samba NAS - Network-attached storage

Share your Raspberry Pi's files and folders across a network

You can share your Raspberry Pi's files and folders across a network using a piece of software called Samba, a Linux implementation of the Server Message Block protocol. You'll need to install this software:

$ sudo apt-get install samba samba-common-bin

Samba contains the SMB protocol, support for the Windows naming service (WINS), and support for joining Windows workgroups. A workgroup is a group of computers on a local network that can access eachother's folders. Samba-common-bin contains a tool that you'll need to register users with Samba. Once these packages have finished installing, you need to edit the Samba configuration file:

$ sudo nano /etc/samba/smb.conf

Find the entries for workgroup and wins support, and set them up as follows:

workgroup = your_workgroup_name
wins support = yes


The name of the workgroup can be anything you want, as long as it only contains alphabetical characters, and it matches the name of the workgroup that you want to join.

You also need to add the following section of code to smb.conf:

[pihome]
comment= Pi Home
path=/home/pi
browseable=Yes
writeable=Yes
only guest=no
create mask=0777
directory mask=0777
public=no


Scroll down smb.conf until you see a section called Share Definitions, and add this code there. The path should point to the drive or folder that you want to share. I've set 'only guest' and 'public' to 'no' so that Samba prompts for a password when I visit the folder that I've shared. This means that when I'm using a Windows PC, I can login to the shared folders on my Pi, and I'll have the same read/write permissions that user pi has.

Now type this command in a terminal, and enter pi's password twice:

$ smbpasswd -a pi

If you have a PC or laptop connected to your workgroup, you should be able to see your Raspberry Pi in Windows
Explorer under Network.

Here the log of the installation of the python code and the test:

pi@raspberrypi ~ $ sudo apt-get install git-core
Reading package lists... Done
Building dependency tree
Reading state information... Done
git-core is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 5 not upgraded.
pi@raspberrypi ~ $ git clone https://github.com/sivel/speedtest-cli.git
Cloning into 'speedtest-cli'...
remote: Counting objects: 208, done.
remote: Compressing objects: 100% (133/133), done.
remote: Total 208 (delta 108), reused 167 (delta 74)
Receiving objects: 100% (208/208), 37.67 KiB, done.
Resolving deltas: 100% (108/108), done.
pi@raspberrypi ~ $ cd speedtest-cli/
pi@raspberrypi ~/speedtest-cli $ python speedtest_cli.py
Retrieving speedtest.net configuration...
Retrieving speedtest.net server list...
Testing from Cablecom GmbH (178.82.157.75)...
Selecting best server based on ping...
Hosted by SolNet (Solothurn) [59.31 km]: 289.361 ms
Testing download speed........................................
Download: 36.04 Mbit/s
Testing upload speed..................................................
Upload: 3.28 Mbit/s
pi@raspberrypi ~/speedtest-cli $


Special: Remote Current Measurements