1. Trang chủ
  2. » Kỹ Thuật - Công Nghệ

Điều khiển Led qua Websever với Raspberry pi

9 590 8

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 9
Dung lượng 496,93 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Điều khiển Led qua Websever với Raspberry pi, bật tắt led tại mọi nơi với giao diện web........................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................

Trang 1

HCMUTE KHOA ĐIỆN-ĐIỆN TỬ NGÀNH ĐIỆN TỬ VIỄN THÔNG ĐIỀU KHIỂN LED QUA WEBSEVER VỚI

RASPBERRY PI

Lã Phương Duy – 14141038

A. CÀI THƯ VIỆN :

1 Apache Web Server

Trang 2

sudo apt-get install apache2 –y

2 php

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

3 wiringpi

sudo apt-get install git-core

sudo git clone git://git.drogon.net/wiringPi

cd wiringPi git pull origin

cd wiringPi /build

• Test thư viện wiringpi: gpio -v gpio readall

B. CÁC BƯỚC THỰC HIỆN ĐỂ TẠO GIAO DIỆN WEB VÀ THIẾT LẬP ĐIỀU KHIỂN ĐÉN CHÂN GPIO ( LED )

- Lấy quyền admin để tạo và lưu file ở thử mục “var/www/html”

- Tạo 3 file gpio.php , index.php , script.js ở trong thử mục trên.(CODE 3 file nằm ở cuối bài)

Trang 3

- Truy cập vào trình duyệt web nhập “(ip của raspberry pi)/index.php” vào thanh URL để truy cập vào giao diện web

- Nối lần lượt các led tới các chân GPIO ( tương ứng với WiringPi : 0 - 17,

1 – 18 , 2 – 27 , 3- 22 , ………) với led để điều khiển thông qua giao diện web

Hình : Cơ chế hoạt động của module

Trang 4

Hình : Giao diện web với 4 led

Hình : Kết quả

Trang 5

C. CODES

1. Gpio.php

<?php

//This page is requested by the JavaScript, it updates the pin's status and then print it

//Getting and using values

if (isset ( $_GET["pic"] )) {

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

if ( (is_numeric($pic)) && ($pic <= 7) && ($pic >= 0) ) {

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

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

if ($status[0] == "0" ) { $status[0] = "1"; } else if ($status[0] == "1" ) { $status[0] = "0"; } system("gpio write ".$pic." ".$status[0] );

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

echo($status[0]);

}

else { echo ("fail"); }

} else { echo ("fail"); }

?>

2. Index.php

<!DOCTYPE html>

<html>

<head>

Trang 6

<meta charset="utf-8" />

<title>Raspberry Pi Gpio</title>

</head>

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

<! On/Off button's picture >

<?php

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

//this php script generate the first page in function of the file

for ( $i= 0; $i<8; $i++) {

//set the pin's mode to output and read them system("gpio mode ".$i." out");

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

}

//for loop to read the value

$i =0;

for ($i = 0; $i < 8; $i++) {

//if off

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

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

$i.".jpg' onclick='change_pin (".$i.");'/>");

} //if on

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

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

$i.".jpg' onclick='change_pin (".$i.");'/>");

Trang 7

} }

?>

<! javascript >

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

</body>

</html>

3. script.js

//JavaScript, uses 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");

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");

//Create an array for easy access later

var Buttons = [ button_0, button_1, button_2, button_3, button_4, button_5, button_6, button_7];

Trang 8

//This function is asking for gpio.php, receiving datas and updating the index.php pictures

function change_pin ( pic ) {

var data = 0;

//send the pic number to gpio.php for changes

//this is the http request

var request = new XMLHttpRequest();

request.open( "GET" , "gpio.php?pic=" + pic, true);

request.send(null);

//receiving informations

request.onreadystatechange = function () {

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

data = request.responseText;

//update the index pic

if ( !(data.localeCompare("0")) ){

Buttons[pic].src = "data/img/red/red_"+pic+".jpg"; }

else if ( !(data.localeCompare("1")) ) {

Buttons[pic].src =

"data/img/green/green_"+pic+".jpg";

} else if ( !(data.localeCompare("fail"))) {

alert ("Something went wrong!" );

return ("fail");

} else {

Trang 9

alert ("Something went wrong!" );

return ("fail");

} }

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

alert ("server error");

return ("fail");

} else if (request.readyState == 4 && request.status != 200 &&

request.status != 500 ) {

alert ("Something went wrong!");

return ("fail"); } }

return 0;

}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /

Ngày đăng: 06/02/2018, 23:36

TỪ KHÓA LIÊN QUAN

🧩 Sản phẩm bạn có thể quan tâm

w