1
1

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 5 years have passed since last update.

mosquitto_pubでJSONを送るときの注意点

Posted at

mosquitto_pubでJSONを送りたい

MQTTBrokerであるmosqiuttoに付属するmosquiotto_pubでJSONを送信しようとしたのですが、ハマったのでメモ

mosquitto_pub

mosquitto_pubはMQTTプロトコルでメッセージを送信することが可能なプログラムです。
以下のようにホスト、ポート、トピック名、メッセージを指定してメッセージを送信することが出来ます。
(-dはデバッグ)

mosquitto_pub.exe -h 127.0.0.0 -p 1803 -t my/topic -m aaaaa -d

jsonが送れない、、、

mosquitto_pubで送信したメッセージをmqtt.jsで受け取って処理しようとしたのですが、以下のようなエラーがでて苦しみました。

SyntaxError: Unexpected token i in JSON at position 1
    at JSON.parse (<anonymous>)

原因は単純でした

30分ほど苦しんでいろいろと試したのですが、原因はエスケープをちゃんとしてなかったという単純な理由でした、、、メッセージ内で使用するダブルクォーテーションは以下のようにバックスラッシュでエスケープしましょう。

-m {\"id\":1}
1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?