GPIO control using Python


The RPi.GPIO Python library allows you to easily configure and read-write the input/output pins on the Pi’s GPIO header within a Python script. Thankfully this library is now including in the standard Raspbian image available from the Foundations Download Page.

If you are using a fresh image you don’t need to install it but I’ve kept the instructions here in case you ever want to try a manually installation.

Method 1 – Install from repository

If the package exists in the Raspbian repository is can be installed using apt-get. First you need to update the available package versions :

sudo apt-get update

Then attempt to install the RPi.GPIO package :

sudo apt-get install rpi.gpio

If it isn’t already installed it will be installed. If it is already installed it will be upgraded if a newer version is available.

Method 2 – Manual Installation

The package is available from and the current version is 0.5.11 (February 2015). If this version is updated you will need to make appropriate changes to the version number in the commands below.

Step 1 – Download the library


Step 2 – Extract the archive to a new folder

tar -xvf RPi.GPIO-0.5.11.tar.gz

Step 3 – Browse to the new directory

cd RPi.GPIO-0.5.11

Step 4 – Install the library

sudo python install

Step 5 – Remove the directory and archive file

cd ~
sudo rm -rf RPi.GPIO-0.*

This will now mean you can use the library within Python.

Example Usage

import RPi.GPIO as GPIO
# to use Raspberry Pi board pin numbers
# set up the GPIO channels - one input and one output
GPIO.setup(11, GPIO.IN)
GPIO.setup(12, GPIO.OUT)
# input from pin 11
input_value = GPIO.input(11)
# output to pin 12
GPIO.output(12, GPIO.HIGH)
# the same script as above but using BCM GPIO 00..nn numbers
GPIO.setup(17, GPIO.IN)
GPIO.setup(18, GPIO.OUT)
input_value = GPIO.input(17)
GPIO.output(18, GPIO.HIGH)

Example to run script on key press

import RPi.GPIO as GPIO
import time
import os
#This will import the necessary libraries in the GPIO namespace and set the pin numbering to correspond to your breakout board.
#Now we need to set the pin as input, pin 18 is used in this example.
#Reading the pin is now as easy as:
input = GPIO.input(18)
#If we want to print Button Pressed each time a button is pressed
while True:
 if (GPIO.input(18)):
 print("Button Pressed")
 print("Button Not pressed")
 # use 0.1 for 1 ms delay


Turning Raspberry Pi into a PLC — Creative ideas

Blinking LEDs example This tutorial will show you how to include Raspberry Pi in your hobby or DIY project in the role of a standalone central control unit. In fact, you will turn your Raspberry Pi into a Programmable Logic Controller (PLC), which is how such control units are called in the industry. At the […]

via Turning Raspberry Pi into a PLC — Creative ideas


  Learn to set up a LAMP (Linux, Apache, MySQL, PHP) stack on your Raspberry Pi and configure it to work as a web server. You’ll download and install WordPress and set up a basic website…


How to NFS root on the raspberry pi

Top of Form

I’d like to start writing a few questionably useful guides for people. We’re going to start with this one cause I find it’s actually quite a handy thing to do.. This is probably for the slightly more experienced user.

I’ll probably be editing this a few times, just to make sure I get it reasonably right, and that the formatting isn’t terrible. Or to add/change the methods of doing things. There’s almost always more than one way to do something in Linux.

So you’ve got your raspberry pi with a 4GB SD card, or like me find yourself saddled with a bunch of otherwise nearly useless 2GB or smaller cards that are really slow. Good news! The raspberry pi really only needs a 64MB boot partition (and a bit of swap if you want). For this guide, I’m going to assume you’re using at least a 4GB card as it makes things somewhat easier. I’m also going to assume that you’re using reserved DHCP addresses for your raspberry pi and other things. I don’t recommend this for those using wireless, wired network only.

It can also make backing up and restoring them a snap. Should you ever find yourself wanting to fully run it off the SD card again, you can just as well copy it back later.

You’ll need a running NFS server to do this. It can be another raspberry pi if you want it to be. I’m not going to go over setting it up at this point. You’ll want to have gparted installed somewhere as well since we’ll be changing the partitioning of the SD card at the end.


Step 1: Write your raspbian image to the SD card, and do your initial boot and setup as you normally would. Make sure it all works.


Step 2: Install nfs-common. If it needs to upgrade anything while doing this then let it. We can save a dist-upgrade for later. Run:

sudo apt-get update

