Click image for larger version  Name:	Toilet_Paper_cover.PNG Views:	1 Size:	34.0 KB ID:	613
This project help caretakers save time for manually checking toilet-paper, especially in a high and large building where there are a lot of toilet. Caretakers just need to sitt down on the chair and check toilet paper via smart phone.

Demonstration





How It Works

Click image for larger version  Name:	Toilet_Paper_How it work.PNG Views:	1 Size:	46.7 KB ID:	614

Distance sensor is used to measure the distance from sensor to the surface of toilet paper roll. The current thickness of paper is calculated as follows:
thickness = max_distance – distance
where max_distance is distance between sensor and surface of axle in paper holder (without paper roll)

when knowing the thickness, we can visualize it on canvas.

The server side program (task0.php) is get distance from distance sensor and send this distance to client side program (index.php) in web browser via websocket. The calculation of thickness and scaling is performed on client side program.



Things Used In This Project

Click image for larger version  Name:	Toilet_Paper_Things.jpg Views:	1 Size:	52.0 KB ID:	615

Click image for larger version  Name:	Toilet_Paper_Install_1.jpg Views:	1 Size:	55.7 KB ID:	616



Wiring Diagram

Click image for larger version  Name:	Toilet_Paper_wiring.PNG Views:	1 Size:	92.8 KB ID:	617



Source code

Main Task (task0.php): This code is run in infinite loop to read distance from sensor and send it to web procgam on web browser via websocket

PHP Code:
<?php

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

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

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

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

ws_setup(0"ToiletPaperMonitoring""text.phpoc");

$pre_dist 0;
$count 0;

while(
1)
{
    if(
ws_state(0) == TCP_CONNECTED)
    {
        
ht_ioctl(0"start"); // we should start capture timer first
        
ht_ioctl(1"start"); // start trigger pulse

        
usleep(10000); // sleep 10ms

        // 1st capture value ("get count 0") is always zero.
        // we should get 2nd capture value;
        
$us ht_ioctl(0"get count 1");

        if(
$us == 0)
            continue;

        
$dist $us 340.0 2// us to meter conversion
        
$dist $dist 10000// meter to centimeter conversion

        //filter noise
        
if((int)$pre_dist !=0)
        {
            if(
abs($dist $pre_dist) <= 1)
                
$count++;
            else
                
$count 0;
        }

        
$pre_dist $dist;

        if(
$count 5)
        {
            
ws_write(0"$dist");
            echo 
"$dist\r\n";
            
$count 0;
        }
        
//echo $dist, "\r\n";
        //usleep(10000);
    
}
    else
    {
        
$pre_dist 0;
        
$count 0;
    }
}
?>



User Interface (index.php): This is client program to provide user interface and visualize data

PHP Code:
<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;}
canvas {
    margin-right: auto;
    margin-left: auto;
    width: 500px;
    height: 500px;
    background:url(background.png) no-repeat;
    background-size: contain;
}
</style>
<script>
var REAL_DISTANCE_MAX = 11.611; // in centimeters. This parameter is got from measurement with no paper-roll
var REAL_DISTANCE_MIN = 0.5; // in centimeters. This parameter is got from measurement with a full paper-roll.

var CANVAS_RADIUS_MAX = 225; // in pixel. This parameter depends on the canvas size and background image
var CANVAS_RADIUS_MIN = 56; // in pixel. This parameter depends on the canvas size and background image

var canvas;
var ctx;
var trash, bin;
var ws = null;

function init()
{
    canvas = document.getElementById("cvs");
    ctx = canvas.getContext('2d');
    ctx.strokeStyle = "LightGray";
    ctx.fillStyle = "white";
    ctx.lineWidth = 5;

}
function ws_onopen()
{
    document.getElementById("ws_state").innerHTML = "OPEN";
    document.getElementById("wc_conn").innerHTML = "Disconnect";
}
function ws_onclose()
{
    document.getElementById("ws_state").innerHTML = "CLOSED";
    document.getElementById("wc_conn").innerHTML = "Connect";

    ws.onopen = null;
    ws.onclose = null;
    ws.onmessage = null;
    ws = null;
}
function wc_onclick()
{
    if(ws == null)
    {
        ws = new WebSocket("ws://<?echo _SERVER("HTTP_HOST")?>/ToiletPaperMonitoring", "text.phpoc");
        document.getElementById("ws_state").innerHTML = "CONNECTING";

        ws.onopen = ws_onopen;
        ws.onclose = ws_onclose;
        ws.onmessage = ws_onmessage;
    }
    else
        ws.close();
}
function ws_onmessage(e_msg)
{
    e_msg = e_msg || window.event; // MessageEvent

    var dist = e_msg.data; // in centimeters
    console.log(dist);

    if(dist < REAL_DISTANCE_MIN)
        dist = REAL_DISTANCE_MIN;

    if(dist > REAL_DISTANCE_MAX)
        dist = REAL_DISTANCE_MAX;

    if(dist > (REAL_DISTANCE_MAX - 0.2))
        ctx.strokeStyle = "Red"; // alert
    else
        ctx.strokeStyle = "LightGray";

    // Scale

    var real_thickness = REAL_DISTANCE_MAX - dist;
    var ratio = real_thickness / (REAL_DISTANCE_MAX - REAL_DISTANCE_MIN);
    var cvs_thickness = ratio * (CANVAS_RADIUS_MAX - CANVAS_RADIUS_MIN);
    var radius = Math.round(cvs_thickness + CANVAS_RADIUS_MIN);

    ctx.clearRect(0, 0, canvas.width, canvas.height);

    ctx.beginPath();
    ctx.arc(canvas.width / 2, canvas.height / 2 , radius, 0, 2*Math.PI);
    ctx.stroke();
    ctx.fill();

    // draw axle
    ctx.save();
    ctx.fillStyle = "DimGray";
    ctx.beginPath();
    ctx.arc(canvas.width / 2, canvas.height / 2 , CANVAS_RADIUS_MIN, 2*Math.PI, 0);
    ctx.fill();
    ctx.restore();
}

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

<h2>
Toilet-Paper Monotoring<br><br>
</h2>
<canvas id="cvs" width="500" height="500"></canvas>
<p><span id="ws_state">CLOSED</span><br></p>
<button id="wc_conn" type="button" onclick="wc_onclick();">Connect</button>
<button id="wc_clear" type="button" onclick="wc_clear();">Clear</button>
</body>
</html>



You can also see this project in hackster.io web site: https://www.hackster.io/phpoc_man/re...terface-d7210a