Monday 23 December 2013

Python, Gmail notification


In this post I will start exploring Python. I will obviously start off with a simple example (which I sort of borrowed)

Before using python on a Raspberry pi, python will need to be installed:


sudo apt-get install python python-dev python-pip
sudo easy_install -U distribute  

More than likely you will use python in conjunction with your Pi's GPIO, in which case you will need the rpi.gpio python module. The command below will alsoload the feedparser which alows RSS and atom feeds to be parsed into the python script.


sudo pip install feedparser RPi.GPIO
Downloading/unpacking feedparser
  Downloading feedparser-5.1.3.tar.bz2 (202Kb): 202Kb downloaded

Please note, that the script below is based on python 2.7. I found this a scripta good start to play around with python and GPIO at the same time. What thescript does, is check for the email account for new emails and if a new unread item is in the inbox, it will turn on the LED on pin 12.
   





First of all, the RPi.GPIO and feedparser modules are loaded





import RPi.GPIO as GPIO, feedparser    <load GPIO and feedparser module
USERNAME="username@gmail.com"
PASSWORD="password"
GPIO_PIN=12     <set the string
GPIO.setmode(GPIO.BOARD)
GPIO.setup(GPIO_PIN, GPIO.OUT)
newmails = int(feedparser.parse("https://" + USERNAME + ":" + PASSWORD + "@mail.google.com/gmail/feed/atom")["feed"]["fullcount"])
set the integer "newmails"
if newmails > 0: 
 GPIO.output(GPIO_PIN, True) <LED on pin 12 on
else: 
 GPIO.output(GPIO_PIN, False)<LED on pin 12 off


















source:  
http://mitchtech.net/raspberry-pi-physical-gmail-notifier/

I have found that when running the script and running it's python debug, I initially got an error message suggesting to run the script as root. In order to do this, I start up IDLE (Or IDLE 3 for Python 3.3 users) from a terminal or root terminal and just enter sudo idle/idle3 or idle/idle3 respectively.

oh yeah, almost forgot, here is the bread board pin out.



Merry Xmas!













Friday 29 November 2013

Raspberry Pi Windows Shared folders, Samba/CIFS client,

Another post that I has lying around in scratch book status for ages, so time to finalise it. This post is based on version 3.6.11+, most of my Raspi is unchanged an very stock standard, so I would assume mounting your drive should work using my instructions below.

SAMBA was originally SMB Server - but the name had to be changed due to SMB Server being an actual product. SMB was the predecessor to CIFS. SMB "server message block" and CIFS "common internet file system" are protocols. Samba implements CIFS network protocol. This is what allows Samba to communicate with (newer) MS Windows systems. Typically you will see it referred to SMB/CIFS. However, CIFS is the extension of the SMB protocol so if someone is sharing out SMB via Samba to a legacy system still using NetBIOS it will typically connect to the Samba server via port 137, 138, 139 and CIFS is strictly port 445 (You will see this is you spin up wireshark on a test machine). 

First of all, verify if the cifs module is installed

pi@raspberrypi ~ $ dpkg-query -S cifs-utils
cifs-utils: /usr/share/doc/cifs-utils
cifs-utils: /usr/share/doc/cifs-utils/changelog.gz
cifs-utils: /usr/share/doc/cifs-utils/NEWS.Debian.gz
cifs-utils: /usr/share/doc/cifs-utils/README
cifs-utils: /usr/share/doc/cifs-utils/changelog.Debian.gz
cifs-utils: /usr/share/doc/cifs-utils/copyright


To verify if the samba software has been installed,  by issuing:

pi@raspberrypi ~ $ dpkg -l | grep samba-common
ii  samba-common      2:3.6.6-6+deb7u1      all common files used by both the Samba server and client

Or issue   dpkg -p samba-common  to check its dependencies. 


First all, you will create a mount point, this is nothing more then a directory that will contain the contents of the shared drive or folder once it is mounted.

Do this by creating a directory, this cold be in your home folder or where ever you want, I have chose to use /mnt/ to keep things consistent:



mkdir /mnt/TPLINK_NAS1

Now mount the drive to the mount point for example:


mount -t cifs -o sec=none //10.19.19.102/volume1/cams/front1 /mnt/TPLINK_NAS1


As you will notice I have used the sec=none switch, this because there is an issue with Raspian, as of version 3.10.19, that causes problems when using username and password. Or so is the consensus on the raspi.org forum:

Source:
http://www.raspberrypi.org/phpBB3/viewtopic.php?f=66&t=60699&p=459637#p459637

What this does is actually explicitly link the /volume1/cams/front1 directory to mnt/TPLINK_NAS, which means everythging that gets dumped into the mount point, essentially gets dumped onto the share. Very much like mapping shares in Windows, but better.


Permanent shares at start up. Well of course the above mount command lasts till reboot, or otherwise explicitly unmounted. In order to mound a share to a mount point at start up. the place to do this is:


/etc/fstab

I have edited fstab similar to the mount command, as per below

proc            /proc           proc    defaults          0       0
/dev/mmcblk0p5  /boot           vfat    defaults          0       2
/dev/mmcblk0p6  /               ext4    defaults,noatime  0       1
# a swapfile is not a swap partition, so no using swapon|off from here on, use  dphys-swapfile swap[on|off]  for that
//10.19.19.102/volume1/cams/front1 /mnt/TPLINK_NAS1 cifs guest,users,uid=1000,auto,_netdev 0 0

To verify If your mount is functioning, issue:

cat /proc/mounts


Goodluck

Sources:

Full book on Samba:

http://www.samba.org/samba/docs/using_samba/ch00.html



https://wiki.samba.org/index.php/Mounting_samba_shares_from_a_unix_client



http://rasspberrypi.wordpress.com/2012/09/04/mounting-and-automounting-windows-shares-on-raspberry-pi/



Raspberry pi, NTP



NTP comes preloaded and preconfigured as part of Raspbian. I found the only thing I had to change was my time zone, as by default it is set to UTC, which is most useless for most people on this planet.

But first a few things on ntp. First of all check if it is running and if it is able to contact any of the ntp servers in the predefined pools. Do this by issuing:


sudo ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*timeserver3.int .GPS.            1 u   24   64    1   45.420   -0.104   1.546
 dns1-ha.au.syra 41.12.6.160      2 u   23   64    1   96.005    2.503   3.844
 ns1.mel.bkb.net 192.189.54.33    3 u   22   64    1   32.453    2.264   2.255


 ds1.zagbot.com  74.189.58.78     2 u   22   64    1   46.616   -0.014   1.141


The way these ntp servers above are picked is in accordance with the NTP server pools configured in /etc/ntp.conf

If you decide to make change to the ntp.conf file, please restart the ntp service to have these changes take effect:

/etc/init.d/ntp restart

OK, so now, how to change the local timezone? In the /etc/ directory, there is a symbolic link, called localtime, that points to the actual  localised time zone  (see below).

lrwxrwxrwx  1 root root      39 Nov 28 23:27 localtime -> /usr/share/zoneinfo/Australia/Melbourne

In my case I am using Melbourne, Australia (because Sydney sucks big hairy monkey balls). You can go to the /usr/share/zoneinfo/ directory and see which geographical part of the world applies to you, then drill into it, until you find the applicable timezone. Easy stuff.

You can also use tzselect to determine your timezone, but note that tzselect has nothing to do with permanently localising your timezone!!! 

pi@raspberrypi /usr/bin $ tzselect
Please identify a location so that time zone rules can be set correctly.
Please select a continent or ocean.
 1) Africa
 2) Americas
 3) Antarctica
 4) Arctic Ocean
 5) Asia
 6) Atlantic Ocean
 7) Australia
 8) Europe
 9) Indian Ocean
10) Pacific Ocean
11) none - I want to specify the time zone using the Posix TZ format.
#? 7
Please select one of the following time zone regions.
1) Lord Howe Island                      8) Queensland - most locations
2) Macquarie Island                      9) Queensland - Holiday Islands
3) Tasmania - most locations            10) South Australia
4) Tasmania - King Island               11) Northern Territory
5) Victoria                             12) Western Australia - most locations
6) New South Wales - most locations     13) Western Australia - Eucla area
7) New South Wales - Yancowinna
#? 5

The following information has been given:

        Australia
        Victoria