sudo apt-get install nfs-common


Step 3: We need to remove dphys-swapfile. This is a must. Swap generally expects direct access to the file (or partition even), and NFS prevents this. If you don’t do this the system will crash at some point when it starts swapping memory. Run:

sudo apt-get purge dphys-swapfile dphys-config

sudo update-rc.d -f dphys-swapfile remove

Then reboot. Next we need to check that no other swap devices are in use. Run “swapon -s”. You should get a single line like this:

Filename Type Size Used Priority

If that’s the case, you can go ahead and run “sudo rm /var/swapfile” to make sure the dphys-swapfile is gone.


Step 4: Check/edit /etc/sysctl.conf. This step involves upping vm.min_free_kbytes. When the first raspberry pis came out they had this set to something like 4MB or so. With 256MB RAM every little bit counted. However this seemed to be causing more issues with the network if you used it heavily. So, run “sudo nano /etc/sysctl.conf”. Scroll to the bottom. There will probably be a comment line “# rpi tweaks”. There will probably be a vm.swappiness=1 and a vm.min_free_kbytes line. If not add it:

vm.min_free_kbytes = 16128

This will try and make sure that when your system is busy it still has enough memory to do the things it needs to. Some people have set it to 24576, but I found my issues went away when set to 16128. You can up the swappiness to 40 if you want now as well. We’ll be re-adding swap in later.


Step 5: Remove the / partition from /etc/fstab. Open up “sudo nano /etc/fstab” find the line that looks like this and remove it:

/dev/mmcblk0p2 / ext4 defaults,noatime 0 1


Step 6: Copy the contents of the root partition on the SD card to your NFS server. Power off your raspberry pi, and plug it into a reader on your NFS server. This needs to be on a proper linux supported partition, don’t use something exFAT or NTFS.

Make a folder somewhere, you’ll have to decide this on your own. In my case, I’m running it off of some external 2TB drives I setup into a raid1 when I got them back from warranty repair. You can also set the permissions how you want, but you’ll notice the uids probably won’t match up (aside from root). Either way, your sequence of commands is going to look somewhat like this, with extra care on the cp arguments:

mkdir rpi-nfs1

chmod 777 rpi-nfs1

cd rpi-nfs1

sudo cp -rapv /mnt/<SD CARD ROOT LOCATION> .


Step 7: Add your export to NFS. We need to export the above folder we copied into, with the full path. Run “sudo nano /etc/exports” and add the following line with your corresponding reserved IP address in:

/mnt/external2tb/rpi-nfs1 <IP ADDRESS HERE>(rw,no_root_squash,async,no_subtree_check)

Save and exit. Then export it by running “sudo exportfs -rav”. Make sure it shows up. It’ll say something like this:

exporting <IP ADDRESS HERE>:/mnt/external2tb/rpi-nfs1


Step 8: Still with me? Still got that SD card inserted into your NFS server? Good. Now we’re going to edit some files on the boot partition of the SD card. MAKE SURE YOU’RE EDITING THE ONES ON THE SD CARD AND NOT THE COMPUTER YOU’RE USING. First file that needs some fixing up is /boot/cmdline.txt. This file is important, and it needs to all be on one line, no returns to make it look pretty.

Yours probably looks exactly like this:

dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait

We now need to tell it we’re using NFS, and where it is, with a few other things. You’ll want to make yours look more like this:

dwc_otg.lpm_enable=0 console=tty1 root=/dev/nfs nfsroot=<YOUR NFS SERVER IP>:/mnt/external2tb/rpi-nfs1,tcp ip=dhcp rootfstype=nfs smsc95xx.turbo_mode=N elevator=deadline rootwait

We’ve changed the root and the rootfstype. We also add in ip=dhcp so that the raspberry pi knows to get it’s IP by dhcp. We’ve also put in smsc95xx.turbo_mode=N, which is a fix for some issues the network chip has/had while under heavy loads. We want this thing rock solid when we’re done.


Step 9: Remove the / ext4 partition from the SD card, convert to swap if desired. You can skip this part if you want, but I recommend doing it. Open up gparted. Select your SD card. Select the 2nd, much larger partition. Click Partition->Format To>Linux-Swap. Once you think you’ve got it right, click the “Apply All Operations” checkmark, or select Edit>”Apply All Operations”


Step 10: Almost done. Panic and worry that you forgot something in the above steps.


Step 11: Unmount/eject the SD card from the computer you’ve been copying/editing on. Stick it into your raspberry pi and turn it on. If you’re lucky it should all start up and run just like before, except over the network now.


Step 12: What about that swap partition? Lets re-enable swap. Run “ls -l /dev/disk/by-id”. You’ll get an output like this:

lrwxrwxrwx 1 root root 15 May 28 15:14 49273d6c-2a19-4dbf-a7b5-58075bc6a1c2 -> ../../mmcblk0p2

lrwxrwxrwx 1 root root 15 May 8 15:40 C522-EA52 -> ../../mmcblk0p1

mmcblk0p2 should be the swap partition. Lets check. Run “sudo fdisk -l”. You should get something like this:

Device Boot Start End Blocks Id System

/dev/mmcblk0p1 8192 122879 57344 c W95 FAT32 (LBA)

/dev/mmcblk0p2 122880 3788799 1832960 82 Linux swap / Solaris

Okay! Now we take that long hex number we got from the ls, and use that in /etc/fstab. That’s our UUID for the swap partition. Add the line to /etc/fstab, yours will have a different ID number, but otherwise it’s the same:

UUID=49273d6c-2a19-4dbf-a7b5-58075bc6a1c2 none swap sw 0 0

Once that’s in your fstab file, run “sudo swapon -a”. Then check it. Run “swapon -s”. You should see it’s now using the leftover space on the SD card as swap. Something like this, except this is from a 2GB card:

Filename Type Size Used Priority

/dev/mmcblk0p2 partition 1832956 0 -1


Step 13: All done. Run your sudo apt-get update, sudo apt-get dist-upgrade, sudo apt-get clean and enjoy your fancy new NFS root using raspberry pi. Check your disk usage by running “df -h”. You’ll now get something like this, the X’s replaced by your NFS server’s IP address.

Filesystem Size Used Avail Use% Mounted on

rootfs 1.8T 70G 1.7T 5% /

192.168.X.XXX:/mnt/external2tb/rpi-nfs1 1.8T 70G 1.7T 5% /

devtmpfs 87M 0 87M 0% /dev

tmpfs 19M 184K 18M 1% /run

tmpfs 5.0M 0 5.0M 0% /run/lock

tmpfs 395M 0 395M 0% /run/shm

/dev/mmcblk0p1 56M 19M 38M 34% /boot

Look at that! Suddenly I’ve got 1.7TB to work with now!


Feel free to complain that I should’ve written a proper guide on how to software raid & samba/nfs on the raspberry pi first. NFS/samba setups are boring, this is way more fun. So you get this one first.

edit: With the Jessie update you need to add ‘rw’ to your cmdline.txt after the ‘nfsroot=***’ bit. Otherwise it will mount it read only.


Reference: of Form

Raspberry Pi with NFS boot


Setting up the Raspberry Pi to boot from Root NFS

To assist with more rapid development, I prefer to mount the root filesystem from NFS.

Understanding the Raspberry Pi Boot Process details the bootloaders and files that are required to boot a Raspberry Pi.

I have a handful of 16 megabyte (yes, megabyte) MMC cards lying around that makes perfect boot media for this task.

Preparing the MMC

The Raspberry Pi will boot from either FAT32 or FAT16. The minimum volume size for FAT32 is 32 megabytes, hence if I’m to work with a 16 megabyte MMC, I must format the card with a FAT16 filesystem.

Partition the card using fdisk where /dev/sdb is your SD/MMC block device:

sudo fdisk /dev/sdb

You will now be presented with a menu. First select p to print the current partition table, ensuring you have selected the right disk.

If the disk is correct, delete any existing partitions.

Now select n to add a new partition. Select primary partition and any other defaults.

The default partition will now be set for linux. Select t to change a partition’s system ID and enter 4 (FAT16 <32M)

Finally, write the partition table to disk and exit by selecting w.

Now format the card:

sudo mkdosfs /dev/sdb1

Mount the new file system, so you can add the required files:

sudo mount -t msdos /dev/sdb1 /media/card

Copy the following files over to your freshly formatted SD/MMC card:






You can obtain the files from an existing Raspberry Pi installation, or you can download them from the Raspberry Pi Git Repository:




Create a cmdline.txt file containing the following parameters:

console=ttyAMA0,115200 root=/dev/nfs rw nfsroot= ip= rootwait

The config.txt file can be omitted if you are using default parameters.

Prepared Image

