前言
よ、始めましょう!
IoT知識
なしけど、代わりにOpenWeatherMapを教えます。
1. 準備
-
天候情報提供サイトOpenWeatherMap へのアクセス
- https://openweathermap.org/
- ユーザ登録を行って下さい
- API key がメイルで送られてきます
-
OpenWeatherMap API guide
2. ユーザ登録
3. APIからのレスポンス
- lat : 緯度 (latitude)
- lon : 経度 (longitude)
- timezone : タイムゾーン名
- timezone_offset UTCとの差分(秒)
- current.dt : 現在の時刻(UTC)
- current.sunrise : 日の出の時刻(UTC)
- current.sunset : 日の入の時刻(UTC)
- current.temp : 気温
(単位: Default: Kelvin, Metric: Celsius, Imperial: Fahrenheit. ) - current.feels_like : 体感温度
- current.pressure : 海面レベルでの気圧(hPa)
- current.humidity : 湿度(%)
- current.dew_point : 露点(度)
- current.clouds : 雲量(%)
- current.uvi : 紫外線量(UV index)
- current.visibility : 視界(m)
- current.wind_speed : 風速(m/s)
- current.wind_gust : 突風(m/s)
- current.wind_deg : 風向(度)
- current.rain : 降雨量(mm)
- current.rain.1h : 過去1時間当たりの降雨量(mm)
- current.snow : 降雪量(mm)
- current.snow.1h : 過去1時間当たりの降雪量(mm)
- current.weather : 天候コード
- current.weather.id : 天候id
- current.weather.main : 天候グループパラメー タ(雨,雪,荒天など)
- current.weather.description : 天候の記述
- current.weather.icon : 天候アイコンid
4. 天候の状態(Weather Conditions)
-
https://openweathermap.org/weather-conditions
Icon list
Day icon Night icon Description 01d.png 01n.png clear sky 02d.png 02n.png few clouds 03d.png 03n.png scattered clouds 04d.png 04n.png broken clouds 09d.png 09n.png shower rain 10d.png 10n.png rain 11d.png 11n.png thunderstorm 13d.png 13n.png snow 50d.png 50n.png mist
ID | Main | Description | Icon |
---|---|---|---|
200 | Thunderstorm | thunderstorm with light rain | 11d |
201 | Thunderstorm | thunderstorm with rain | 11d |
202 | Thunderstorm | thunderstorm with heavy rain | 11d |
210 | Thunderstorm | light thunderstorm | 11d |
211 | Thunderstorm | thunderstorm | 11d |
212 | Thunderstorm | heavy thunderstorm | 11d |
221 | Thunderstorm | ragged thunderstorm | 11d |
230 | Thunderstorm | thunderstorm with light drizzle | 11d |
231 | Thunderstorm | thunderstorm with drizzle | 11d |
232 | Thunderstorm | thunderstorm with heavy drizzle | 11d |
ID | Main | Description | Icon |
---|---|---|---|
300 | Drizzle | light intensity drizzle | 09d |
301 | Drizzle | drizzle | 09d |
302 | Drizzle | heavy intensity drizzle | 09d |
310 | Drizzle | light intensity drizzle rain | 09d |
311 | Drizzle | drizzle rain | 09d |
312 | Drizzle | heavy intensity drizzle rain | 09d |
313 | Drizzle | shower rain and drizzle | 09d |
314 | Drizzle | heavy shower rain and drizzle | 09d |
321 | Drizzle | shower drizzle | 09d |
ID | Main | Description | Icon |
---|---|---|---|
500 | Rain | light rain | 10d |
501 | Rain | moderate rain | 10d |
502 | Rain | heavy intensity rain | 10d |
503 | Rain | very heavy rain | 10d |
504 | Rain | extreme rain | 10d |
511 | Rain | freezing rain | 13d |
520 | Rain | light intensity shower rain | 09d |
521 | Rain | shower rain | 09d |
522 | Rain | heavy intensity shower rain | 09d |
531 | Rain | ragged shower rain | 09d |
ID | Main | Description | Icon |
---|---|---|---|
600 | Snow | light snow | 13d |
601 | Snow | Snow | 13d |
602 | Snow | Heavy snow | 13d |
611 | Snow | Sleet | 13d |
612 | Snow | Light shower sleet | 13d |
613 | Snow | Shower sleet | 13d |
615 | Snow | Light rain and snow | 13d |
616 | Snow | Rain and snow | 13d |
620 | Snow | Light shower snow | 13d |
621 | Snow | Shower snow | 13d |
622 | Snow | Heavy shower snow | 13d |
Group 7xx: Atmosphere(雰囲気)
ID | Main | Description | Icon |
---|---|---|---|
701 | Mist | mist | 50d |
711 | Smoke | Smoke | 50d |
721 | Haze | Haze | 50d |
731 | Dust | sand/ dust whirls | 50d |
741 | Fog | fog | 50d |
751 | Sand | sand | 50d |
761 | Dust | dust | 50d |
762 | Ash | volcanic ash | 50d |
771 | Squall | squalls | 50d |
781 | Tornado | tornado | 50d |
Group 800: Clear(晴)
ID | Main | Description | Icon |
---|---|---|---|
800 | Clear | clear sky | 01d 01n |
Group 80x: Clouds(曇)
ID | Main | Description | Icon |
---|---|---|---|
801 | Clouds | few clouds: 11-25% | 02d 02n |
802 | Clouds | scattered clouds: 25-50% | 03d 03n |
803 | Clouds | broken clouds: 51-84% | 04d 04n |
804 | Clouds | overcast clouds: 85-100% | 04d 04n |
python
pythonプログラミング(8)
1. OpenWeatherMapへのアクセス
- モジュールのインポート
- pequests: http通信モジュール
- pprint: pretty printer モジュール
- json: JSON形式のファイル入出力を行うためのモジュール
- webサイトへのリクエスト
- requests.get()
- JSON形式への変換
- .json()
import pprint
import requests
import json
url = "https://api.openweathermap.org/data/2.5/onecall?"
loc = "lat=35.0211&lon=135.7538"
unit= "&units=metric"
lang= "&lang=ja"
excl= "&minutely,hourly,daily"
key = "&appid=*******************************"
# 「***」を自分のkeyで変更する
r = requests.get(url + loc + unit + lang + excl + key)
rj= r.json()
pprint.pprint(rj)
#天気データをjasonで書き出し
with open("work.json","w") as fw:
json.dump(rj,fw)
2. 時刻の取り扱い方
- OpenWeatherMapでは,時刻がUTC(秒単位)で表現 されている
- 現在の時刻(UTC)を取得するには,timeモジュールをイ ンポートし,time.time()関数を用いる
- 年,月,日,時,分,秒の情報に変換するには,datetimeモ ジュールをインポートして,次の関数を用いる
- datetime.fromtimestamp()
- datetime.utcfromtimestamp()
import time
from datetime import datetime
now = time.time()
print("Current time:",now)
loc = datetime.fromtimestamp(now)
print("Local time (Tokyo/Asia):",loc)
utc = datetime.utcfromtimestamp(now)
print("Current Time (UTC):",utc
# Current time: 1593502692.6013591
# Local time (Tokyo/Asia): 2020-06-30 16:38:12.601359
# Current Time (UTC): 2020-06-30 07:38:12.601359
演習課題
演習課題(1)
- OpenWeatherMapにユーザ登録を行い, API key を取得しなさい
- One Call API を用いて,現在の天候情報を取得し なさい(API Keyが必要)
- 取得した天候情報をJSON形式のファイルに書き込み なさい(ファイル名: work.json)
- work.jsonの内容を読み込み,どのような構造に なっているかを解析しなさい
import pprint
import requests
import json
url = "https://api.openweathermap.org/data/2.5/onecall?"
loc = "lat=35.0211&lon=135.7538"
unit= "&units=metric"
lang= "&lang=ja"
excl= "&minutely,hourly,daily"
key = "&appid=*******************************"
# 「***」を自分のkeyで変更する
r = requests.get(url + loc + unit + lang + excl + key)
rj= r.json()
pprint.pprint(rj)
#天気データをjasonで書き出し
with open("work.json","w") as fw:
json.dump(rj,fw)
演習課題(2)
work.jsonの内容を解析し,得られる情報を標準出 力に print しなさい。ただし,出力はJSON形式の データをそのままプリントせず,個々の項目(キーとバ リュー)をそれぞれ識別して印刷して下さい。
import json
import time
from datetime import datetime
#jasonデータを読み込み
with open("work.json","r") as f:
data = json.loads(f.read())
#時間を変換する
time = datetime.fromtimestamp(data["current"]["dt"])
Sunrisetime = datetime.fromtimestamp(data["current"]["sunrise"])
Sunsettime = datetime.fromtimestamp(data["current"]["sunset"])
#天気状況
temp = data["current"]["temp"]
weather= data["current"]["weather"][0]["description"]
#プリント
print("天気データ更新の時間 (Tokyo/Asia):",time)
print("日の出時間:",Sunrisetime)
print("日の落時間:",Sunsettime)
print("温度:",temp)
print("天気状況:",weather)
print("家に出る前、傘を忘れないでください")
# 天気データ更新の時間 (Tokyo/Asia): 2020-06-30 17:19:31
# 日の出時間: 2020-06-30 04:46:05
# 日の落時間: 2020-06-30 19:14:57
# 温度: 23.45
# 天気状況: 適度な雨
# 家に出る前、傘を忘れないでください