RooWifi, Node.js and MQTT working together

As I wrote yesterday, the RooWifi has both a Web Interface and it can also connect to a remote TCP server. The RooWifi prioritizes the TCP server connection to the Web Interface, so if there’s a remote TCP server running and accepting connections, this TCP Server is in full control. The IP address and port number can be set from the RooWifi Web Interface:

RooWifi TCP server settings

This afternoon I tried to get the RooWifi to connect to a TCP server. I decided to use Node.js this time. I’ve used Node.js before and I wanted to see if I could get it running on one my Raspberry Pi‘s, create a TCP server, accept connections, parse a JSON payload and send the results to a MQTT broker (mosquitto).

I remembered a set of commands on JeeLabs to install Node.js, so I used that:

sudo usermod -aG staff pi && sudo reboot
v=v0.10.7
cd
sudo curl http://nodejs.org/dist/$v/node-$v-linux-arm-pi.tar.gz | tar xz
cp -a node-$v-linux-arm-pi/{bin,lib} /usr/local/

The first script I made was a TCP server that kept track of the clients that connected and disconnected and wrote the incoming data to the console:

net = require('net');
var connections = [];

net.createServer(function (socket) {

socket.name = socket.remoteAddress + ":" + socket.remotePort
 connections.push(socket);

socket.on('data', function (data) {
 var newDate = new Date();
 var time = newDate.toLocaleTimeString();
 console.log(time + " | " + socket.name + " < " + data);
 var obj = eval('('+data+')');
 console.log(time + " | temp=" + obj.roomba.temp);
 console.log(time + " | dirt=" + obj.roomba.dirt);
 });

socket.on('end', function () {
 var newDate = new Date();
 var time = newDate.toLocaleTimeString();
 console.log(time + " | " + socket.name + " ended the connection");
 connections.splice(connections.indexOf(socket), 1);
 });
}).listen(8001);

console.log("Server listening on port 8001n");

That’s it?? Yep.. 26 lines of code, wow. The output looked like this:

19:57:29 | 192.168.10.201:1561 < { "roomba": { "status": "3", "cleaning": "0", "battery": ":0", "temp": "36", "dirt": "0" } }
19:57:29 | temp=36
19:57:29 | dirt=0
19:57:34 | 192.168.10.201:1561 ended the connection

I also installed the MQTT package from adamvr:

npm install mqtt

I added a mqtt client to the script so I could publish all the information inside the JSON data and made some preparations to receive commands from the outside world as well:

net = require('net');
var mqtt = require('mqtt');
var connections = [];

var mqttc = mqtt.createClient(1883, '192.168.10.17', {
 keepalive: 30000
 });

mqttc.on('connect', function() {
 mqttc.subscribe('command/roomba');
 mqttc.on('message', function(topic, message) {
 console.log('topic: ' + topic + ' payload: ' + message);
 });
});

net.createServer(function (socket) {
 socket.name = socket.remoteAddress + ":" + socket.remotePort
 connections.push(socket);

socket.on('data', function (data) {
 var newDate = new Date();
 var time = newDate.toLocaleTimeString();
 console.log(time + " | " + socket.name + " < " + data);
 var obj = eval('('+data+')');
 for(var key in obj.roomba){
 console.log(time + " | "+key+" "+obj.roomba[key]);
 mqttc.publish('/value/roomba/'+key, obj.roomba[key]);
 }
 });

socket.on('end', function () {
 var newDate = new Date();
 var time = newDate.toLocaleTimeString();
 console.log(time + " | " + socket.name + " ended the connection");
 connections.splice(connections.indexOf(socket),1);
 });
}).listen(8001);

console.log("Server listening on port 8001n");

Still a very tiny script! I started the script in the background and let it run for a while (it’s still running):

node tcpmqtt.js &

Now let’s see if I can get the RooWifi information visible on another machine, my Win7 PC for instance:

RooWifi data published

Bingo… Now it’s easy to write a webpage to display the information, make certain values historic in my SQL database, control the Roomba from any User Interface (Touchscreen, Smart phones, tablets) – whatever you can think of!

Of course, I still have to add the code to control my Roomba, but that’s just a matter of time and will probably add just a couple of lines of code to the script.

What I’ve learned today is that the Remote TCP server feature of the RooWifi is the best way to monitor & control your Roomba and that Node.js is very powerful and most important: it works like I think: event-driven.

Tagged , , . Bookmark the permalink.

6 Responses to RooWifi, Node.js and MQTT working together

  1. Rene Klootwijk says:

    The documentation of mqtt on github is based on a newer version than will be installed using npm. If you find any problem you better use a clone.

    • Thanks, after installing with npm I compared file sizes in the lib directory to see if there were differences and they all matched, so I figured that using the npm version should be OK. If not I would’ve cloned it, cause I saw there were some recent changes 😉

  2. Bogdan says:

    Hi
    I´d like to thank you for sharing you´re work with everybody.
    For the past month i have try ed to reproduce you’re “display temp on a web page” and today i´ve made it work.
    Thank you again for sharing with the rest of us.
    Regards

  3. Hans Joergensen says:

    Hey,

    Did you ever do more work with this?
    Not being much of a coder myself looks like the perfect solution to integrate RooWifi with OpenHAB, so if possible I’d like to see what ever work you have done on the scripts since this post..

Leave a Reply

Your email address will not be published. Required fields are marked *