Visit my website www.sarathiblog.com New

Sunday, 20 December 2015

Raspberry Pi Apache Server Led ON OFF

Install Apache

Apache is a Popular web server, it can serve HTML files over HTTP, and with additional modules can serve dynamic webpages using scripting language such as PHP. It can be installed on Raspberry Pi.

First install apache2 package

sudo apt-get install apache2 -y

Test the web server 

By default, Apache puts a test HTML file in the web folder. The default webpage is served when you browse to http://localhost/ on the pi itself or http://192.168.10.1 (Your Pi's ip address).

raspberry pi default apache page








This means you have Apache working!

The default webpage is just html file on the system. It is located at /var/www/index.html

Navigate to this directory in the terminal and have a look at what's inside
cd  /var/www/ 

ls -al

This shows the content of the file along with user which owns the file, file size and last modification date & time.

Additional -Install PHP

PHP helps to execute the python file dynamically on the webpage. To install PHP on Raspberry Pi,
sudo apt-get install php5 libapache5-mod-php5

Creating a PHP file to execute the python file on the webpage:

Move to the directory /var/www 

Create a file named index.php and place the following codes.

Download the index.php file
The webpage could look like below



The python file for each button should be like this
LED ON (light1.py)      LED OFF (light0.py)


Monday, 30 November 2015

Access Raspberry Pi Wireless using Laptop (Pi as Access Point)

If you would like to make your raspberry pi as access point or wifi router, the following tutorial would help.

You will need a Wi-Fi adapter for raspberry pi. The following adapter i prefer,

A USB WiFi (http://fkrt.it/gemIouuuuN)adapter.

Edimax wifi adapter for raspberry pi


Getting it done!


Plug the adapter of on the Raspberry pi's USB port. On the terminal enter,

dmesg | more

You will see some thing similar to the following lines at the end..


[3.282651] usb 1-1.2: new high-speed USB device number 4 using  dwc_otg

[3.392819] usb 1-1.2: New USB device found, idVendor=392, idProduct=7811

[3.407489] usb 1-1.2: New USB device strings: Mfr=1, Product=2, SerialNumber=3

[3.420530] usb 1-1.2: Product: 802.11n WLAN Adapter

If you get this, then your OS has recognised the Wi-Fi adapter.

Install the necessary Software


      In order to make the Raspberry Pi to act as a WiFi router and access point we need to install some extra softwares.

hostapd

HostAPD is a user space daemon for access point and authentication servers. That is it will turn the raspberry pi as access point so that the other computers can connect to it. It will also handle security such that we can setup a Wifi password for it.

isc-dhcp-server

isc-dhcp-server is the internet system consortium's implementation of a DHCP server. A DHCP server is responsible for assigning address to computers and devices connection to the Wi-Fi acess point.

Run the following command to install DHCP.

sudo apt-get install isc-dhcp-server

Next up is the HostAPD software. Because our USB stick needs an access point driver, we need to install a custom version compiled with the driver we need.

wget https://github.com/jenssegers/RTL8188-hostapd/archive/v1.1.tar.gz

tar -zxvf v1.1.tar.gz


cd RTL8188-hostapd-1.1/hostapd

sudo make

sudo make install


Configure the ISC-DHCP-Server

To configure the DHCP server open the file /etc/dhcp/dhcp.conf in your favorite text editor.

sduo nano /etc/dhcp/dhcp.conf

Find the following section and comment it out by placing a hashtag at the beginning of the line.

option domain-name "example.org";

option domain-name-servers nsl.example.org, ns2.example.org;


Next find the section below and un-comment authoritative.

#If this DHCP server is the official DHCP server for the local

#network, the authoritative directive should be uncommented.

#authoritative;


The file should look as like below:



Next we need to define the network and network address that the DHCP server will be serving. This done as shown below at the end of the file.




This will enable the DHCP server to hand out the ip address from 192.168.10.10 to 192.168.10.20 in its own local network. This configuration will use the Google DNS servers at 8.8.8.8 and 8.8.4.4.

To save the file press Ctrl+O this will write the file to the disk - afterwards you can exit nano by Ctrl+X.

Next file to edit is /etc/default/isc-dhcp-server


sudo nano /etc/default/isc-dhcp-server

Scroll down to the line saying interfaces and update the line to say:

INTERFACES="wlan0"

The last step in configuring DHCP server is to configure ip address for the wireless network adapter. This is done in the file /etc/network/interfaces before opening it make sure the WLAN is down.

sudo ifdown wlan0

sudo nano /etc/network/interfaces


Change the file to look like this:



This will make the wireless adapter take the address 192.16810.1.

Configuring HostAPD

To configure HostAPD, open the file called /etc/hostapd/hostapd.conf

sudo nano /etc/hostapd/hostapd.conf

The standard configuration will create a new wireless network called wifi with the password YourPassPhrase . You can edit the parameter "ssid=wifi" to the SSID name you want and "wpa_passphrase=YourPassPhrase" to your own password.

Enable NAT

The last step before we can start the access point is setting up Network address Transmission(NAT).
Open /etc/sysctl.conf with 

sudo nano /ect/sysctl.conf

Scroll down to the last line of the file and add the line:

net.ipv4.ip_forward=1

Next start the translation right away by running:

sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"


Start the wireless network by running;

Next step up the actual translation between the ethernet port called eth0 and the wireless card called wlan0.

sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

sudo iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED, ESTABLISHED -j ACCEPT

sudo iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT

Thus NAT configured

Starting your wireless router

sudo service -sc-dhcp-server start
sudo service hostapd start

At this point you should be able to find your wireless network access on your laptop.

Final

The point it is not so cool you have to login every time it reboots to start the HostAPD and DHCP software...

To avoid this run the following commands:-

sudo update-rc.d hostapd enable
sudo update-rc.d isc-dhcp-server enable

To avoid to configure NAT every time the Raspberry Pi reboots you can do the following.
Run this command to backup the NAT configuration.

sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"


Add the following to the end of the file /etc/network/interfaces to restore the configuration
when the network interfaces comes up

up iptables-restore < /etc/iptables.ipv4.nat


The file should look like:

sudo nano /etc/network/interfaces



Finally reboot the system

sudo reboot