Skip to content

Instantly share code, notes, and snippets.

@vppillai
Last active August 29, 2015 14:08
Show Gist options
  • Save vppillai/9c0357a0ee7c42b070f9 to your computer and use it in GitHub Desktop.
Save vppillai/9c0357a0ee7c42b070f9 to your computer and use it in GitHub Desktop.
#!/bin/bash
stty -F /dev/ttyUSB0 speed 115200 cs8 -cstopb -parenb
coproc d { nc -l -p 81 -q 0; }
nc -l -p 80 -q 1 > /dev/null <<-ENDOFPAGE
<html>
<body>
<script type="text/javascript">
var exampleSocket=0;
exampleSocket = new WebSocket("ws://localhost:81", "protocolOne");
if (0!=exampleSocket)
{
exampleSocket.onmessage = function (e) {
var res = e.data.split("\t");
w = Number(res[0]);
x = Number(res[1]);
y = Number(res[2]);
z = Number(res[3]);
document.getElementById("content").innerHTML = z ;
}
}
else
{
window.alert("socket Failed");
}
</script>
<div id="content"></div>
</body>
</html>
ENDOFPAGE
echo $$
until read line; line=`tr -d '\r\n'<<<$line`; test -z "$line"; do
test "${line:0:18}" = "Sec-WebSocket-Key:" && key=${line:19}
test "${line:0:22}" = "Sec-WebSocket-Version:" && ver=$line
done <&${d[0]}
rkey=`echo -n ${key}258EAFA5-E914-47DA-95CA-C5AB0DC85B11|openssl dgst -sha1 -binary|base64`
echo -ne "HTTP/1.1 101 Switching Protocols\r\n" >&${d[1]}
echo -ne "Upgrade: websocket\r\nConnection: Upgrade\r\n" >&${d[1]}
echo -ne "Sec-WebSocket-Accept: $rkey\r\n$ver\r\n\r\n" >&${d[1]}
while true; do
sleep 5
while read line
do
if [ ! -z "$line" ]; then
size=`expr ${#line} + 2`;
printf -v hexSize "%x" "$size"
printf "\\x81\\x$hexSize$line\r\n" >&${d[1]}
# sleep 1
fi
done </dev/ttyUSB0
done
#while true; do
#echo -ne "\x81\x7hello\r\n" >&${d[1]}
#sleep 1
#echo -ne "\x81\x7mello\r\n" >&${d[1]}
#sleep 1
#done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment