Transcript 다운로드
시스템소프트웨어
1401552 김 영욱
1401586 양 오석
목적
라즈베리파이에서 GPIO를 이용하여 초음파 센서를 연결하고 거리를 측정해 일
정한 거리안에 사람이 들어오면 파이 카메라를 이용하여 사진을 찍고 데이터 베
이스에 올려 웹에서 사진을 보고 검색을 할 수 있게 했습니다.
프로젝트 재료
라즈베리파이
카메라모듈
초음파 센서
Flow chart
시작
시작
없으면
앞에 물체
있는지 판단
있으면
사진을 찍고 저장한뒤 파일
이름을 db에 저장한다
Db에
파일이
있는지
판단
없으면
있으면
화면에 출력
회로도
프로젝트 과정
라즈베리파이에 Python과 mysql을 연동시켜주는 파일을 설치하고 초음파 센서
와 카메라 모듈을 연결하여 python을 이용하여 일정거리 안에 들어오면 사진을
찍고 mysql에 접속하여 파일명을 DB에 저장하였습니다.
Apache2,mysql,php를 설치하여 웹서버를 구축하고 php를 이용하여 mysql에 접
속하고 DB에서 파일명들을 불러와 <img> 태그를 이용하여 DB에 저장되어있는
이미지들을 불러오고 검색할 수 있게 하였습니다.
Python소스
#-*- coding: utf-8 -*-
import RPi.GPIO as GPIO
import time
import picamera
import MySQLdb
mysqlid='root'
mysqlpasswd='1234'
database='image'
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
# 20번 GPIO를 출력 방향, 21번 GPIO를 입력 방향으로 설정 한다.
TRIG = 20
ECHO = 21
Python 소스
GPIO.setup(TRIG, GPIO.OUT)
GPIO.setup(ECHO, GPIO.IN)
GPIO.output(TRIG, False)
time.sleep(2) # Wait for sensor settle
while True:
GPIO.output(TRIG,True)
time.sleep(0.00001) #why?
GPIO.output(TRIG, False)
# 음파를 방출
while GPIO.input(ECHO)==0:
s_time= time.time()
Python 소스
# 반파를 송신
while GPIO.input(ECHO)==1:
e_time= time.time()
#비행시간측정
duration = e_time-s_time
#거리 = 시간*속도/2
distance = duration *17150
#소수 둘째자리까지 반올림
distance = round(distance, 2)
print "distance :" , distance, " cm"
Python 소스
if(distance<50):
db=MySQLdb.connect('localhost',mysqlid,mysqlpasswd,database);
with picamera.PiCamera() as camera:
camera.start_preview()
now=time.localtime()
s="%04d_%02d_%02d_%02d_%02d_%02d.jpg"%(now.tm_year,now.tm_mon,now.tm_md
ay,now.tm_hour,now.tm_min,now.tm_sec)
print s
file="/var/www/image/"+s
camera.capture(file)
camera.stop_preview()
Python 소스
cur=db.cursor()
cur.execute("insert into img(name) values(\'"+s+"\')")
db.commit()
cur.close()
db.close()
time.sleep(10)
GPIO.cleanup()
Python 설명
ECHO를 21번에 TRIG을 20번에 연결하고 gnd를 끌어다 echo에 저항 3K를 연결
하였습니다.
Rpi.GPIO 모듈을 이용하여 초음파센서를 설정하고 초음파가 갔다 오는 시간을
duration 변수에 넣고 거리를 시간*속도/2를 하여 계산을 하였습니다.
MySQLdb 모듈과 picamera 모듈을 이용하여 거리가 50 미만이면 사진을 찍고
DB에 저장을 하게 하였습니다.
웹 소스(Index.php)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> Double-Y</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" type="text/css" href="css/common.css">
</head>
<body>
<div id="header">
<a href="index.php"><img src="./img/logo.png" border="0“></a></div>
</div> <!-- end of header -->
<div id="menu">
<a href= "index.php"> <h1>Index</a>
<a href= "search.php">Img</h1></a>
웹 소스(Index.php)
</div><!--end of menu-->
<div id="mysql">
<table border=1>
<thead>
<tr>
<th>Image</th>
<th>Date</th>
</tr>
</thead>
<? include "mysql.php";?>
</table>
</div>
</body>
</html>
웹 소스(Index.php)
HTML소스를 이용하여 웹에 대한 기본틀을 짜고 PHP 소스를 이용하여 다른 php
파일을 include 하였습니다.
<table>태그를 이용하여 표를 만들고 표안에 이미지를 넣게 하였습니다.
웹 소스(mysql.php)
<?php
$hostname="127.0.0.1";
$username="root";
$password="1234";
$dbname="image";
$connect=mysql_connect($hostname,$username,$password) or die("MySQL Fail");
$dbconn=mysql_select_db($dbname,$connect);
$result=mysql_query("select name from img");
echo "<tbody>";
while($row=mysql_fetch_assoc($result)){
$n=$row['name'];
웹 소스(mysql.php)
$date = substr($n,0,4)."/".substr($n,5,2)."/".substr($n,8,2)."
".substr($n,11,2).":".substr($n,14,2).":".substr($n,17,2);
echo "<tr>";
echo "<td> <img src = 'image/".$row['name']."' width = \"360\" height = \"240\"> </td>";
echo "<td>";
echo $date;
echo "</td>";
echo "</tr>";
}
echo "</tbody>";
mysql_close();
웹 소스(mysql.php)
Hostname과 username, password, dbname을 변수로 선언하여 저장해놓고
mysql_connect를 이용하여 mysql에 접속하여 connect변수에 상태를 넣었습니
다.
Mysql_select_database를 이용하여 데이터베이스를 설정하고 dbconn변수에 넣
었습니다.
Img테이블에 있는 모든 name컬럼을 가져오는 Sql문인 ‘select name from img’
을 Mysql_query를 이용하여 result 변수에 저장하였습니다.
mysql_fetch_assoc를 이용하여 result 변수에 있는 것들을 배열로써 불러와 컬
럼명을 이용하여 접근할수 있게 하였습니다.
Substr을 이용하여 문자열을 잘라 찍은 시간을 볼 수 있게 하였습니다.
<img> 태그를 이용하여 이미지를 불러오고 width와 height 를 이용하여 크기를
조정하였습니다.
웹 소스(search.php)
<head>
<title> Double-Y</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" type="text/css" href="css/common.css">
</head>
<body>
<div id="header">
<a href="index.php"><img src="./img/logo.png" border="0“></a></div>
</div> <!-- end of header -->
<div id="menu">
<a href= "index.php"> <h1>Index</a>
<a href= "search.php">Img</h1></a>
</div><!--end of menu-->
웹 소스(search.php)
<div>
<form method="post" action='./lib/s_m.php'>
<input type="text" name="date1" value="yyyy_mm_dd_hh_mm_ss" size = 24> ~
<input type="text" name = "date2" value="YYYY_MM_DD_hh_mm_ss" size=24>
<input type="submit" value="search">
</form>
</body>
</div>
웹 소스(search.php)
Html로 기본적인 웹 틀을 작성하고 <form>태그를 이용하여 검색버튼을 누르면
검색을 하는 파일이 있는 경로로 가 그 파일을 불러올 수 있게 했고, <input> 태
그를 이용하여 text를 넣는 창과 검색버튼을 넣을수 있게 하였습니다.
웹 소스(s_m.php)
<div id="header">
<a href="../index.php"><img src="../img/logo.png" border="0“></a></div>
</div> <!-- end of header -->
<div id="menu">
<a href= "../index.php"> <h1>Index</a>
<a href= "../search.php">Img</h1></a>
</div>
웹 소스(s_m.php)
<?php
$Date1 = $_POST["date1"];
$Date2 = $_POST["date2"];
$hostname="127.0.0.1";
$username="root";
$password="1234";
$dbname="image";
$connect=mysql_connect($hostname,$username,$password) or die("MySQL
Fail");
$dbconn=mysql_select_db($dbname,$connect);
웹 소스(s_m.php)
$result=mysql_query("select name from img where name between '".$Date1.".jpg'
and '".$Date2.".jpg'");
echo "<table border=1>";
echo "<thead> <tr> <th>Img</th><th>Date</th></tr></thead>";
echo "<tbody>";
while($row=mysql_fetch_assoc($result)){
$n=$row['name'];
echo "<tr>";
$date = substr($n,0,4)."/".substr($n,5,2)."/".substr($n,8,2)."
".substr($n,11,2).":".substr($n,14,2).":".substr($n,17,2);
echo "<td>";
echo "<img src = '../image/".$row['name']."'>";
웹 소스(s_m.php)
echo "</td>";
echo "<td>";
echo $date;
echo "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
mysql_close();
?>
웹 소스(s_m.php)
_POST를 이용하여 search.php 에서 입력한 text를 불러왔습니다.
데이터베이스에 접속하고 ‘select name from img where between $Date1 and
$Date2’를 이용하여 전에 입력한 text 사이의 기간에 찍었던 사진들을 불러오게
하였습니다.
실행 화면 (index.php)
실행화면 (search.php)
실행화면 (s_m.php)(검색결과)