Created
October 6, 2014 10:17
-
-
Save smukkejohan/b7ccf7079077de68aaf2 to your computer and use it in GitHub Desktop.
Arduino to Web with Node.js, johnny-five and express
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var five = require("johnny-five") | |
// or "./lib/johnny-five" when running from the source | |
var express = require('express'); | |
var app = express(); | |
var value = 0; | |
// Add headers | |
app.use(function (req, res, next) { | |
// Website you wish to allow to connect | |
res.setHeader('Access-Control-Allow-Origin', 'http://127.0.0.1:9080'); | |
// Request methods you wish to allow | |
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE'); | |
// Request headers you wish to allow | |
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type'); | |
// Set to true if you need the website to include cookies in the requests sent | |
// to the API (e.g. in case you use sessions) | |
res.setHeader('Access-Control-Allow-Credentials', true); | |
// Pass to next layer of middleware | |
next(); | |
}); | |
app.get('/', function(req, res){ | |
res.send('value:' + value); | |
}); | |
app.listen(3000); | |
board = new five.Board(); | |
board.on("ready", function() { | |
// Create a new `sensor` hardware instance. | |
sensor = new five.Sensor({ | |
pin: "A0", | |
freq: 250 | |
}); | |
sensor.scale([0, 100]).on("data", function() { | |
value = this.value; | |
console.log(this.value, this.raw); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<head> | |
<meta charset="utf-8"/> | |
<meta name="viewport" content="width=device-width, initial-scale=1"/> | |
<title>BAmse test</title> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> | |
<script src="./main.js"></script> | |
</head> | |
<body> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$( document ).ready(function() { | |
console.log("DOM Ready"); | |
var interval = function() { | |
setTimeout(function() { | |
$.ajax({ | |
url: "http://127.0.0.1:3000", | |
cache: false, | |
data: "", | |
}).done(function(data) { | |
console.log("Data received:"); | |
console.log(data); | |
interval(); | |
}); | |
}, 20 // Interval time | |
); | |
}; | |
interval(); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "bamseapp", | |
"description": "test", | |
"version": "0.0.1", | |
"private": true, | |
"dependencies": { | |
"express": "4.x", | |
"johnny-five": "*" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment