Monthly Archives: January 2016

Raspberry Pi – Home Automation – PHP

Opening your garage door with your laptop is really pretty cool, but not what we are shooting for.

The fastest easiest way to open the garage door from your android phone is to install:

Telnet / SSH Simple Client – by Advanced Planning Corp. (Android)

With this you can run the same scripts that you run on your laptop, but you still have to be on the same network.

The first step to making the Raspberry Pi viewable from outside your home network is to install Apache server on it.

The command you need to install Apache is:

sudo apt-get install apache2 -y

Then you need to install PHP so you can run Python scripts from the Apache Web Server:

sudo apt-get install php5 libapache2-mod-php5 -y

Now you have a web directory that you can place PHP scripts in that run Python scripts located in your home directory.

By default when you log in you are placed in the pi home directory, to get to the location where you need to create the PHP scripts:

cd .. (gets you to the /home directory)
cd .. (gets you to the root directory)
cd var
cd www

(OS version Jessie update)
if you have installed Raspbian version Jessie you need to go one folder deeper:
cd html

Now you can create a PHP script to run the Python script.

I again use nano.

sudo nano garageTog.php

<?php exec("sudo python /home/pi/");

You only need that one line, now lets save the script:




We still need to set permissions before we will be able to run the PHP scripts.
To do that we need to use a special sudo account to add permissions to a file that is in use. You MUST use this account, the first time I tried using the sudo account and the file got corrupted and I had to start all over from scratch and completely re-install Raspbian.

The command you type is:

sudo pkexec visudo

this will automatically open the:

/etc/sudoers.tmp file

go down to the very bottom and add:





This file will get saved when it is not in use. And now you will be able to run the PHP scripts on your Raspberry Pi web server by typing:

http://raspberry pi ip/garageTog.php

in your web browser on your phone.

Now you have everything you need to make my App from the App Store work except the python and php scripts that my App uses to get those set up go here:

Python and PHP Scripts from GIT

Raspberry Pi – Home Automation – Python

Python is a programming language that you can use to make the Raspberry Pi turn on and off GPIO pins which in turn can turn on and off relays which allow you to turn off and on lights and outlets, “push” buttons on remotes, and “push” buttons on garage door openers.

Python version 2 comes with the Raspbian OS by default, you do not need to install python to create and run scripts after installing Raspbian. There are newer versions that you can install, but from what I have seen there are differences with the newer versions and if you are just starting out, most basic examples that you will be looking at first were written with version 2 of python and if you don’t know what you are doing some things might not work if you have installed a newer version and try to use a version 2 script.

Here is a very basic script to turn a GPIO “on”

import RPi.GPIO as GPIO


To write this script first from the command line you type:

sudo nano

sudo is the admin account that has permission to create files.

nano is a simple editor (there are several different ones you can use which ever one you are most familiar with)

and is the name of the file you want to create.

When using nano the process to create this file is:

sudo nano – creates a file and takes you to the blank script.

you then type in the code above or the script you create.

then you do a ctrl-o command to tell the editor you want to save.

it makes sure you want to save the file and if you do you hit enter.

then you do a ctrl-x to exit.

To run the script you type:

sudo python and hit enter

If you use the code above this will turn on the GPIO pin and if it is connected to a relay the relay will come on and stay on indefinitely until you shut down the Raspberry Pi.

Make sure to always shut down your Raspberry Pi before you un-plug it. If you just unplug it there is a pretty good chance that the OS will get corrupted and you will have to re-install the OS again.

A good command for shutting down the Raspberry Pi is:

sudo halt -p

Now let me explain the script.

import RPi.GPIO as GPIO – gives you a library of commands that allow you to work with the GPIO pins.

GPIO.setmode(GPIO.BCM) – tells the computer what pin numbering scheme you want to use, there are 2 kinds .BCM stands for Broadcom and their numbering scheme seems to be the most common, it jumps around a little and isn’t as straight forward as the next kind. The other kind is .BOARD and it follows the physical pins on the board. If you hold the board up where the GPIO pins run vertical, with the usb ports and ethernet port facing down, the .BOARD version starts on the left and reads right. So row 1 goes pin 1 and pin 2 then you go to the next row and it goes pin 3 and pin 4 and so on.

You can find both the layouts here (.BCM is shown first, .BOARD is at the bottom):
GPIO Layouts

GPIO.setup(3,GPIO.OUT) – this sets the “mode” of the pin. Pins can be set to OUT for output to turn on relays or to IN to “listen” for buttons or switches to change and perform actions when a change is “heard”. 3 is the pin you are setting the mode for.

GPIO.output(3,GPIO.LOW) – This performs an action, it sets the GPIO pin (3) state to LOW which causes a relay to turn on or off. Most pins are set to HIGH by default so to get it to turn on or off you set it to LOW.

