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.

Roomba goes WiFi

Last Tuesday I received a really cool device, the Roomba Wi-Fi Remote. In short: RooWifi.
The RooWifi turns your Roomba Robot Vacuum cleaner into a Wifi-enabled device!

RooWifi board with cover

Some specs:

Physical

  • Mini-DIN 7 pins (PS/2) plug
  • PCB dimensions: 53 x 32.5 mm.

WLAN

  • Wi-Fi IEEE 802.11b
  • Range up to 400m, Baud rate: 1 and 2 Mbps, AES128 encryption, WPA(2)-PSK, Hotspot and ‘Home network’ mode

Interfaces

  • Web Server with authentication.
  • TCP Socket Server for client-server applications

Protocols and Standards:

  • XML: Read-only with data refresh of 500ms.
  • JSON: Read-only with data refresh of 500ms.
  • AJAX Request through GET Calls and text format output.

Operating Systems, Compatible devices:

  • Android >=2.0
  • iOs >=3.1
  • Blackberry
  • WebOs
  • Windows Phone
  • Linux
  • MacOs
  • Windows.

Power consumption: 200mA @ Roomba Battery Voltage (around 17V).

We have a Roomba 500-series vacuum cleaner in our house since 2010. It helps us to keep the floors clean, cause with 3 cats running around things can get messy really quick (oh, and don’t forget the kids, they’re good at making a mess too 😉
I control the Roomba with an IRTrans Ethernet module that’s located in the living room; I can send a ‘CLEAN’ command to the Roomba, but that’s about it – the DOCK command doesn’t work, and every now and then the IR command doesn’t work at all (I think due to too much sun reflections inside). So controlling the Roomba has always been far from perfect.

I’ve been looking at other solutions to control the Roomba in the past, like attaching an Arduino to it but all the solutions I found resulted in a big ‘bulb’ on top of the Roomba, which I didn’t like. But this RooWifi is small enough!

All the Roomba’s (I know about) have a 7-pin mini-DIN SCI (Serial Command Interface) socket somewhere. And there’s even (official) documentation about this SCI port.

To control the Roomba, this SCI has to be accessible; where the socket is and how easy the physical access to the socket is differs per model. To get access to the SCI socket of my Roomba 563-PET I had to remove the top plate. After removing the battery and using a screw driver the top plate came off relatively easy. This plate will not be needed anymore. The RooWifi module can be inserted into the socket like this:

RooWifi connected to Roomba

Other models may need a different approach.

RooWifi with coverAfter plugging in the RooWifi into the SCI socket the RooWifi starts up in Hotspot mode and creates a ROOMBA WR wireless network.

I connected to it with a laptop which got an IP address of 10.0.0.2, opened a web browser to and found the Web Interface. From there you can alter the settings of the RooWifi so that it connects to your own Home Wifi Network if you want to.

Below you see a screen-dump of the Driver’s Remote:

RooWifi Drivers's Remote

Other screens show four main buttons for operating the Roomba (CLEAN, SPOT, DOCK, IDLE) and an overview of all the sensor values:

Roomba sensor values

Whooaahh, and all in real-time of course, really cool 😉

It was fun driving the Roomba through the living room, the whole family was suddenly very interested in a test-drive, but that’s not why I bought this RooWifi of course; I’ve got other toys for that. The RooWifi is for automating the cleaning process, integration of the Roomba in the rest of my Home Automation system is cool, cause that means I have full control over when and for how long the Roomba will do its job – and with the Wifi connection we can use one single Roomba for more rooms than just the living room, cause Wifi is ‘everywhere’, and Infrared is not…

By the time I’ve finished the software for controlling and monitoring the Roomba I’ll write a follow-up with all the details like working with the Remote TCP server, the JSON data and all the other neat stuff the RooWifi has to offer.. so stay tuned!