LowPowerLab Forum

Software support => Pi Gateway => Topic started by: jorgmuskens on February 03, 2016, 01:35:15 PM

Title: Multiple Network ID's > running over 255 device's
Post by: jorgmuskens on February 03, 2016, 01:35:15 PM
Hi Felix,
I am working on a project where i require more then 255 devices. So i was investigating a possible solution with the PiGateway.
I looked at the code and could not find an option for multiple network id's. Therefore i was wondering if I understand the current limitations correctly.
1x PiGateway only accepts one network id which can handle only 255 devices?
If this is the case I had an idea to redesign my PiGateway's as nodejs socket.io tcp clients which all connect to a Pi as master tcp server.
I was also wondering if i could rebuild to code to use the network id like subnetting in ip topology.
But perhaps i am missing something in the current code and this already exists?
BR
Jorg Muskens
Title: Re: Multiple Network ID's > running over 255 device's
Post by: Felix on February 03, 2016, 02:52:15 PM
You could do this several different ways.
You could modify the library to allow a larger ID (use 2 bytes in header for that). Or make the gateway pass messages from all networks - in this case to send a message from the UI you will need to modify the gateway.js app to include the network ID in a message to a node. That's just 2 ideas.
Title: Re: Multiple Network ID's > running over 255 device's
Post by: jorgmuskens on February 05, 2016, 07:01:20 PM
Hi Felix,
I went for the option to run a PiGateway with custom socket.io code. Below is what i learned so far and for those who are also investigation this issue please reply.
I currently have 3 PiGateway's in my network. 1 Master running your 8.1 image and 2 with customized code
On my customized PiGateway's i removed all the files from the "gateway"directory except for gateway.js
Then i installed socket.io-client
npm install socket.io-client
Then I turned the gateway.js file so everything is send via socket to my master node running the standard image

var serialport = require('serialport');    //https://github.com/voodootikigod/node-serialport
serial = new serialport.SerialPort('/dev/ttyACM0', { baudrate : 115200, parser: serialport.parsers.readline("\n") });

var io = require('socket.io-client');
var serverUrl = 'http://192.168.0.104:3000'; // connect to master PiGateway
var socket = io.connect(serverUrl, {
    transports: ['websocket'],   // Use websocket for transport method
    reconnectionAttempts: Infinity  // Keep reconnecting till master is back
});

socket.on('connect', function () {
    socket.emit('join', {gateway: 200}); //on connect send join request to socket server for room <gateway network id>
});

socket.on('reconnect', function () {
    socket.emit('join', {gateway: 200}); //on reconnect send join request to socket server for room <gateway network id>
});

socket.on('welcome', function(msg){
    console.log("Welcome Received, I am : " + msg.id); // receive welcome from server
});

socket.on('time', function(msg){
    console.log("Time Received : " + msg.time); // receive time ping for long polling > just for testing now
});

serial.on("data", function(data) {
    socket.emit('message', { gateway: 200, msg: data }) // parse the data receive from the serial port over to the master
});


for testing purposes i am using a nodejs server which acts as master with the following code

// Socket.io server listens to our app
var io = require('socket.io').listen(3000);

// Send current time to all connected clients
function sendTime() {
    io.emit('time', { time: new Date().toJSON() });
}

// Send current time every 10 secs
setInterval(sendTime, 10000);

io.on('connection', function(socket) {
   
socket.on('join', function (data) {
socket.join(data.gateway); // We are using room with name <gateway network id> of socket io
});

        // Emit welcome message on connection
// Use socket to communicate with this particular client only, sending it it's own id
        socket.emit('welcome', { message: 'Welcome!', id: socket.id });

socket.on("message", function(data){
socket.name = data.gateway
console.info('[%s] %s', socket.name, JSON.stringify(data, null, 2)); //for testing we are parsing data received by client PiGateway to console
});

socket.on('disconnect', function(){
                console.info( socket.name + ' has disconnected.' + socket.id); // Who disconnected
    });
});


This is what i receive on sever side.

[200] {
  "gateway": 200,
  "msg": "[2] 5:0 6:0   [RSSI:-32][ACK-sent]\r"
}


I only need to figure out how to add the network id in the database and change the frontend.
With this i can group on specific clients and there nodes and send specific messages to individual nodes over multiple gateways with different network id's.

Much can still be done of course. These are just my first test on this
Br Jorg
Title: Re: Multiple Network ID's > running over 255 device's
Post by: Felix on February 07, 2016, 06:40:49 PM
If you're forking and doing your own custom solution I am afraid I can only give some pointers.
You will need to get familiar with NeDB if you use that for storage, look at how I store and retrieve data. Each record has an "_id" property, I use that to store the node id.
From the code you posted it's not clear what you're using.
Good luck with node.js and javascript, it's fun to learn those things.