-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample35_srv_led3_jma.py
executable file
·132 lines (121 loc) · 7.43 KB
/
example35_srv_led3_jma.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env python3
# coding: utf-8
# Example 35 インターネット照る照る坊主 【IoT カラーLEDを制御】
# 接続図
# [天気情報] (インターネット)
# ↓
# [本機] ------> [IoTカラーLED] or [IoTフルカラーLED]
#
# 機器構成
# 本機 天気情報を色番号に変換してIoTカラーLEDを点灯制御
# IoTカラーLED example14_iot_btn.py 初期設定内の colors_full=False
# IoTフルカラーLED example19_iot_ledpwm.py 初期設定内の colors_full=True
#
# ESP8266用:
# https://github.com/bokunimowakaru/esp/blob/master/2_example/example16f_led/example16f_led.ino
#
# ESP32用:
# https://github.com/bokunimowakaru/esp/blob/master/2_example/example48f_led/example48f_led.ino
# 参考文献 天気予報 API(livedoor 天気互換サービス):
# https://weather.tsukumijima.net/
#
# 【重要なご注意】
# 本ソフトウェアの利用に関して、筆者(国野 亘)は、責任を負いません。
# 気象業務法や、下記の予報業務許可に関する情報、上記参考文献の注意事項を
# 良く読んでから利用ください。
# 気象業務法 https://www.jma.go.jp/jma/kishou/info/ml-17.html
# 予報業務許可 https://www.jma.go.jp/jma/kishou/minkan/q_a_m.html
# 初期設定
ip_leds = ['127.0.0.1'] # IoTカラーLEDのIPアドレス
colors = ['消灯','赤色','緑色','黄色','青色','赤紫色','藍緑色','白色']
# フルカラーLEDを使用する場合はcolors_full=Trueを設定
colors_full = True # フルカラー有効化フラグ
city_id = 270000 # 気象庁=130000(東京地方など)
# 大阪管区気象台=270000(大阪府など)
# 京都地方気象台=260000(南部など)
# 横浜地方気象台=140000(東部など)
# 銚子地方気象台=120000(北西部など)
# 名古屋地方気象台=230000(西部など)
# 福岡管区気象台=400000(福岡地方など)
url_s = 'https://www.jma.go.jp/bosai/forecast/data/forecast/'
url_s += str(city_id) + '.json'
interval = 10 * 60 # 動作間隔10分(単位=秒)
# ライブラリ
import urllib.request # HTTP通信ライブラリを組込む
import json # JSON変換ライブラリを組込む
from time import sleep # スリープ実行モジュール
def getWeather(): # 天気情報取得関数を定義
try: # 例外処理の監視を開始
res = urllib.request.urlopen(url_s) # HTTPアクセスを実行
res_s = res.read().decode() # 受信テキストを変数res_sへ
res.close() # HTTPアクセスの終了
res_dict = json.loads(res_s) # 辞書型の変数res_dictへ代入
except Exception as e:
print(e) # エラー内容を表示
return None # Noneを応答
return res_dict[0]['timeSeries'][0]['areas'][0]['weathers'][0] # 天気を応答
def led3(ip,color): # IoTカラーLED
if color is None or color < 0 or color > 7: # 範囲外の値の時に
return # 何もせずに戻る
url_led_s = 'http://' + ip # アクセス先
if colors_full: # フルカラーの設定
colors_3 = ['R','G','B'] # 3原色名R,G,Bを代入
colors_rgb = ['000','933','393','770','339','717','276','666'] # カラー
s = '/?' # 文字列変数sの初期化
for i in range(len(colors_3)): # 文字変数cにR、G、Bを代入
s += colors_3[i] + "=" # 変数sにR=、G=、B=を追加
s += colors_rgb[color][i] # 各色の輝度(0~9)を追加
if i < len(colors_3) - 1: # forに次の3原色がある場合
s += '&' # 結合を示す「&」を追加
else:
s = '/?COLOR=' + str(color) # 色番号(0~7)の設定
try:
urllib.request.urlopen(url_led_s + s) # IoTカラーLEDへ色情報を送信
except urllib.error.URLError: # 例外処理発生時
print('URLError :',url_led_s) # エラー表示
# ポート8080へのアクセス用 (下記の5行)
url_led_s = 'http://' + ip + ':8080' # ポートを8080に変更
try:
urllib.request.urlopen(url_led_s + s) # 再アクセス
except urllib.error.URLError: # 例外処理発生時
print('URLError :',url_led_s) # エラー表示
while True:
weather = getWeather() # 天気情報を取得
i = weather.find(' ') # 3語中1語目の終わり位置を取得
i += weather[i+1:].find(' ') + 1 # 3語中2語目の終わり位置を取得
i += weather[i+1:].find(' ') + 1 # 3語中3語目の終わり位置を取得
telop = weather[0:i] # 3語をtelopに代入
print('telop =', telop) # telopの内容を表示
if telop is not None:
color = colors.index('消灯') # 初期カラー番号を白色=7に
if telop.find('晴') >= 0: # 晴れが含まれているとき
color |= colors.index('赤色') # 赤色を混合
if telop.find('くもり') >= 0: # くもりが含まれているとき
color |= colors.index('緑色') # 緑色を混合
if telop.find('雨') >= 0 or telop.find('雪') >= 0: # 雨or雪のとき
color |= colors.index('青色') # 青色を混合
color %= len(colors) # colorは0~7
print('Color =',color,colors[color]) # 色番号と色名を表示
for ip in ip_leds: # 各機器のIPアドレスをipへ
led3(ip,color) # 各IoTカラーLEDに色を送信
sleep(interval) # 動作間隔の待ち時間処理
'''
実行例
--------------------------------------------------------------------------------
pi@raspberrypi:~/iot/learning $ ./example35_srv_led3_jma.py
telop = くもり 時々 晴れ
Color = 3 黄色telop = 曇り
--------------------------------------------------------------------------------
pi@raspberrypi:~/iot/learning $ sudo ./example19_iot_ledpwm.py
127.0.0.1 - - [11/Oct/2019 17:54:24] "GET /?R=7&G=7&B=0 HTTP/1.1" 200 17
Color = [7, 7, 0]
GPIO17 = 35
GPIO27 = 35
GPIO22 = 0
127.0.0.1 - - [11/Oct/2019 18:04:24] "GET /?R=3&G=9&B=3 HTTP/1.1" 200 17
Color = [3, 9, 3]
GPIO17 = 4
GPIO27 = 100
GPIO22 = 4
--------------------------------------------------------------------------------
'''