• Thanks for the tips @Gordon. I gave websockets a try on Espruino Wifi, like the following, and it seems to work at a reasonable rate that could be used for animation, however every few seconds or so the incoming values on the UI seems to pause for a very noticeable while. Could it be GC on Espruino? Maybe there's some way to smooth it out?

    By values, I meant that Chrome console shows a counter for every time the same thing is logged. So this counter increments, it isn't really a value sent from Espruino, but the counter pauses every short while, which could cause "jank". I wonder if it is possible to fix that.

    function getMPU() {
        I2C1.setup({scl:B6,sda:B7, bitrate: 100000});
    
        var MPU6050 = require("MPU6050");
        console.log(MPU6050);
    
        var mpu = MPU6050.connect(I2C1);
    
        return mpu
    }
    
    function connectToWifi(wifiName, options) {
        var resolve, reject
        var promise = new Promise(function(res, rej) {resolve = res; reject = rej})
    
        var wifi = require("EspruinoWiFi");
    
        wifi.connect(wifiName, options, function(err) {
            if (err) reject(err);
            resolve();
        });
    
        return promise
    }
    
    connectToWifi("starlancer", { password : "Next stop: Mars." })
    .then(function() {
        const mpu = getMPU();
    
        const wifi = require("EspruinoWiFi");
        wifi.getIP(function(err, info) {
            if (err) throw err
            console.log('IP:', info.ip)
        });
      
        var page = '<html><body><script>var ws;setTimeout(function(){';
        page += 'ws = new WebSocket("ws://" + location.host + "/my_websocket", "protocolOne");';
        page += 'console.log("host", location.host);'; // <--------------------- this is logged slowly.
        page += 'ws.onmessage = function (event) { console.log("MSG:"+event.data); };';
        page += 'setTimeout(function() { ws.send("Hello to Espruino!"); }, 1000);';
        page += '},1000);</script></body></html>';
    
        function onPageRequest(req, res) {
          res.writeHead(200, {'Content-Type': 'text/html'});
          res.end(page);
        }
    
        var server = require('ws').createServer(onPageRequest);
        server.listen(8000);
        server.on("websocket", function(ws) {
          ws.on('message',function(msg) { print("[WS] "+JSON.stringify(msg)); });
          setInterval(function() {ws.send("Hello from Espruino!");}, 42);
        });
    });
    
    function onInit() {
        console.log('Espruino started!');
    }
    

    Thanks for all the help! I am loving to learn, but I have a long ways to go.

About

Avatar for trusktr @trusktr started