net module: data receiving unreliable?

Posted on
  • Hi,

    I have a small ESP8266 module which is supposed to receive simple data via simple TCP. The net module should do that and it works mostly:

    var server = net.createServer(function(socket) {
      socket.on('data', function(data) {
        try {
          let a=JSON.parse(data);
          console.log(">"+JSON.stringify(a));
        } catch(e) {
          console.log("ERROR: ", e);
        }
      });
      socket.end();
    });
    
    server.listen(1234);
    

    On the sender side I do a simple test via netcat:

    for i in {0..4} ; do echo "{'toggle':$i}" | nc 192.168.21.105 1234 ; done
    

    But what I get on the Espruino side is:

    {"toggle":0}
    {"toggle":2}
    {"toggle":3}
    {"toggle":4}
    {"toggle":0}
    {"toggle":1}
    {"toggle":3}
    {"toggle":4}
    {"toggle":0}
    {"toggle":1}
    {"toggle":3}
    {"toggle":4}
    {"toggle":0}
    {"toggle":1}
    {"toggle":1}
    {"toggle":2}
    {"toggle":3}
    {"toggle":4}

    which is not what I'd expect from TCP. I should see 0, 1, 2, 3, 4.
    Even if I send one string at a time (and not 5 in a loop), it gets lost in about 20% of the cases.

    Am I doing something wrong? Is the net module on ESP8266 not working reliably?

  • Maybe i should simple not use those ESP8266 anymore...

    ESP32 tested and works 100% as expected.

    BTW in both cases I use v2.0x (x=0 for ESP32, x=1 for ESP8266).

  • Yes, ESP8266 is not the strongest... it has single processor and you may run into resource contentions... (What else is running on it, especially in JS?). The other thing I'm not sure about is: is data always a complete send / receive? or could it be chopped and you have to compose the message? What speaks against this is that the output always starts with the begin of a sent message. A third thing is: is your power supply ok? EPS8266 do spikes in current for calibration of the (RF) transmission. What kind of 8266 are you using?

  • Nothing else running on it. I had more, but to debug this problem I removed anything else (not that much was running since it's very much space constrained).
    Since the point is to display whatever is sent (up to 6 bytes, since it's a 6 character display), not much chopping up possible.
    For power: It's using a USB power adapter. Never had problems with it.
    Two different ESP8266 modules. Both with ESP12E: Wemos D1mini and a random NodeMCU module.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

net module: data receiving unreliable?

Posted by Avatar for HaraldK @HaraldK

Actions