Below is a prepared FAT16 image. It can be restored using the Win32 Disk Imager

Reference from:

Cherokee Web server on Raspberry Pi


Why to go for another web server, if there are lot of web servers we discussed and available in market. The simple answer to that question is, embedded light weight web server with IoT support.

In this topic, I am not going to say a again from scratch on how to use raspberry pi. If you are at really having a fresh Raspberry pi board and want to do this exercise.Please read my blog on “getting started with raspberry pi for Windows users”

Installation Guide

First of all if you are interested in installation of Cherokee web server, go the reference at the end of this document. Do remember to read the dependencies for installation of Cherokee. After installation of Cherokee the most important work is to provide the virtual path of file system.

How it works:

Basically after successful installation of server need to do a command

sudo Cherokee –admin –b

Keep the user name and password to enter on webpage.

During configuration Cherokee cannot able to write into default path. So reason is simple as pi user do not have the credential to access. So to avoid that type of issues two ways can be adapted, 1st option is preferable.


Make all default paths with in Pi user, I have used the following path



Provide credential to Cherokee by login as root user.


Now using putty, I typed the following command at terminal. Then for admin the OTP will be generated.

 pi@raspberrypi ~ $ sudo cherokee-admin -b

[01/06/2013 06:37:44.734] (warning) rrd_tools.c:121 – Could not find the

    rrdtool binary. | A custom rrdtool binary has not been defined, and the

    server could not find one in the $PATH.

 [01/06/2013 06:37:44.734] (warning) socket.c:479 – IPv6 support is disabled.

    Configuring for IPv4 support only. | The issue seems to be related to your system.

Cherokee Web Server 1.2.99 (Nov  4 2012): Listening on port ALL:9090, TLS

disabled, IPv6 enabled, using epoll, 4096 fds system limit, max. 2041

connections, caching I/O, single thread


  User:              admin

  One-time Password: kgEF1C7KMv1fCYNS

 Web Interface:

  URL:               http://localhost:9090/

 The next step is open internet explorer, type the IP address along with port 9090.

If everything is OK, then the server will start and then the button will change to stop server.

After that I have copied all web support package of controller /home/pi/New1

Then using internet explorer, I have entered the IP address of device.

Note: Remember the default port for web server is 80

Then the web pages start loading without any issues as shown below. I have extended my test by using chrome, firefox and the things are quite normal.

I had the initial issue in firefox that the configuration file was not found. But using internet explorer, I could able to resolve that issue.

First I have used the following command:

cherokee-admin –b

The result is shown below.

Can you able to guess the issue? It is very simple only you need to run the cherokee admin as power user. That means the command should like this:

sudo cherokee-admin –b

Have more question? Write back to me or comment.

Crunch Password list generation in Kali Linux

In this tutorial we will be using the Crunch Password list generation tool in Kali Linux. Crunch is an easy to use tool for generating a custom made password list used for brute force password cracking. Crunch comes as a standard tool in Kali Linux. This tutorial shows you how easy it is to generate a password list containing all combinations of 4 letters, 5 letters and a password list containing 5 letters followed by a year. You can also use Crunch to generate password lists based on default router passwords as demonstrated in a few other tutorials. UPC Broadband routers use 8 capital letters as default password for Wifi and TP Link routers use the 8 digit default WPS PIN. Both can be easily generated with Crunch.

Crunch password list generation

Let’s use the following command to have Crunch generate a wordlist containing all combinations of 4 letters:

crunch 4 4 ABCDEFGHIJKLMNOPQRSTUVWXYZ -o /root/Desktop/wordlist.txt

The generated Crunch password list has a size of 2 MB

Let’s use the following command to have Crunch generate a wordlist containing all combinations of 5 letters:

crunch 5 5 ABCDEFGHIJKLMNOPQRSTUVWXYZ -o /root/Desktop/wordlist.txt

The generated Crunch password list has a size of only 67 MB

The next wordlist we are going to make is a wordlist containing all possible combinations with 5 letters follow by 1980. The 1980 represents a year of birth for instance, commonly used as a password in combination with a name:

-t @@@@1980 -o /root/Desktop/wordlist.txt

The generated Crunch password list has a size of only 3 MB.

Let us take another example where we need to create India cellphone number wordlist starting from 9000000000 to 9999999999

crunch 10 10 0123456789 -o /root/Desktop/wordlist.txt -s 9000000000

The generated Crunch password list has a size of 10 GB. After compression it became 2 GB.