• 沒有找到結果。

第三章 系統概觀

3.4 系統流程演算法

系統流程演算法旨在詳述說明上述在使用介面的程式執行流程,包括酒精感測 器和藍芽模組(Alcohol Sensor and Bluetooth Module)、心跳感測器和藍芽模組(Heartbeat

Sensor and Bluetooth Module)、行動感測系統(Mobile Sensing System)、在網頁中駕駛 緊急情況(Emergency Situation of Driver in Web)、控制伺服器寄送簡訊給使用者(Control Server Send Message to User)。演算法以 Pseudo-Code 表示如下:

Figure 3.9 所示,車輛駕駛員在程式環境提供的監看視窗下利用酒精感測器吹一口 氣的秒數感測值相減來看判定是否喝酒。圖中分別顯示在正常狀況和喝酒狀況時之比較。

由圖中可以看出車輛駕駛員在正常狀況下,酒精的秒數感測值相減最多不超過 20。在喝 酒狀況下,酒精的秒數感測值相減會急遽上升,超過 20 的會發現有好多次。所以根據 以上所述酒精感測器的標準,在 Figure 3.10 的 Pseudo-Code 以此標準將讀取數值經由藍 芽傳送給智慧型手機。

19

Figure 3.9:Seeeduino 程式環境視窗(酒精)

Algorithm 1:酒精感測器和藍芽模組 Rx; //Receive of Bluetooth Module Tx; //Transmit of Bluetooth Module PinA; //Pin of Alcohol Sensor

Repeat Workflow() {

DifferencePinA = New(PinA) - Old(PinA);

if(Bluetooth has sensor data) {

Bluetooth to Smartphone("Alcohol:" DifferencePinA);

Delay(10000);

} }

BlueToothSerial.begin(38400) // Baud Rate of Bluetooth

SendBlueToothCommand(Search) // Slave Device Search Surrounding SendBlueToothCommand(Slave) // Slave Device

SendBlueToothCommand(GroveBluetooth) // Slave Device Name "GroveBluetooth"

SendBlueToothCommand(Connection) // Slave Device Establish Connection SendBlueToothCommand(0000) // Slave Device Pincode 0000

Figure 3.10:酒精感測器和藍芽模組

20

Figure 3.11 所示,車輛駕駛員在程式環境提供的監看視窗下利用穿戴式心跳感測器 來判斷車輛駕駛員心跳是否正常。心跳感測器以計數器 20 次計算一次心跳率,20 次是 為了取得平均心跳速率。採用 Seeeduino 板子時間去計算 20 次總間隔時間,在代入公式

(3.1)去計算。公式(3.1)[17]包括 Minute 指一分鐘的時間、Twenty Counter 表示計數 器執行 20 次、1000 為程式函數為毫秒轉成秒、Twenty Board Interval Time 總共 20 次間 隔時間,透過式子(3.1)可以得到駕駛員的心跳速率。如以下表示:

Heartbeat = 𝑀𝑖𝑛𝑢𝑡𝑒×𝑇𝑤𝑒𝑛𝑡𝑦 𝐶𝑜𝑢𝑛𝑡𝑒𝑟×1000

𝑇𝑤𝑒𝑛𝑡𝑦 𝐵𝑜𝑎𝑟𝑑 𝐼𝑛𝑡𝑒𝑟𝑣𝑎𝑙 𝑇𝑖𝑚𝑒 (3.1)

Figure 3.11:Seeeduino 程式環境視窗(心跳)

在 Figure 3.12 的 Pseudo-Code 內心跳公式以此公式去計算,然後在將心跳率經由藍 芽模組傳送至智慧型手機。

Algorithm 2:心跳感測器和藍芽模組 PinH; //Pin of Heartbeat Sensor

21

//Initial Setting Setup()

