loading

When I was new to IOT (Internet Of Things), I saw that there were hardly any tutorials which were simple enough for a beginner to understand and try out. There was either to much technical jargon, or the hardware was too complex.

So now that I’ve played around with IOT a bit, I decided to make a 10 step tutorial on controlling an LED over a Local Area Network (LAN).

In this tutorial, we’ll be using an LED, a Raspberry Pi, a Wireless ADSL Router with internet connection and a device with a web browser. (Smartphone, Laptop, Computer, PSP, etc.)

On the software side, we’ll be using Apache2, MySQL and PHP.

If you’re new to the Raspberry Pi, you might want to have a look at Getting started with Raspberry Pi before trying out this project.

(Note: This project only uses an internet connection for software installation. After the installation and coding is done, the internet connection is not required. For more info on making the project available on the internet, check port forwarding)

Step 1: Gather the Components

  • Raspberry Pi (I've used a Raspberry Pi 2 model B, but any model will suffice)
  • ADSL Wireless Router
  • Power adaptor for the router
  • Computer monitor / TV screen which has an HDMI/VGA port (If you're using a VGA port then you will have to use a VGA-HDMI converter)
  • Ethernet/LAN cable
  • 2 Female-Female jumper wires
  • Small LED
  • USB Keyboard and Mouse
  • A computer/laptop connected to the same modem as the Raspberry Pi (This will just be for the final test so even a smartphone is ok)

Step 2: Hardware Setup

Step 3: Creating a Website for the Raspberry Pi

Step 1:

Start your Raspberry Pi and open the Graphical User Interface (GUI) with the command:

startx

Step 2:

Once the interface is active, open the terminal and type the following commands:

sudo apt-get install apache2 -y

An IOT webpage will require a web server. This command will install a web server called Apache2.

Step 3:

To test the web server, you will need to know your Raspberry Pi’s IP address. Enter the command:

hostname -I

A number will be displayed. Start your Pi’s web browser and enter this number in the search engine. You should see something like this:

Congratulations! Your Apache server is up and running!

Step 4:

This is a default webpage which is stored in the ‘/var/www’ directory. To make changes to it and customise it, you need to enter this command:

sudo nano /var/www/index.html

Whenever you’re modifying a file, don’t forget to add ‘sudo’ at the beginning.

This indicates that you are modifying it as a superuser. Press Ctrl + X and hit enter to exit the file.

Step 5:

You will also need a preprocessor called PHP. Install it with the following command:

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

Step 6:

Now enter the following commands:

cd /var/www sudo rm index.html

Step 4: Make the Website an IOT Remote Control!

Step 1:
Enter the following commands:

cd /var/www

sudo rm index.php

sudo nano index.php

The last command will open a new index.php file. Enter the text from the above-mentioned PDF document into this file. (Since part of it is an HTML code, there was a problem with pasting it directly into this post.)

Exit the file by pressing CTRL + X. You will be asked if you want to save changes. Press Y and hit enter.

Step 2:

You will now need the Python files for controlling the LED.

There are three Python files. One to turn on the LED, one to turn it off, and one to make it blink.

Please note that the following Python codes are for Raspberry Pi models with 40 pins.

i.e. Pi model A+, Pi model B+ and Pi 2 model B

If you’re using a 26 pin Raspberry Pi (Model A or B), then you will have to change the GPIO pin number in all three codes to 13 instead of 40 and accordingly connect the LED.

Use the jumper wires to connect the negative lead of the LED to Pin 6 on the Raspberry Pi’s GPIOs and connect the positive lead to Pin 40. (Pin 13 in the case of a 26 pin GPIO Raspberry Pi.)

First, let’s create a file to turn on the LED. Enter these commands:

cd /var/www

sudo nano ledON.py

Type the following text in the blank file:

import time, RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)

GPIO.setup(40,GPIO.OUT)

GPIO.setwarnings(False)

GPIO.output(40, True)

time.sleep(1)

Exit the file by pressing CTRL + X. You will be asked if you want to save changes. Press Y and hit enter.

Now create a file to turn it off:

sudo nano ledOFF.py

Type the following text in the blank file:

import time, RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)

