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 Apache2MySQL 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)

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)

Hardware Setup:

DSCN4191
DSCN4186
DSCN4188
DSCN4198

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:

apache-it-works

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

sudo nano index.php

The last command will open a blank file. You will need to enter some PHP content to test it. Type the following:

<?php echo “hello world”;

Open the web browser and refresh the same web page. It should display ‘hello world’ in the top right corner.

You could also try:

<?php echo date(‘Y-m-d H:i:s’);

This will display the current date and time.

Step 7:

Install MySQL with the following command:

sudo apt–get install mysql–server php5–mysql –y

When installing MySQL, you will be asked for a password. You will need to remember this password in case it is required in the future.

Step 8:

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 following text in it:

<?php

    if (isset($_POST[‘on’]))

    {

exec(“sudo killall python”);

exec(“sudo python /var/www/mystuff/ledON.py”);

    }

    else if (isset($_POST[‘off’]))

    {

exec(“sudo killall python”);

exec(“sudo python /var/www/mystuff/ledOFF.py”);

    }

    else if (isset($_POST[‘blink’]))

    {

                exec(“sudo python /var/www/mystuff/ledBLINK.py”);

    }

?>

<html>

<style type=”text/css”>

#form{font: bold 12px/30px Georgia, serif;}

button{width: 150px;height: 75px;border: none;border: 3px solid black;border-radius:20px;}

#container{margin:0px auto;width:80%;min-width:400px;}

</style>

<body>

<div id=”container”>

<form id=”form” method=”post”>

<center>

<button name=”on”>Led ON</button>

<button name=”off”>Led OFF</button>

<button name=”blink”>Led BLINK</button>

</center>

</form>

</div>

</body>

</html>

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

Step 9:

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 10:

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

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 on 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):

DSCN4196
The final setup with minimal connections

For more, check out Internet Of Things with Raspberry Pi – 2.

One comment

Leave a Reply