こちらと同様のことを、
esp-idf: mqtt/ws の使い方
esp-idf/examples/protocols/mqtt/wss で行いました。
MQTT over WebSocket Secure になります。
ブローカー wss://mqtt.eclipseprojects.io:443/mqtt
トピック /topic/qos0
プログラム
プロジェクトのコピー
cp -r ~/esp/esp-idf/examples/protocols/mqtt/wss .
wss/sdkconfig を作成
idf.py build
Wifi の接続情報を変更
wss/sdkconfig
(省略)
CONFIG_EXAMPLE_WIFI_SSID="****"
CONFIG_EXAMPLE_WIFI_PASSWORD="****"
(省略)
main/mqtt_eclipseprojects_io.pem の作成
ダウンロード (README.md に記述されています)
echo "" | openssl s_client -showcerts -connect mqtt.eclipseprojects.io:443 | sed -n "1,/Root/d; /BEGIN/,/END/p" | openssl x509 -outform PEM >mqtt_eclipse_org.pem
mqtt_eclipse_org.pem を wss/main/mqtt_eclipseprojects_io.pem
にコピーする
コンパイル、実行
idf.py -p /dev/ttyUSB0 flash
テスト
esp-idf: mqtt/ws の使い方
の publish.js subscribe.js の const brokerUrl を次に変更
const brokerUrl = 'wss://mqtt.eclipseprojects.io:443/mqtt'
実行結果
$ cu -l /dev/ttyUSB0 -s 115200
Connected.
(省略)
I (768825) mqttwss_example: MQTT_EVENT_DATA
TOPIC=/topic/qos0
DATA=Good Afternoon Feb/13/2025 PM 15:31
I (772715) mqttwss_example: MQTT_EVENT_DATA
TOPIC=/topic/qos0
DATA=Good Afternoon Feb/13/2025 PM 15:31
I (775485) mqttwss_example: MQTT_EVENT_DATA
TOPIC=/topic/qos0
DATA=Good Afternoon Feb/13/2025 PM 15:31