Avatar for user104772

user104772

Member since Nov 2019 • Last active Dec 2019
  • 0 conversations
  • 1 comments

Hello I need help writing a value set from espruino MPU6050 to the dashboard gage. Below is some of the code I have been working with. Unsure how to combine certain things. I don't know how to map values in array to gage : (. Any guidance would be appreciated.

%html code that prints array from mpu6050

<html>
 <head>
 </head>
 <body>
  <script src="https://www.puck-js.com/puck.js"></­script>
  <script>
    function getData() {
      Puck.eval('data', function(data) { 
        document.write(data.join('<br/>')); 
      });
    }
  </script>
  <button onclick="getData()">Get Data</button>
 </body>
</html>

%realtime dashboard.

<html>
 <head>
  <title>Dashboard</title>
  <meta name="viewport" content="width=210, initial-scale=1">
 </head>
 <body style="width:210px;height:450px">
  <link href="https://espruino.github.io/TinyDas­h/tinydash.css" rel="stylesheet">
  <script src="https://espruino.github.io/TinyDash­/tinydash.js"></script>
  <script src="https://www.puck-js.com/puck.js"></­script>  
  <script>
  // Called when we get a line of data - updates the light color
  function onLine(line) {
    try {
      var j = JSON.parse(line);
      console.log("Received JSON: ",j);
      elements.light.setValue(j.light*100);
    } catch(e) {
      console.log("Received: ",line);
    }
  }
  var connection;
  function connectDevice() {
    Puck.connect(function(c) {
      if (!c) {
        alert("Couldn't connect!");
        return;
      }
      connection = c;
      // remove modal window
      elements.modal.remove();
      // Handle the data we get back, and call 'onLine'
      // whenever we get a line
      var buf = "";
      connection.on("data", function(d) {
        buf += d;
        var i = buf.indexOf("\n");
        while (i>=0) {
          onLine(buf.substr(0,i));
          buf = buf.substr(i+1);
          i = buf.indexOf("\n");
        }
      });
      // First, reset Puck.js
      connection.write("reset();\n", function() {
        // Wait for it to reset itself
        setTimeout(function() {
          // Now tell it to write data on the current light level to Bluetooth
          // 10 times a second. Also ensure that when disconnected, Puck.js
          // resets so the setInterval doesn't keep draining battery.
          connection.write("setInterval(function()­{Bluetooth.println(JSON.stringify({light­:Puck.light()}));},100);NRF.on('disconne­ct', function() {reset()});\n",
            function() { console.log("Ready..."); });
          }, 1500);
        });
      });

  }
  // Set up the controls we see on the screen    
  var elements = {
    heading : TD.label({x:10,y:10,width:190,height:50,­label:"My Dashboard"}),
    light : TD.gauge({x:10,y:70,width:190,height:220­,label:"Light",value:0,min:0,max:100}),
    modal: TD.modal({x:10,y:10,width:190,height:430­,label:"Click to connect",onchange:connectDevice})
  }
  for (var i in elements)
    document.body.appendChild(elements[i]);
  </script>
 </body>
</html>

%MPU6050 function and array of three values.

I2C1.setup({scl:D4,sda:D31});
var mpu = require("MPU6050").connect(I2C1);
mpu.getAcceleration();
mpu.getGravity();
mpu.getRotation();
mpu.getDegreesPerSecond();


var data = [];
function storeData() {
  var newData = mpu.getRotation(); // for instance...
  data.push(newData);
}
setInterval(storeData, 1000);

Recent activity

This person has not yet posted anything.

Actions