Hackster is hosting Hackster Holidays, Finale: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Tuesday!Stream Hackster Holidays, Finale on Tuesday!
정혁채
Published

Parking Lot IoT System

This is a parking lot IoT system.

IntermediateShowcase (no instructions)1,708
Parking Lot IoT System

Things used in this project

Hardware components

PHPoC Blue
PHPoC Blue
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×2
Servo motor
×2
LED
×2

Software apps and online services

PHPoC Debugger
PHPoC Debugger

Story

Read more

Code

task0.php

PHP
<?php 
if(_SERVER("REQUEST_METHOD"))
   exit; // avoid php execution via http request

include "/lib/sd_340.php";
include "/lib/sn_tcp_ws.php";

ht_ioctl(0, "set mode output pulse");
ht_ioctl(0, "set div us");
ht_ioctl(0, "set repc 1");
ht_ioctl(0, "set count 5 10"); // 10us pulse width
 
// setup echo capture timer
ht_ioctl(3, "reset");
ht_ioctl(3, "set div us");
ht_ioctl(3, "set mode capture toggle");
ht_ioctl(3, "set trigger from pin rise");
ht_ioctl(3, "set repc 4");

ht_ioctl(1, "reset");
ht_ioctl(1, "set div us");
ht_ioctl(1, "set mode capture toggle");
ht_ioctl(1, "set trigger from pin rise");
ht_ioctl(1, "set repc 4");

ws_setup(0, "parking", "parking.phpoc");

$time=0;
$time2=0;
$a=0;
$q1=0;
$q2=0;

uio_setup(0,2,"out");
uio_setup(0,3,"out");
$rwbuf = "";
while(1)
{
    ht_ioctl(1, "start"); // we should start capture timer first
    ht_ioctl(3, "start"); // we should start capture timer first
    ht_ioctl(0, "start"); // start trigger pulse

    usleep(100000); // sleep 100ms
    ht_ioctl(3, "stop");
    ht_ioctl(1, "stop");
 
    // 1st capture value ("get count 0") is always zero.
    // we should get 2nd capture value;
    $us = ht_ioctl(1, "get count 1");
    $us2 = ht_ioctl(3, "get count 1");
 
    $dist = $us * 340.0 / 2; // us to meter conversion
    $dist = $dist / 10000; // meter to centimeter conversion
    $dist2 = $us2 * 340.0 / 2; // us to meter conversion
    $dist2 = $dist2 / 10000; // meter to centimeter conversion
    sleep(1);
    printf("%d us, %.1f cm, %d, %d, %d, %d us, %.1f cm\r\n", $us, $dist,$time,$fee,$rst,$us2,$dist2);
   $a= "$dist;;$time;;$q1;;$q2;;$dist2;;$time2";
    
      if(ws_state(0) == TCP_CONNECTED)
      {         
             if(($dist < 5) && ($dist > 0))
            {
               $q1=0;
               $time = $time +1;
               $rst=0;
               uio_out(0,2,LOW);
            }else{
               $q1=($q1 + $time);
               $time = 0;
               uio_out(0,2,HIGH);
            }
            
            if(($dist2 < 5) && ($dist2 > 0))
            {
               $q2=0;
               $time2 = $time2 +1;
              uio_out(0,3,LOW);
            }else{
               $q2=($q2 + $time2);
               $time2 = 0;
               uio_out(0,3,HIGH);
              }
       ws_write(0,(string)$a."\r\n");       
     }
}
?>

index.php

PHP
<!DOCTYPE html>
<html>
<head>
   <title>PHPoC / <?echo system("uname -i")?></title>
   <meta name="viewport" content="width=device-width, initial-scale=0.7">
   <style> body { text-align: center; } </style>
<script>
var canvas_width = 400, canvas_height = 300;
var pivot_x = 200, pivot_y = 200;
var parking_img = new Image();
var parking_img2 = new Image();
var parking_img3 = new Image();
var ws;
var str;

parking_img.src = "주차장_RR.png";
parking_img2.src = "주차장_RRR.png";
parking_img3.src = "주차장_RRRR.png";

function init()
{
   var park = document.getElementById("park_01");
   var ctx = park.getContext("2d");

   park.width = canvas_width;
   park.height = canvas_height;
   park.style.backgroundImage = "url('/주차장_R.png')";

   //ctx.translate(pivot_x, pivot_y);   

   park_01(0);
   
   ws = new WebSocket("ws://<?echo _SERVER("HTTP_HOST")?>/parking", "parking.phpoc");
   document.getElementById("ws_state").innerHTML = "CONNECTING";

   ws.onopen  = function(){ document.getElementById("ws_state").innerHTML = "OPEN" };
   ws.onclose = function(){ document.getElementById("ws_state").innerHTML = "CLOSED"};
   ws.onerror = function(){ alert("websocket error " + this.url) };

   ws.onmessage = ws_onmessage;
}
function ws_onmessage(e_msg)
{
   e_msg = e_msg || window.event; // MessageEvent
   str = e_msg.data.split(';;');
   var dist = Number(str[0]);
   var q1 = Number(str[2]);
   var q2 = Number(str[3]);
   var dist2 = Number(str[4]);
   park_01(dist,q1,q2,dist2);
}
function park_01(dist,q1,q2,dist2)
{
   var park = document.getElementById("park_01");
   var ctx = park.getContext("2d");
   var text;
  
   if((dist < 0) || (dist > 3000000) || (dist2 < 0) || (dist2 > 3000000))
      return;

   ctx.clearRect(0,0, canvas_width, canvas_height);
   
   if((dist < 5) && (dist > 0) && (dist2 < 5) && (dist2 > 0)){
   ctx.drawImage(parking_img3, 0, 0);
   }else if((dist < 15) && (dist > 5) && (dist2 < 5) && (dist2 > 0)){
   ctx.drawImage(parking_img2, 0, 0);
   }else if((dist < 5) && (dist > 0) && (dist2 < 15) && (dist2 > 5)){
   ctx.drawImage(parking_img, 0, 0);
   }
     
}


window.onload = init;
</script>
</head>


<body>
      <center><h1> <<주차예약시스템 >></h1> <br>
      <canvas id="park_01"></canvas>

      <section>
      <article>

      <h3> M E N U <h3>
      <ul> 
         
          <a target="iframe1" href="choose.php">주차예약 </a><br><br>
          <a target="iframe1" href="Check.php">예약자 확인 </a><br>
         
      </ul>
      
      </article>
      </section>
   <p>   
   WebSocket : <span id="ws_state">CLOSED</span><br>
   </p>
      
</body>
</html> 

reservation.php

PHP
<?php
   include_once "/lib/sd_340.php";
      $pw="";
      $zero=0;
?>

<!DOCTYPE    html>
<head>
   <title>PHPoC / <?echo system("uname -i")?></title>
   <meta charset="utf-8">
   <meta name="viewport" content="width=device-width, initial-scale=0.7">
   <style> body { text-align: center; } </style>
</head>

<body>

   <?php um_read(1, 15, $pw, 15);  ?>
      <?php      if((int)$pw !== (int)$zero){            ?>
   
      <script>
      alert('이미 예약 된 곳입니다!! '); 
      window.location.href = 'choose.php'; 
      </script>

      <?    }else{       ?>


   
    <center><h2> << 주차 예약 시스템 >></h2> <br>
    <center><h2>  '구역 1' 을 선택하셨습니다.</h2> <br>
   <center>
                      <!--pwinput.php -->                     
   <form action="pwsavepage.php" method="post" >  
   
      ID : <input type="text" name="id"> <br>
      PW : <input type="password" name="pw"> <br>
      
   <input type="submit" value='예 약'>
   <!-- submit이 하는 일은 action 에서 지정된 페이지로 input에 입력된 값을 가져가면서 이동시킴 -->
   </form>
      <? } ?>
   

</body>
</html>

reservation2.php

PHP
<?php
   include_once "/lib/sd_340.php";
     $pw2="";
      $zero=0;
?>

<!DOCTYPE    html>
<head>
   <title>PHPoC / <?echo system("uname -i")?></title>
   <meta charset="utf-8">
   <meta name="viewport" content="width=device-width, initial-scale=0.7">
   <style> body { text-align: center; } </style>
</head>

