Change the color of traffic LED light depending on the weather, using OpenWeatherMap Weather API.
- Keyestudio Traffic Light Module (Traffic LED)*
- obniz Board*
- mobile battery
- API Key of Weather API
* included in Starter Kit.
How to makeHardware connection
Referring to the parts library of traffic LED, connect it to the obniz Board as below.
obniz Keyestudio Traffic Light Module (traffic LED) 0 GND 1 GREEN 2 YELLOW 3 RED
Software
Go to the site of OpenWether API, then click sign up, and fill out the required information to create your account.
After creating an account, click on the API Keys tab, and the API Key is displayed as shown below.Take note of this API Key. It is necessary when coding later.
Replace WEATHER_API_KEY_HERE
with the API Key mentioned above.(In this code, the API Key is written in the same place, but if you put the code to a public place, you should write the API Key in another file to protect it.)
It gets the weather information every 30 seconds and update the color of traffic LED (It is free up to 60 call/min).
Check Weather API – OpenWeatherMap for more information about Weather API of OpenWeatherMap.
Program<!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>
ExecuteThe color of traffic LED changes as follows:
- When an umbrella is needed, such as raining or snowing → red
- When it is cloudy and wether an umbrella is needed depends on the person → yellow
- When it is sunny and no umbrella is needed. → blue
Before we get into the project, let's look into obniz.
Here → obniz for DIY electronics
obniz is a cloud-connected IoT development board. You can program on the web browser of any smartphone or computer and the command is sent to obniz through the internet via obniz cloud. By connecting the obniz to the cloud through wifi, users can remotely control devices that are physically connected to obniz.
Thanks to this cloud based approach, you can program with Python, Javascript, or other languages you prefer and control hardware directly. You don't need to integrate firmware into the device side. Recording and analyzing data is also easy with obniz cloud service.
Want to control hardware things with your current Python or Javascript skill? Want to start IoT project but don't know where to start? Want to learn programming with languages you prefer?
obniz will help you broaden your viewpoint and develop both your SW and HW skills.
For more information, please visit our official website → Official Website
Where to get obniz board? → Amazon /Official Store
Comments
Please log in or sign up to comment.