[HTML5/WS/TEXT] Using an ultrasonic ranger

[HTML5/WS/TEXT] Using an ultrasonic ranger

Postby admin » Wed Nov 04, 2015 5:06 pm

We will go through an example of printing input signal of an ultrasonic ranger by using Websocket of a PHPoC Blue.

Image

Diagram

Image

Source Codes
1. task0.php

Code: Select all

<?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(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(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, "WebConsole", "text.phpoc");

while(
1)
{
    if(ws_state(0) == TCP_CONNECTED)
    {
        ht_ioctl(1, "start"); // we should start capture timer first
        ht_ioctl(0, "start"); // start trigger pulse
 
        usleep
(100000); // sleep 100ms
        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");

        $dist = $us * 340.0 / 2; // us to meter conversion
        $dist = $dist / 10000; // meter to centimeter conversion

        ws_write(0, sprintf("%d us, %.1f Cm\r\n", $us, $dist));

        sleep(1);
    }
}

?>



2. index.php

Code: Select all

<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; }
textarea { width:400px; height:400px; padding:10px; font-family:courier; font-size:14px; }
 </style>
<script>
var ws;
var wc_max_len = 32768;
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")?>/WebConsole", "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 wc_text = document.getElementById("wc_text");
    var len = wc_text.value.length;

    if(len > (wc_max_len + wc_max_len / 10))
        wc_text.innerHTML = wc_text.value.substring(wc_max_len / 10);

    wc_text.scrollTop = wc_text.scrollHeight;
    wc_text.innerHTML += e_msg.data;
}
function wc_clear()
{
    document.getElementById("wc_text").innerHTML = "";
}
</script>
</head>
<body>

<h2>
<p>
Web Console : <span id="ws_state">CLOSED</span><br>
</p>
<textarea id="wc_text" readonly="readonly"></textarea><br>
<button id="wc_conn" type="button" onclick="wc_onclick();">Connect</button>
<button id="wc_clear" type="button" onclick="wc_clear();">Clear</button>
</h2>

</body>
</html>


Please download a PSP (PHPoC Support Package) of libraries and examples from PHPoC <http://www.phpoc.com/download.php>, and upload it in the PHPoC Blue.




Video

phpBB [video]
admin
Site Admin
 
Posts: 167
Joined: Mon Jan 20, 2014 4:40 pm

Return to Example

Who is online

Users browsing this forum: No registered users and 1 guest

cron