<body>

   <?php um_read(2, 15, $pw2, 15);  ?>
      <?php      if((int)$pw2 !== (int)$zero){            ?>
   
      <script>
      alert('이미 예약된 곳입니다!!'); 
      window.location.href = 'choose.php'; 
      </script>

      <?    }else{       ?>
   
    <center><h2> << 주차 예약 시스템 >></h2> <br>
    <center><h2>  '구역 2' 을 선택하셨습니다.</h2> <br>
   <center>
                                  
   <form action="pwsavepage2.php" method="post" >  
      ID : <input type="text" name="id2"> <br>
      PW : <input type="password" name="pw2"> <br>
   <input type="submit" value='예 약'>
   
   </form>
      <? } ?>
   

</body>
</html>

reservationCheck.php

PHP
<?php;
   
   include_once "/lib/sd_340.php";
 
   define("PWM_PERIOD",20000);
   define("WIDTH_MIN", 600);
   define("WIDTH_MAX",2450);

   ht_pwm_setup(2,(WIDTH_MIN + WIDTH_MAX)/2, PWM_PERIOD, "us");
      
   $id="";
   $pw="";
   $pwpw = _POST("pwpw");
   
   
   //2 번 um의 0번지부터 15 byte를 저장
   um_write(3,0, $pwpw, 15); 
   
   //0번 um의 0번지부터 10byte를 읽어옴
   //um_read(0, 0, $pw, 10);
?>

<!DOCTYPE HTML>
<html>
<head>
   <title>PHPoC / <?echo system("uname -i")?></title>
   <meta charset="utf-8">
   <meta name="viewport" content="width=device-width, initial-scale=0.7">
   <style> body { text-align: center; } </style>
</head>

<body>
   
   <?php um_read(1, 15, $pw, 15);  ?>
   <?php printf("사전에 입력한 비밀번호=  %d  ", (int)$pw);?> <br>
   
   <?php um_read(3, 0, $pwpw, 15);  ?>
   <?php printf("확인을 위해 입력한 비밀번호=  %d  ", (int)$pwpw);?> <br>
   
   <?php
   //넘어온 패스워드를 확인하여  비밀번호가 일치하지 않는다면 경고문 출력
   if((int)$pwpw !== (int)$pw){
      
   ?>
   
   <script>
   alert('비밀번호가 일치하지 않습니다!!'); 
   window.location.href = 'pwinput.php'; 
   </script>
   
   <?
   }else{
      $cw=0;
      $width= (WIDTH_MIN + (int)round((WIDTH_MAX - WIDTH_MIN)*$cw/180 , 0));
      
      ht_pwm_width(2,  $width, PWM_PERIOD);
   ?>
   
   <?php um_write(1, 0, 0000, 15);  ?> 
     <?php um_write(1, 15, 0000, 15);  ?>

   <script>
   alert('Gate OPEN'); 
   window.location.href = 'index.php'; 
   </script>
   }

</body>
</html>

reservationCheck2.php

PHP
<?php;
   
   include_once "/lib/sd_340.php";
 
   define("PWM_PERIOD",20000);
   define("WIDTH_MIN", 600);
   define("WIDTH_MAX",2450);

   //ht_pwm_setup(2,(WIDTH_MIN + WIDTH_MAX)/2, PWM_PERIOD, "us");
   st_pwm_setup(0,13, (WIDTH_MIN + WIDTH_MAX) / 2, PWM_PERIOD, "us");
      
   $id2="";
   $pw2="";
   $pwpw2 = _POST("pwpw2");
   
   //1 번 um의 0번지부터 10 byte를 저장
   um_write(3, 15, $pwpw2, 15); 
   
   //0번 um의 0번지부터 10byte를 읽어옴
   //um_read(0, 0, $pw, 10);
?>

<!DOCTYPE HTML>
<html>
<head>
   <title>PHPoC / <?echo system("uname -i")?></title>
   <meta charset="utf-8">
   <meta name="viewport" content="width=device-width, initial-scale=0.7">
   <style> body { text-align: center; } </style>
</head>

