• 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.

About

Avatar for trusktr @trusktr started