TCPClient Read not working on Electron

I need to be sure of data that I am sending to service and so I need to read a response from the server to confirm that it has received data from the electron.

I tried the following example on the photon and it works fine but on the electron I get not response in the console. In particular I need to read the ‘OK’ returned by the server. Does my node service have to return data with a particulare encoding?

This my test firmware

TCPClient client;

char c;

void setup() {
    Serial.begin(9600); // Open serial connection to report values to host
    Serial.println("Logger Start v0.5:");
    delay(10000);
    
}

void loop() {
    Serial.println("==");
    Serial.println("Loop:");


   // byte server[] = { 185, 99, 132, 84 }; // node.southmark.nz
    if(client.connect("node.southmark.nz", 8008)){
        Serial.println("connected");
    } else {
        Serial.println("connection failed");
    }
   
   
    String data = "1234546,5645645,22,44,67,##"; 
    Serial.println(data);
    client.print(data);     

    while(client.available()){
        c = client.read();
        Serial.println(c);
    }
    
    client.flush();  //for safety
    delay(400);
    Serial.println("disconnect");  
    client.stop();
    delay(10000);
    
}

this is my node code

#!/usr/bin/env node

var net = require('net');
var dateFormat = require('dateformat');
var mysql      = require('mysql');

var connection = mysql.createConnection({
  host     : 'localhost',
  user     : 'xxxxxx',
  password : 'xxxxxxxx',
  database : 'xxxxxxx'
});
var server = net.createServer(function(c) { //'connection' listener
        console.log('Unit connected');

        var text = "";
        var last_full_line_pos;
        var full_lines;
        var lines;
        var idx;

        c.on('data', function(data){
                text = text + data;
                complete = text.indexOf('##');
                if(complete>0){
                    var data_array = data.toString().split(",");
                    data = '';
                    console.log("UID                    :"+data_array[0]);
                    console.log("Time                   :"+data_array[1]);
                    console.log("Temp                   :"+data_array[2]);
                    console.log("Humid                  :"+data_array[3]);
                    console.log("BAT                    :"+data_array[4]);
                    date = new Date();
                    if(typeof data_array[2] !== 'undefined'){
                                var uid = data_array[0];

                                var sql = 'INSERT INTO readings SET tstamp = "'+dateFormat(date, "yyyy-mm-$
                                console.log(sql);
                                connection.query(sql, mysql_result_);

                                console.log("OK");
                                c.write('OK',function(){
                                        setTimeout(function() {
                                                c.end();
                                        }, 500);
                                });
                   }


                }

        });

                    date = new Date();
                    if(typeof data_array[2] !== 'undefined'){
                                var uid = data_array[0];

                                var sql = 'INSERT INTO readings SET tstamp = "'+dateFormat(date, "yyyy-mm-$
                                console.log(sql);
                                connection.query(sql, mysql_result_);

                                console.log("OK");
                                c.write('OK',function(){
                                        setTimeout(function() {
                                                c.end();
                                        }, 500);
                                });
                   }


                }

        });

        c.on('end', function() {
                console.log("Unit Disconnected");
        });

});

function mysql_result_(err, rows, fields){
        if(err){
                console.log(err.code);
        }
}

server.listen(8008, function() { //'listening' listener
    console.log('server bound 8008');
    connection.connect();
});

This is what the console shows.

Opening serial monitor for com port: "/dev/ttyACM0"
connected
1234546,5645645,22,44,67,##
disconnect
==
Loop:
connected
1234546,5645645,22,44,67,##
disconnect
==
Loop:
connected
1234546,5645645,22,44,67,##
disconnect

I believe you have a small logical flaw, and it makes sense that the problem happens on the Electron. After you send the data using client.print(data) you immediately have a loop that tests while(client.available()). With the slower network on the Electron, it’s almost guaranteed that you won’t have any data back yet, so client.available() will be 0 and the loop is skipped over and the connection closes. You’ll probably need wait for a period of time and/or number of characters to determine whether to stop looping there.

2 Likes

I have found the issue. It looks as though the cellular network vodafone in NZ is not flushing data to the unit until it gets a certain amount. I was just sending ‘OK’ and that was not being pushed through. So I have just a sent a longer string and it works.

Justin