<body>
   
   <?php um_read(2, 15, $pw2, 15);  ?>
   <?php printf("사전에 입력한 비밀번호=  %d  ", (int)$pw2);?> <br>
   
   <?php um_read(3, 15, $pwpw2, 15);  ?>
   <?php printf("확인을 위해 입력한 비밀번호=  %d  ", (int)$pwpw2);?> <br>
   
   <?php
   //넘어온 패스워드를 확인하여  비밀번호가 일치하지 않는다면 경고문 출력
   if((int)$pwpw2 !== (int)$pw2){
      
   ?>
   
   <script>
   alert('비밀번호가 일치하지 않습니다!!'); 
   window.location.href = 'pwinput.php'; 
   </script>
   
   <?
   }else{
      $cw=0;
      $width= (WIDTH_MIN + (int)round((WIDTH_MAX - WIDTH_MIN)*$cw/180 , 0));
      
      st_pwm_width(0, $width, PWM_PERIOD);
   ?>
   
   <?php um_write(2, 0, 0000, 15);  ?> 
     <?php um_write(2, 15, 0000, 15);  ?>

   <script>
   alert('Gate OPEN'); 
   window.location.href = 'index.php'; 
   </script>
   }

</body>
</html> 

[Check]
<html>
<head>
<title>PHPoC / <?echo system("uname -i")?></title>
<meta name="viewport" content="width=device-width, initial-scale=0.5">
<style> body { text-align: center; }</style>
</head>

<body>
 
   <center><h2> <<예 약 자 확 인 >> </h2> <br>
    <center><h2> 예약 구역을 선택하세요 </h2> <br>
   
      <a href ="pwinput.php">
          <img src="park1.png"></a>
          
          <a href ="pwinput2.php">
      <img src="park2.png"></a><br>
      
      
      <img src="park3.png"><br>
      
</h2>
 
</body>
</html>

init.php

PHP
<?php

system("php task0.php");

?>

choose.php

PHP
<!DOCTYPE html>
<html>
<head>
<title>PHPoC / <?echo system("uname -i")?></title>
<meta name="viewport" content="width=device-width, initial-scale=0.5">
<style> body { text-align: center; }</style>
</head>

<body>
   <center><h2> << 주차 예약 시스템 >> </h2><br>
   <center><h2>  어느 곳을 예약하시겠습니까? </h2> <br>
   
   <br>
          <a href ="reservation.php">
          <img src="park1.png"></a>
          
          <a href ="reservation2.php">
      <img src="park2.png"></a><br>
      <img src="park3.png"><br>
</body>
</html>

pwinput.php

PHP
<?php
   
   include_once "/lib/sd_340.php";

   $id=0;
   $pw=0;   
   
   $id = _POST("id");
   $pw =_POST("pw");
   $zero=0;
   
?>

<!DOCTYPE HTML>
<html>
<head>
   <title>PHPoC / <?echo system("uname -i")?></title>
   <meta charset="utf-8">
   <meta name="viewport" content="width=device-width, initial-scale=0.7">
   <style> body { text-align: center; } </style>
   
</head>

<body>
   
   <?php um_read(1, 15, $pw, 15);  ?>
      <?php      if((int)$pw == (int)$zero){            ?>
   
      <script>
      alert(' 예약 되지 않은 곳입니다 '); 
      window.location.href = 'Check.php'; 
      </script>

      <?    }else{       ?>
      
   주차구역 1 예약자 정보  <br>
   <?php um_read(1, 0, $id, 15);  ?>
   <?php printf(" ID =  %s  ", $id );?> <br>

   
   <form action="reservationCheck.php" method="post">
   비밀번호를 입력하세요 = <input type="text" name="pwpw" ><br>
   <input type="submit" value='확인'>

   </form>
   
   <? } ?>
</body>
</html>

pwinput2.php

PHP
<?php
   
   include_once "/lib/sd_340.php";

   $id2=0;
   $pw2=0;   
   
   $id2 = _POST("id2");
   $pw2 =_POST("pw2");
   $zero=0;

?>

<!DOCTYPE HTML>
<html>
<head>
   <title>PHPoC / <?echo system("uname -i")?></title>
   <meta charset="utf-8">
   <meta name="viewport" content="width=device-width, initial-scale=0.7">
   <style> body { text-align: center; } </style>
   
</head>

<body>
   <?php um_read(2, 15, $pw2, 15);  ?>
      <?php      if((int)$pw2 == (int)$zero){            ?>
   
      <script>
      alert(' 예약 되지 않은 곳입니다 '); 
      window.location.href = 'Check.php'; 
      </script>

      <?    }else{       ?>
   주차구역 2 예약자 정보  <br>
   <?php um_read(2, 0, $id2, 15);  ?>
   <?php printf(" ID =  %s  ", $id2 );?> <br>

   
   <form action="reservationCheck2.php" method="post">
   비밀번호를 입력하세요 = <input type="text" name="pwpw2" ><br>
   <input type="submit" value='확인'>
   
   </form>
   <? } ?>

