Mosquitto を bridge にして AWS IOT に publish

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

サーバー

```bash
sudo systemctl status mosquitto
```

example/test に publish してみる

```bash
#
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

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

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

## Mosquitto に bridge の設定をする

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

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

設定ファイル

```text:/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 の再起動

```bash
sudo systemctl restart mosquitto
```

## AWSIoT で確認

トピック　example/test に　publish する

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/b34e7b92-1971-7a4a-f1d7-1269b9a40d50.png)

## 確認した mosquitto のバージョン

```text
$ mosquitto -h
mosquitto version 2.0.11
```

## 検証プログラム

```py: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を使う](https://aws.amazon.com/jp/blogs/news/how-to-bridge-mosquitto-mqtt-broker-to-aws-iot/)