Therefore TZ='Australia/Melbourne' will be used.
Local time is now:      Thu Nov 28 23:08:38 EST 2013.
Universal Time is now:  Thu Nov 28 12:08:38 UTC 2013.
Is the above information OK?
1) Yes
2) No
#? 1

You can make this change permanent for yourself by appending the line
        TZ='Australia/Melbourne'; export TZ
to the file '.profile' in your home directory; then log out and log in again.

Here is that TZ value again, this time on standard output so that you
can use the /usr/bin/tzselect command in shell scripts:
Australia/Melbourne



So the last thing to do is to link the /etc/localtime to the timezone you want:


sudo ln -sf /usr/share/zoneinfo/Australia/Melbourne /etc/localtime


To verify if it has worked, issue:

lrwxrwxrwx  1 root root      39 Nov 28 23:27 localtime -> /usr/share/zoneinfo/Australia/Melbourne


Halleee!

Sunday 24 November 2013

Using Motion mmal cam on raspberry pi

Installing, configuring the Motion software on a Raspberry pi. I will keep this post short and sweet and will only touch the basics. There is a tonne of stiff out there already, but I reckon the more it gets shared the more standard it will become.


I started mucking around with motion, when trying to make a wireless surveillance camera. The standard raspistill and raspivid command, simply won't cut the mustard and are only good for testing to see if the camera is pulgged in properly and in general if it captures at all. So forget about those commands.
At this stage I am assuming you have your OS installed (and updated  sudo apt-get update and sudo apt-get upgrade) and you have your Rpi networked up. And 


So what does the Motion software do?

well first of all the motion software is software that detects motion on  a video camera (it will also run on a USB connected webcam). It is created by a Danish guy called Kenneth Lavrsen and the software is open source and free for usage and download. You might want to read up on it in more details on: http://www.lavrsen.dk/foswiki/bin/view/Motion

The problem with the motion software is that it does NOT support the CSI RPi Camera, it is pretty much usable for webcams only, and it works well with it.


mmalcam is an adaptation to the original motion software that allows you to run motion features, using the CSI camera.  Really what is does is start capturing video as soon as "motion" is detected. Motion is a flexible term, because really what motion does is calculate changes in the stills. When a certain threshold has been exceeded, it assumes this is motion (but it could well be the wind moving the trees, which is motion we are not interested in). 



How to install motion-mmalcam?

Of course you will still need to install the motion software.

Type "sudo apt-get install motion".

Note: do not go any further configuring motion. Do not run it as a deamon, (which is where I went wrong the first time), just install the motion software

Type "sudo apt-get install -y libjpeg62 libjpeg62-dev libavformat53 libavformat-dev libavcodec53 libavcodec-dev libavutil51 libavutil-dev libc6-dev zlib1g-dev libmysqlclient18 libmysqlclient-dev libpq5 libpq-dev".

Type "wget https://www.dropbox.com/s/xdfcxm5hu71s97d/motion-mmal.tar.gz".

Type "tar zxvf motion-mmal.tar.gz".




Configuration

Most all configuration exists in the /etc/motion/motion.conf file.


to run mmal:

./motion -n -c motion-mmalcam.conf

You will find there is some tweaking of the mmalcam.conf file involved to get the motion running the way you want it. Because there is a thousand ways do tweak it, I will not get into it. but inbox me if you want a working conf file. I would be more than happy to share it.

An alternative source can be found at:

http://simpledev.webs.com/apps/blog/show/31519067-tutorial-for-motion-detecting-raspberry-pi-security-camera-that-notifies-you-through-an-email-alert-with-a-snapshot-attached-

Saturday 23 November 2013

Raspberry pi setting up wireless


Wireless on a raspberry pi. Written while is was setting up a wireless motion camera. I am a network guy so as a raspberry pi starter; I am on some familiar territory. First thing to do after you plugged in the the wireless dongle, is to check if it is being recognized. To do this issue:


lsusb

pi@raspberrypi ~ $ lsusb
Bus 001 Device 002: ID 0424:9512 Standard Microsystems Corp.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp.
Bus 001 Device 004: ID 148f:5370 Ralink Technology, Corp. RT5370 Wireless Adapter

