LoginSignup
1
0

More than 1 year has passed since last update.

現在の天候状況をKeyestudio_HT16K33に表示してみた

Last updated at Posted at 2022-12-12

はじめに

この記事は、obniz性能を検証するために、Openweather APIを用いて、現在の天候状況をKeyestudio_HT16K33に表示してみた。

備考:Keyestudio_HT16K33の表示範囲の限界があるため、天候状況を表す英単語の先頭文字を抽出して表示した。例えば、曇りの場合は、Cloud⇒C;晴れの場合は、Sun⇒S

必要なもの

(1)obniz board

(2)Keyestudio_HT16K33
(3)Openweather API
(4)Wi-Fi

Openweather APIの発行

Openweatherの公式サイト:ここから

実装

APIから取得したJsonを日本語化しないので、そのままで使う。 以下はコードです。
weather_API.js
const location = "Tokyo";
const APIKEY = 'Your APIKEY';

obniz.onconnect = async function() {
    matrix = obniz.wired("Keyestudio_HT16K33", { 
      gnd:0, 
      vcc:1, 
      sda:2, 
      scl:3
    });
};

obniz.onloop = async function() {
   const data = await (await fetch(`https://api.openweathermap.org/data/2.5/weather?q=${location}&units=metric&appid=${APIKEY}`
    )).json();
    
    const currentWeather = data.weather[0].main;
    
    matrix.brightness(7);    
    const ctx = obniz.util.createCanvasContext(matrix.width, matrix.height);
    ctx.fillStyle = "black";
    ctx.fillRect(0, 0, matrix.width, matrix.height);
    ctx.fillStyle = "white";
    ctx.font = "7px sans-serif";
    const text = currentWeather.slice( 0, 1 ) //現在の天候状況を表す英単語の先頭文字を抽出
    ctx.fillText(text, 1, 7);
    matrix.draw(ctx);
}

プレゼンテーション1.jpg

ちなみに、天候状況を表す英単語は:
"Rain",
"Snow",
"Thunderstorm",
"Drizzle",
"Fog",
"Squall",
"Clouds",
"Mist",
"Smoke",
"Dust",
"Haze",
"Sand",
"Ash",
"Tornado",
"Sun",

さいごに

Obnizで実装するのは意外と簡単でした。次回MatrixLED_MAX7219をやってみる。
1
0
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
1
0