</body>
</html>

pwsavepage.php

PHP
<?php;
   
   include_once "/lib/sd_340.php";
 
   define("PWM_PERIOD",20000);
   define("WIDTH_MIN", 600);
   define("WIDTH_MAX",2450);

   ht_pwm_setup(2,(WIDTH_MIN + WIDTH_MAX)/2, PWM_PERIOD, "us");   

   $id=0;
   $pw=0;   
   $id2=0;
   $pw2=0;   
   
   $id = _POST("id");
   $pw =_POST("pw");
   $id2 = _POST("id2");
   $pw2 =_POST("pw2");
   
   
   //0번 um의 0번지부터 15 바이트를 저장
   //ID 를 저장
   um_write(1, 0, $id, 15);
   
   //0번 um의 15번지부터 15 바이트를 저장
   //PW 를 저장
   um_write(1, 15, $pw, 5);
   
   //1번 um의 0번지부터 15 바이트를 저장
   //ID 를 저장
   um_write(2, 0, $id2, 15);
   
   //1번 um의 15번지부터 15 바이트를 저장
   //PW 를 저장
   um_write(2, 15, $pw2, 15);

?>

<!DOCTYPE    html>
<head>
   <title>PHPoC / <?echo system("uname -i")?></title>
   <meta charset="utf-8">
   <meta name="viewport" content="width=device-width, initial-scale=0.7">
   <style> body { text-align: center; } </style>
</head>


<body>
   <?php um_read(1, 15, $pw, 15);  ?>
   <?php um_read(1, 0, $id, 15);  ?>
   <?php um_read(2, 15, $pw2, 15);  ?>
   <?php um_read(2, 0, $id2, 15);  ?>
   
   <?php
      $cw=90; //닫는게 cw 90 
      $width= (WIDTH_MIN + (int)round((WIDTH_MAX - WIDTH_MIN)*$cw/180 , 0));
      
      ht_pwm_width(2,  $width, PWM_PERIOD);
   ?>
   <script>
   alert('예약이 완료 되었습니다'); 
   window.location.href = 'index.php'; 
   </script>
</body>
</html>

pwsavepage2.php

PHP
<?php;
   
   include_once "/lib/sd_340.php";
 
   define("PWM_PERIOD",20000);
   define("WIDTH_MIN", 600);
   define("WIDTH_MAX",2450);

   //ht_pwm_setup(2,(WIDTH_MIN + WIDTH_MAX)/2, PWM_PERIOD, "us");   
   st_pwm_setup(0,13, (WIDTH_MIN + WIDTH_MAX) / 2, PWM_PERIOD, "us");

   $id2=0;
   $pw2=0;   

   $id2 = _POST("id2");
   $pw2 =_POST("pw2");
   

   //1번 um의 0번지부터 15 바이트를 저장
   //ID 를 저장
   um_write(2, 0, $id2, 15);
   
   //1번 um의 15번지부터 15 바이트를 저장
   //PW 를 저장
   um_write(2, 15, $pw2, 15);

?>

<!DOCTYPE    html>
<head>
   <title>PHPoC / <?echo system("uname -i")?></title>
   <meta charset="utf-8">
   <meta name="viewport" content="width=device-width, initial-scale=0.7">
   <style> body { text-align: center; } </style>
</head>


<body>

   <?php um_read(2, 15, $pw2, 15);  ?>
   <?php um_read(2, 0, $id2, 15);  ?>
   
   <?php
      $cw=90; //닫는게 cw 90 
      $width= (WIDTH_MIN + (int)round((WIDTH_MAX - WIDTH_MIN)*$cw/180 , 0));
      
      
      st_pwm_width(0, $width, PWM_PERIOD);
   ?>
   <script>
   alert('예약이 완료 되었습니다'); 
   window.location.href = 'index.php'; 
   </script>
</body>
</html>

Credits

정혁채
0 projects • 2 followers
I'm student inhaUniv I'm trying to make Iot application now

Comments