Raspberry Pi – Button as a digital toggle switch

Ability to directly control the GPIO (general purpose input output) pins is one of the reasons the miniature Raspberry Pi is so famous with hobbyist and educationalists.

In this self learning exercise, I will read the status of the button, determine if it is pressed or not and then based on that, switch the LED on or off.

Very basic right? Let’s follow along.

Components

  1. Raspberri Pi 2 Model B (40 pin) with Raspbian OS
  2. Breadboard
  3. Connectors
  4. LED 5mm
  5. Push Button
  6. Resistors – R1 (330 ohm) & R2 (1K ohm)

Wiring It Up

My setup uses the GPIO 23 (pin #16) and GPIO 24 (pin #18), however you can use any GPIO pins for this purpose. If you are using different pins, make sure to update the pin numbers in the program below.

  • GPIO 23 (pin #16) –  Used as input to read the status of button
  • GPIO 24 (pin #18) – Used as output for switching the LED on and off
  • +3.3V (pin #1) – Connected to power rail on the breadboard
  • GND (pin #9) – Connected to ground rail on the breadboard
  • R1 – Connected between LED and ground
  • R2 – Connected between 3.3V power rail and button
20150820-RPI_Push_Button_Toggle_Switch_bb
Push button programmed as a toggle switch to power on/off a load (LED)

Program

You need a program to tie these hardware together; to read the status of the button and send the signals to light up the LED.

I used ‘C’ with WiringPi libraries. I’m quite new to these myself. If you prefer, you can also do the same with Python. Geany is my favorite editor on the Raspberry Pi, works for both C and Python.

Let me quickly explain the functions of WiringPi library used in the program:

digitalWrite() – used to send an output to the GPIO pin. The first parameter is the pin number. Second parameter determines if the pin should be set as either HIGH or LOW (it can take only 2 states).

digitalRead() – used to read the status of the GPIO pin. The only parameter passed is the pin number. The output is either HIGH or LOW and can be checked as below:

Once this part is understood, whipping up a code to capture the pressed state of the button and lighting up an LED is  a piece of cake. Not joking, it really is!

The next part is how to toggle. This is done by remembering the previous state of the button and when it changes, determine whether to switch the LED on or off.

Since the program involves low level access to gpio pins, it will need ‘sudo’ permissions to run this program.

Summary

Here, we saw how to read input from a button and write output to light up an LED using the GPIO pins of a Raspberry Pi. Once we have these basics right, then the same knowledge can be used to drive other loads like a relay, motor etc.

References

WiringPi – GPIO interface library for the Raspberry Pi

WiringPi GPIO Pins – Pin layout for WiringPi specific and Broadcom specific pin modes

Adafruit – Tons of stuff related with Raspberry Pi and Arduino

 

 

 

 

Setting Up A Git Repository

All the version control systems I have worked with so far like CVS, VSS and Team Foundation Server are centralized version control systems. Git, however, is a distributed version control system. Clients don’t just checkout the latest snapshot from the server, but they mirror the entire repository.

So, when I decided to setup my own version control system for personal use, I decided to give Git a try.

Git can be installed on any computer and you have a version control system right away. However, I also wanted a remote repository on my main file server (maybe bias from working with centralized version control systems for many years!)

To achieve this, Git has to be installed on the main file server (remote repository) as well as on the local computer (local repository). The good part about this setup is that the local computer does not require to be connected to the server all the time (which is usually the case with centralized version control systems). It needs to be connected only when the changes has to be pushed back to the main server (remote repository). This is different from other centralized version control systems where the clients has to be connected to do any operation like compare previous versions / branch / check-in / check-out etc.

Installing Git on the remote server

Let’s first install Git on the remote server. I use a Synology disk station that runs BusyBee Linux as my main file server.

Instead of ipkg, use whatever package manager is available on your distribution. If you are using Synology and do not have ipkg package manager, you can install ipkg by following my earlier post Bootstrapping Synology DiskStation – Unleash The Power.

Initiate a bare repository on the remote server

Now that Git is installed, initiate a bare repository. There are 2 types of repositories in Git – bare and working. Bare repository is one without a working directory. For the remote, initiate a bare repository.

Install Git on the local computer

I use a Windows 7 machine as my local. Installed the Git core for Windows from msysgit. You can also find the download for Windows from here.

This installs the core, a bash and a minimal GUI.

image

Even though this is enough for most of the Git operations, I prefer to use the Git Extensions locally. Git Extensions has windows explorer integration and also integrates with Visual Studio.

Initiate a new repository from Git Extensions. If you already have a source code folder, you can choose that to create a new repository and convert the existing folder as a working folder. Choose ‘Personal repository’ as we want a working folder on the local.

image

After a few commits and branching and merging, the local repository looks like the below. It gives a good visual interpretation of what’s going on.

image

Connecting the local repository with the remote

Being distributed, one local repository can be linked to one or many remote repositories. In Git Extensions, go to Menu > Remote > Manage remote repositories > New

The recommended method of connecting to a remote is to use a SSH connection with public / private key authentication, without getting prompted for the password.

However, I chose to use another method. My remote is on a file server that I have access to and can be accessed from Windows machines on the local network. I  created a samba share to the remote repository folder, and created a network drive mapping from my windows machine. (P: as shown in the below screenshot, points to the remote repository created above in /volume1/git ). This is not a recommended method if there are many users, but works fine in my case.

image

Push changes to the remote

When the changes are ready to be pushed to the remote, click on the Push button. There is a also Pull button to pull any changes other users might have done.

image

When the changes are pushed to the remote, all the commits, branches, merges and other operations done locally are pushed to the remote repository.

If another user has committed to the remote during the time you were making changes locally, Git will not let you push to the remote directly. You will need to pull the changes from the remote, merge those changes into your own repository, resolve any conflicts and then push the changes to the remote. This built-in mechanism protects one user’s changes to be overwritten by another user accidentally.

GitList – A beautiful web interface for the repositories

I tried a few web interfaces for viewing my remote repositories, but GitList stood out from the rest by it’s minimalistic approach.

It was a pretty easy installation on a PHP / Apache The below installation instructions are from the GitList author.

  • Download GitList from gitlist.org and decompress to your /var/www/gitlistfolder, or anywhere else you want to place GitList.
  • Rename the config.ini-example file to config.ini.
  • Open up the config.iniand configure your installation. You’ll have to provide where your repositories are located and the base GitList URL (in our case, http://localhost/gitlist).
  • Create the cache folder and give read/write permissions to your web server user:

The options in the config.ini are simple at best.

See few screenshots below:

image

image

image

 

References

An excellent book on all things Git:
http://git-scm.com/book

Git downloads:
http://git-scm.com/downloads

List of GUI clients:
http://git-scm.com/downloads/guis

GitList – web interface:
https://github.com/klaussilveira/gitlist
http://gitlist.org/

Bootstrapping Synology DiskStation – Unleash The Power

Synology disk station comes locked and does not allow any modifications as it comes out of the factory, however there is a healthy community supporting the boot strapped operations. Bootstrapping is required to install additional packages on the server, other than the default packaged ones, which I should say are not any less.

Let’s start!

1. Bootstrapping program is dependent on the type of CPU on your disk station. See here to find your CPU Type.

2. Make sure the disk station has the login over SSH enabled.

image

3. Login to your disk station over SSH (I use putty) as root. Enter your admin password when prompted.

4. Change to the @tmp directory.

5. Download the bootstrap file for your CPU to the @tmp directory using wget. The example below is for the CPU type ‘Marvel Kirkwood mv6281 ARM processor’. Please find out the bootstrap file for each CPU type from the page Overview on modifying Synology server.

7. Change the bootstrap file permissions, so that it’s executable.

8. Execute the bootstrap file

9. Now, comment out the PATH and EXPORT in the .profile file and save.

10. Reboot the disk station.

11. Login to the disk station using SSH as root.

12. Update ipkg list. ipkg is the default package manager.

13. Install coreutils

14. Check what other packages are available to install

15. Check what packages are already installed through ipkg

At this point, you have all the software installed to add other packages and make any changes to configure the disk station as required.

Have Fun!

Install SCP

DSM 4.0 disk stations do not have SCP installed by default. I find SCP to be the easiest for copying files to the disk station. Moreover, if you are setting up public/private key authentication for password free authentication, the arrangement works very well with SCP, allowing, say a backup program, access the disk station without having to know it’s password.

1. On the disk station, change to @tmp directory

2. Get the OpenSSH package

3. Un-package

4. Inflate the tar file

5. Copy the binary to the bin

SCP is now installed, give it a spin!

Setting up a RAID 1 NAS For Your Home

There are different levels of RAID, like RAID 0 through 5. “RAID 1” is a setting where there are 2 hard disks in the RAID array. Data on one hard disk is replicated exactly on the other one.

What is RAID?

RAID is a “Redundant Array of Independent Disks” and as its name suggests, it is an array of hard disks connected together so that data can be replicated automatically without the user having to do it manually.

Why redundancy?

Storage devices are not always very reliable. Hard disk failure is still one of the common causes of data loss. Redundancy increases reliability by replicating the data across multiple disks.

There are two types of reliability
  • Reliability of the data being safe.
  • Reliability of data being available when you need it. The availability of data is the amount of time when the data is “online”.

Data backed up on a DVD or an external hard disk satisfies the first type of reliability, that it is being kept safe, but not the second one as it is not “online”.

This is where RAID comes into picture. RAID being a redundant array of disks, data will always be stored on more than one disk (except for RAID 0).

If one disk fails, the RAID controller will automatically start pulling data from the other disk. So that means, even if one disk fails, the data is still “online”. When the faulty disk is replaced with the new one, the RAID controller will automatically start replicating the data to the new disk.

Did you say RAID 1?

There are different levels of RAID, like RAID 0 through 5. “RAID 1” is a setting where there are 2 hard disks in the RAID array. Data on one hard disk is replicated exactly on the other one. This is probably not the best of the RAID arrays, however, for a home setting I think this would more than suffice.

Do we need a RAID server in a home setting?

The quick answer would be no. But if you are like me who has a ton of photos accumulated over the past many years and plenty of home videos of family, friends and kids and really care about all those data not getting lost, a RAID will give that additional layer of protection to your data.

Can RAIDs fail too?

Of course it can! If you have a 2 disk RAID array and if both the disks goes bad (like a power surge), or if there is a hardware failure on your RAID controller, then all the data will be gone. Even if you have a RAID, you will still need a good backup strategy. A RAID cannot replace a good backup strategy, but can only complement it.

What is NAS?

NAS or a Network Attached Storage is a device that can connect to your network, in this case your home network. Multiple computers and devices in your home can access all the files stored in the NAS.

Can’t you connect your storage device directly to your computer?

Well, yes. However, it will be limited to just one computer. I wanted a solution where all the computers at home can access all the files on the storage device. Network Attached Storage is what makes this possible. Your storage device is directly connected to your home network through the router. All your computers are also connected to the home network through the router, which in turn can connect to the storage device.

Hardware Setup

After evaluating a lot of RAID systems, I finally decided on a Synology DS210j. This has 2 drive bays in it and allows wither RAID 0 or a RAID 1 setup.

It turned out that setting this up was the easiest part. The model that I bought does not come bundled with hard disks. So I bought 2 x 1TB hard disks separately and installed it. It supports a maximum of 4TB. The installation of the hard disks was a breeze. Remove couple of screws, slide the hard disks in the slot one at a time and then put the screws back on.

Then I connected this to my router through an ethernet cable, connected the power supply and switched it on.

In a RAID 1 setting, 2TB of storage space would give me effective storage of 1TB, as the rest will be used for redundancy.

Software Setup

Synology’s management user interface is just incredible. It is web based, very user friendly and ajax supported. Multi-tasking is also supported in the management web page.

See the control panel, shown in Figure A, to get a glimpse of what features are being supported on this device. It has its own embedded Web Server running on php and a MySql database. It also supports dynamic DNS notifications. It has a lot of other nicer features like a Media Server, iTunes Server, Audio Station, Photo Station, Download Station and a Surveillance Station.

Figure A

I connected to the Synolgy’s managemet software, which I installed on my computer. It allows me to manage the device including the hard disks, the RAID configuration and other network settings.

To start with, I formatted both the hard disks on RAID1 configuration. Configured the network to use DHCP. Created users and groups and assigned permissions to each of them.

Then I moved all the data from my computer / external hard drives / CDs / DVDs  etc. The copying out is not all that fast as I’m connecting to this device from my computer over a wireless connection. However, when I connected my computer over ethernet, the file transfer was reasonably fast.

Please see a sample screenshot of the management user interface below in Figure B.


Figure B

I’m very glad that all my scattered data is now stored in a central location. All my computers including our xbox media center can access all the photos, videos and other data anytime and anywhere in the house.

I love it!

Footnotes:

You can find out more about RAID in this wiki:
http://en.wikipedia.org/wiki/RAID

You can find more about NAS in this wiki:
http://en.wikipedia.org/wiki/Network-attached_storage

Synology link:
http://www.synology.com/enu/products/DS210j/index.php