Build a Simple Fitbit Weather App (Part 3)

This is the third part in a series on developing a simple Fitbit Smartwatch App. Now that we have the weather forecast, we need to send it to the watch. If you missed either of the first parts, please see them first. You can find Part 2 here.

In the last part, we retrieved a 14-day forecast from the National Weather Service. Now we will send that data to the watch for display.

  1. Import the file-transfer utility in the companion file: index.js.
  2. Define a global variable to store the weather forecast.
import { outbox } from "file-transfer"

var forecast;
  1. Populate this variable with the 14-day forecast returned from getForecast().
async function locationSuccess(position) {
  current_location = position.coords.latitude + "," + position.coords.longitude
  console.log("Current location: " + current_location);
  forecast = JSON.stringify(await getForecast(current_location));
}
  1. Create the sendFile() function.
  2. Create a buffer.
  3. Fill the buffer with the contents of the forecast string.
function sendFile() {
  console.log("Sending file...");
  let data = new Uint8Array(forecast.length)
  for (let counter = 0; counter < data.length; counter++) {
    data[counter] = forecast.charCodeAt(counter)
  }
  outbox.enqueue("forecast.txt", data)
}
  1. Call the sendFile() function to send the forecast to the watch.
setTimeout(sendFile, 2000)
  1. Import the inbox and fs libraries in the app file: index.js. Note that these are the first changes we are making to this file.
  2. Ensure that the watch displays something meaningful while it waits for the forecast.
import { inbox } from "file-transfer";
import * as fs from "fs";

demotext.text = "Waiting...";
  1. Define the onnewfile() event.
// Event occurs when new file(s) are received
inbox.onnewfile = () => {
  console.log("New file!");
  let fileName;
  do {
    // If there is a file, move it from staging into the application folder
    fileName = inbox.nextFile();
    if (fileName) {
      console.log(`Received File: <${fileName}>`);
      let data = fs.readFileSync(fileName, "ascii");
      demotext.text = `Received: ${data}`;
    }
  } while (fileName);
};
  1. Run the App. You will see the JSON forecast has been sent to the app.
A first look at the watch app. No icons yet!

Now that we have the weather information where we want it, we can work on displaying it elegantly. The project source is available on Github. I will see you in Part 4!

Leave a comment