obniz developer team
Published

Control Traffic LED depending on the weather

Change the color of traffic LED light depending on the weather, using OpenWeatherMap Weather API.

BeginnerFull instructions provided1 hour214
Control Traffic LED depending on the weather

Things used in this project

Hardware components

obniz
Cambrian Robotics obniz
×1

Story

Read more

Code

source code

HTML
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Let's know the weather</title>
    <script src="https://unpkg.com/obniz@3.x/obniz.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <script src="https://openweathermap.org/"></script>
  </head>
  <body>
    <script>
      const APIKEY = "WEATHER_API_KEY_HERE";

      const RED_WEATHERS = [
        "Rain",
        "Snow",
        "Thunderstorm",
        "Drizzle",
        "Fog",
        "Squall"
      ];
      const YELLOW_WEATHERS = [
        "Clouds",
        "Mist",
        "Smoke",
        "Dust",
        "Haze",
        "Sand",
        "Ash",
        "Tornado"
      ];
      const GREEN_WEATHERS = ["Clear"];
      
      let obniz = new Obniz("OBNIZ_ID_HERE");
      obniz.onconnect = async () => {
        let light = obniz.wired("Keyestudio_TrafficLight", {
          gnd: 0,
          green: 1,
          yellow: 2,
          red: 3
        });
      
        obniz.repeat(async () => {
          //現在の天気データ呼び出し
          let data = await (await fetch(
            `http://api.openweathermap.org/data/2.5/weather?q=Tokyo,jp&appid=${APIKEY}`
          )).json();
          console.log(data);
      
          let currentWeather = data.weather[0].main;
      
          if (currentWeather === undefined || currentWeather === null) {
            light.red.off();
            light.yellow.off();
            light.green.off();
            console.log("no data");
            return;
          }
      
          if (await isMatched(RED_WEATHERS, currentWeather)) {
            light.single("red");
          } else if (await isMatched(YELLOW_WEATHERS, currentWeather)) {
            light.single("yellow");
          } else if (await isMatched(GREEN_WEATHERS, currentWeather)) {
            light.single("green");
          } else {
            light.red.off();
            light.yellow.off();
            light.green.off();
            console.log("no data");
          }
        }, 30000);
      };
      
      async function isMatched(array, _currentWeather) {
        for await (let weatherName of array) {
          if (_currentWeather === weatherName) {
            return true;
          }
        }
        return false;
      }

    </script>
  </body>
</html>

Credits

obniz developer team
80 projects • 35 followers
Development board "obniz" is controlled via the internet.
Contact

Comments

Please log in or sign up to comment.