GPIO’s are digital so they are supposed to be on / high (1) or off / low (0) if they are neither the pin is said to “float” which means bounce around which would not be good for something like a garage door opener, the thing would open and close whenever the connection bounced the “right” direction. So they can be pulled up or down by default to set their base reading.

Speaking of garage door openers here is a good script for one that will turn on and then off a relay switch that is connected to GPIO pin 4.

Garage Door Opener Script:

import RPi.GPIO as GPIO
import time


SleepTimeL = 1


In this script we also import the time library so we can make a delay / pause / sleep timer to wait 1 second after turning on to turn off again, thats because we are imitating a push button that does not stay on. If the GPIO pin stayed on it would burn up the garage door opener motor.

And finally we run the GPIO.cleanup() command so everything gets reset and we don’t get any “connection is already in use” warnings.

Old Switch

Old Switch

New Switch

New Switch

YouTube – Raspberry Pi – Home Automation – Garage Door Opener

Raspberry Pi Setup Tutorial

Raspberry Pi

Video Overview of Raspberry Pi hardware:
Raspberry Pi

What you will need:

For Install:
1) Raspberry Pi
2) Power Supply (usually not supplied)
3) Micro SD Card (4 GB min) (Raspberry Pi 2 Model B supposedly goes to 64 GB) (I use 16 GB)
4) A PC/Mac to download the image and write it to the SD card

To use the Raspberry Pi after setup:
1) USB Keyboard
2) HDMI capable monitor
3) Network Cable
4) USB Mouse

Next, we need to get the Raspberry Pi OS in image form (.img) from the internet so we can write it to our Micro SD card.

I just love new upgrades!!! (Luckily I checked the page today 4/10/2017)
The lite version does not have pkexec which you will need to make it so you can run commands on the Raspberry Pi from your phone!

Maybe my download was corrupt, if so I will update this page later, just didn’t want you to waste your time ! ! !

Just verified that process still works with FULL BLOWN JESSIE VERSION ! ! !

The command:

Looked like it might have changed, but it did not, it is still correct.


Go to : Raspberry Pi .org Downloads Page

Or if you need an older version –

Here is a good video that shows you what I am about to tell you:
Getting Started with Raspberry Pi

And click on Raspbian and it will take you to another page with different OS versions on it for downloading. Get Raspbian Jessie version for this tutorial. Click on the download zip button. When I downloaded it, it gave me the unzipped .img version, not sure if that is a Apple thing that makes it automatically unzip, but you may have to unzip it so that you end up with a 2016-05-27-raspbian-jessie.img file. It does take a while to download.

NEW! Windows Image Creation Process – when done, skip down to – DO NOT PLUG THE POWER IN FIRST!!!

So, when using a Mac first you want to put the Micro SD card into a SD card adapter and put it in the SD card slot.

Next, when you insert the card into the computer the computer will mount it and begin reading from it. When this happens you will not be able to write the Raspian image to the disk so we need to do some preparation first.

On a Mac go to Launchpad (Icon with a rocketship), click on Other icon, you will get a window with multiple programs (looks kind of like control panel on PC’s) and click on Terminal.

This will give you a command line terminal (like a dos window) that we can type commands on.

When that window comes up type in:

diskutil list

This will give you a list of disks you have attached to your Mac. The SD card should show up at the bottom, but it will be the one that matches the size of your memory card and it should be FAT_32. What we need is the disk number, mine was disk2.

Next we need to go to Launchpad again, then other, then disk utility.

You should see your SD Card in the list on the left and it will have a couple images that look like SD cards indented and underneath it. One will probably say boot, click on that one and up above the unmount button should become active. Click on unmount and the boot label should turn light grey to show that it is unmounted. Now we are ready to write our image to the SD card.

I move the image from downloads to my desktop, but you can put it anywhere you know how to get to from the terminal command window.

By default when you run the terminal program it puts you in your home directory. If you do a list command ( ls ) it will show you all the files and directories where your are at in the directory structure. If you are in you home directory you should see a Desktop directory. I go there ( cd desktop ) and do another ls command.

I should see the 2016-05-27-raspbian-jessie.img file there.

Next we need the command that we will run to move the .img to the SD card. That is on Raspberry Pi’s webpage also.

On the same page you downloaded the .img from there is a link towards the top that says:

installation guide

Click on it and it takes you to another page.

Scroll down and there is a list of different OS types to click on, I click on Mac OS.

That will take you to another page and you can scroll down and you will find.

sudo dd bs=1m if=path_of_your_image.img of=/dev/rdiskn

Copy that line, go to your terminal window, hopefully you are in the directory where the image is, if not make sure you go there first. Then paste the line in the terminal window.

Now we need to edit the line before you press enter. we need to replace rdiskn with the label for our SD card, mine was disk2. Then you need to arrow over to just after path_of_your_image.img and start backspacing to remove it. Then you can type in 2016-05-27-raspbian-jessie.img, or highlight it in your file list and drag and drop it and it will populate the command for you.

So when the command is ready to be executed it will look something like:

sudo dd bs=1m if=2016-05-27-raspbian-jessie.img of=/dev/disk2

I have not researched yet what all the options mean, but sudo is the admin account used for this Linux system that allows you to break….. do things that can really mess things up. You will use this account many times in this tutorial.

After you hit enter, it will ask you for your password (Mac admin password you set up when you first got it) and then it should begin to transfer the .img to the SD Card, it doesn’t give you any info to tell you it’s doing anything, your computer just usually get’s really slow, and it will take awhile.(if you have a PC using it is WAY faster)

If you get a message that says the disk is in use, that means you unmounted the wrong one or didn’t do what I said, so you will need to go back and do that first.

When it’s done the command line will come back and it should give you some info about all the data that was transferred.

After that you need to tell the computer to eject the SD card, this can be done from the Disk Utility program or from Finder.

Now you can take the SD card out of the computer, take the Micro SD card out of the adapter and put it in the Raspberry Pi in the disk slot underneath on the bottom of the motherboard.

Now we are ready to work on the Raspberry Pi itself.


You can use the Raspberry Pi with just a power cord and a network connection, but you must know the IP Address to do anything and that requires a HDMI capable monitor. You also need a USB Keyboard and Mouse. So after you have the Network cable, HDMI cable, Keyboard and Mouse plugged in, plug in the power.

Things should begin to fly across the screen.

Then towards the end it will show your IP Address you need, but then it will instantly start the windows like interface and you won’t have time to read the Address, that’s fine, we will get it in a minute.

When it is done booting you can click on the Terminal app in the top left (looks like a monitor with a black screen). Once in there you can type ifconfig and it will give you your IP address(inet addr: in the eth0 section if you are using a network cable).

With this information you will be able to log into the Raspberry Pi remotely so you will only need a power cord and a network connection to the Raspberry Pi to administer it via SSH.

To log in to the Raspberry Pi via SSH on a Mac you go to the terminal program and type:

ssh -l pi

replace my ip address with the ip address for your Raspberry Pi, -l means log in I suppose and pi is the user account you want to log in with. It will ask you for the pi accounts password, then you are in! It will put you in the pi accounts home directory.

Even though we downloaded the latest version of the OS there might be some updates available to install.

To check for these type:

sudo apt-get update

after that type:

sudo apt-get upgrade

That should get you all up to date and ready to use the Raspberry Pi without issues.

Next post we will write some python code which is what you use to turn on and off gpio pins which are what make relays (switches) turn on and off and that’s where the real magic begins when you use the Raspberry Pi for home automation.

Go here

Select Raspbian

Download this

Wait til done

Unzip image using 7zip

Select downloaded zip file

Extract it

let finish

when you have a .img file

Use disk imager on windows machine

write image to micro sd card

when write is complete, eject micro sd card from PC and put in Raspberry Pi

Open terminal app

Update Raspbian

Upgrade Raspbian

Upgrade can take a while, so watch some TV or do something else for a half hour.

Install Apache Server

Install PHP

Open file to set permissions

Scroll to end of file and add permission line

ctrl-o to save
ctrl-x to exit

Install GIT:

Install GIT

Make a GIT directory in /home/pi (you should already be there)

Go into GIT directory:

GIT directory

Get my files from GIT:

Get my files from GIT

Go into new directory with my files:


Enter into dangerous admin mode:

sudo su

Copy PHP files:

cp *.php /var/www/html

Copy Python files:

cp *.py /home/pi



DONE! You can make sure the files are where they need to be if you want:

file check

PHP files

Raspberry Pi – Internet of Things – Here we go!

I’ve been doing all kinds of things that relate to Internet of Things that I will be adding to my blog over the next couple of weeks.

First, let’s take a look at some pictures of what we will be doing . . .

(in backwards order)

Voice Control – YouTube Video
Webcam, Lights, and TV control
Garage Door and Garage Lights control

We will end with using your phone to turn on and off wall outlets and power strip outlets from anywhere in the world with a phone connection, with each project costing about $50 a piece at the most, minus the phone.

Powerstrips . . .


YouTube Links of Smart PowerStrips in action!
App Demo
Hardware close-up

Wall Outlets . . .

YouTube Links of Smart Outlets in action!
Voice Commands – App View
Voice Commands – Wide View
Button Control – App View
Button Control – Wide View

iOS Interface
Smart Outlet – iOS Interface

Smart Outlet Installed - Close Up

Smart Outlet Installed – Close Up

Smart Outlet Installed - Wide View

Smart Outlet Installed – Wide View

Smart Outlet Installed and in use.

Smart Outlet Installed and in use.

Smart Outlet Preparation

Smart Outlet Preparation





Light Switches . . .


Remote Control Wireless Outlets . . .





Garage Door Opener . . .

YouTube – Raspberry Pi – Home Automation – Garage Door Opener



First we will start with how to setup your Raspberry Pi . . .