{

// Specifies a function to call when an external interrupt occurs

attachInterrupt(0,Interrupt,RISING) //Pin, Interrupt, Interrupt Mode }

Repeat Workflow() {

PinH = (60x20x1000)/Twenty Board Interval Time If(Bluetooth has sensor data)

//Interrupt Process Interrupt()

if(IntervalBoardTime > 2 second)

"Heart rate measure error,test will restart";

if(Counter == 20)

SendBlueToothCommand(Search) // Slave Device Search Surrounding SendBlueToothCommand(Slave) // Slave Device

SendBlueToothCommand(GroveBluetooth) // Slave Device Name "GroveBluetooth"

22

SendBlueToothCommand(Connection) // Slave Device Establish Connection SendBlueToothCommand(0000) // Slave Device Pincode 0000

Figure 3.12:心跳感測器和藍芽模組

在 Figure 3.13 的 Pseudo-Code 是在智慧型手機上運作的程式流程,包括接收生物感 測器的感測資料、GPS 定位、感測資料與地理位置傳至控制伺服器。

Algorithm 3:行動感測系統 alcohol_value; //酒精讀數 heartbeat_value; //心跳速率 //UUID Bluetooth Message Format

UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

MAC-address of Seeeduino Bluetooth module = "00:13:EF:12:1A:27" //Seeeduino BT //Check Smartphone Bluetooth

checkBTState() {

if(BluetoothAdapter == close) Bluetooth turn on;

}

//Smartphone connect Seeeduino Process

BluetoothServerSocket = createRfcommSocketToServiceRecord(MY_UUID);

getRemoteDevice(Seeeduino Bluetooth module MAC-address);

createBluetoothSocket(Seeeduino Bluetooth module MAC-address);

socket.connect();

socket.close();

ConnectedThread.start();

//Thread Process ConnectedThread {

socket.getInputStream(); //Receive Sensor Data InputStream to Handler; //Handler Display Screen }

23

// Handler Process Handler()

{

if(Text == "Alcohol") //Alcohol Sensor Data alcohol_value;

if(Text == "Heartbeat") //Heartbeat Sensor Data heartbeat_value;

}

//GPS Location of Driver

getSystemService(LOCATION_SERVICE); //GPS Service

location = getLastKnownLocation(BestProvider); // Provider Optimal Location getLocation(location);

{

Double longitude = location.getLongitude(); //經度 Double latitude = location.getLatitude(); //緯度 }

//Smartphone to Database Timer2.schedule(10000, 100000 {

DefaultHttpClient(); //Use HttpPost HttpPost("http://120.108.205.42/test.php"); //Web Server ArrayList Parameter; //User Parameter Parameter(ID);

Parameter(driver);

Parameter(alcohol_value);

Parameter(heartbeat_value);

Parameter(longitude);

Parameter(latitude);

request http URL;

HttpClient execute(HttpPost);

}

Figure 3.13:行動感測系統

24

在 Figure 3.14 中 Pseudo-Code 表示感測資料與地理位置儲存在控制伺服器和控制伺 服器與網頁伺服器同步,顯示車輛駕駛員緊急事故在網頁伺服器上,以文字和 Google MAP 顯示車輛駕駛人的緊急事故位置提供給使用者。

Algorithm 4:在網頁中駕駛緊急情況

<?php

//Control Server Database information

Hostname = localhost; //Host Name Database = systemdatabase; //Custom Database Username = siaojiang; //Database Account Password = 19880825; //Database Password // Establish a database connection

mysql_connect(Hostname, Database, Username, Password);

mysql_select_Database;

//Driver Sensor Data and Location Insert to Database if(Receice Smartphone Data)

insert ID, driver, alcohol_value, heartbeat_value, longitude, latitude, Time;

// Get Data from the database

if(alcohol_value > 20 (heartbeat_value < 60 || heartbeat_value > 100)){

ID, driver, alcohol_value, heartbeat_value, longitude, latitude, Time;

} }

?>

//Google MAP

25

<!DOCTYPE html>

<html>

maker ID, driver, alcohol_value, heartbeat_value, longitude, latitude, Time;

}

Figure 3.14:在網頁中駕駛緊急情況

在 Figure 3.15 中車輛駕駛員發生酒精濃度或心跳速率超過門檻值時,控制伺服器經

26

由中華電信的伺服器以簡訊方式傳送車輛駕駛員的相關資訊給相關使用者。

Algorithm 5:控制伺服器寄送簡訊給使用者

<?php

//Control Server Database information $hostname = "localhost"; //Host Name

$database = "systemdatabase"; //Custom Database

$username = "siaojiang"; //Database Account

$password = "19880825"; //Database Password // Establish a database connection

mysql_connect(Hostname, Database, Username, Password);

mysql_select_Database;

//Driver Sensor Data and Location Insert to Database if(Receice Smartphone Data)

insert ID, driver, alcohol_value, heartbeat_value, longitude, latitude, Time;

// Threshold

if(alcohol_value > 20 (heartbeat_value < 60 || heartbeat_value > 100)){

include "SendSMS/sms2.inc";

error_reporting (E_ALL); //specify the PHP program returns an error //Web Display mobile_number= "0972836863"; //User Phone

message= "使用者" 'driver "於經度" latitude "緯度" longitude "發生異常";

/* Establish a connection */

27

Figure 3.15:控制伺服器寄送簡訊給使用者 create_conn(server_ip, server_port, TimeOut, user_acc, user_pwd);

getmessage();

/*Connection Successfully Sent Messages*/

if(Connection is Successful){

mysms->send_text(mobile_number, message);

mysms-> getmessage();

echo "Connection Failed";

}

/*Connection Closed */

close_conn();

}

?>

28

相關文件