Wednesday, 15 January 2014

SSH with DSA public key authentication - password less login

After taking pictures on your raspberry pi you might want to transfer the images to your PC. You can use several methods but I'm going to use scp over ssh using password less login. That way I can automate the transfer.
In this example I used following devices

Raspberry pi, IP 192.168.1.10 and user pi
Linux laptop, IP 192.168.1.20 and user foo

Step 1: Create Authentication SSH-Kegen Keys on the raspberry pi

Login to your rpi 192.168.1.10 with user pi to generate a pair of public keys
[pi@raspbmc ~]$ ssh-keygen -t dsa
If you do not want to give your file a name, just press enter. Or you can
Generating public/private dsa key pair.
Enter file in which to save the key (/home/pi/.ssh/id_dsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in .ssh/id_dsa.
Your public key has been saved in .ssh/id_dsa.pub.
The key fingerprint is:
83:8d:95:48:d3:25:11:48:f5:de:53:47:98:54:a2:b6 pi@pi
The key's randomart image is:
+--[ DSA 1024]----+
|         .++==++o|
|         ..+.+o..|
|          +   o.o|
|       o o   . +o|
|      . B   . .Eo|
|         .   .  .|
|            . +  |
|    a   o.       |
|          .      |
+-----------------+

Step 2: Upload Generated Public Keys to – 192.168.1.20

Use SSH from server 192.168.1.1 and upload new generated public key (id_rsa.pub) on server 192.168.1.20 under pi‘s .ssh directory as a file name authorized_keys.
[pi@raspbmc ~]$ cat .ssh/id_rsa.pub | ssh foo@192.168.1.20 'cat >> .ssh/authorized_keys'
foo@192.168.1.20's password:

Step 3: Set Permissions on – 192.168.1.20

Due to different SSH versions on servers, you might need to set permissions on the .ssh directory and the authorized_keys file.
[pi@raspbmc ~]$ ssh foo@192.168.1.20 "chmod 700 .ssh; chmod 640 .ssh/authorized_keys"
foo@192.168.1.20's password: [Enter Your Password Here]

From now onwards you can log into 192.168.1.20 as foo user from server 192.168.1.10 as pi user without password.
[pi@raspbmc ~]$ ssh foo@192.168.1.20

And now you can use scp to transfer files manually or you can automate transfer with script
scp file foo@192.168.1.20:/tmp

Monday, 13 January 2014

cURL timeout problem and solution

One page I made uses cURL to scrape small data from external web page. The problem I faced was when the page was offline for a while, my page did not load.

The fix I stumbled into was to add these three lines to my code.
 curl_setopt($ch, CURLOPT_TIMEOUT_MS, 5000);
 $curl_errno = curl_errno($ch);
 $curl_error = curl_error($ch);
 
Now my page still runs when the remote page is down and I get a error message.
 $url = $_GET['url'];
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $url);
 curl_setopt($ch, CURLOPT_NOSIGNAL, 1);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 /* set timeout in ms */
 curl_setopt($ch, CURLOPT_TIMEOUT_MS, 5000);
 $data = curl_exec($ch);
 $curl_errno = curl_errno($ch);
 $curl_error = curl_error($ch);
 curl_close($ch);

 if ($curl_errno > 0) {
  echo "cURL Error ($curl_errno): $curl_error\n";
  } else {
  echo $match[0][0]);
  }
Wednesday, 8 January 2014

Timelapse with Rapberry Pi camera module

As I have mentioned before I always wanted the camera module for the Raspberry Pi. Until now I had only tried out timelapse using my laptop web camera, and that gave me really crappy images. Now I have the camera module to play with and the images are way better. This is timelapse from 12:00 31. Des 2013 to 12:00 1. Jan 2014 shows 24 Hours in 5 minutes and 26 sec. including some fireworks.



This script will take image every 5 seconds 1440 times leaving you with just enough to create 60 sec timelaps video using 24 fsp

#!/bin/bash
# Timelapse controller for USB webcam
DIR=/home/user/timelapse
x=1
while [ $x -le 1440 ]; do                                                       
filename=$(date -u +"%Y%m%d-%H%M-%S").jpg                                       
raspistill -o $DIR/$filename -w 1280 -h 960 -n -t 1000
x=$(( $x + 1 ))
sleep 5;
done

Now you have 1440 images or the number you entered and next step is to create the video. Copy the images to your computer using scp or rsync, you can add that to your script if the Raspberry pi is connected to network. When you have all your images on your computer you can render video with mencoder. Install mencoder if needed.

sudo apt-get install mencoder

