Nodebots 101
What we will cover
- Socket.io
- Johnny-five and Arduino
- Raspberry Pi and others
What's Socket.io
- JavaScript library for realtime web applications
- realtime, bi-directional communication between web clients and server
- client-side library that runs in the browser,
- server-side library for node.js
Server side
//Express server setup
var express = require("express");
var app = express();
var server = require("http").createServer(app);
var port = 8888;
server.listen(port, function () {
console.log("Server started on port " + port);
});
app.use(express.static(__dirname + "/../"));
//Socket setup
var io = require("socket.io")(server);
io.on("connection", function (socket) {
socket.on("messageFromClient", function (data) {
socket.broadcast("messageFromServer", data);
});
});
Client Side
<body>
<input type="text" id="textField" /> <button type="button" id="sendMsg">Send</button>
<textarea id="msgBox"></textarea>
</body>
<script src="/socket.io/socket.io.js"></script>
<script type="text/javascript">
var socket = io();
document.getElementById("sendMsg").onclick = function() {
socket.emit("messageFromClient", {text: document.getElementById("textField").value});
};
socket.on("messageFromServer", function(data) {
document.getElementById("msgBox").value += data.text;
});
</script>
What's Johnny-Five
- IoT and Robotics programming framework
- Johnny-Five programs can be written for Arduino, Beagle Bone, Intel Galileo & Edison, Pinoccio, Raspberry Pi, Spark Core, TI Launchpad and more!
Hello World
var five = require("johnny-five");
var board = new five.Board();
var led1;
board.on("ready", function() {
led1 = new five.Led("13");
led1.blink();
});
Arduino demo
Led circuit
Button Circuit
Spark
- (yn)
Raspberry Pi
- (yn)
Nodebots 101
By Joel Lord
Nodebots 101
- 1,040