As you can see in the output above, my wireless dongle has the Ralink 5370 chipset in it. The chipset is important, more important than the dongle's type. This because manufacturers sometimes change chipsets, without proper notice. 
If you wireless dongle is not get recognized  don't continue on yet, and sort that out first. Try a powered hub for instance. If you are already using a powered hub, try another computer to make sure it does work (particularly when its a new dongle).

DRIVERS/MODULES

I decided to check the compatibility of my dongle (Deal Extreme, special cheap peace of %^& for a cheap arse);

http://elinux.org/RPi_USB_Wi-Fi_Adapters


Now check the modules that are loaded. For this, issue:

lsmod 

In my case this gave me the following output:

pi@raspberrypi ~ $ lsmod
Module                  Size  Used by
snd_bcm2835            16432  0
snd_pcm                77728  1 snd_bcm2835
snd_seq                53482  0
snd_timer              20110  2 snd_pcm,snd_seq
snd_seq_device          6462  1 snd_seq
snd                    58744  5 snd_bcm2835,snd_timer,snd_pcm,snd_seq,snd_seq_device
snd_page_alloc          5169  1 snd_pcm
arc4                    1684  2
rt2800usb              14948  0
rt2800lib              55359  1 rt2800usb
crc_ccitt               1530  1 rt2800lib
rt2x00usb              11240  1 rt2800usb
rt2x00lib              42342  3 rt2x00usb,rt2800lib,rt2800usb
mac80211              273979  3 rt2x00lib,rt2x00usb,rt2800lib
cfg80211              184390  2 mac80211,rt2x00lib
rfkill                 18298  2 cfg80211
leds_gpio               2243  0

led_class               3570  2 leds_gpio,rt2x00lib

Because the rt2800usb module was loaded, I decided to configure the interface and see if it would associate with my access point. 

CONFIGURE INTERFACE

Once you have ascertained that the stick is recognised and the module is loaded, configure the interface.

sudo nano /etc/network/interfaces


For example:

iface lo inet loopback
iface eth0 inet dhcp

allow-hotplug wlan0

auto wlan0      <----start up interface at boot
iface wlan0 inet dhcp
   wpa-ssid "ssid"
        wpa-psk "password"
     wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf


(Including the "  " )



With the wpa-roam command, you can configure multiple SSIDs in one single place through the wpa_supplicant.conf file. If your Pi is not going to be roaming between different SSIDs, or will roam and stay withing the same SSID, then there is no need to configure this file, and all configuration can be done in your /etc/network/interfaces file.



Because I prefer not to use DHCP on my pi's (for easier SSH access). Here is the config for a static IP address:


iface wlan0 inet static
address 10.88.33.241
netmask 255.255.255.0
gateway 10.88.33.1
broadcast 10.88.33.255
wpa-ssid "El_Pendejo"
wpa-key-mgmt WPA-PSK
wpa-group TKIP
wpa-psk "Pietjepuk!"

 To manually bounce the interface:

 sudo ifdown wlan0 && sudo ifup wlan0

WPA2
Now, if you can, configure WPA2, as it is more secure. First thing do do, is check if your access point is actually set up to support WPA2 (which most do nowadays)

pi@raspberrypi ~ $ sudo iwlist wlan0 scan
wlan0     Scan completed :
          Cell 01 - Address: 90:F6:52:F6:11:2D
                    Channel:6
                    Frequency:2.437 GHz (Channel 6)
                    Quality=49/70  Signal level=-61 dBm
                    Encryption key:on
                    ESSID:"El_Pendejo"
                    Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s;6
                              9 Mb/s; 12 Mb/s; 18 Mb/s
                    Bit Rates:24 Mb/s; 36 Mb/s; 48 Mb/s; 54 Mb/s
                    Mode:Master
                    Extra:tsf=00000028b9d730c1
                    Extra: Last beacon: 0ms ago
                    IE: Unknown: 000D50756B6B69655F6E65745F3234
                    IE: Unknown: 010882848B960C121824
                    IE: Unknown: 030106
                    IE: Unknown: 2A0100
                     IE: IEEE 802.11i/WPA2 Version 1
                        Group Cipher : TKIP
                        Pairwise Ciphers (1) : TKIP
                        Authentication Suites (1) : PSK

As you can see from the scan output above my AP supports wpa2 on SSID "El_pendejo".


File/etc/wpa_supplicant/wpa_supplicant.conf

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=wheel
#ap_scan=0
#update_config=1

network={
        ssid="YourSSID"
        psk="your-secret-key"
        scan_ssid=1
        proto=WPA2
        key_mgmt=WPA-PSK
        group=CCMP TKIP
        pairwise=CCMP TKIP
        priority=5
}


DIAGNOSTICS


To verify the wireless interface's signal strenth, SSID and associated AP issue:


iwconfig



http://wiki.gentoo.org/wiki/Wpa_supplicant

Tuesday 12 November 2013

Video4Linux2 (V4L2) driver for the Raspberry Pi CSI Camera Board



I will describe the steps necessary to install the  Video4Linux2 (V4L2) driver for the Raspberry Pi CSI Camera Board. 


<elaborate on issues with MOtion>












This driver is necessary to run  a Pi Camera to the CSI port  (see picture, right arrow, next to the ethernet port):





Anyway, you probably by now already know how to hook it up, but just to be sure. 


xxxxxxx

Step 1



pi@raspberrypi ~ $  wget http://www.linux-projects.org/listing/uv4l_repo/lrkey.asc && sudo apt-key add ./lrkey.asc
--2013-11-12 10:09:28--  http://www.linux-projects.org/listing/uv4l_repo/lrkey.asc
Resolving www.linux-projects.org (www.linux-projects.org)... 62.149.140.25
Connecting to www.linux-projects.org (www.linux-projects.org)|62.149.140.25|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1337 (1.3K) [application/pgp-signature]
Saving to: `lrkey.asc'

100%[==============================================================================================================================>] 1,337       --.-K/s   in 0s

2013-11-12 10:09:29 (11.2 MB/s) - `lrkey.asc' saved [1337/1337]

OK
pi@raspberrypi ~ $

<what does this do?>

Step 2

Add the following line to the file /etc/apt/sources.list :
deb http://www.linux-projects.org/listing/uv4l_repo/raspbian/ wheezy main

You can use the nano text editor to do this. What does this do? Well, as part of its operation, Apt uses a file that lists the 'sources' from which packages can be obtained. Apt will be used in the next step. It is beyond the scope of this post to go into APT in any further detail.

Step 3 

Run the following commands


$ sudo apt-get update
$ sudo apt-get install uv4l uv4l-raspicam
The last two commands will upgrade UV4L to the most recent version, if it's already installed.

Now the UV4L core component and the Video4Linux2 driver for the CSI Camera Board are installed. If you occasionally get unexpected errors from the driver, consider updating the firmware with the following command:

$ sudo rpi-update
For detailed information, options, etc... about the modules installed type accordingly:
$ man uv4l
$ man uv4l-raspicam (the manpage is work in progress)
To get a list of available options:
$ uv4l --help --driver raspicam --driver-help
To quicly test uv4l, simply run:
$ uv4l --driver raspicam --auto-video_nr --width 640 --height 480 --encoding jpeg
To take a JPEG snapshot from the Camera:
$ dd if=/dev/video0 of=snapshot.jpeg bs=11M count=1

pi@raspberrypi /etc/init.d $ uv4l --driver raspicam --auto-video_nr --width 320 --height 240 --framerate 2
<notice> [core] Trying driver 'raspicam'
<warning> [core] Driver not found
<notice> [core] Loading external library libraspicam.so
<notice> [core] Trying driver 'raspicam'
<notice> [driver] Raspicam Video4Linux2 Driver v1.9 built Nov  9 2013
<notice> [driver] Selected format: 320x240, encoding: mjpeg, JPEG Video Capture, max. 2 fps
<notice> [driver] ROI: 0, 0, 1, 1
<warning> [driver] NOTE: you might need to run the driver with a RealTime scheduling (--sched-rr), if you are going to use the (slow) Read I/O interface at high resolutions.
<notice> [core] Device detected!
<notice> [core] Registering device node /dev/video0




For a list of other use cases click here.