#!/bin/bash
DIR=/home/user/timelapse/
ls $DIR/*.jpg > list.txt
mencoder -nosound -ovc lavc -lavcopts vcodec=mpeg4:aspect=16/9:vbitrate=8000000 -vf scale=640:480 -o $DIR/$(date -u +"%Y%d%m-%H%M-%S").avi -mf type=jpeg:fps=24 mf://@list.txt
rm list.txt

All ideas about improving this code are welcome.
-cheers
Monday, 16 December 2013

Simple python lotto number picker

We had unusually big lotto the other day, so I wanted to play. I decided to write my own lucky number picker. This one picks 5 numbers out of 40 number pool, excludes last drawn number and returns your 5 number row in order.


#!/usr/bin/python
import random   
from random import choice
for k in range(10): # number of row to print
    pick = list(range(1,41))  # size of pool 40 +1 
    numbers = [] 
    i=1 
    for j in range(5): 
        num = random.choice(pick) 
        pick.remove(num) # remove selected number from pool
        numbers.append(num) 
        i=i+1
    print sorted(numbers)   # return sorted row
All ideas about improving this code are welcome.

--cheers
Tuesday, 26 November 2013

Vim editor is frozen after CTRL+S

Many programs and clients use CTRL+S to save documents. In VI and VIM it's quite different. Even though I still press CTRL+S by accident every once in awhile.
To “unfreeze” the console simply hit CTRL+Q

Apparently CTRL+S actually does XOFF, which means the terminal will accept key strokes but won’t show the output of anything. Press CTRL+Q to turn flow-control on (XON)

To disable keyboard listening to XOFFs, add this to your .bashrc (man stty for more options)
stty ixany
stty ixoff -ixon
If you need to send CTRL+S and/or CTRL+Q you can just add
stty stop undef
stty start undef
Monday, 18 November 2013

Capture images on webcam

I own a raspberry pi and have been thinking about getting the camera module, but still I haven't. One of the things I like to do is time lapse.
You can also do this with your web cam on your computer.

First check if your web cam is detected
$ ls -l /dev/video*
crw-rw----+ 1 root video 81, 0 nov 16 17:08 /dev/video0
Then install following programs
sudo apt-get install fswebcam
sudo apt-get install mencoder
Create your time lapse folder and use editor to create your tilme lapse file, the script.
mkdir timelapse
vim runtimelapse
Paste in the following:
#!/bin/bash
# Timelapse controller for USB webcam
frames=1440 # The numer of images to be taken
pause=10 # Dealy in seconds between images
DIR=~/timelapse
x=1
while [ $x -le $frames ]; do
filename=$(date -u +"%Y%m%d_%H%M-%S").jpg
fswebcam -d /dev/video0 -r 640x480 $DIR/$filename
x=$(( $x + 1 ))
sleep $pause;
done;
To make the script executable, use:
chmod 755 runtimelapse
Then run it using:
./runtimelapse
Now you have some number of images, time to create video
cd timelapse
ls *.jpg > list.txt
mencoder -nosound -ovc lavc -lavcopts vcodec=mpeg4:aspect=16/9:vbitrate=8000000 -vf scale=640:480 -o timelapse.avi -mf type=jpeg:fps=24 mf://@list.txt
And now you have time lapse video.

Instructions on making timelapse using raspberry pi


Saturday, 16 November 2013

Fix Ubuntu privacy settings

Ran into interesting thing few days ago.
If you're an Ubuntu user and you're using the default settings, each time you start typing in Dash (to open an application or search for a file on your computer), your search terms get sent to a variety of third parties, some of which advertise to you.

Open a terminal Ctrl+Alt+T. Paste code, press enter. and enjoy your privacy.


V=`/usr/bin/lsb_release -rs`; if [ $V \< 12.10 ]; then echo "Good news! Your version of Ubuntu doesn't invade your privacy."; else gsettings set com.canonical.Unity.Lenses remote-content-search none; if [ $V \< 13.10 ]; then sudo apt-get remove -y unity-lens-shopping; else gsettings set com.canonical.Unity.Lenses disabled-scopes "['more_suggestions-amazon.scope', 'more_suggestions-u1ms.scope', 'more_suggestions-populartracks.scope', 'music-musicstore.scope', 'more_suggestions-ebay.scope', 'more_suggestions-ubuntushop.scope', 'more_suggestions-skimlinks.scope']"; fi; if ! grep -q productsearch.ubuntu.com /etc/hosts; then echo -e "\n127.0.0.1 productsearch.ubuntu.com" | sudo tee -a /etc/hosts >/dev/null; fi; echo "All done. Enjoy your privacy."; fi
 Source https://fixubuntu.com/