Jack
Published © GPL3+

MikroBUS Vibra Sense Click

This project is using a shock vibration sensor with PHPoC Blue.

BeginnerShowcase (no instructions)8 hours545
MikroBUS Vibra Sense Click

Things used in this project

Hardware components

PHPoC Blue
PHPoC Blue
×1
PHPoC mikroBUS Expansion Board
PHPoC mikroBUS Expansion Board
×1
MikroE mikroBUS Vibra sense click.
×1

Story

Read more

Code

init.php

PHP
<?php
system("php task0.php");
?>

task0.php

PHP
<?php
if(_SERVER("REQUEST_METHOD"))
    exit; // avoid php execution via http request
 
include_once "/lib/sd_340.php";
include_once "/lib/sn_tcp_ws.php";
 
define("PIN_EN", 12);
define("PIN_INT", 8);
 
uio_setup(0, PIN_INT, "in"); // INT
uio_setup(0, PIN_EN, "out"); // EN
 
uio_out(0, PIN_EN, 1);
 
ws_setup(0, "vibra", "csv.phpoc");
 
$previous_state = 0;
$current_state = 0;
 
while(1)
{
    $current_state = uio_in(0, PIN_INT);
    if($previous_state != $current_state)
    {
        $previous_state = $current_state;
        if(ws_state(0) == TCP_CONNECTED)
        {
            ws_write(0, "$current_state\r\n");
        }
    }
}
?>

index.php

HTML
<!DOCTYPE html>
<html>
<head>
<title>PHPoC / <?echo system("uname -i")?></title>
<meta name="viewport" content="width=device-width, initial-scale=0.7, maximum-scale=0.7">
<style> body { text-align: center; } </style>
<script>
var canvas_width = 95;
var canvas_height = 95;
var ws;
function init()
{
    var button = document.getElementById("button");
 
    button.width = canvas_width;
    button.height = canvas_height;
 
    button.style.backgroundImage = "url('/button_pop.png')";
}
function connect_onclick()
{
    if(ws == null)
    {
        var ws_host_addr = "<?echo _SERVER("HTTP_HOST")?>";
        var debug = document.getElementById("debug");
 
        if((navigator.platform.indexOf("Win") != -1) && (ws_host_addr.charAt(0) == "["))
        {
            // network resource identifier to UNC path name conversion
            ws_host_addr = ws_host_addr.replace(/[\[\]]/g, '');
            ws_host_addr = ws_host_addr.replace(/:/g, "-");
            ws_host_addr += ".ipv6-literal.net";
        }
 
        ws = new WebSocket("ws://" + ws_host_addr + "/vibra", "csv.phpoc");
 
        document.getElementById("ws_state").innerHTML = "CONNECTING";
 
        ws.onopen = ws_onopen;
        ws.onclose = ws_onclose;
        ws.onmessage = ws_onmessage;
    }
    else
        ws.close();
}
 
function ws_onopen()
{
    document.getElementById("ws_state").innerHTML = "<font color='blue'>CONNECTED</font>";
    document.getElementById("bt_connect").innerHTML = "Disconnect";
    
    button.style.backgroundImage = "url('/button_pop.png')";
}
function ws_onclose()
{
    document.getElementById("ws_state").innerHTML = "<font color='gray'>CLOSED</font>";
    document.getElementById("bt_connect").innerHTML = "Connect";
 
    ws.onopen = null;
    ws.onclose = null;
    ws.onmessage = null;
    ws = null;
    
    button.style.backgroundImage = "url('/button_pop.png')";
}
function ws_onmessage(e_msg)
{
    e_msg = e_msg || window.event; // MessageEvent
    
    if(e_msg.data == 0)
        button.style.backgroundImage = "url('/button_pop.png')";
    else if(e_msg.data == 1)
        button.style.backgroundImage = "url('/button_push.png')";
}
 
window.onload = init;
</script>
</head>
 
<body>
<p>
<h1>Vibra sense click</h1>
</p>
 
<canvas id="button"></canvas>
 
<h2>WebSocket <font id="ws_state" color="gray">CLOSED</font></h2>
<button id="bt_connect" type="button" onclick="connect_onclick();">Connect</button>
<span id="debug"></span

</body>
</html>

Credits

Jack

Jack

5 projects • 9 followers

Comments