0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

NETATMOが送信した温度/湿度/二酸化炭素濃度をAPIで取得する

Last updated at Posted at 2019-01-10

前回は認証トークンを取得する事ができた。

今回は会社に設置してあるNETATMOが送信したWeather Stationsデータの 温度、湿度、二酸化炭素濃度 を取得する。

使用するNETATMO API

getstationsdata APIを使ってみる

コード

前回のトークン取得コードを改造して、値を取得してみた

const axios = require('axios')
const qs = require('qs')

const axiosClient = axios.create({
  baseURL: 'https://api.netatmo.com',
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded'
  }
})

/**
 * トークン取得
 */
function getAccessToken() {
  const auth = {
    client_id: 'xxx',
    client_secret: 'xxxxx',
    username: 'xxx',
    password: 'xxxx',
    grant_type: 'password'
  }
  return axiosClient
    .post('/oauth2/token', qs.stringify(auth))
    .then(respons => {
      console.log('Post token Success')
      console.log(respons.data)
      return respons.data.access_token
    })
    .catch(error => {
      console.log('Post token Fail')
      console.log(error.response.config)
      console.log(error.response.data)
    })
}

/**
 * ステーションデータ取得
 */
function getStationsData(token) {
  const param = {
    access_token: token
  }
  return axiosClient
    .post('api/getstationsdata', qs.stringify(param))
    .then(respons => {
      console.log('Post getstationsdata Success')
      console.log(respons.data)
      return respons
    })
    .catch(error => {
      console.log('Post getstationsdata Fail')
      console.log(error.response.config)
      console.log(error.response.data)
    })
}

// 取得開始
getAccessToken()
  .then(token => getStationsData(token))
  .then(station => console.log(station.data.body.devices[0].dashboard_data))

かなりのレスポンス量が返ってくるが、今回欲しいのはdashboard_dataの中にある。

温度/湿度/二酸化炭素濃度を得る

dashboardの中身

{ time_utc: 1547127010,
  Temperature: 23.3,
  CO2: 688,
  Humidity: 28,
  Noise: 46,
  Pressure: 1004.5,
  AbsolutePressure: 1004.5,
  min_temp: 16.6,
  max_temp: 24.9,
  date_min_temp: 1547073639,
  date_max_temp: 1547113757,
  temp_trend: 'stable',
  pressure_trend: 'down' }

現在の会社は、

  • 温度はTemperature23℃
  • 湿度はHumidity28%
  • 二酸化炭素濃度はCO2688

寒くて湿度が低すぎるが、二酸化炭素濃度はいい感じのようだ。
この記事を書いてる時間は夜だし誰もいないし当然か…

最後に

とりあえず欲しいデータは取得する事ができた。

あとはSlackに通知したり、スマートスピーカーから取得できるよう色々作っていきたい。

参考

API Reference Getstationsdata
https://dev.netatmo.com/resources/technical/reference/weather/getstationsdata

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?