GPIO.setup(40,GPIO.OUT)

GPIO.setwarnings(False)

GPIO.output(40, False)

time.sleep(1)

Exit the file by pressing CTRL + X. You will be asked if you want to save changes. Press Y and hit enter.

Lastly, create a file to make it blink:

sudo nano ledBLINK.py

Type the following text in the blank file:

import time, RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)

GPIO.setup(40,GPIO.OUT)

GPIO.setwarnings(False)

while True:

GPIO.output(40, False)

time.sleep(1)

GPIO.output(40, True)

time.sleep(1)

Exit the file by pressing CTRL + X. You will be asked if you want to save changes. Press Y and hit enter.

Step 3:

Now, you will need to change certain file permissions. Enter the command:

sudo nano /etc/sudoers

This will open a file which contains permissions for directories, files, etc.

Go to the last line of the file which says:

pi ALL=(ALL) NOPASSWD: ALL

Below it, type this:

www-data ALL=(ALL) NOPASSWD: ALL

Exit the file by pressing CTRL + X. You will be asked if you want to save changes. Press Y and hit enter.

Reboot the Raspberry Pi with the command:

sudo reboot

Step 5: Test the Setup!

Congratulations! Your first IOT project is now ready! You can try it out from any device which is connected to the same network as the Raspberry Pi.
If you’re having problems with this project, go over the whole tutorial once again. If it still doesn’t work, then feel free to contact me at: sataklela@gmail.com

Once you know for sure that everything is functioning properly, try modifying the codes to play songs, run motors, etc.

You can even add a small relay circuit and control the lights in your house!

IOT is an amazing thing and once you understand it, there are almost no limits to what you can do. In the video below, I tried to control the LED using my PSP (Play Station Portable):

For more, check out Internet of Things with Raspberry Pi - 2

<p>its really work for me only problem is with php code you didn't mention mystuff folder in /var/www to run program we have to remove it from php code</p><p>but thanks for your help</p>
<p>I have worked on your post 4 Hours! </p><p>You have little mistakes; </p><p>first : you folders of py files in mystuff folder. But you didnt tell it.<br>second. index.php isnt opening. also there is a html folder when i put it it doesnt open again. </p><p>third. when i try to change .php to html. it works. but it doesnt send command raspberry to light led. </p><p>-_- </p>
<p>Keep the file as .php, check over your code, check you set up the permissions properly as well (bottom of page 4)</p>
<p>Wahey I fixed it! index.php does indeed work - I had a minor syntax error in the .php file. I think that was actually the only problem, though I was chipping away at it till late and early this morning. Classic right? Works over local WIFI as well with no Ethernet link </p>
<p>After some code modifications it worked beautifully. I could not have gotten started with IoT without this 'ible. Thank you :-)</p>
<p>Any tips on what you needed to do?</p>
<p>Is there any way to debug this? my .py files are tested and working - I <br>can display the buttons on a seperate device if I change index.php to <br>index.html but can't get the .py files to execute. Is there any way I <br>can see if the commands are received?</p>
<p>Thanks for the nice project.. very interesting to work on as a beginner!!!</p>
<p>Hey excellent tutorial. :)</p><p> I wanted to do something like home automation, for my room specifically, like you did here but also want side-by-side to stream video from my Pi cam (or webcam) of my room to seen in actual when I turn on a light does it actually lit up. Any idea how to integrate a web stream of my pi cam (or webcam) into your program?</p><p>Thanks</p>
I've never tried to integrate a control panel and side-by-side stream on a single webpage, but I'm sure it's possible with a bit of tweaking.<br> <br> You could try using 'motion'. It's quite good and you can tweak the settings for streaming different sizes and frame-rates.<br> <br> I learn't about it from here:&nbsp;<a href="http://pimylifeup.com/raspberry-pi-webcam-server/" rel="nofollow">http://pimylifeup.com/raspberry-pi-webcam-server/</a><br> <br> Best of luck with your project.

About This Instructable

27,944views

101favorites

License:

More by RoboCoder:Internet of Things with Raspberry Pi-2 DIY Portable Smartphone Charger Mini power distribution bus 
Add instructable to: