Sunday 29 July 2012

Home Automation For Under £30 - Part 2



PART 2 of the Home automation project. Again, using my limited knowledge, I have sellotaped together a Raspberry PI with the using of the GPIO pins to automate the process of switching home lighting (or LED in this case to emulate home lighting).

The Idea here is to emulate the upstairs and downstairs landing lighting which works in a process where either one can be switched.

 Additionally, we will use the Asterisk PBX (http://www.asterisk.org) and a Softphone client (http://www.zoiper.com) on the Android to control the relay circuit. All instructions will be available on the here soon....

Saturday 23 June 2012

Friday 8 June 2012


Carputer or CarPC using a Raspberry PI, RaspbMC beta - XMBC

Friday 1 June 2012

Setup And Run Asterisk and FreeBPX on A Raspberry PI

Finally recieved the Raspberry PI on Friday (25th May 2012) after a two and half year wait! My plan, which I had all that time to think about, discuss with others and have ready is still not concrete but initially, I have order 3 devices to play with.

As a Asterisk Engineer, one of the first things I wanted to tackle was to install the latest Asterisk and FreePBX on it. I build Asterisk based PBX systems for a living, so this shouldn't be that difficult. I responded on http://www.raspberry-asterisk.org/ to say I was going to attempt this and report back but so far the Admin has not got back to me on how I can add there so I have written this up here.



This guide is for complete newbies in getting the Raspberry PI to get set-up and run as a IP PBX using Asterisk and FreePBX
 

I intended to install Asterisk and FreePBX 2.9 (this is a matter of choice as I am accustomed to the layout before 2.10). I purchased a 8GB SD card instead of 2GB, which is sufficient for just Asterisk 1.8 but not enough for FreePBX. Here's the install process....

Install Squeeze - Get The PI Ready
Anyone with a Raspberry PI in their hands should first go though the following process and set up Debian Squeeze. For my setup, I had an 8GB SD Card. I downloaded Debian Squeeze for the PI from here and the Win32DiskImager from here, Stuck my SD card in to my windows laptop and fired up Win32 Disk Imager. Next, Unzipped the debian file and browsed to it in Win32 Disk Imager, select the correct drive letter in the drop down and let the imaging start. This is by far the quickest and dirtiest way to get Squeeze on to the SD.

After completion, I launched a Live CD of GParted, which is an amazing tool to partition disks. I increased the swap partition to 300Mb and moved it to the end of the drive. I then rezised the rootfs partition to used all the unallocated space on the drive.

Alternatively, you can follow the information on this great guide to go through and set up the drive properly and learn how to do the GParted stuff I mentioned above. 

Internet Access on the Raspberry PI

If you connect the PI to your local network which has a ADSL/CABLE router or mode, then it should have DHCP on. DHCP will provide it an IP address and this will be displayed on-screen when the PI boots up before the log in screen. If it doesnt, you need to get an IP and add this in manually. To do this, log in to the PI using the default username: pi and password: Raspberry and type the following command:

sudo vi /etc/network/interfaces


Using VI : Edit the file using by pressing 'a' to insert text. Once editied, press escape to come out of editing mode.

The IP parameters should be as follows with your specific IP address information:

iface eth0 inet static
address 192.168.1.100
gateway 192.168.1.1
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255


NOTE: Dont forget to comment/remove line stating:  

iface eth0 inet dhcp

Additionally you will need to set up DNS in order to get out on the internet. This setting should be done in the resolve.conf file as follows:

sudo vim /etc/resolv.conf

This should say something like:

nameserver 8.8.8.8
nameserver 192.168.1.1

to test network connectivity, at the command line, type in

ping <your favourite serach engine.com>

Press CTRL+C to stop pinging.



Update Asterisk Install Pre-requisites

To start with, update the system and install the required componants by copying the command below:

sudo apt-get update

sudo apt-get -y install make gcc g++ libxml2 libxml2-dev ssh libncurses5 libncursesw5 libncurses5-dev libncursesw5-dev linux-libc-dev sqlite libnewt-dev libusb-dev zlib1g-dev libmysqlclient15-dev libsqlite0 libsqlite0-dev bison openssl libssl-dev libeditline0 libeditline-dev libedit-dev mc sox libedit2 libedit-dev curl libcurl4-gnutls-dev apache2 libapache2-mod-php5 php-pear openssh-server build-essential openssh-client zlib1g zlib1g-dev libtiff4 libtiff4-dev libnet-telnet-perl mime-construct libipc-signal-perl libmime-types-perl libproc-waitstat-perl mpg123 libiksemel-dev php5 php5-cli mysql-server php5-mysql php-db libapache2-mod-php5 php5-gd php5-curl mysql-client vim

Dowload and Extract Asterisk

To Download and Extract asterisk, enter the commands as below:

cd /usr/src 
sudo wget http://downloads.asterisk.org/pub/telephony/asterisk/asterisk-1.8-current.tar.gz 
sudo tar xvfz asterisk-1.8-current.tar.gz 
cd asterisk-1.8*

Set Up Correct Compilation For ARM Platform
Using your favorite editor, make the change below. I prefer VIM, an enhanced version of the popular VI command line editor:

sudo vim makeopts.in

Search for the word "proc=" in the file and change this to read "proc=arm". For those new to VIM, move to the end of this line using the arrow keys and press 'a', then remove all the chractors before the = sign using back space and type in 'arm'. Finally press escape to come out of editing mode. Then press SHIFT+Z+Z to save and exit the file.

Compile and Install Asterisk
Using the following commands:

sudo ./configure
sudo make
sudo make install
sudo make samples
sudo make config


Finally, restart the PI by typing the fillowing command:

sudo init 6

Congratulations - Asterisk installed is now installed



To test the install, log in to the PI and type the following command:

sudo asterisk -r



If this all works, great...lets move on to installing FreePBX.

Create And Setup Asterisk Users And Groups Permissions

sudo useradd -c "Asterisk PBX" -d /var/lib/asterisk asterisksudo chown -R asterisk:asterisk /var/run/asterisksudo chown -R asterisk:asterisk /var/log/asterisksudo chown -R asterisk:asterisk /var/lib/php/session/


Install Pear and DB
FreePBX uses the php pear repository and specifically requires the database repositor. Enter the commands below to install them:

sudo apt-get install php-pear
sudo pear install db


Download And Extract FreePBX

To Download and Extract FreePBX, enter the commands as below:

cd /usr/src/
sudo wget http://mirror.freepbx.org/freepbx-2.9.0.tar.gz
tar zxvf freepbx*



Start The MySQL Server And Import In The FreePBX Data
Lets start the database server and import in the tables FreePBX will require using th following commands:

sudo service mysql start
cd /usr/src/freepbx*

sudo mysqladmin create asterisk
sudo mysqladmin create asteriskcdrdb
sudo mysql asterisk < SQL/newinstall.sql
sudo mysql asteriskcdrdb < SQL/cdr_mysql_table.sql
sudo mysql





Lets add the mysql user and give it the correct permissions under the MySQL prompt:

GRANT ALL PRIVILEGES ON asteriskcdrdb.* TO asteriskuser@localhost IDENTIFIED BY '******';
GRANT ALL PRIVILEGES ON asterisk.* TO asteriskuser@localhost IDENTIFIED BY '******';

flush privileges;

exit
NOTE where I have put '******', replace with your own password. Please keep a log of this as it will be required later. The password should secure (aplhanumeric with symbols) and in single speech marks as seen above

Now lets secure MySQL with a root password of your choice (See note above).

sudo mysqladmin -u root password '******'


Restart Asterisk With Correct Permissions

cd /use/src/freepbx*
sudo service asterisk stop
sudo ./start_asterisk start


Set The Correct Timezone for PHP
Open the PHP Configuration file:

sudo vim /etc/php5/apache2/php.ini

Under Module Settings, change the timezone accordingly:

;;;;;;;;;;;;;;;;;;;
; Module Settings ;
;;;;;;;;;;;;;;;;;;;


[Date]
; Defines the default timezone used by the date functions
; http://www.php.net/manual/en/datetime.configuration.php#ini.date.timezone


Uncomment date.timezone and set to the correct setting according to the link above. In my case, this was as follows:


date.timezone = Europe/London


To do this, move over the first charactor in the line, press 'a' the backspace to uncomment the line. Then move to the end of the line and type in your timezone after the '='. Press escape to come out of editing mode and save the file using the SHIFT+Z+Z combination.


Setup Apache For FreePBX and Asterisk
Apache user and group will need to be set to asterisk. To do this, run the following command to open the Apache configuration file:

sudo vim /etc/apache2/apache.conf

Find the line that states:

User www-data
Group www-data



Change these to read:

User asterisk
Group asterisk

For those new to VIM, move to the end of this line using the arrow keys and press 'a', then remove all the charactors before the user or group using back space and type in 'asterisk'. Finally press escape to come out of editing mode. Then press SHIFT+Z+Z to save and exit the file.

Additionally, change the default sites settings by:

sudo vim /etc/apache2/sites-available/default

Under tags "<Directory />" and "<Directory /var/www/>" change the following settings:



Options FollowSymLinks
AllowOverride All



For those new to VIM, follow the origional instuction on editing above using the 'a' to go in to editing mode and escape key to come out of editing mode. Remember to save the file using SHIF+Z+Z.

Finally restart apache using the following command:

sudo service apache2 restart



Install FreePBX
To install FreePBX, you will need to specify the username and password to the database we setup earlier, start by typing in the following command:

sudo ./install_amp --username=asteriskuser --password=******

For each of the prompts, just hit enter except when prompted for

Enter the path to use for your AMP web root:
 [/var/www/html]


in this case type the following:

/var/www

Then hit enter. You should see something like this :

Checking for PEAR DB..OK
Checking for PEAR Console::Getopt..OK
Checking user..OK
Checking if Asterisk is running..running with PID: 2591..OK
Checking for /etc/amportal.conf../etc/amportal.conf does not exist, copying default
Creating new /etc/amportal.conf

Enter the hostname of the 'asterisk' database:
 [localhost]

Enter a USERNAME to connect to the Asterisk Manager interface:
 [admin]
Enter a PASSWORD to connect to the Asterisk Manager interface:
 [amp111]
Enter the path to use for your AMP web root:
 [/var/www/html] /var/www

Enter the IP ADDRESS or hostname used to access the AMP web-admin:
 [xx.xx.xx.xx]
Enter a PASSWORD to perform call transfers with the Flash Operator Panel:
 [passw0rd]
Use simple Extensions [extensions] admin or separate Devices and Users [deviceanduser]?
 [extensions]
Enter directory in which to store AMP executable scripts:
 [/var/lib/asterisk/bin]
Created /var/lib/asterisk/bin
Enter directory in which to store super-user scripts:
 [/usr/local/sbin]


One this is complete, lets set the file permissions for FreePBX to be accessable to apache users:

sudo chmod 777 /var/www/* -R

Finally Restart all services
Although in a lot of the cases, service restart is not required, I have had issues in previous versions of FreePBX, therefore as a final step, restart all the service to ensure they start with the correct permissions:

sudo amportal stop
sudo service apache2 stop
sudo service mysql stop

sudo service apache2 start
sudo service mysql start
sudo amportal start

Congratulations - Free PBX is now installed.

 To test the install, open your favourite browser on another PC and browse to the address of your PI