Announcement

Collapse
No announcement yet.

Ultrasonic Sensor - Monitoring Distance via WebSocket

Collapse
X
Collapse

  • Ultrasonic Sensor - Monitoring Distance via WebSocket


    Tutorial Video



    Hardware
    • PHPoC Blue (+ USB WLAN) or PHPoC Black (+ Ethernet cable)
    • Micro USB to USB Cable (to upload source code to PHPoC Device)
    • Ultrasonic Sensor
    • Jumper Cable


    About Ultrasonic Sensor

    Ultrasonic sensor HC-SR04 is used to measure distance to an object by using ultrasonic waves. The ultrasonic sensor HC-SR04 includes four pins:
    • VCC pin.
    • GND pin.
    • TRIG pin: this pin receives a 10-microsecond pulse from controller to emit ultrasonic wave.
    • ECHO pin: this pin outputs a pulse when receiving echo ultrasonic wave. The duration of pulse is used to calculate the distance.
    How the ultrasonic sensor works:
    • If receiving 10-microsecond pulse on TRIG pin , ultrasonic sensor automatically emits 40KHz ultrasonic wave.
    • If there is an obstacle, ultrasonic wave is reflected back.
    • Ultrasonic sensor measures duration of time from emiting ultrasonic wave to receiving the echo ultrasonic wave. And then ultrasonic sensor generate a pulse with the measured duration on ECHO pin.
    How to use ultrasonic sensor:
    • Connect TRIG pin of ultrasonic sensor to HT0 pin of PHPoC device. Set HT0 timer to "ouput pulse" mode.
    • Connect ECHO pin of ultrasonic sensor to HT1 pin of PHPoC device. Set HT1 timer to "capture" mode.
    • Use HT0 timer to generate 10-microsecond pulse.
    • Use HT2 timer to capture the pulse from ECHO pin.
    • Calculate distance based on the duration of captured pulse. The duration of pulse is the travel time of ultrasonic wave. Travel speed of ultrasonic wave is about 340 m/s. Travel distance is calculated according to the formula: distance = speed * time
    • Distance to obstacle is half of the travel distance of ultrasonic wave.


    Wiring Diagram



    Quick Steps

    Source code of this example is a part of PHPoC Support Packet (PSP). You need to:
    • Download PHPoC Support Package.
    • Upload example\p4s\04.html5_text\03.ht_ultrasonic to PHPoC Blue/Black.
    • Configure network parameters (e.g. WiFi SSID, password, IP address ...).
    • Click "Run" button on PHPoC Debugger.
    • Access webpage on PHPoC using Web Browser on your PC or smart phone (See How To).
    If you use PHPoC for the first time, see How To Use PSP.


    Source Code

    Source files includes:
    • init.php: this file is run when PHPoC system is powered or reset. It is used to specify which file is run is system loop.
    • task0.php: this file is run in system loop of PHPoC devices. It acts as WebSocket server and also interacts with ultrasonic sensor.
    • index.php: this file contains source code of web page.It is only run in response to request from Web Browser. It contains webpage (user interface) and acts WebSocket client.

    init.php

    This file is run when PHPoC system is powered or reset. It is used to specify that task0.php is run is system loop.
    PHP Code:
    <?php

    system
    ("php task0.php");

    ?>



    task0.php

    [Full Code]
    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(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(0sprintf("%d us, %.1f Cm\r\n"$us$dist));

      
    sleep(1);
     }
    }

    ?>



    [Explanation]

    Source code of this file does:
    • Setup HT0 timer to generate pulse and HT1 timer to capture signal.
    • Setup and initialize WebSocket.
    In infinite loop:
    • Start capture timer to generate a pulse
    • Start the trigger pulse timer to capture echo signal
    • Get duration of captured pulse
    • Calculate distance according to the duration of captured pulse
    • Send distance from sensor to obstacle to Web Browser via WebSocket.

    index.php

    [Full Code]
    PHP Code:
    <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>



    [Explanation]

    Source code of index.php file is composed of HTML, CSS, JavaScript and PHPoC code.

    PHPoC code is interpreted on on PHPoC device.

    PHPoC code may add/update the content of HTML, CSS or JavaScript code. Once PHPoC code is interpreted in PHPoC, the remaining code is client-side code and it is returned to Web Browser. Web Browser receives this code and interpret it to display the webpage.
    • HTML: describes the structure of Web pages
    • CSS: describes how HTML elements are to be displayed
    • JavaScript: This code:
      - Receive data (distance from sensor to obstacle of ultrasonic sensor) from PHPoC devices via WebSocket,
      - Display distance from sensor to obstacle on webpage
    For detail explanation, see How to Monitor Sensors/Devices via WebSocket




    See Also

    Other Resources
    Last edited by support; 02-09-2018, 08:42 AM.
      Posting comments is disabled.

    Categories

    Collapse

    Latest Articles

    Collapse

    • Arduino - RS-485 Expansion Board
      by support
      PES-2607 is an easy-to-use RS422/RS485 Expansion Board for Arduino Uno and Mega, which allows Arduino to exchange data with serial device via RS422 or RS485.
      Especially, Arduino does NOT use UART pins to communicate with RS422/RS485 expansion board. Therefore, users can use Arduino UART pins for other purposes.
      Moreover, A single Arduino Uno/Mega can communicate with multiple RS422/RS485 expansion boards (up to 14) without using Arduino UART pins.

      Library and examples for...
      11-13-2018, 02:45 PM
    • Arduino - RS-422 Expansion Board
      by support
      PES-2607 is an easy-to-use RS422/RS485 Expansion Board for Arduino Uno and Mega, which allows Arduino to exchange data with serial device via RS422 or RS485.
      Especially, Arduino does NOT use UART pins to communicate with RS422/RS485 expansion board. Therefore, users can use Arduino UART pins for other purposes.
      Moreover, A single Arduino Uno/Mega can communicate with multiple RS422/RS485 expansion boards (up to 14) without using Arduino UART pins.

      Library and examples for...
      11-13-2018, 02:44 PM
    • Arduino - RS-232 Expansion Board
      by support
      PES-2606 is an easy-to-use RS-232 Expansion Board for Arduino Uno and Mega, which allows Arduino to exchange data with serial device via RS-232.
      Especially, Arduino does NOT use UART pins to communicate with RS-232 expansion board. Therefore, users can use Arduino UART pins for other purposes.
      Moreover, A single Arduino Uno/Mega can communicate with multiple RS-232 expansion boards (up to 14) without using Arduino UART pins.

      Library and example for the RS-232 expansion board...
      11-13-2018, 02:43 PM
    • Arduino - Stepper Motor Controller
      by support
      PES-2605 is an easy-to-use stepper motor controller for Arduino Uno and Mega, which uses micro-stepping method to precisely control stepper motor.
      Library and example for the stepper motor controller are part of of PhpocExpansion library for Arduino. The library reference is available here.

      This tutorial shows how to use the step motor controller with an example of PhpocExpansion library for Arduino.


      Hardware Required...
      11-13-2018, 02:41 PM
    • Arduino - DC Motor Controller
      by support
      PES-2604 is an easy-to-use DC motor controller for Arduino Uno and Mega.
      Library and example for the DC motor controller are part of of PhpocExpansion library for Arduino. The library reference is available here.

      This tutorial shows how to use the DC motor controller with an example of PhpocExpansion library for Arduino.


      Hardware Required...
      11-13-2018, 02:40 PM
    • Arduino - Digital Input Board
      by support
      PES-2602 is an easy-to-use 4-port Input Expansion Board for Arduino Uno and Mega, which allows Arduino to monitor state of DC electric device. In addition, it can monitor NPN, PNP and dry contact(relay).
      Library and example for the 4-port input expansion board are part of of PhpocExpansion library for Arduino. The library reference is available here.

      This tutorial shows how to use 4-port input expansion board with an example of PhpocExpansion library for Arduino.

      ...
      11-13-2018, 02:39 PM
    Working...
    X