Raspberry Internet Rover Part 3 – NodeJS server

maxresdefaultInstall socket.io by typing: npm install socket.io

Install zeparser: npm install zeparser

, then copy paste this instruction below:

var fs, http, io, server, net;
fs = require(‘fs’);
http = require(‘http’);
net = require(‘net’);

var light1 = true;
var light2 = true;

server = http.createServer(function(req, res) {
return fs.readFile(“” + __dirname + “/socket.io.mobil2an.html”, function(err, data) {

res.writeHead(200, {
‘Content-Type’: ‘text/html’
});
return res.end(data, ‘utf8’);
});
});
server.listen(1338);

//return socket.broadcast.send(message);
var dgram = require(‘dgram’);
//var message = new Buffer(“PyHB”);
var server_ip = “localhost”;
var server_port = 9090;
var beat_period = 5;
var debug = 1;
console.log(“Sending heartbeat to IP ” + server_ip + ” , port ” + server_port);
console.log(“press Ctrl-C to stop”);

var client = dgram.createSocket(“udp4”);

//io = require(‘socket.io’).listen(9091);

/*
net.createServer(function(socket2){
socket2.on(‘data’, function(data){
console.log(“The message data” + data);
socket2.write(‘This is your request: “‘ + data + ‘”‘);
});
}).listen(9999);
*/

io = require(‘socket.io’).listen(server);

io.sockets.on(‘connection’, function(socket) {
//console.log(socket);
socket.emit(“publish”, “Connected.”);

fs.writeFile(“” + __dirname + “/log2.txt”, “Hello! My name is Rasver your Raspberry Rover!\n”);
/*
socket.on(‘publish’, function(message) {
return io.sockets.send(message);
});*/
socket.on(‘broadcast’, function(message) {
var message = new Buffer(message);
return client.send(message, 0, message.length, server_port, server_ip, function(err,bytes){
client.close();
});
if (debug)
console.log(“Time: ” + new Date());

});

socket.on(‘message’, function(data){
console.log(“The message data ” + data);
socket.emit(“data”,data);
});

socket.on(‘getfile’, function(data){
console.log(“The message data ” + data);
socket.emit(“data”,data);
});

socket.on(‘forward’, function(message2) {
//socket.emit(“publish”,message2)
var message = new Buffer(“w”);

return client.send(message, 0, message.length, server_port, server_ip, function(message2,bytes){

});

if (debug)
console.log(“Time: ” + new Date());

});

socket.on(‘reverse’, function(message) {

//socket.emit(“publish”,message)
var message = new Buffer(“x”);
return client.send(message, 0, message.length, server_port, server_ip, function(err,bytes){

});

if(debug)
console.log(“Time: ” + new Date());

});
socket.on(‘left’, function(message) {
//socket.emit(“publish”,message);
var message = new Buffer(“a”);
return client.send(message, 0, message.length, server_port, server_ip, function(err,bytes){

});

if (debug)
console.log(“Time: ” + new Date());

});
socket.on(‘right’, function(message) {
//socket.emit(“publish”,message);
var message = new Buffer(“d”);
return client.send(message, 0, message.length, server_port, server_ip, function(err,bytes){

});

if (debug)
console.log(“Time: ” + new Date());

});
socket.on(‘stop’, function(message) {
//socket.emit(“publish”,message);
var message = new Buffer(“s”);
return client.send(message, 0, message.length, server_port, server_ip, function(err,bytes){

});

if (debug)
console.log(“Time: ” + new Date());

});
socket.on(‘1’, function(message2) {
//socket.emit(“publish”,”Camera ” + message2);
var message = new Buffer(message2);
return client.send(message, 0, message.length, server_port, server_ip, function(err,bytes){

});

if (debug)
console.log(“Time: ” + new Date());

});
socket.on(‘2’, function(message2) {
//socket.emit(“publish”,”Camera ” + message2);
var message = new Buffer(message2);
return client.send(message, 0, message.length, server_port, server_ip, function(message2,bytes){

});

if (debug)
console.log(“Time: ” + new Date());

});
socket.on(‘3’, function(message2) {
//socket.emit(“publish”,”Camera ” + message2);
var message = new Buffer(message2);
return client.send(message, 0, message.length, server_port, server_ip, function(err,bytes){

});

if (debug)
console.log(“Time: ” + new Date());

});
socket.on(‘fl’, function(message) {
//socket.emit(“publish”,message);
var message = new Buffer(“q”);
return client.send(message, 0, message.length, server_port, server_ip, function(err,bytes){
//client.close();
});
if (debug)
console.log(“Time: ” + new Date());

});

socket.on(‘fr’, function(message) {
//socket.emit(“publish”,message);
var message = new Buffer(“e”);
return client.send(message, 0, message.length, server_port, server_ip, function(err,bytes){
//client.close();
});
if (debug)
console.log(“Time: ” + new Date());

});

socket.on(‘reverse’, function(message) {
//socket.emit(“publish”,message);
var message = new Buffer(“x”);
return client.send(message, 0, message.length, server_port, server_ip, function(err,bytes){
//client.close();
});
if (debug)
console.log(“Time: ” + new Date());
});

socket.on(‘rl’, function(message) {
//socket.emit(“publish”,message);
var message = new Buffer(“z”);
return client.send(message, 0, message.length, server_port, server_ip, function(err,bytes){
//client.close();
});
if (debug)
console.log(“Time: ” + new Date());

});

socket.on(‘rr’, function(message) {
//socket.emit(“publish”,message);
var message = new Buffer(“c”);
return client.send(message, 0, message.length, server_port, server_ip, function(err,bytes){
//client.close();
});
if (debug)
console.log(“Time: ” + new Date());

});

socket.on(‘light1’, function(message) {
//socket.emit(“publish”,message);
var message = new Buffer(“i”);
if(light1){
message = new Buffer(“i”);
light1 = false;
return client.send(message, 0, message.length, server_port, server_ip);

}else{
message = new Buffer(“p”);
light1 = true;
return client.send(message, 0, message.length, server_port, server_ip);

}
if (debug)
console.log(“Time: ” + new Date());
});
socket.on(‘light2’, function(message) {
//socket.emit(“publish”,message);
var message = new Buffer(“u”);
if(light2){
message = new Buffer(“u”);
light2 = false;
return client.send(message, 0, message.length, server_port, server_ip);

}else{
message = new Buffer(“o”);
light2 = true;
return client.send(message, 0, message.length, server_port, server_ip);

}
if (debug)
console.log(“Time: ” + new Date());
});
socket.on(‘whisper’, function(message) {
return socket.broadcast.emit(‘secret’, message);
});
});

net.createServer(function(socket2){
socket2.on(‘data’, function(data){
console.log(“The message data: ” + data);
//socket2.write(‘This is your request: “‘ + data + ‘”‘);
//return ii.sockets.send(“data”,data);
//return ss.send(“data”,data);
//fs.writeFile(“test.txt”, data);

//Set our log file as a writestream variable with the ‘a’ flag
var logFile = fs.createWriteStream(“” + __dirname + “/log2.txt”, {
flags: “a”,
encoding: “encoding”,
mode: 0744
})
//call the write option where you need to append new data
logFile.write(data+”\n”);

});
}).listen(1234);

The corresponding HTML can be found in Part 4.

[post_view]

(Visited 1,306 times, 1 visits today)

6 thoughts on “Raspberry Internet Rover Part 3 – NodeJS server”

  1. Tommy Klausen says:

    Hi. I`m a complete newbee on this, but where is this code suppose to be pasted?

    1. Dipto Pratyaksa Dipto Pratyaksa says:

      In a javascript file named carserver.js
      Then you run it from command intrepreter with: nodejs carserver.js
      After that you run your mobil2an.html client on the browser.
      Read through basic nodejs tutorial. It’s basically server-side program written in JavaScript

      1. Tommy Klausen says:

        Hi Dipto. Thank you for your answer.
        I know I`m on a Linux forum here, but I`m using a Windows machine. I have some experience with Linux.
        Which command interpreter are you referring to? The one on the RPi ?
        And can you advice me if anything OS- related should be taken into consideration in my work with your tutorial?
        Looking forward to proceed. Have bought some equipment.

      2. Dipto Pratyaksa Dipto Pratyaksa says:

        The robot is controlled by the Pi. So you would program for the movement in the pi using Python, NodeJS or any language of your preference.
        Once you know how to control GPIO you cam create control interface like a website with buttons.
        You cannot achieve a true freedom and creativity without Linux. The Pi supports Raspbian which is Debisn Linux dedicated for ARM embedded device. If you want I can run an online Skype session to explain the whole lot.

      3. Tommy Klausen says:

        Ok.
        I will try and set up Linux on one the laptops I`ve got lying around :-).
        I might as well go back and work some more with Linux.

        You see, I am trying out this on behalf of a tech-lab for kids/youths and the only thing they`ve got is Windows. At least for the time beeing.

        That`s why I need to do this on Windows.

        Can you recommend good Linux distro ? 🙂 Is it the Debian?

      4. Dipto Pratyaksa Dipto Pratyaksa says:

        To program a Raspberry Pi, you can also do it from Windows using Telnet program. This will give you SSH access to your Pi Command Line Interpreter remotely, as long as you are connected in the same network. Read about Telnet and SSH.
        The best distro for PC is Linux Mint. Email me at dipto.pratyaksa2@gmail.com to arrange Skype session

Comments are closed.