//tips
//Biz
本日の見回り終了。
狩猟免許がないと保険がつかないので基本的には連れていけないという建前があるなどの説明をされた。罠まわり付き添い終了のお知らせになりそうだったが何とかokになった。ICT囲い罠視察記事が市だよりで取り上げてしまっているのでだいぶ話が広まっている模様。パイセン・・・。12月の見回りは終了で、1月9日から再度始動。
罠と銃の免許を持っていても、罠の見回りの際に銃を持ち歩いてはダメで、獲物を見つけたら銃を家に取りに戻らなければいけないという掟があるよう。
基本的に自販機前でおいちゃんと話していると車で通る人全て知り合いなので、少なくとも30分で5プウは鳴らされた。基本的に全て見られている前提で動く。
テストの時の目測は電柱幅35メートル前後を基準にして答えると良い。
山で4WDのジープ(バン)が大事。恥ずかしいことに山で鉄パイプをバックで越えられず、師匠に運転して越えてもらうなどなった。
・簡易サイトの作り方
1.Pinterestで参考のデザインを探しまくる
「web design vivid」
2.手書きで大まかなページのどの順番に何を配置するかを下書きする
3.adobexdへデザインをはりつける
「iPhoneを選択し、作成を初め最後に拡大してwebに対応させる」
「canvaで集めたステッカーたち大集合PING書き出し・貼り付け」(背景透過で素材集め)
IOTデバイス制作シミュレーション
client_name = id + 'nightlight_client'
client_telemetry_topic = id + '/telemetry'
mqtt_client = mqtt.Client(client_name)
mqtt_client.connect('test.mosquitto.org')
mqtt_client.loop_start()
print("MQTT connected!")
while True:
light = light_sensor.light
telemetry = json.dumps({'light' : light})
print("Sending telemetry ", telemetry)
mqtt_client.publish(client_telemetry_topic, telemetry)
time.sleep(5)
結局mqttに送ったjsonを見れてない。
なのでMQTT を扱うサーバー用のアプリも作る必要がある。
再度ターミナルにて
mkdir nightlight-server
cd nightlight-server
python3 -m venv .venv
source ./.venv/bin/activate
これでサーバー側の準備ができた。
Mqttパッケージのインストール。
pip install paho-mqtt
コードを追加するので
touch app.py
code .
<ID>は先のメッセージ送付用と同じものを使用。
client_telemetry_topic = id + '/telemetry’のオープンフォルダに入れられたものを
mqtt_client.subscribe(client_telemetry_topic)からの
mqtt_client.on_message = handle_telemetryで
取り出すということをサーバー側で行う。
両方のコードを実行したところ
nightlight: Sending telemetry {"light": 3}
nightlight-server: Message received: {'light': 3}
このように表現された。
サーバーで現状値を確認できるようになったのでセンサーデバイスから送られてくる値を判断して制御していく。
def handle_telemetry(client, userdata, message):
payload = json.loads(message.payload.decode())
print("Message received:", payload)
command = { 'led_on' : payload['light'] < 300 }
print("Sending message:", command)
client.publish(server_command_topic, json.dumps(command))
結果は下記となる。
Message received: {'light': 3}
Sending message: {'led_on': True}
サーバ側から送られたコマンドをデバイス側で受け取り処理させる必要がある。
def handle_command(client, userdata, message):
payload = json.loads(message.payload.decode())
print("Message received:", payload)
if payload['led_on']:
led.on()
else:
led.off()
mqtt_client.subscribe(server_command_topic)
mqtt_client.on_message = handle_command
もうほんとにこの通り。
handle_command, that reads a message as a JSON document and looks for the value of the led_on property. If it is set to True the LED is turned on, otherwise it is turned off.
Sending telemetry {"light": 3}
Message received: {'led_on': True}
きちんとサーバ側から受け取れた。