Skip to content

kaiokan/IOT_Project

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

IOT_Project: 人體監測系統

由人體紅外線感測器感測人體方向,再由step motor帶著相機轉至偵測方向並進行拍攝,並可於網頁上看到目前鏡頭畫面。

Input:人體紅外線感測器。
Output:鏡頭畫面顯示於網頁、拍攝照片存於本機。
Video Demo: https://drive.google.com/open?id=1eMStMv6OOEz_Fmdua6y77xWdLzWBTzqE

所需材料:
樹莓派 * 1
麵包板 * 1
鏡頭 * 1
人體紅外線感測器 * 6
步進馬達 * 1
杜邦線 約80條
筷子一雙
雙面膠 * 1
泡綿膠 * 1

步驟一:安裝Node.js

參考教學:https://www.w3schools.com/nodejs/nodejs_raspberrypi.asp
Update your system package list:
pi@w3demopi:~ $ sudo apt-get update
Upgrade all your installed packages to their latest version:
pi@w3demopi:~ $ sudo apt-get dist-upgrade
Doing this regularly will keep your Raspberry Pi installation up to date.
To download and install newest version of Node.js, use the following command:
pi@w3demopi:~ $ curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
Now install it by running:
pi@w3demopi:~ $ sudo apt-get install -y nodejs
Check that the installation was successful, and the version number of Node.js with:
pi@w3demopi:~ $ node -v

步驟二:建立於網頁上監控鏡頭畫面的Server端檔案

參考教學:http://thejackalofjavascript.com/rpi-live-streaming/
在Terminal中run
mkdir node_programs
To step inside that folder, run
cd node_programs
For this post, we will create a new folder named liveStreaming and will step inside this folder. Run
mkdir liveStreaming && cd liveStreaming
First we will initialize a new node project here. Run
npm init
Fill it up as applicable.
Now, we will install express and socket.io modules on our pi. Run
npm install express socket.io --save
Once they are installed, create a new file named index.js.
於index.js中拷入index.js的程式碼
Now we will create a new folder named stream at the root of the liveStreaming folder. This is where our image will be saved.

步驟三:建立於網頁上監控鏡頭畫面的Client端檔案

參考教學:http://thejackalofjavascript.com/rpi-live-streaming/
在livestreaming資料夾中建立index.html檔
於index.html中拷入index.html的程式碼
程式碼完成後,即可執行
node index.js
執行後,於瀏覽器網址列輸入自己的IP位址+:3000即可看到鏡頭的畫面
http://Your_IP_Address:3000

步驟四:接電路與鏡頭

樹莓派GPIO參考:https://pinout.xyz/
步進馬達使用、連接參考:https://www.youtube.com/watch?v=LUbhPKBL_IU&t=182s
人體感測器連接參考:http://iot.pcsalt.com/detecting-obstacle-with-ir-infrared-sensor-raspberry-pi-3/
鏡頭連接參考:https://projects.raspberrypi.org/en/projects/getting-started-with-picamera
我們一共會使用樹莓派的10個Pin(BCM制):
人體感應器使用PIN 5, 6, 13, 19, 26, 21
馬達使用PIN 4, 17, 27, 22
接法請參考上述連結

步驟五:撰寫人體感測與鏡頭旋轉程式

IR.py檔案即為我們主要邏輯控制的Python檔
首先載入我們所需的套件與設定GPIO的格式
from picamera import PiCamera
from gpiozero import LED
from signal import pause
import sys
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

再來,設定相機、人體感測器、和步進馬達的PIN
camera = PiCamera()
LED_PIN = 12
IR_PIN1 = 5
IR_PIN2 = 6
IR_PIN3 = 13
IR_PIN4 = 19
IR_PIN5 = 26
IR_PIN6 = 21

ControlPin = [4,17,27,22]

for pin in ControlPin:
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin,0)

indicator = LED(LED_PIN)
GPIO.setup(IR_PIN1, GPIO.IN)
GPIO.setup(IR_PIN2, GPIO.IN)
GPIO.setup(IR_PIN3, GPIO.IN)
GPIO.setup(IR_PIN4, GPIO.IN)
GPIO.setup(IR_PIN5, GPIO.IN)
GPIO.setup(IR_PIN6, GPIO.IN)

