Things used in this project

Hardware components:
Phpoc blue per 100x1000 euk22wtv9i
PHPoC Blue
×1
Screen shot 2017 02 16 at 2 2fejqs9vfo
MikroElektronika RELAY click
×1
51gzz5eu9pl. sx425
HC-SR04
×1
On/off Switch
×1

Code

User Interface (index.php)PHP
This code provide the user interface and allows user set schedule via webpage
<?php
set_time_limit(30);

include_once "/lib/sc_envu.php";

$envu =  envu_read("envu");

if(_SERVER("REQUEST_METHOD") == "POST")
{
    $data       = _POST("data");
    
    envu_update($envu, "schedule", $data);
    envu_write("envu", $envu, strlen($envu), 0);
    system("reboot sys 1000");
}
else
{
    if($envu == "")
    {
        $envu = "schedule=\r\n";
        envu_write("envu", $envu, strlen($envu), 0);
    }
}

?>
<!DOCTYPE html>
<html>
<?php
$stypeno_table = array(0, 1, 2);
$stype_table = array("RS232", "RS485", "RS422");
$baud_table = array(230400, 115200, 57600, 38400, 19200, 14400, 9600, 4800, 2400, 1200);
$parityno_table = array(0, 1, 2, 3, 4, 5);
$parity_table = array("None", "Even", "Odd", "Mark", "Space");
$fctrlno_table = array(0, 1, 2);
$fctrl_table = array("None", "RTS/CTS", "Xon/Xoff");
$operationno_table = array(0, 2);
$operation_table = array("TCP Server", "TCP Client");
?>
<head>
    <title><?php echo system("uname -i");?> WEB Configuration</title>
    <meta content="initial-scale=0.5, maximum-scale=1.0, minimum-scale=0.5, width=device-width, user-scalable=yes" name="viewport">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.min.css">
    <style type="text/css">
        body { font-family: verdana, Helvetica, Arial, sans-serif, gulim; }
        h1 { font-weight: bold; font-family : verdana, Helvetica, Arial, verdana, sans-serif, gulim; font-size:15pt; padding-bottom:5px;}
        .schedule {border-collapse:collapse; width:640px;  font-size:14pt; table-layout: fixed;}
        .theader { font-weight: bold;}
        tr {height :28px; border-bottom: 1px solid #ccc;}
        td { padding-left: 10px; text-align: center;}
        .delete { font-weight: bold; text-align: center !important;}
        .superHeader {height: 2em; color: white; background-color: rgb(0,153,153); font-size:9pt; position:fixed; left:0; right:0; top:0; z-index:5;  }        
        .right {
          color: white;
          position: absolute;
          right: 3em;
          bottom: 4px;
          font-size:12pt;          
        }    
        .left {
          color: white;
          position: absolute;
          left: 3em;
          bottom: 4px;
          font-size:12pt;          
        }
        .right a, .left a
        {
          color: white;
          background-color: transparent;
          text-decoration: none;
          margin: 0;
          padding:0 2ex 0 2ex;
        }            
        .right a:hover, .left a:hover 
        {
          color: white;
          text-decoration: underline;
         }         
        .midHeader {color: white; background-color: rgb(6, 38, 111);  position:fixed; left:0; right:0; top:1.5em;  z-index:3;}
        .headerTitle {
          font-size: 250%;
          font-weight: normal;
          margin: 0 0 0 4mm;
          padding: 0.25ex 0 1ex 0;
          font-family: impact;
        }
        #footer{margin:0 auto; height:auto !important; height:100%; margin-bottom:-100px;  }
        .superFooter {
            height: 2em; color: white; background-color: rgb(6, 38, 111); font-size:12pt; position:fixed; left:0; right:0; bottom:0; z-index:4; 
        }                
        .zebra {background-color : #ECECEC;}
        .del_icon {
            color: #cc0000;
            font-size: 120%;
            font-weight: bold;
        }
        .del_icon:hover { 
            color: red;
            cursor: pointer;
        }
        .add_icon {
            color: #009933;
            font-size: 150%;
            font-weight: 900;
        }
        .add_icon:hover { 
            color: #1aff66;
            cursor: pointer;
        }
    </style>
    <script type="text/javascript">
    <?php
        $envu =  envu_read("envu");
        $schedule = envu_find($envu, "schedule");
        
    ?>
    var schedule_list = new Array();
    
    function init() 
    {    
        var scheduleTable = document.getElementById("scheduleTable");
        
        var data = "<?php echo $schedule?>";
        
        if(data != "")
            schedule_list = data.split("||");  
        
        for(var i = 0; i < schedule_list.length; i++)
        {
            schedule_list[i] = schedule_list[i].split("|");
            // display schedule on schedule table
            var row = scheduleTable.insertRow();
            //row.style.color = "#1E90FF";
            var rowHtml = '<td width="190px">';
            rowHtml += schedule_list[i][0];
            rowHtml += '</td><td>';
            rowHtml += schedule_list[i][1];
            rowHtml += '</td><td>';
            rowHtml += schedule_list[i][2];
            rowHtml += '</td><td class="delete"><span class="del_icon" onclick="del_schedule(this)">&#x2716;</span></td>';    
            
            row.innerHTML = rowHtml;
        }
    }
    
    function del_schedule(e)
    {
        var row_index = e.parentElement.parentElement.rowIndex;
        console.log(row_index);
        document.getElementById("scheduleTable").deleteRow(row_index);
        schedule_list.splice(row_index-1, 1); 
    }
    
    function update_schedule(e)
    {
        var table = document.getElementById("scheduleTable");
        var row = table.insertRow();
        row.innerHTML = '<td width="190px">DAY</td><td>FROM</td><td>TO</td><td class="delete"><span class="del_icon" onclick="del_schedule(this)">X</span></td>';    
    }
    
    function add_schedule()
    {
        var addTable = document.getElementById("addTable");
        var scheduleTable = document.getElementById("scheduleTable");
        
        var numRow = addTable.rows.length;
        if(numRow > 0)
        {
            var row  = addTable.rows[0];
            var days = row.cells[0].childNodes[0];
            var day = days.options[days.selectedIndex].value;
            var startTime = row.cells[1].childNodes[0].value;
            var endTime = row.cells[2].childNodes[0].value;
            console.log(day + ":" + startTime + ":" + endTime);
            
            //add new row in schedule table
            row = scheduleTable.insertRow();
            row.style.color = "#1E90FF";
            var rowHtml = '<td width="190px">';
            rowHtml += day;
            rowHtml += '</td><td>';
            rowHtml += startTime;
            rowHtml += '</td><td>';
            rowHtml += endTime;
            rowHtml += '</td><td class="delete"><span class="del_icon" onclick="del_schedule(this)">&#x2716;</span></td>';    
            schedule_list.push([day, startTime, endTime]);
            
            row.innerHTML = rowHtml;
            
            //delete old row in add table
            addTable.deleteRow(0);
            
            //change confirm to add sign
            document.getElementById("add_icon").innerHTML = '&#x271A;';
        }
        else
        {        
            var newRow = addTable.insertRow();
            var cell0 = newRow.insertCell();
            var cell1 = newRow.insertCell();
            var cell2 = newRow.insertCell();
            var cell3 = newRow.insertCell();
            
            var select = '<select>';
            select += '<option value="Sun">Sunday</option>';
            select += '<option value="Mon">Monday</option>';
            select += '<option value="Tue">Tuesday</option>';
            select += '<option value="Wed">Wednesday</option>';
            select += '<option value="Thu">Thursday</option>';
            select += '<option value="Fri">Friday</option>';
            select += '<option value="Sat">Saturday</option>';                          
            select += '</select>';
            cell0.innerHTML += select;
            cell1.innerHTML = '<input type="time" name="usr_time" value="09:00">';
            cell2.innerHTML = '<input type="time" name="usr_time" value="18:00">';
            cell3.innerHTML = "";
            
            //change add to confirm sign
            document.getElementById("add_icon").innerHTML = '&#x2714;';
        }
    }
    
    function excSubmit()
    {    
        var schedule_form = document.schedule_form;
        
        var data = new Array();
        
        for(var i = 0; i < schedule_list.length; i++)
        {
            data.push(schedule_list[i].join("|")) ;
        }
        
        schedule_form.data.value = data.join("||");
        
        schedule_form.submit();        
    }
    
    window.onload = init;
    </script>
</head>
<body>
    <div id="header">
        <div class="superHeader">        
            <div class="left">
            </div>    
            <div class="right">
                <a href="http://www.phpoc.com/forum/" target="_blank">PHPoC Forum</a>
            </div>
        </div>

        <div class="midHeader">
            <h1 class="headerTitle"><center>Scheduler</center></h1>
            <div class="left">            
            </div>
        </div>
        
        <div class="subHeader">
        </div>        
    </div>    
    <br /><br /><br /><br />    
    <center>
        <form name="schedule_form" action="index.php" method="post">    
        <hr style="margin:50px 0 -10px 0; width:640px;" size="6" noshade>
        <h1></h1>
        
        <table id="scheduleTable" class="schedule">
            <tr class="zebra">
                <td class="theader">DAY</td>    
                <td class="theader">FROM</td>
                <td class="theader">TO</td>
                <td class="delete"><i class="fa fa-trash"></i></td>    
            </tr>    
        </table>
        <input type="hidden" name="data" value="">
        </form>
        <br>
        <table id="addTable" class="schedule">
        </table>
        
        <table style="width:640px;">
            <tr>
                <td class="theader" style="text-align: right;"><span class="add_icon" id="add_icon" onclick="add_schedule(this)">&#x271A;</span></td>    
            </tr>
            <tr>
                <td class="theader" style="text-align: center;"><a href="javascript:excSubmit();">SAVE</a></td>    
            </tr>
        </table>
        
    </center>    
    <br /><br /><br /><br />
    <div id="footer">
        <div class="superFooter">
        </div>
    </div>    
</body>
</html>
Main task (task0.php)PHP
This is general code for main program. This code is responsible for checking schedule and take action based on the schedule.
<?php

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

include_once "/lib/sc_envu.php";

$envu =  envu_read("envu");
$schedules = envu_find($envu, "schedule");

$count = 0;

if($schedules != "")
{
    $schedules = explode("||", $schedules);
    
    $count = count($schedules);
    
    for($i = 0; $i < $count; $i++)
    {
        $schedules[$i] = explode("|", $schedules[$i]);
    }
}
else
    $schedules = array();

$is_activate = true;

while(1)
{
    if($count)
    {
        $is_activate = false;
        
        $day = date("D");
        $hour = (int)date("H");
        $minute = (int)date("i");
        $current_time = $hour*60 + $minute;//in minute
        
        for($i = 0; $i < $count; $i++)
        {
            if($day == $schedules[$i][0])
            {
                $temp = explode(":", $schedules[$i][1]);
                $start_time = ((int)$temp[0])*60 + (int)$temp[1];//in minute
                
                $temp = explode(":", $schedules[$i][2]);
                $end_time = ((int)$temp[0])*60 + (int)$temp[1];//in minute
                
                if($current_time >= $start_time && $current_time <= $end_time)
                {
                    $is_activate = true;
                    break;
                }
            }
        }
    }
    
    if($is_activate)
    {
        //TODO
    }
    else
    {
        //TODO
    }
}    
?>
Example 1: Only allowing people to turn on/off the light in working time (task0.php)PHP
index.php is kept the same. It just need to modify task0.php
<?php

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

include_once "/lib/sc_envu.php";
include_once "/lib/sd_340.php";

$envu =  envu_read("envu");
$schedules = envu_find($envu, "schedule");

$count = 0;

if($schedules != "")
{
    $schedules = explode("||", $schedules);
    
    $count = count($schedules);
    
    for($i = 0; $i < $count; $i++)
    {
        $schedules[$i] = explode("|", $schedules[$i]);
    }
}
else
    $schedules = array();

$is_activate = false;

$in_pin = 7;
$out_pin = 18;
//Input pin 
uio_setup(0, $in_pin, "in");

//Output pin 
uio_setup(0, $out_pin, "out");
uio_out(0, $out_pin, LOW);

while(1)
{
    $is_activate = false;
    
    if($count)
    {    
        $day = date("D");
        $hour = (int)date("H");
        $minute = (int)date("i");
        $current_time = $hour*60 + $minute;//in minute
        
        for($i = 0; $i < $count; $i++)
        {
            if($day == $schedules[$i][0])
            {
                $temp = explode(":", $schedules[$i][1]);
                $start_time = ((int)$temp[0])*60 + (int)$temp[1];//in minute
                
                $temp = explode(":", $schedules[$i][2]);
                $end_time = ((int)$temp[0])*60 + (int)$temp[1];//in minute
                
                if($current_time >= $start_time && $current_time <= $end_time)
                {
                    $is_activate = true;
                    break;
                }
            }
        }
    }
    
    if($is_activate)
    {
        //TODO
        $input_value = uio_in(0, $in_pin);
        
        uio_out(0, $out_pin, $input_value);
    }
    else
    {
        uio_out(0, $out_pin, LOW);
    }
}    
    
?>
Example 2: Detecting people going into entrance. Make alert in working time.PHP
index.php is kept the same. It just need to modify task0.php
<?php

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

include_once "/lib/sc_envu.php";
include_once "/lib/sd_340.php";

$envu =  envu_read("envu");
$schedules = envu_find($envu, "schedule");

$count = 0;

if($schedules != "")
{
    $schedules = explode("||", $schedules);
    
    $count = count($schedules);
    
    for($i = 0; $i < $count; $i++)
    {
        $schedules[$i] = explode("|", $schedules[$i]);
    }
}
else
    $schedules = array();

$is_activate = false;

//seting to work with distance sensor
$ht_trig = 1;
$ht_echo = 0;

// setup trigger pulse timer
ht_ioctl($ht_trig, "set mode output pulse");
ht_ioctl($ht_trig, "set div us");
ht_ioctl($ht_trig, "set repc 1");
ht_ioctl($ht_trig, "set count 5 10"); // 10us pulse width

// setup echo capture timer
ht_ioctl($ht_echo, "reset");
ht_ioctl($ht_echo, "set div us");
ht_ioctl($ht_echo, "set mode capture toggle");
ht_ioctl($ht_echo, "set trigger from pin rise");
ht_ioctl($ht_echo, "set repc 4");

//Control the alert
st_ioctl(0, "set mode output pulse");
st_ioctl(0, "set count 100 200");
st_ioctl(0, "set output dev uio0 18");

while(1)
{
    $is_activate = false;
    
    if($count)
    {    
        $day = date("D");
        $hour = (int)date("H");
        $minute = (int)date("i");
        $current_time = $hour*60 + $minute;//in minute
        
        for($i = 0; $i < $count; $i++)
        {
            if($day == $schedules[$i][0])
            {
                $temp = explode(":", $schedules[$i][1]);
                $start_time = ((int)$temp[0])*60 + (int)$temp[1];//in minute
                
                $temp = explode(":", $schedules[$i][2]);
                $end_time = ((int)$temp[0])*60 + (int)$temp[1];//in minute
                
                if($current_time >= $start_time && $current_time <= $end_time)
                {
                    $is_activate = true;
                    break;
                }
            }
        }
    }
    
    if($is_activate)
    {
        //TODO
        ht_ioctl($ht_echo, "start"); // we should start capture timer first
        ht_ioctl($ht_trig, "start"); // start trigger pulse
     
        usleep(100000); // sleep 100ms
        ht_ioctl($ht_echo, "stop");
     
        // 1st capture value ("get count 0") is always zero.
        // we should get 2nd capture value;
        $us = ht_ioctl($ht_echo, "get count 1");
     
        $dist = $us * 340.0 / 2; 
        $dist = $dist / 10000; //in centimeter
        
        if($dist < 70)
        {
            st_ioctl(0, "start");        
        }
        else
        {
            st_ioctl(0, "stop");
            st_ioctl(0, "set output low");
        }
        usleep(10000);
    }
    else
    {
        st_ioctl(0, "stop");
        st_ioctl(0, "set output low");
    }
}    
?>

Credits

Replications

Did you replicate this project? Share it!

I made one

Love this project? Think it could be improved? Tell us what you think!

Give feedback

Comments

Similar projects you might like

PHPoc Blue to Control Door Knob by MQTT Protocol and Cortana
Intermediate
  • 52
  • 7

Programming a PHPoc Blue board to control a servo motor to rotate the knob of a door using MQTT Protocol through voice command.

Parking Lot IoT System
Intermediate
  • 84
  • 7

This is a parking lot IoT system.

PHPoC Drink Mixer and Dispenser
Intermediate
  • 268
  • 10

A drink mixer and dispenser that uses PHPoC Blue and mini water pumps.

Controlling IR Appliances by Schedule
Intermediate
  • 202
  • 8

This project helps you control your IR appliances by setting schedule.

 Controlling IR-Controlled Appliances Through Internet
Intermediate
  • 333
  • 9

Turning your AC on before arriving at the office, changing your room temperature before coming back home by imitating IR signal.

Alarm System with motion and temperature sensors
Intermediate
  • 103
  • 3

System constantly detects temperature and movement variations. If at least one change is detected, a notification will be sent.

ProjectsCommunitiesTopicsContestsLiveAppsBetaFree StoreBlogAdd projectSign up / Login