To terminate a running driver, close the applications and kill the corresponding uv4l process:
$ pkill uv4l
Apart from the driver for the Raspberry Pi Camera Board, the following drivers can be installed:
$ sudo apt-get install uv4l-uvc
$ sudo apt-get install uv4l-xscreen
$ sudo apt-get install uv4l-mjpegstream
To get help:
$ man uv4l-uvc
$ man uv4l-xscreen
$ man uv4l-mjpegstream
---
Useful links:

http://sourceforge.net/projects/mjpg-streamer/
http://www.videolan.org/

Wednesday 6 November 2013

Raspberry Pi useful commands


This post is very much an evolving, growing thing, that I will add to as I am exploring the world of raspberry pi. N00bs galore!

SYSTEM UPDATES and CONFIGURATION


To check the current version of Linux:

uname -r

To get to the raspberry pi config utility: 

sudo raspi-config

To verify the installed packages:

dpkg -l 


Graceful shutdown

sudo shutdown -hP now

sudo shutdown -r now

Reboot:

sudo reboot



Updating the OS and or components:

apt-get update     This will update the list of available packages, but will not actually upgrade them   

apt-get upgrade -y  After updating the list (apt-get update),the package manager knows about the available updates for the software you have installed. This is why you want to run apt-get update first

Removing components is as follows (example):

sudo apt-get remove php5 php5-cgi php5-fpm

sudo apt-get install php5 php5-cgi php5-fpm
packages are stored in /var/cache/apt/archives and you can refer back to them, if needed for instances

# dpkg -i --force-depends /var/cache/apt/archives/"php5-fpm_5.3.6-2_i386.deb"


To find out which packages have been installed:
(can be use in conjunction with "grep")

root@raspberrypi:/var/run# dpkg -l | grep openm
rc  openmediavault                 3.0.59                          all          Open network attached storage solution
rc  openmediavault-flashmemory     3.4.3                           all          folder2ram plugin for OpenMediaVault
ii  openmediavault-keyring         1.0                             all          GnuPG archive keys of the OpenMediaVault archive
rc  openmediavault-omvextrasorg    3.4.16                          all          OMV-Extras.org Package Repositories for OpenMediaVault
rc  openmediavault-resetperms      3.2                             all          Reset Permissions




DIAGNOSTICS:

Bootlog:

dmesg

this will also give you an overview of the linux version, cpu etc. that you have


Tracing

strace -e trace=open motion

Overview of USB devices:

lsusb                  =overview of all USB devices

ldd <ex.motion>      =shows the shared library dependency (motion for ex.)



Most log file can be found in 

 /var/log/   use vi to view the contents of the log files.



FILES AND FOLDERS and MOUNTS

ls -l                                                                  = to view long listings, files and folders permissions
find /usr/lib -iname libjpeg*     =search function 

find . -size -900000c -delete     =search and delete all files smaller than 900000 bytes


cat /proc/mounts                                     =check which devices are mounted



PERMISSIONS, USERS AND USER GROUPS


sudo usermod -a -G video pi  = this will add the user "pi" to the "video" permissions group

su - <user>                                      = change the user for example   su - pi , will change the user to "pi"

id -u pi                                   = will display the UID of the user "pi", in this case 1000.     



Find Files

use grep or find

Example: find . -name "www.conf"





Using vi

Vi uses what is called command mode; this does not allow you to edit a file straight away.

so to insert a line, press "i" and start adding/editing text.

To go back into command mode, press ":"  then you can issue "w" to write the changes and "q" to quit vi.

to remove a hole line, where the cursor is situated: use "dd"

Services

to check and see what services are running use "ps aux"




Camera

raspistill -o image.jpg -w 640 -h 480     =Take a 640x480 shot 



raspivid -t 20000 -o video.h264                      =Capture 20s of h264 video  (t=time o=output file)

Saturday 2 November 2013

Change IP address on Raspberry Pi



check current settings:


$cat /etc/network/interfaces





change:


$sudo nano  /etc/network/interfaces


under interface eth0 inet   (change to static if no dhcp is required)

address 192.168.100.10
netmask 255.255.255.0
network 192.168.100.0
broadcast  192.168.100.255
gateway 192.168.100.254


$sudo reboot




To add DNS server:


sudo nano /etc/resolv.conf     and add the IP addresses of the DNS servers