Blog Post

Simple TCP service with Node.js to capture weight data from wireless weighing scales


How do you use Node.js to build a TCP server to collect weight data from a set of wireless enabled scales?

TCP works by establishing a connection between server and client. The server listens on a particular port and then makes a connection when messages come in from a client device.

We want a TCP server to listen for messages from some scales on the same wireless network. The scales are already configured with the server IP address and port number.

A weight is placed on the scales and they automatically begin to send messages to the server. The server picks up the messages, makes the connection and then asks for the weight data. The scales send the data as 12 lines of text, each one 31 bytes long. The scales will close the connection when all the data has been sent. We then log the data to the console.

Our sequence of events is:

  1. Start the server and listen for messages.

  2. Place weight on scales - auto start messaging the server on the right IP address/port number.

  3. Server hears the scales and creates a connection.

  4. Server checks the message is from the scales and responds with a request for the weight data.

  5. Collect the weight data - it comes as 12 lines of text.

  6. Scales close the connection and the server logs the weight data.

Here's all the code we need for our simple TCP server:

                  
                     //We're using the Node.js Net module
                     var net = require('net');

                     //Create our server and at the same time pass in the function we call when we get a connection
                     var server = net.createServer((c) => {    
                           
                        var remoteAddress = c.remoteAddress + ':' + c.remotePort;  

                        var res = '';
                        
                        console.log('new scales connection from %s', remoteAddress);

                        c.on('data', onConnData);  
                        c.once('close', onConnClose);  
                        c.on('error', onConnError);
                        c.setEncoding('utf8');

                        function onConnData(d) {  
                           
                           console.log('connection data from %s: %j', remoteAddress, d);  
                           console.log(d);
                           
                           switch(d){

                                 case '\u0007':    //Scales keep sending this until the server responds and asks for a weight
                                    console.log('Responding...');
                                    c.write('ID&WT\r\n');  //server requests weight data
                                    break;
                                 default:
                                    res = res + d;  //weight data comes as 12 seperate lines
                                    
                           }
                           
                        }

                        function onConnClose() {  
                           console.log('connection from %s closed', remoteAddress); 
                           console.log(res);   //output weight data to console
                           res = '';
                        }

                        function onConnError(err) {  
                           console.log('Connection %s error: %s', remoteAddress, err.message);  
                        }  

                     });    

                     server.listen(60001, "192.168.1.123", function() {    
                        console.log('server listening to %j', server.address());  
                     });
                  
               

It’s relatively straightforward to build a simple networking service with Node.js. Node’s event driven non blocking input/output model means it is a good choice when you want to build a lightweight and efficient backend service.



Related Posts