2015. 7. 30.

감시카메라를 만들어보자(Let's make a cctv using a raspberry pi) - 1

살면서 값싸게 감시카메라 하나 만들어 놓으면 어떨까?
How about make a cheap cctv?

  사실 최종적으로 만들것은 라즈베리파이를 이용해 스마트폰으로 확인할 수 있는 간단한 cctv기능과 더불어, 누군가 무단으로 문을 열고 들어왔을 때 그 분(?)의 사진을 찍고 알려주는 것을 만드는게 목표다.  나는 그냥 라즈베리파이에 전원만 꼽으면 모든것이 자동적으로 설정되도록 하고 싶었다. 외출할 때 마다 로그인을 하고, 프로그램을 실행시키고 하는 것은 모양빠지지 않는가? 전원만 딱! 주면 모든게 쫘라락 되도록 하고 싶다.
  이번 글에서는 로그인을 하지 않고 startx를 자동으로 실행시키는 것과 vnc서버를 이용하여 원격으로 라즈베리파이에 접속하는 것을 할 것이다.

  Actually, I will make a simple device that can be watched by smartphone and detect a man who is trespasser. I just want that just RPi's power is on, then everything is work automatically. If I have to set up my device, it  is inconvenient. 
  The contents of this post is running startx, vnc server automatically with out login.




1. 라즈베리파이에 vnc서버 설치 (Install a vnc server on raspberry pi)


1. 먼저 vnc서버를 아래의 명령어로 라즈베리파이에 설치한다.
1. First, install a vnc server on raspberry pi.

sudo apt-get install tightvncserver


2. 그 후에 아래의 명령어로 server를 만들고 비밀번호를 지정한다. 비밀번호는 최대 8자다.
    ( 비밀번호는 한번 지정하면 다음에 서버를 만들 때는 다시 설정하지 않아도 된다. )
2. Then input command below and set a passward.

vncserver


3. 서버가 잘 실행 중인지 확인한다.
3. Check the server is running well.

sudo netstat -tulpn


 <port 5901과 6001에서 실행되는 것을 알 수 있다. - 기본적으로 5901이다.>
<You can see that the server is running in port 5901, 6001 - Basically port 5901>

그런데 문제는 라즈베리파이를 부팅할 때 마다 vncserver 명령어를 입력해야한다는 것이다. 그래서 다음과 같이 설정한다.
But the problem is you have to input command 'vncsever' everytime. So you have to do a little more.






2. 부팅 시 자동으로 vnc서버 실행하기(Make a vnc server run automatically)



1. 먼저 vi를 이용하던 해서 /etc/rc.local의 환경설정을 수정해준다.  
1. First, using vi, change /etc/rc.local

sudo nano /etc/rc.local


rc.local 의 내용 중에서 부팅 시에 자동 실행되고 싶은 내용을 'fi'와 'exit 0' 사이에 추가한 후 재부팅한다.
Add something whatever you want that automatically run beside 'fi' and 'exit 0'.

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will “exit 0″ on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
# Print the IP address
_IP=$(hostname -I) || true
if [ “$_IP” ]; then
printf “My IP address is %sn” “$_IP”
fi
# ADD line :
su pi -c ‘tightvncserver :1′ &
exit 0



2. 이제 update-rc.d 명령어로 등록을 해야한다.
2. Then you have to add that using update-rc.d

sudo nano /etc/init.d/tightvncserver

/etc/init.d 안에 tightvncserver를 만들고 내용을 아래와 같은 내용으로 스크립트를 추가한다.
make tightvncserver in /etc/init.d, and save like below.

### BEGIN INIT INFO
# Provides: vncboot
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start VNC Server at boot time
# Description: Start VNC Server at boot time.
### END INIT INFO

#! /bin/sh
# /etc/init.d/vncboot

USER=root
HOME=/root

export USER HOME

case “$1″ in
start)
echo “Starting VNC Server”
#Insert your favoured settings for a VNC session
/usr/bin/vncserver :0 -geometry 1280×800 -depth 16 -pixelformat rgb565
;;

stop)
echo “Stopping VNC Server”
/usr/bin/vncserver -kill :0
;;

*)
echo “Usage: /etc/init.d/vncboot {start|stop}”
exit 1
;;
esac

exit 0



3. 그 후에 /etc/init.d/tightvncserver 실행등록을 한다.
3. Then, make run registration about /etc/init.d/tightvncserver.

sudo chown pi:pi /etc/init.d/tightvncserver
sudo chmod 755 /etc/init.d/tightvnc
sudo update-rc.d /etc/init.d/tightvnc defaults
sudo reboot

 자 이제 부팅시 자동으로 vnc서버가 켜지게 되었다! 하지만 한가지 더 해야할일이 남았다. 라즈베리파이 로그인을 자동으로 해야한다.
 Well, vnc server is running automatically when the raspberry pi boot. But we have to one more thing. Automatically login.




3. 부팅 시 자동으로 로그인 및 startx 실행하기(Login and run startx automatically)


1. vi를 이용하여 /etc/inittab 파일을 열고 :set nu 명령어를 이용해 줄번호가 나오게 하자(그래야 찾기 쉽다)
1. Using vi, open  /etc/inittab


<원래의 54번째 줄을 주석처리하고 그 아래에 내용을 추가한다.>
<Comment out the original 54th line and add the new contents below.>

 원래 54번째 줄에 있는 내용을 #으로 주석처리하고 아래의 내용을 추가한 후 저장한다.
Comment out the original 54th line using '#' and add the new contents below and save.

1:2345:respawn:/bin/login -f pi tty1 </dev/tty1 >/dev/tty1 2>&1


2. 그리고 위에서 수정했던 /etc/rc.local 파일의 su pi -c ‘tightvncserver :1′ & 의 아래에 다음과 같은 내용을 추가한다.
2. Then add a content like below in a file '/etc/rc.local' that we changed before.

su -l pi -c startx






  이제 재부팅을 해보면 자동으로 로그인을 하고 startx를 실행하며, vnc서버를 실행 하는 것을 알 수 있다.

Now, We can see that the raspberry pi will login, run startx, run vnc server automatically.








4. vnc서버 접속해보기(Check vnc server)


1. pc 에서 vnc 서버 접속하기
1. Connect in PC.


<pc용 vnc프로그램으로 접속을 한다. 이때 'ip주소:포트번호' 로 입력한다.>
<Connect a vnc program for PC version. Input 'ip address:port number'>

<비밀번호를 입력한다.>
<Input a password>


<접속이 되었다.>
<It is connected well>




2. 스마트폰에서 vnc 서버 접속하기
2. Connect in smartphone.

<playstore에서 검색했더니 앱이 있었다.  ip주소, port번호, 비밀번호 등을 입력하고 접속한다.>
<I find an application on playstore.>


<접속이 된다.>
<It is connected well>




이제 기본적인 발판은 마련이 되었다.
Now, the basic work is finished.


참고 사이트 : http://www.rasplay.org/?p=4854


댓글 없음: