Dropbox Download

Get a file and use it for configrue obniz

How it works

In Last Lesson, We use dropbox to store sensor data.
This lesson shows how to get a file from dropbox.
Use a file to configre obniz.

Download

dropbox provide download function filesDownload(). It's simple to use.
But it's respons contains file Blob. We should read it as text.FileReader will do that.

See example.

This example download obniz.txt file from your dropbox App folder.
And FileReader will read Blob as text file.
console.log() will show text in obniz.txt.

var dbx = new Dropbox.Dropbox({ accessToken: '<Your access token>' });
var meta = await dbx.filesDownload({path: '/obniz.txt'})
var blob = meta.fileBlob;
var reader = new FileReader();
reader.addEventListener("loadend", function() {
  console.log(reader.result);
});
reader.readAsText(blob);

Use it as config file

Let's use a file in dropbox as config file.

Connect a LED to obniz io0,1.
And prepare obniz.txt file in your App folder.
The content of txt file is like this.

{"led": "on"}

Yes, It's json.
We will download this file and turn on led regarding this json file.

See full code.

<html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <script src="https://obniz.io/js/jquery-3.2.1.min.js"></script>
  <script src="https://unpkg.com/obniz@2.4.0/obniz.js"></script>
  <script src="https://unpkg.com/dropbox@4.0.3/dist/Dropbox-sdk.min.js"></script>
</head>
<body>

<div id="obniz-debug"></div>

<script>
var obniz = new Obniz("OBNIZ_ID_HERE");
var dbx = new Dropbox.Dropbox({ accessToken: '<Your access token>' });

obniz.onconnect = async function () {
  var led = obniz.wired("LED", {anode:0 , cathode:1 });
  // try download file
  const meta = await dbx.filesDownload({path: '/obniz.txt'})
  console.log('downloaded');

  // read file
  var blob = meta.fileBlob;
  var reader = new FileReader();
  reader.addEventListener("loadend", function() {
    // parse json
    console.log(reader.result);
    var obj = JSON.parse(reader.result);

    if (obj.led === "on") {
      led.on();
    } else if (obj.led === "blink") {
      led.blink();
    }
  });
  reader.readAsText(blob);
}
</script>
</body>
</html>

If it works fine, you will see a log like this.
And Connected LED will be turned ON!

Try to change "on" to "blink" in obniz.txt and run a program again.
LED will blink.





You will Get in Few Days

Circuit for Starter “obniz Board” is available on Amazon and other online stores.
You can get it at below

Our products and resellers

Forum

Visit our developer’s forum to discuss and discover technologies.

Forum

Contact

Feel free to contact out support and technical team.

Contact us