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 1 year has passed since last update.

Mosquitto を bridge にして AWS IOT に publish

Last updated at Posted at 2022-12-03

Mosquitto が動いていることの確認

サーバー

sudo systemctl status mosquitto

example/test に publish してみる

#
TOPIC="example/test"
HOST="example.com"
#
#
mosquitto_pub -d -t orz -m '{"x":30, "y": 25, "z": 53}' -h $HOST --topic $TOPIC

example/test に subscribe

#
TOPIC="example/test"
HOST="example.com"

mosquitto_sub -d -t orz -h $HOST --topic $TOPIC

Mosquitto に bridge の設定をする

証明書を、/etc/mosquitto/certs に置く

$ tree /etc/mosquitto/certs
/etc/mosquitto/certs
├── ****-certificate.pem.crt
├── ****-private.pem.key
├── AmazonRootCA1.pem
└── README

設定ファイル

/etc/mosquitto/conf.d/bridge.conf
# ============================
# Bridges to AWS IOT
# ============================

# AWS IoT endpoint, use AWS CLI 'aws iot describe-endpoint'
connection M5thing
address ****-ats.iot.ap-northeast-1.amazonaws.com:8883 

# Specifying which topics are bridged
topic example/test out 1
topic both_directions both 1


# Setting protocol version explicitly
bridge_protocol_version mqttv311
bridge_insecure false

# Bridge connection name and MQTT client Id,
# enabling the connection automatically when the broker starts.
cleansession true
clientid bridgeawsiot
start_type automatic
notifications false
log_type all

# ============================
# Certificate based SSL/TLS support
# ----------------------------
#Path to the rootCA
bridge_cafile /etc/mosquitto/certs/AmazonRootCA1.pem

# Path to the PEM encoded client certificate
bridge_certfile /etc/mosquitto/certs/****-certificate.pem.crt

# Path to the PEM encoded client private key
bridge_keyfile /etc/mosquitto/certs/****-private.pem.key
#

Mosquitto の再起動

sudo systemctl restart mosquitto

AWSIoT で確認

トピック example/test に publish する

image.png

確認した mosquitto のバージョン

$ mosquitto -h
mosquitto version 2.0.11

検証プログラム

publish.py
#! /usr/bin/python
#
#	publish.py
#
#						Dec/03/2022
#
# ------------------------------------------------------------------
import  sys
import  json
from time import sleep
import paho.mqtt.client as mqtt

# ------------------------------------------------------------------
def publish_proc(icount,c_out):
	unit_aa = {}
	unit_aa["count"] = icount
	unit_aa["c"] = c_out
	unit_aa["program"] = "publish.py"
	unit_aa["version"] = "2022-12-03 PM 16:29"
	out_str = json.dumps (unit_aa)
#
	client.connect(host, port=port, keepalive=60)
	client.publish(topic, out_str)
	client.disconnect()
#
	sys.stderr.write(out_str + "\n")
# ------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")

host = 'example.com'
port = 1883
topic = 'example/test'


client = mqtt.Client(protocol=mqtt.MQTTv311)

# tt_wait = 5.0
# tt_wait = 10.0
# tt_wait = 20.0
tt_wait = 30.0
cx=[0.12501,0.12602,0.12503,0.12704,0.12505,0.12806,0.12509]
for it in range(len(cx)):
	publish_proc(it,cx[it])
	sleep(tt_wait)
#
#

sys.stderr.write("*** 終了 ***\n")
# ------------------------------------------------------------------

参考ページ

ローカルのMosquitto MQTT BrokerをブリッジにAWS IoTを使う

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?