Introduction: Control LED Via Website

Step 1: What Are We Going to Do in This Project

I've been working with the Raspberry Pi 2 for about a month now, and when I got it I knew I want to control it, via a website from a mobile phone or computer.

I finally got the chance to find the easiest way to do this, it only needs a Raspberry Pi 2 (of course), resistor, led and an internet connection.

Let's get into it!

Step 2: Connecting the Raspberry Pi, Resistor, LED

When you get the resources, you sit down, and start connecting.

I have attached an image so it's easier for you.

As I mentioned in the previous step, the breadboard is there just for testing, it's okay if you just attach the LED and the resistor to the pins. (Do it on your own risk)

How do we attach a simple LED to our Raspberry Pi?

It's very easy.

First!

You need to determine the + and - of your LED. This is very IMPORTANT

To determine which is + and which is - you look at the length of the "legs" of the LED.

The LONGER is the + and the SHORTER is the -

Find more about this on: https://learn.sparkfun.com/tutorials/polarity/diod...

Now, get your LED and put it in your breadboard and with a jumper cable, connect the + to a GPIO (in the code I'm using GPIO 21, but you can use another GPIO, but don't forget to change it in the code later)

Connect the resistor on the - on your LED and the other side in a free slot of the breadboard.

!!!WARNING!!!

As you can see on the picture, a resistor has color codes, and at the end is a golden color code.

The golden color code goes into the breadboard, NOT into the - of the LED.


Now connect the resistor to a GND (ground) pin on your Raspberry Pi to close the circuit as shown on the picture!

Congrats, you connected an LED and a resistor to your Raspberry Pi

Comments

author
gproduct (author)2015-09-03

The files have been corrected. Enjoy

author
jamills99 (author)2015-09-03

there is an error in your code???

exec("sudo python /home/pi/lightoff_1.py");#if the button is pressed we execute led2.py which turns off the light

should read:

exec("sudo python /home/pi/led2.py");#if the button is pressed we execute led2.py which turns off the light

author
gproduct (author)jamills992015-09-03

Yes that is an error. SOrry.

author
DanielL58 (author)2015-08-31

This is an outstanding tutorial gproduct! been working with computers for years and ive only recently started my treck with python. I really appreciate the well maintained and structured approach on this raspberry pi tutorial. Had mine up and running in no time. you really helped me feel extremely comfortable in my new environment here. thank you sir. well done!!!

author
noel.kuck (author)2015-08-28

The controller.php code is looking for file names different than the led1.py and led2.py. Need to be corrected to work.

author
noel.kuck (author)2015-08-27

I created a brief html file and it shows up when I direct my browser to the pi IP. So php is running.

author
gproduct (author)noel.kuck2015-08-27

I think that you only copied the code on the instructable not from the fike, thats why its not showing

author
noel.kuck (author)gproduct2015-08-27

I'm too much of a noob to know what I should have done differently. What is a "fike"? Where do I get the proper code? Thanks for all of your help.

author
gproduct (author)noel.kuck2015-08-27

Please check your inbox, and we can discuss

author
noel.kuck (author)gproduct2015-08-27

Also, fyi, the controller.php file is not accessible from the Instructables page by anyone other than the administrator (you). That is what I typed it in.

author
noel.kuck (author)2015-08-26

not getting the "buttons" to show, only a webpage titled "Index of /"..........

author
gproduct (author)noel.kuck2015-08-26

Check the code, it's working 100%

author
noel.kuck (author)gproduct2015-08-27

This is what I keep getting. Can you explain where the files need to be placed? I followed your instructions, at least I think so. Thank you.
 

php.tiff
author
gproduct (author)noel.kuck2015-08-27

Well I think you should click on controller.php, if the buttons don't pop up you did something wrong.

Check if you have php installed.

author
gproduct (author)gproduct2015-08-27

Did you download the php file and paste the code in? or did you write #this is the php part

author
noel.kuck (author)gproduct2015-08-27

i actually typed it in from the instructions one time, and copied and pasted another time. Same results. The "#this is the php part" is from your instructables code.

author
noel.kuck (author)gproduct2015-08-27

Did the complete install twice, per your instructions. Clicking on "controller.php" gives: "#this is the php part"

No idea what could have been done incorrectly.

author
noel.kuck (author)gproduct2015-08-27

Can you confirm the directory locations for the ledx.py and controller.php files?

author
gproduct (author)noel.kuck2015-08-27

You put the controller.php into the apache servers directory and the ledx.py into your main directory (cd ~)

author
noel.kuck (author)gproduct2015-08-27

Thanks. That is where they are located. The controller.php is in /var/www and the ledx.py(s) are in the cd ~ directory.

author
veggy37 (author)2015-08-27

Ok so I admit I am new to programming the pi, but cannot get far with this one.

The two lines above stating:-

sudo nano /etc/sudoers

insert to the bottom of the page: www-data ALL=(root) NOPASSWD:ALL


should that be all on one line? I entered the first line and the ip address on a browser window and got the required text, but now how do I type anything into the pi as it doesn't have a prompt!? how do I get back to a prompt?

author
gproduct (author)veggy372015-08-27

Yes in one line.

If I'm correct you write out the file with ctrl+o and enter

and then you will get the prompt

author
hrabje (author)2015-08-25

Your warning about resistor heading doesn't make sense. It absolutely doesn't matter which side of resistor is connected to LED. Resistor is not semi-conductor.

author
gproduct (author)hrabje2015-08-25

I know that it's not important, but everyone puts them like that. Cheers