33

embedded report

Embed Size (px)

Citation preview

Page 1: embedded report
Page 2: embedded report

ABSTRACT

Raspberry Pi is a credit card sized computer that plugs into a computer monitor or TV, and uses a

standard keyboard and mouse. It is a capable little device that enables people of all ages to explore

computing and to learn how to program in languages like java and python. It is capable of doing

everything you would expect a desktop computer to do, from browsing the internet, playing high

definition video, making spreadsheets, word processing and playing games. In this project, we are using

the raspberry pi controller to monitor any devices wirelessly over a network. We have chosen wifi as we

can directly interface them with your computer. It means that you don’t need any additional module or

antenna. We need to design a webpage using html, php and java script which is the displaying screen for

this project. We write the main code using the java script and php and html for calling the code which is

in java script. We need LEDs to check the output if it’s working right and few resistors to support the

LEDs. We input the code in the raspberry pi itself so that we can use the controller anywhere and we can

get the output by simply connecting it to any external display device. We can even control the devices

on laptops and even on mobile phones. This would help a lot for the home automation.

Page 3: embedded report

ACKNOWLEDGEMENT

We, group #8: Kushal Singampalli (FS3620), Prandurangarao Challagundla (FQ8137) and Gopikanth

Srikar Manoj Tatavarthi (FR5950) have completed our project, “WiPi home, a wifi based home control

unit” as our main project for the course ECE 5620 for winter 2015 semester, under the supervision of

our professor Dr.Syed Masud Mahmud in Wayne state university. We thank our instructor for all the

help and for the course he taught us.

Page 4: embedded report

CONTENTS

1. Components

2. Project description

3. Hardware connections

4. Configuring the raspberry pi

Page 5: embedded report

5. Writing the code

6. Inputting the code

7. Testing

Page 6: embedded report

INTRODUCTION

We wanted to do something to control devices wirelessly. Upon some research, we chose raspberry pi

controller as it is compatible for wireless transfer of data. A raspberry pi controller, though it is

compatible for wireless data transfer, it does not have a wireless adapter on board. So a simple wireless

adapter plugged in to it through a usb port would add an additional feature. Then we start the project

by configuring the raspberry pi, enabling the wireless adapter by coding and then deploying the written

code in java script for calling the functions. We have used http and php languages for designing the web

page and a web server is required to interface the device and the web page. So we have used a web

server by name Apache web server that interfaces both the raspberry pi and the web page as well. We

then connect the hardware to test the code. We don’t have to connect the whole circuit but just a single

connection would do. If the test gives out positive result, we then script the main code for calling all the

8 LEDs. Then comes the main task of globalizing this wireless network from local to control the device

from any device that has Internet in it. By using a port forwarding on the router, we can achieve this.

Thus we can make this project work.

Page 7: embedded report

BACKGROUND:

Our project has so much to deal with. Beginning from the controller, we had many challenges to be

faced during the process of beginning till finishing it. We wanted to control the devices wirelessly, but

we do not really know which controller to use. There were a variety of controllers to choose from like

PIC micro controller, Arduino Uno, Raspberry pi and others. But we chose Raspberry pi as our controller

as we found that it’s features enables to explore more in our project. We basically wanted to do

something wirelessly, as the raspberry pi has this feature of being compatible for wireless transfer of

data, we thought this would be the best for us. So we bought an external wireless adapter that connects

the raspberry pi through USB. Thus we can enable the wireless feature on our raspberry pi. Then

choosing the hardware like LEDs, resistors to support LEDs were the next two tasks that we did. We

chose 270 ohms resistors as they glow the LEDs bright enough. We used a router that we have at home,

so we dint have any trouble creating a local with in our raspberry pi and our laptop. The toughest part is

coding. We are not savvy with coding a raspberry pi. So we went through few instructions and tutorials

on youtube and other websites where they taught us the basic commands and other commands for the

desired operations. And as we wanted to use a website as an interface instead of an application, as it is

difficult for us at this point of time to get expertise in computer languages to build an app. So we chose a

website as basic http would be enough to design the skeleton and CSS for the page style, PHP for the

page interactions with the server and Java script for the interactions with the user. Thus just the basic

knowledge in these four languages can help us in building a website for our project.

Page 8: embedded report

REPORT:

HARDWARE USED:

1. Raspberry pi controller

2. WiPi wireless adapter

3. Wireless router

Page 9: embedded report

4. Bread board

5. LEDs

6. Resistors

Page 10: embedded report

7. Connecting wires

8. Display device

Page 11: embedded report

9. HDMI cable

10. Key board

Page 12: embedded report

11. Mouse

LANGUAGES USED:

1. HTTP

2. CSS

3. PHP

4. JAVA

SERVER USED:

1. Apache web server

PROCEDURE:

1. Configuring the raspberry pi:

2. Configuring the wireless adapter

3. Testing the Wireless connectivity

4. Writing the raspberry code

5. Installing a webserver

6. Creating an interface

a. HTML

b. PHP

c. JavaScript

Page 13: embedded report

d. CSS

7. Deploying the written codes to Raspberry pi

8. Connecting the hardware

9. Testing the project

10. Globalizing the interface

PROCESS IN DETAIL:

1. Configuring the raspberry pi:

Raspberry pi comes with many features but we have to configure it as per our

requirements like creating directories and other files which we need for our project.

First part is installing a WiPi library. It simplifies a lot using the raspberry pi GPIOs that is

general input/output, a single command instead of a long process. It also means that

you can use it in any of our C codes. But, however we dint use C programming as we felt

that programming and controlling our raspberry pi with GPIOs would help us deal with

the controller to some more extent and would even help us in debugging the errors if

we get any. This is a software that was made by Gordan that with a library. It allows us

to control the GPIOs directly in a single command line. But however, this process is a lot

slower than using a C program. We first need to install it on our RasPi using a ./build

command which enables us to use the GPIO utility. Now we type “gpio mode 0 out”

command to test it and if no error pops up, then it’s fine else we have to build a library

again.

2. Testing the GPIO