接著定義步進馬達的旋轉速度與方向
步進馬達的運作模式可參考:https://www.youtube.com/watch?v=LUbhPKBL_IU&t=182s
seq1為順時鐘旋轉、seq2為逆時鐘旋轉
seq1 = [ [1,0,0,0],
[1,1,0,0],
[0,1,0,0],
[0,1,1,0],
[0,0,1,0],
[0,0,1,1],
[0,0,0,1],
[1,0,0,1]]

seq2 = [ [1,0,0,0],
[1,0,0,1],
[0,0,0,1],
[0,0,1,1],
[0,0,1,0],
[0,1,1,0],
[0,1,0,0],
[1,1,0,0]]

接著定義使步進馬達旋轉的函數
用count作為觀測目前程式跑了多久的指標,以及用current_position紀錄目前步進馬達的位置
count = 1
current_position = 1

rotate旋轉函數主要判斷
(1) 偵測到的位置在目前位置的相對方向
(2) 偵測到的位置距離目前位置多少個半圈(128)
來決定要用順時鐘旋轉還是逆時針旋轉,以及旋轉多少角度
最後在旋轉完後開啟相機進行拍攝
def rotate(current, detect):
print(current)
print(detect)
if current == detect:
range_num = 0
seq = seq1
elif current > detect:
seq = seq1
if current - detect == 1:
range_num = 128
elif current - detect == 2:
range_num = 256
elif current - detect == 3:
range_num = 384
elif current - detect == 4:
range_num = 512
elif current - detect == 6:
range_num = 640
elif current < detect:
seq = seq2
if detect - current == 1:
range_num = 128
elif detect - current == 2:
range_num = 256
elif detect - current == 3:
range_num = 384
elif detect - current == 4:
range_num = 512
elif detect - current == 5:
range_num = 640
for i in range(range_num):
for halfstep in range(8):
for pin in range(4):
GPIO.output(ControlPin[pin], seq[halfstep][pin])
time.sleep(0.001)
camera.start_preview()
camera.capture('/home/pi/Desktop/image.jpg')
camera.stop_preview()

最後,開一個無限迴圈進行人體監測,若監測到後輸入監測到的感測器方位執行rotate函數,步進馬達即會旋轉到該方位
while True:
detect_1 = GPIO.input(IR_PIN1)
detect_2 = GPIO.input(IR_PIN2)
detect_3 = GPIO.input(IR_PIN3)
detect_4 = GPIO.input(IR_PIN4)
detect_5 = GPIO.input(IR_PIN5)
detect_6 = GPIO.input(IR_PIN6)
if detect_1 == False:
print("{:>3} Sensor1 Detected!".format(count))
time.sleep(0.5)
rotate(current_position, 1)
current_position = 1
elif detect_2 == False:
print("{:>3} Sensor2 Detected!".format(count))
rotate(current_position, 2)
time.sleep(0.5)
current_position = 2
elif detect_3 == False:
print("{:>3} Sensor3 Detected!".format(count))
time.sleep(0.5)
rotate(current_position, 3)
current_position = 3
elif detect_4 == False:
print("{:>3} Sensor4 Detected!".format(count))
time.sleep(0.5)
rotate(current_position, 4)
current_position = 4
elif detect_5 == False:
print("{:>3} Sensor5 Detected!".format(count))
time.sleep(0.5)
rotate(current_position, 5)
current_position = 5
elif detect_6 == False:
print("{:>3} Sensor6 Detected!".format(count))
time.sleep(0.5)
rotate(current_position, 6)
current_position = 6
else:
indicator.off()
print("{:>3} Nothing detected".format(count))
count += 1
time.sleep(0.2)
GPIO.cleanup()

步驟六:固定相機及執行IR.py程式

利用1/4的竹筷利用雙面膠+泡綿膠固定於步進馬達的轉軸上
再將相機固定於竹筷上即可使相機隨著步進馬達轉動 最後,再將步進馬達同樣使用雙面膠+泡綿膠固定於樹莓派的塑膠殼上即可完成本次人體監控系統的專案!

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors