MQTTブローカに接続するデバイスに任意のクライアントIDを割り当てることで、IDによる接続デバイスの制限をします。エラーの発生したデバイスの特定にも役立ちます。ArduinoのMQTTライブラリではデフォルトで任意のIDがランダムに割り当てられるためデバイスの特定ができません。以下スケッチ例です。
//add mqtt clientid
String modelNum = "Client-";
String rndNum = String(random(0xffff), HEX);
String clientId = String(modelNum + rndNum);
const char* MQTT_CID = clientId.c_str();
Mosquittoの設定ファイル の以下の項目を指定します。 client-xxxx の記載のあるデバイスのみがブローカに接続できます。
clientid_prefixes client-
If defined, only clients that have a clientid with a prefix that matches clientid_prefixes will be allowed to connect to the broker. For example, setting “secure-” here would mean a client “secure-client” could connect but another with clientid “mqtt” couldn’t. By default, all client ids are valid.
Arduino Reference String
The Arduino programming language Reference, organized into Functions, Variable and Constant, and Structure keywords.
MAC AddressまたはhostnameをIDに反映させる場合
:orphan:
Station Class
-------------
The number of features provided by ESP8266 in the station mode is far more extensive than covered in original `Arduino WiFi library <https://www.arduino.cc/en/Reference/WiFi>`__. Therefore, instead of supplementing original documentation, we have decided to write a new one from scratch.
Description of station class has been broken down into four parts. First discusses methods to establish connection to an access point. Second provides methods to manage connection like e.g. ``reconnect`` or ``isConnected``. Third covers properties to obtain information about connection like MAC or IP address. Finally the fourth section provides alternate methods to connect like e.g. Wi-Fi Protected Setup (WPS).
Table of Contents
-----------------
- `Start Here <#start-here>`__
- `begin <#begin>`__
- `config <#config>`__
- `Manage Connection <#manage-connection>`__
- `reconnect <#reconnect>`__
This file has been truncated. show original
上記リファレンスからWiFi.macAddress()、WiFi.hostname()共にstring型です。
以下char型ポインターに受け渡すためchar型にコンバートする例です。 toCharArray() を使用します。
The Arduino programming language Reference, organized into Functions, Variable and Constant, and Structure keywords.
char mqtt_cid[40] = "mqtt client id";
const char* MQTT_CID = mqtt_cid;
String modelNum = "Client-";
String hostName = WiFi.hostname();
String clientId = String(modelNum + hostName);
clientId.toCharArray(mqtt_cid, 40);