Introduction: Wifi Controlled LED Using Raspberry Pi 3

About: Studies EEE at CET, Trivandrum.

This project demonstrates a basic idea of the Internet of Things(IOT) whose applications are extending from smart homes to wearables to healthcare.

"The Internet of Things has the potential to change the world just as the Internet did. Maybe even more so" - Kevin Ashton.

Here in this project, I am demonstrating a method to switch on/off and blink an LED from a website which can be accessed worldwide. This project can be developed further making it useful in our day to day lives. Instead of the LED used here, if we connect the PI to AC mains via a relay, it becomes a Home Automation.

Step 1: Components Required :

The components required for this project are :

1. Raspberry Pi 3

2. LED

3. 270 ohm resistor

The raspberry pi should be provided with an SD card loaded with an appropriate OS(preferably Raspbian) and should be connected to a monitor via HDMI to VGA converter or to a laptop via SSH. Apart from the above components a router with internet connection should also be present, with the raspberry pi being connected to it.

Step 2: Connections

The connections in this project are very simple. Connect the positive pin of LED to GPIO 17 pin and the negative to a 270 ohm resistor, the other side of which is connected to GND pin.

Step 3: Installing WiringPi Library

WiringPi is basically a GPIO interface library for Raspberry Pi. There are 2 methods to install wiringPi library.

(All the commands given below are to be executed on the Pi's terminal).

Method 1

1. Make sure your Pi is up to date with latest versions of Raspbian by :

sudo apt-get update

2. Install git by the command:

sudo apt-get install git-core

3. Obtain WiringPi using git by:

git clone git://git.drogon.net/wiringPi

4. To build/install WiringPi library

cd wiringPi
./build


Method 2

Try out this method only if method 1 doesn't work out.

https://git.drogon.net/?p=wiringPi;a=summary

Click the above URL and check for the link marked snapshot at the right hand side. Click the top one which will download a tar.gz file like wiringPi-xxxxxxx.tar.gz (xxxxxxx represents a combination of letters and numbers which is unique for each download).

Then open the terminal and type these commands:

tar xfz wiringPi-xxxxxxx.tar.gz
cd wiringPi-xxxxxxx ./build

Thus the WiringPi library will be installed by one of the above methods. For more details about WiringPi library, please visit http://wiringpi.com/. To make sure Wiring Pi is installed and works properly, run the gpio -v command; it should return the current version of Wiring Pi along with the basic Raspberry Pi info.

Step 4: Installing a Web Server

Apache is a popular web server application you can install on the Raspberry Pi to allow it to serve web pages. On its own, Apache can serve HTML files over HTTP, and with additional modules can serve dynamic web pages using scripting languages such as PHP.

In our project we are using an HTTP server and its PHP extension. MySQL database is not used here.

First install Apache HTTP server and its PHP extension by:

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

In order to test if the Apache server is working properly, navigate to the browser and type your Pi's IP address.

(To get Raspberry Pi's IP address type ifconfig on the terminal. Next to the wlan0 entry you will see inet addr: 192.168.x.x which is the IP address of the Raspberry Pi.)

You should see an It works! page.

Now we should test whether its PHP extension is working. The above "It works!" html page is present in "/var/www/" directory as index.html. Now, delete this html file and create a new PHP file index.php. Then type the php code below :

phpinfo();

Save it and refresh the page in your browser. A long page with lots of information about PHP will appear. Thus the PHP extension is installed properly. If any of the pages do not appear try reinstalling apache server and its PHP extension.

?>

Step 5: Start Coding

Here in this project, we will be coding in PHP to control the GPIO pins, which is embedded in an HTML page. GPIO pins of the Raspberry Pi can be controlled by PHP using shell_exec() function. This function executes command via shell and returns the complete output as a string.

Now delete the code in index.php file and insert PHP code to control GPIO pins inside body of HTML code. If you are unable to edit the index.php file due to some administrative problems, navigate to "/var/www/" directory using cd /var/www/ command and type the code - sudo chmod777 index.php. Now you will be able to edit the php file.

First create a form in HTML consisting of 3 buttons - ON, OFF and BLINK .

Then set the GPIO 17 pin as output with the help of WiringPi library using the statement:

shell_exec("/usr/local/bin/gpio -g mode 17 out");

Check for the values returned by the button and accordingly make the GPIO pin HIGH/LOW using if else-if statements.

if(isset($_GET['off']))
{

echo "LED is off";

shell_exec("/usr/local/bin/gpio -g write 17 0");

}

else if(isset($_GET['on']))

{

echo "LED is on";

shell_exec("/usr/local/bin/gpio -g write 17 1");

}

else if(isset($_GET['blink']))
{

echo "LED is blinking";

for($x = 0;$x<=4;$x++)

{

shell_exec("/usr/local/bin/gpio -g write 17 1");

sleep(1);

shell_exec("/usr/local/bin/gpio -g write 17 0");

sleep(1);

}

}

The entire code can be downloaded from the link below.

Attachments

Step 6: Running Code on Local Host

After completing the code, the next step is to run the code on your browser. Type the ip address of raspberry pi on the browser. You will see 3 buttons - ON, OFF and BLINK as shown in the image and you will be able to control your LED by clicking those buttons.

This control can be obtained from only those devices connected to your router(local network). The LED cannot be controlled from any other network. In order to do that, follow the instructions in the next step.

Step 7: Port Forwarding and Obtaining a Free Domain Name

Now we are able to control the LED by the local host network. In order to make it accessible world-wide, we need to change few settings in the router and also obtain a free domain name.

First obtain the unique public IP address of your router from https://www.whatismyip.com/.

The next step is to log in to the router by typing this address. Take the port forwarding option and add a new port(not 80 since it is the default port) by providing the ip address of the raspberry pi and enable it.

Now if you type type your public ip address and the port number separated by a colon in the format:

<Public ip address>:<Port number> in the browser of any network, you will be able to access the page.

Since this ip address is too long and hard to remember, you can get a free domain name from http://www.noip.com/ where you have to configure the device by setting up the right port.

This will enable you to control the LED from anywhere in the world.

LED Contest

Participated in the
LED Contest

Lamps and Lighting Contest 2016

Participated in the
Lamps and Lighting Contest 2016