Introduction: Wifi Controlled LED Using Raspberry Pi 3

Picture of Wifi Controlled LED Using Raspberry Pi 3

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

Picture of 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

Picture of 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.

Step 6: Running Code on Local Host

Picture of 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

Picture of 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.

Comments

rickevans (author)2017-11-16

Thanks for this tuto. I am quite a newbie - will it work generally with any LED bulb? I would like to go with https://www.sortedreviews.com/active-light-cool-to-warm-white-bulb-e27/ but I am a bit confused if I manage to make it work.

KokY2 (author)2017-08-19

Hi, thanks for the tutorial.

Actually, i cannot make the LED light up. Isn't only run the php code to make it light up or we have another .py file?

Anyone can help? Thank you.

VTJW (author)2017-07-05

Hello,

It all worked until I got to the part to have it work anywhere in the world.

I got my IPaddress and logged into the router and changed the port as stated.

I have xfinity so I made the common service = Other, then I gave it a service name, I then entered my Pi IP4 Address for the Pi and made the port 3389 and saved it.

It will not recognize when I put in the "IPaddress:Port" into the browser.

Any thoughts

SauravR9 (author)2017-04-24

hey i did everything correctly up to the webpage part but when i press any buttons no action is to be seen in the pi..what to do which step must hav i gone wrong???

Chami Jay (author)2017-02-14

Hi!! I'm currently trying out this project.
But I get a error when I try to press one of the three
buttons. My error is,

Not Found

The requested URL /gpio.php was not found on this server.

Apache/2.4.10 (Raspbian) Server at IP of my Pi

Any suggestions?

kabhishek247 (author)Chami Jay2017-02-16

Same Error Here. Were you able to resolve ?

Chami Jay (author)kabhishek2472017-02-16

yeah..just simply replace the action="gpio.php" to action="index.php". It should work now.

hareeshiyer (author)2016-09-21

Great post, Vaishnav. The instructions are very detailed and easy to follow.

James C Dcruz (author)2016-09-18

Let it be a starting 4 u,inspirational

James C Dcruz (author)2016-09-18

Bro super

DIY Hacks and How Tos (author)2016-09-17

Great introduction to Raspberry Pi project

About This Instructable

6,426views

27favorites

Bio: Studies EEE at CET, Trivandrum.
More by Vaishnav J:Wifi controlled LED using Raspberry Pi 3
Add instructable to: