Skip to content

Instantly share code, notes, and snippets.

@gorrotowi
Forked from iddar/app.js
Created October 3, 2014 06:48
Show Gist options
  • Save gorrotowi/7832d373361d8e3bbe62 to your computer and use it in GitHub Desktop.
Save gorrotowi/7832d373361d8e3bbe62 to your computer and use it in GitHub Desktop.
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var serialport = require("serialport");
var SerialPort = serialport.SerialPort;
var port = new SerialPort("/dev/tty.usbserial-A9007WoZ", {
baudrate: 9600,
parser: serialport.parsers.readline("\n")
});
port.on('data', function(line){
var data = JSON.parse(line + "");
console.log(data.sensor);
io.emit('sensor', data.sensor);
});
app.get('/', function(req, res){
res.sendfile('index.html');
});
io.on('connection', function(socket){
console.log('a user connected');
socket.on('disconnect', function(){
console.log('user disconnected');
});
});
http.listen(3000);
<!doctype html>
<html>
<head>
<title>Socket.IO chat</title>
<style>
*{
border: 0;
margin: 0;
padding: 0;
}
.graph{
height: 100vh;
background: #3498db;
}
</style>
<script src="/socket.io/socket.io.js"></script>
<script src="//code.jquery.com/jquery-2.1.1.min.js"></script>
</head>
<body>
<div class="graph"></div>
<script>
var socket = io();
var $graph = $('.graph');
// $('a#bt').on('click', function(e) {
// socket.emit('msg', 'bu!');
// });
// socket.on('msg', function(value){
// alert(value);
// });
socket.on('sensor', function(value) {
var width = (value * 100) / 1023;
// console.log(width);
$graph.css({"width": width + "%" });
});
</script>
</body>
</html>
{
"name": "Test-Socket.io",
"version": "0.0.1",
"description": "my first socket.io app",
"dependencies": {
"express": "4.9.5",
"serialport": "1.4.6",
"socket.io": "1.1.0"
}
}
// Ultrasonic - Library for HR-SC04 Ultrasonic Ranging Module.
// Rev.4 (06/2012)
// J.Rodrigo ( www.jra.so )
// more info at www.ardublog.com
#include <Ultrasonic.h>
Ultrasonic ultrasonic(12,11); // (Trig PIN,Echo PIN)
void setup() {
Serial.begin(9600);
}
void loop()
{
Serial.print("{\"sensor\":\"");
Serial.print(ultrasonic.Ranging(CM));
Serial.print("\"}\n");
delay(10);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment