0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

リモートカメラシステム (MQTT ブローカーに画像をそのまま送る)

Last updated at Posted at 2021-08-17

次のシステムを構築するための検証です。
Grove IoT スターターキット for SORACOM で作るリモートカメラシステム

調べている過程で、MQTT ブローカーに画像をそのまま送れることが分かりました。

次のページを参考にしました。
mosquitto,paho-mqttで画像のやりとり

パブリッシュ
camera.jpg という画像を送ります。

go_pub.sh
#
#	go_pub.sh
#
#					Aug/17/2021
#
BROKER='violet.local'
TOPIC='sample/imageTopic'
#
mosquitto_pub -h ${BROKER} -p 1883 -t ${TOPIC} -f camera.jpg
#

サブスクライブ
受け取った画像を output.jpg に保存します。

subscribe_jpg.py
#! /usr/bin/python
#
#	subscribe_jpg.py
#
#						Aug/17/2021
# ------------------------------------------------------------------
import sys
import paho.mqtt.client as mqtt

host = 'violet.local'
port = 1883
topic = 'sample/imageTopic'

# ------------------------------------------------------------------
def on_connect(client, userdata, flags, respons_code):
	print('status {0}'.format(respons_code))
	client.subscribe(topic)

# ------------------------------------------------------------------
def on_message(client, userdata, msg):
	sys.stderr.write("*** on_message ***\n")
	str_out = msg.payload
	sys.stderr.write("%d\n" % len(str_out))
	outfile = open('./output.jpg', 'wb')
	outfile.write(msg.payload)
	outfile.close

# ------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
client = mqtt.Client(protocol=mqtt.MQTTv311)
client.on_connect = on_connect
client.on_message = on_message
client.connect(host, port=port, keepalive=60)
client.loop_forever()
#
# ------------------------------------------------------------------
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?