Introduction: Internet of Things With Raspberry Pi - 1

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