• Sometimes I receive the following error in Chrome when it is receiving websocket messages directly from Espruino WiFi:

    WebSocket connection to 'ws://192.168.43.247/my_websocket' failed: Could not decode a text frame as UTF-8.
    

    I'm using the ws module for Espruino. Maybe there's something in that module that causes the websocket response to be invalid?

    What I'm trying to do in practice is read values from the MPU6050 module, and send them through the websocket connection. Something like:

        var server = require('ws').createServer(function() {});
    
        server.listen(80);
        server.on("websocket", function(ws) {
            ws.on('message', function(msg) {
                print("[WS] "+JSON.stringify(msg));
            });
            const mpu = getMPU();
            setInterval(function() {
                let result = {
                    acceleration: mpu.getGravity(),  // returns acceleration array in G's
                    rotation: mpu.getDegreesPerSecond(), // returns gyro array in degrees/s
                }
                result = JSON.stringify(result)
                ws.send(result);
            }, 16.666);
        });
    

    Where getMPU() just return an MPU instance as per the MPU6050 docs, connected over I2C.

    So I have that setInterval loop that just continuously sends values over the websocket. The client code in Chrome does the following:

        let mpuData = {
            acceleration: [0,0,0],
            rotation: [0,0,0],
        }
    
        let ws = new WebSocket("ws://192.168.43.247/my_websoc­ket", "protocolOne");
        ws.addEventListener('message', ({data}) => {
            mpuData = parseEspruinoJson(data)
        });
        ws.addEventListener('open', () => {
            ws.send("Hello to Espruino!");
        })
    
        setInterval(() => console.log('data:', mpuData), 2000)
    
        // ... requestAnimationFrame loop uses mpuData for graphics ...
    

    Eventually, after a minute or two, I might get that error, and the connection breaks.

  • I'm not sure if this has anything to do with it, but I'm getting a bunch of errors in the Espruino console:

     _____                 _
    |   __|___ ___ ___ _ _|_|___ ___
    |   __|_ -| . |  _| | | |   | . |
    |_____|___|  _|_| |___|_|_|_|___|
              |_| http://espruino.com
     1v91 Copyright 2016 G.Williams
    >
    =undefined
    IP: 192.168.43.247
    {
      "connect": function (a,c) {return new b(a,c)}
     }
    [WS] "Hello to Espruino!"
    Uncaught InternalError: Timeout on I2C Read Receive
     at line 1 col 66
    ...is.i2c.readFrom(this.addr,6);a=c[0]<<­8|c[1];var b=c[2]<<8|c[...
                                  ^
    in function "readSXYZ" called from line 1 col 17
    this.readSXYZ(67)
                    ^
    in function "getRotation" called from line 1 col 36
    ...is;return this.getRotation().map(function(c){retur­n c/a.gyro...
                                  ^
    in function "getDegreesPerSecond" called from line 5 col 51
    ...n: mpu.getDegreesPerSecond(), // returns gyro array in degre...
                                  ^
    in function called from system
    ERROR: Out of Memory!
    Execution Interrupted
    Execution Interrupted
    Execution Interrupted
    Execution Interrupted
    Uncaught InternalError: Timeout on I2C Read Receive
     at line 1 col 66
    ...is.i2c.readFrom(this.addr,6);a=c[0]<<­8|c[1];var b=c[2]<<8|c[...
                                  ^
    in function "readSXYZ" called from line 1 col 17
    this.readSXYZ(59)
                    ^
    in function "getAcceleration" called from line 1 col 40
    ...eturn this.getAcceleration().map(function(c){r­eturn c/a.acc_...
                                  ^
    in function "getGravity" called from line 3 col 46
    ...celeration: mpu.getGravity(),  // returns acceleration array...
                                  ^
    in function called from system
    Execution Interrupted
    Execution Interrupted
    Execution Interrupted
    Execution Interrupted
    Execution Interrupted
    Execution Interrupted
    ERROR: Ctrl-C while processing interval - removing it.
    Execution Interrupted during event processing.
    at line 1 col 66
    ...is.i2c.readFrom(this.addr,6);a=c[0]<<­8|c[1];var b=in function "getRotation" called from line 1 col 36
    ...is;return this.getRotation().in function "getDegrees" called fromin function called from system
    > 
    
  • It also seems like up until the this crash happens, the network responses take longer and longer until the crash. I'm keeping a running average, f.e.

        let ws = new WebSocket("ws://192.168.43.247/my_websoc­ket", "protocolOne");
        let lastTime = performance.now()
        let timesCount = 0
        let totalTime = 0
        let averageTime = 0
        ws.addEventListener('message', ({data}) => {
            timesCount++
    
            const thisTime = performance.now()
            const elapsed = thisTime - lastTime
    
            totalTime += elapsed
            averageTime = totalTime / timesCount
    
            lastTime = thisTime
    
            mpuData = parseEspruinoJson(data)
        });
        ws.addEventListener('open', () => {
            ws.send("Hello to Espruino!");
        })
    
        setInterval(() => console.log('avg time:', averageTime), 2000)
    

    and at first the average is about 38 ms for the time between responses, and by the time it crashes the running average is around 50 ms. It increases steadily the whole time.

  • Yes, I'd totally sort out those other errors from the Espruino first.

    Uncaught InternalError: Timeout on I2C Read Receive

    Do you have I2C pullup resistors somewhere? I think I asked this on one of your other posts when you were getting the same error.

    ERROR: Out of Memory!

    As in the other post, I think 16.6ms interval for sending data is just too fast. It totally explains the increasing delay for responses (as the buffer increases) and the out of memory error.

    When you get that error, execution gets interrupted, and I imagine it messes up the stream of data sent to the PC. Try it running at 30fps and see if you still get the error, then work back from there?

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

Sometimes a websocket message from Espruino WiFi is invalid UTF-8? Crashes the connection.

Posted by Avatar for trusktr @trusktr

Actions