We can simply connect any LED to any gpio pins(##) on the raspberry pi and

select it as an output. We input a command “gpio mode ## out” for it, where ## is the

pin number and out is for output. Now, we use “gpio –g write ## 1” to turn on the

LED and “gpio –g write ## 0” to turn it off. Thus we can make sure that the wipi

library is installed correctly on our raspberry pi.

3. Configuring the wireless adapter

Page 14: embedded report

We are using an external wireless adapter to enable the wireless connectivity

feature of our raspberry pi. We bought a wipi, as told before, we connected it to a usb

slot given on raspi. Now, we need to configure it and even connect it to our local. Before

doing this, we have turn on our wireless router and make sure that our devices are

connected to it other than the raspi as we are configuring it now. Initially, we turn on

our raspi and log in to it using our credentials. We have to configure our wireless

adapter and connect it to our local. We need to know the network SSID and the

password to connect it. After loging in our raspi, we then give the command “sudo nano

/etc/network/interfaces” which takes us to the interfaces page where we can see a

program for it. In it, we can observe some space for the local credentials written as

“wpa-ssid” where we give our local SSID and “wpa-psk” where we enter our local

password in a double inverted commas “ ”. Then we have to hit control+x to exit the

page where “x” represents exiting the page. It will ask us if we want to save it, we will

save it. Then we have to restart our raspi, so we give a command “sudo shutdown

“now” –r” where “r” represents the restart function.

4. Testing the Wireless connectivity

Once the raspi is successfully rebooted after configuring the wireless interfaces,

we check its connectivity with our local. So after logging in, we give a command

“ifconfig –a” which gives us the detailed list of raspi connections. In the wlan section

we can find the inet address for our raspi and even we can observe that the raspi is

communicating with the local by sending and receiving packets. If the local is connected

to internet though Ethernet cable, we can check the internet connectivity by opening

our browser and checking some random website and wait to see if it’s loading. Thus we

can configure the wireless adapter on a raspberry pi.

5. Installing a web server

Controlling the LEDs remotely with SSH is pretty good but the interface isn’t very

user friendly and typing the gpio commands is annoying. So we thought a graphical

interface would be better for project. First thing that came up was designing an

application, but we it takes a lot of time for us to learn the languages required for

Page 15: embedded report

designing an application. We went looking for alternatives for it on internet and found

that creating a website is the best and little easier way to create an interface as it is

compatible with all devices and we only need four basic languages. But before that, we

need to install a web server on our raspberry pi. We don’t need any database for it,

other than just the HTTP server and it’s PHP extension. Let’s get to installing the web

server on the raspberry pi. Give in a command “sudo apt-get update” which updates

the present server on the raspberry pi. And once it’s done, type “sudo apt-get

install apache2 php5 liapache2-mod-php5” which installs the apache webserver

with its php extension. It will take sometime to install, and when it finishes, open the

browser and type the IP address of the raspberry pi or simply localhost in the address

bar of the browser. It should show us a message saying “it works!” which means that

the web server is installed correctly without any errors. Now, we create a index.php

page with the installed php extension by giving a command “sudo nano index.php”

where nano is the command used to create something and index.php is the file that we

are going to use in future.

6. Creating an interface

a. HTML: We are using the html to design the basic skeleton of the page. This is what

design the page that we are going to see and all the other calling functions.

Page 16: embedded report

b. PHP: The interactions with the server and the pages generation are done using the

PHP language. This is the page that contains 8 buttons that we are going to use for

controlling 8 devices; here LEDs are our devices for this project. This is the code that

turns on or turns off the LEDs.

c. JavaScript: Now we have to detect when and what the user is clicking on out of these

8 buttons. And that’s where the Java script is useful. Here, we have simply added

calling events to all the 8 buttons. So when each time any of these buttons are

pressed, it uses a function that asks gpio.php file for it and returns the answer. To be

clear, the function of Java script here is to simply change the color of the button from

red to green.

d. CSS: We have used the CSS just for the basic black background. That’s it.

7. Deploying the written codes to Raspberry pi

When the interface is ready, we need to deploy it to our raspi for the interface to work.

We need to transfer the written codes from our laptop to our device i.e. raspberry pi.

We have used a software for windows operating system by name WinSCP for

transferring files. This is one of the most efficient and easiest software to use. We have

gone through internet for this and decided to use this software. We can fine the inet

address of our raspi by opening the wireless routers’ home page. Now, in WinSCP we

enter the inet address as the host id and our raspi login id as the user and the password

as password. And before transferring the files, we have to create a new directory, we

named it as www. So command it as “sudo chown –R pi /var/www” where –R means a

recursive call and www is the folder that we are planning to create. Then we have to get

access to transfer files from or to the raspi. Thus we deploy the files to our raspberry pi.

8. Connecting the hardware

Page 17: embedded report

The above circuit shows the diagram for connecting the raspberry pi, LEDs and resistors.

We have randomly used gpio pins 27, 25, 24, 23,22, 18, 17 and 4. We connect the GND

to the ground of the bread board and the LEDs and resistors are connected in series

with each other and in parallel among each other.

The above picture is the circuit that we made with 8 different LEDs.

9. Testing the project

Thus, after all the above steps, we test our project by connecting the raspberry pi and

the laptop or the mobile phone, that we want to use to test the project, be connected

to the same local network. Now, we turn on the raspberry pi which directs us to page

that asks for login id and password.

Page 18: embedded report

The above picture is the page that gets displayed when we turn on the raspberry

Pi. Now we have to check it’s connectivity with the local network, so we input the

command and check the wlan status.

Then we start our raspi and check for our index page and the output. The below picture

is the screen shot of the real time view of our projects’ index page on a mobile phone.

We can observe the feedback in the picture, i.e. LEDs 0, 2, 5, 7 are turned on, so they

are green in color and LEDs 1, 3, 6, 8 are in off state, so they are red in color where

192.168.1.101 is the inet address of the raspberry pi.

Page 19: embedded report

The below picture is the real time output of our project.

10. Globalizing the interface

Our project is now working on a local network where the operating mobile phone or the

laptop has to be connected to the same local network to which the raspberry pi is

connected. In order to globalize it, we have to do the port forwarding so that we can

operate our raspberry pi device from anywhere and from any network connected to

Internet. For this, we tried to open a port on our router, port 80 to be specific as it’s the

http port. We tried adding our raspberry pis’ inet address to the port forwarding so that

when we type the ip to which raspberry pi is connected colon the forwarding port

number slash index.php redirects it to the raspberry pis’ inet address. For suppose,

214.256.219.147 is our IP and we are using port 8080 for forwarding it, then we can

Page 20: embedded report

simply type “214.256.219.147:8080/index.php” for controlling the raspberry pi from any

device that has Internet in it. But we failed doing it as we opened a port in our router for

forwarding it but we could not open a port in the gateway and as it’s the ultimate one

for the data transfer, we can globalize it but if the university gives us an open port.

CODE:

1. INDEX File

<!DOCTYPE html>

<html>

Page 21: embedded report

<head>

<meta charset="utf-8" />

<title>Raspberry Pi Gpio</title>

</head>

<body style="background-color: black;">

<!-- On/Off button's picture -->

<?php

//this php script generate the first page in function of the gpio's

status

$status = array (0, 0, 0, 0, 0, 0, 0, 0);

for ($i = 0; $i < count($status); $i++) {

//set the pin's mode to output and read them

system("gpio mode ".$i." out");

exec ("gpio read ".$i, $status[$i], $return );

//if off

if ($status[$i][0] == 0 ) {

echo ("<img id='button_".$i."' src='data/img/red/red_".$i.".jpg'

alt='off'/>");

}

//if on

if ($status[$i][0] == 1 ) {

echo ("<img id='button_".$i."' src='data/img/green/green_".

$i.".jpg' alt='on'/>");

}

}

?>

<!-- javascript -->

<script src="script.js"></script>

</body>

</html>

2. GPIO

<!-- This page is requested by the JavaScript, it updates the pin's status and

then print it -->

<?php

//Getting and using values

Page 22: embedded report

if (isset ($_GET["pin"]) && isset($_GET["status"]) ) {

$pin = strip_tags($_GET["pin"]);

$status = strip_tags($_GET["status"]);

//Testing if values are numbers

if ( (is_numeric($pin)) && (is_numeric($status)) && ($pin <= 7) && ($pin

>= 0) && ($status == "0") || ($status == "1") ) {

//set the gpio's mode to output

system("gpio mode ".$pin." out");

//set the gpio to high/low

if ($status == "0" ) { $status = "1"; }

else if ($status == "1" ) { $status = "0"; }

system("gpio write ".$pin." ".$status );

//reading pin's status

exec ("gpio read ".$pin, $status, $return );

//printing it

echo ( $status[0] );

}

else { echo ("fail"); }

} //print fail if cannot use values

else { echo ("fail"); }

?>

3. SCRIPT //JavaScript, use pictures as buttons, sends and receives values to/from the Rpi

//These are all the buttons

var button_0 = document.getElementById("button_0");

var button_1 = document.getElementById("button_1");

var button_2 = document.getElementById("button_2");

Page 23: embedded report

var button_3 = document.getElementById("button_3");

var button_4 = document.getElementById("button_4");

var button_5 = document.getElementById("button_5");

var button_6 = document.getElementById("button_6");

var button_7 = document.getElementById("button_7");

//this function sends and receives the pin's status

function change_pin (pin, status) {

//this is the http request

var request = new XMLHttpRequest();

request.open( "GET" , "gpio.php?pin=" + pin + "&status=" + status );

request.send(null);

//receiving information

request.onreadystatechange = function () {

if (request.readyState == 4 && request.status == 200) {

return (parseInt(request.responseText));

}

//test if fail

else if (request.readyState == 4 && request.status == 500) {

alert ("server error");

return ("fail");

}

//else

else { return ("fail"); }

}

}

//these are all the button's events, it just calls the change_pin function and

updates the page in function of the return of it.

button_0.addEventListener("click", function () {

//if red

if ( button_0.alt === "off" ) {

//use the function

var new_status = change_pin ( 0, 0);

if (new_status !== "fail") {

button_0.alt = "on"

button_0.src = "data/img/green/green_0.jpg";

return 0;

Page 24: embedded report

}

}

//if green

if ( button_0.alt === "on" ) {

//use the function

var new_status = change_pin ( 0, 1);

if (new_status !== "fail") {

button_0.alt = "off"

button_0.src = "data/img/red/red_0.jpg";

return 0;

}

}

} );

button_1.addEventListener("click", function () {

//if red

if ( button_1.alt === "off" ) {

//use the function

var new_status = change_pin ( 1, 0);

if (new_status !== "fail") {

button_1.alt = "on"

button_1.src = "data/img/green/green_1.jpg";

return 0;

}

}

//if green

if ( button_1.alt === "on" ) {

//use the function

var new_status = change_pin ( 1, 1);

if (new_status !== "fail") {

button_1.alt = "off"

button_1.src = "data/img/red/red_1.jpg";

return 0;

}

}

} );

button_2.addEventListener("click", function () {

Page 25: embedded report

//if red

if ( button_2.alt === "off" ) {

//use the function

var new_status = change_pin ( 2, 0);

if (new_status !== "fail") {

button_2.alt = "on"

button_2.src = "data/img/green/green_2.jpg";

return 0;

}

}

//if green

if ( button_2.alt === "on" ) {

//use the function

var new_status = change_pin ( 2, 1);

if (new_status !== "fail") {

button_2.alt = "off"

button_2.src = "data/img/red/red_2.jpg";

return 0;

}

}

} );

button_3.addEventListener("click", function () {

//if red

if ( button_3.alt === "off" ) {

//use the function

var new_status = change_pin ( 3, 0);

if (new_status !== "fail") {

button_3.alt = "on"

button_3.src = "data/img/green/green_3.jpg";

return 0;

}

}

//if green

if ( button_3.alt === "on" ) {

//use the function

var new_status = change_pin ( 3, 1);

Page 26: embedded report

if (new_status !== "fail") {

button_3.alt = "off"

button_3.src = "data/img/red/red_3.jpg";

return 0;

}

}

} );

button_4.addEventListener("click", function () {

//if red

if ( button_4.alt === "off" ) {

//use the function

var new_status = change_pin ( 4, 0);

if (new_status !== "fail") {

button_4.alt = "on"

button_4.src = "data/img/green/green_4.jpg";

return 0;

}

}

//if green

if ( button_4.alt === "on" ) {

//use the function

var new_status = change_pin ( 4, 1);

if (new_status !== "fail") {

button_4.alt = "off"

button_4.src = "data/img/red/red_4.jpg";

return 0;

}

}

} );

button_5.addEventListener("click", function () {

//if red

if ( button_5.alt === "off" ) {

//use the function

var new_status = change_pin ( 5, 0);

if (new_status !== "fail") {

button_5.alt = "on"

Page 27: embedded report

button_5.src = "data/img/green/green_5.jpg";

return 0;

}

}

//if green

if ( button_5.alt === "on" ) {

//use the function

var new_status = change_pin ( 5, 1);

if (new_status !== "fail") {

button_5.alt = "off"

button_5.src = "data/img/red/red_5.jpg";

return 0;

}

}

} );

button_6.addEventListener("click", function () {

//if red

if ( button_6.alt === "off" ) {

//use the function

var new_status = change_pin ( 6, 0);

if (new_status !== "fail") {

button_6.alt = "on"

button_6.src = "data/img/green/green_6.jpg";

return 0;

}

}

//if green

if ( button_6.alt === "on" ) {

//use the function

var new_status = change_pin ( 6, 1);

if (new_status !== "fail") {

button_6.alt = "off"

button_6.src = "data/img/red/red_6.jpg";

return 0;

}

}

Page 28: embedded report

} );

button_7.addEventListener("click", function () {

//if red

if ( button_7.alt === "off" ) {

//use the function

var new_status = change_pin ( 7, 0);

if (new_status !== "fail") {

button_7.alt = "on"

button_7.src = "data/img/green/green_7.jpg";

return 0;

}

}

//if green

if ( button_7.alt === "on" ) {

//use the function

var new_status = change_pin ( 7, 1);

if (new_status !== "fail") {

button_7.alt = "off"

button_7.src = "data/img/red/red_7.jpg";

return 0;

}

}

} );

CONCLUSION

Technology is developing at day by day at fast rate, Yester years Livestock carts are replaced by Motor

vehicles and they are replaced with vehicles high speed and gears later automatic gear shift and now its

like your car driving itself. So automation is the future everything you posses will be automated why not

your house? The whole beauty of this project lies in gaining both the technical, practical and coding

Page 29: embedded report

experience and knowledge. This project can thus used as a demo for future enhancements in near

future.