最寄りのバス停にバスがあと何分で着くかだけを表示するミニサイネージ（M5Stack、UIFlow）

# はじめに
最寄りのバス停にバスがあと何分で着くかだけを表示するミニサイネージを作りました

<blockquote class="twitter-tweet"><p lang="ja" dir="ltr">最寄りのバス停にバスがあと何分で着くかだけを表示するものなんだけど、1番よく使う<a href="https://twitter.com/hashtag/%E7%86%8A%E6%9C%AC?src=hash&amp;ref_src=twsrc%5Etfw">#熊本</a> <a href="https://twitter.com/hashtag/MaaS?src=hash&amp;ref_src=twsrc%5Etfw">#MaaS</a> <a href="https://twitter.com/hashtag/M5Stack?src=hash&amp;ref_src=twsrc%5Etfw">#M5Stack</a> <a href="https://twitter.com/hashtag/GTFS?src=hash&amp;ref_src=twsrc%5Etfw">#GTFS</a> <a href="https://t.co/pjmSrhtCe0">pic.twitter.com/pjmSrhtCe0</a></p>&mdash; がちもとさん@メタバース熊本 (@sotongshi) <a href="https://twitter.com/sotongshi/status/1471274080100040705?ref_src=twsrc%5Etfw">December 16, 2021</a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>

# 開発環境
- Windows 10 PC
- M5Stack

# 実装
1．M5Stackの初期設定はこちらからお願いします

https://qiita.com/SatoshiGachiFujimoto/items/33b7e23b232b3bfecda6

2．UIFlowでプログラムを書きます

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/63863/15f304bb-e798-019e-7615-fa79480c9c92.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/63863/4f637ff4-d245-f716-bd38-d659da82ff2a.png)


~~~main.py
from m5stack import *
from m5ui import *
from uiflow import *
import urequests
import wifiCfg
import json


setScreenColor(0x222222)


response_data = None
json_data = None
j = None
i = None


# Describe this function...
def dosomething():
  global response_data, json_data, j, i
  try:
    req = urequests.request(method='GET', url='https://xxxxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/api/xxxx', headers={})
    response_data = (json.loads((req.text)))['results']
    j = 1
    for i in response_data:
      lcd.print(str(i), 0, (j * 36), 0xffffff)
      j = j + 1
  except:
    lcd.print('API error', 0, 0, 0xffffff)


@timerSch.event('timer1')
def ttimer1():
  global response_data, json_data, j, i
  lcd.clear()
  dosomething()
  pass


lcd.font(lcd.FONT_UNICODE)
with open('/sd/settings.json', 'r') as fs:
  json_data = json.loads((fs.read()))
wifiCfg.doConnect(str((json_data['SSID'])), str((json_data['Password'])))
if wifiCfg.wlan_sta.isconnected():
  dosomething()
  timerSch.run('timer1', 60000, 0x00)
else:
  lcd.print(((str('Not connected to ') + str(str((json_data['SSID']))))), 0, 0, 0xffffff)
~~~

3．SDカードからWiFi設定を読み込むようにしています。詳細はこちら

https://qiita.com/SatoshiGachiFujimoto/items/f529cb9fb513bd5eab0c

4．GTFS Realtimeを使用してあと何分で着くかを返すAPIを作ってます

https://qiita.com/SatoshiGachiFujimoto/items/541a42b5f60d83b23cad

# 追記
- 乗降指定できるようにした

<blockquote class="twitter-tweet"><p lang="ja" dir="ltr">SDカードから乗降指定できるようになったんだけど、URLエンコードせなんのよ…<a href="https://twitter.com/hashtag/M5Stack?src=hash&amp;ref_src=twsrc%5Etfw">#M5Stack</a> <a href="https://twitter.com/hashtag/UIFlow?src=hash&amp;ref_src=twsrc%5Etfw">#UIFlow</a> <a href="https://t.co/iSOSo1fbTl">pic.twitter.com/iSOSo1fbTl</a></p>&mdash; がちもとさん@メタバース熊本 (@sotongshi) <a href="https://twitter.com/sotongshi/status/1478722265974210565?ref_src=twsrc%5Etfw">January 5, 2022</a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>

~~~settings.json
{
    "SSID":"x",
    "Password":"xxxxxxxxx",
    "stop_name_1":"%E7%AB%8B%E7%94%B0%E8%87%AA%E7%84%B6%E5%85%AC%E5%9C%92%E5%85%A5%E5%8F%A3",
    "stop_name_2":"%E6%A1%9C%E7%94%BA%E3%83%90%E3%82%B9%E3%82%BF%E3%83%BC%E3%83%9F%E3%83%8A%E3%83%AB",
    "x-api-key":"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}
~~~

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/63863/7726af55-349c-e2e3-4bc2-0819021de425.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/63863/812d98f2-3b8e-58f7-8a0c-9d764db16541.png)


~~~main.py
from m5stack import *
from m5ui import *
from uiflow import *
import urequests
import wifiCfg
import json


setScreenColor(0x222222)


response_data = None
json_data = None
j = None
i = None



# Describe this function...
def dosomething():
  global response_data, json_data, j, i
  try:
    req = urequests.request(method='GET', url=(str('https://xxxxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/api/xxxxxxx?stop_name_1=') + str(((str(str((json_data['stop_name_1']))) + str(((str('&stop_name_2=') + str(str((json_data['stop_name_2'])))))))))), headers={'x-api-key':str((json_data['x-api-key']))})
    response_data = (json.loads((req.text)))['results']
    j = 1
    for i in response_data:
      lcd.print(str(i), 0, (j * 36), 0xffffff)
      j = j + 1
  except:
    lcd.print('API error', 0, 0, 0xffffff)


def buttonA_wasPressed():
  global response_data, json_data, j, i
  lcd.clear()
  dosomething()
  pass
btnA.wasPressed(buttonA_wasPressed)

@timerSch.event('timer1')
def ttimer1():
  global response_data, json_data, j, i
  lcd.clear()
  dosomething()
  pass


lcd.font(lcd.FONT_UNICODE)
with open('/sd/settings.json', 'r') as fs:
  json_data = json.loads((fs.read()))
wifiCfg.doConnect(str((json_data['SSID'])), str((json_data['Password'])))
if wifiCfg.wlan_sta.isconnected():
  dosomething()
  timerSch.run('timer1', 60000, 0x00)
else:
  lcd.print(((str('Not connected to ') + str(str((json_data['SSID']))))), 0, 0, 0xffffff)
~~~