その6から続き
24. index.jsを作成します。
「1234-5678」というところは、obnizIDに置き換えてください。「your_project_id」というところは、GCPのプロジェクトIDに置き換えてください。

const Obniz = require("obniz");
const express = require('express');
const app = express();

const ObnizId = "1234-5678";
require('date-utils');

app.get('/', async (req, res) => {
  let obniz = new Obniz(ObnizId)
  let connected = await obniz.connectWait({timeout:10});
  if(connected){
    var tempsens = obniz.wired("Keyestudio_TemperatureSensor", {signal:0, vcc:1, gnd:2});
    await obniz.wait(5000);
    var temp = await tempsens.getWait();
    var _temp = Math.round(temp * 100) / 100;
    obniz.display.clear();

    const jstNow = new Date(Date.now() + ((new Date().getTimezoneOffset() + (9 * 60)) * 60 * 1000));
    obniz.display.print(formatDate(jstNow));
    obniz.display.print("temprature:" + _temp);
    console.log("temprature:" + temp);
    await obniz.wait(10000);
    obniz.close();
    res.send({ status: "success", temprature : temp});

    // Import the Google Cloud client library
    const {BigQuery} = require('@google-cloud/bigquery');

    const options = {
      keyFilename: './credential.json',
      projectId: 'your_project_id',
    };

    // Create a client
    const bigqueryClient = new BigQuery(options);

    async function insertRow() {
      const datasetId = 'obniz_temperature';
      const tableId = 'temperature_table';
      const rows = [
        {timestamp: bigqueryClient.timestamp(new Date()), temperature: temp},
      ];

      await bigqueryClient
        .dataset(datasetId)
        .table(tableId)
        .insert(rows);
        console.log(`Inserted ${rows.length} rows`);
      }
      insertRow();

  } else {
    res.send({ status: "failed", error : "Can not connect to obniz."});
  }
});

const port = process.env.PORT || 8080;
app.listen(port, () => {
  console.log(`listening on port ${port}`);
});

function formatDate(dt) {
  var y = dt.getFullYear();
  var m = ('00' + (dt.getMonth()+1)).slice(-2);
  var d = ('00' + dt.getDate()).slice(-2);
  var h = ('00' + dt.getHours()).slice(-2);
  var mi = ('00' + dt.getMinutes()).slice(-2);
  return (y + '/' + m + '/' + d + ' ' + h + ':' + mi);
};

その8へ続く