raspberrypi.local みたいなあれ
いちいち静的IPとかやってられないのでmDNSで名前解決してIPアドレスを取得したい. ESP32なら標準ライブラリでできる.
MDNS#queryHost
で取得できるが, 戻り値が IPAddress
クラスなので注意. そのまま接続できるのかは不明. HTTPClient#begin
は引数が String
なのでとりあえず IPAddress#toString
で渡しておけばOK.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <WiFi.h> | |
#include <ESPmDNS.h> | |
const char MDNS_NAME[] = "client_name"; | |
const char SSID[] = "ssid"; | |
const char PASSWORD[] = "password"; | |
const char API_DOMAIN[] = "mdns_host_name"; | |
void setup() { | |
Serial.begin(115200); | |
while (!Serial); | |
WiFi.begin(SSID, PASSWORD); | |
Serial.print("WiFi connecting"); | |
while (WiFi.status() != WL_CONNECTED) { | |
Serial.print("."); | |
delay(100); | |
} | |
Serial.print("success!\n"); | |
Serial.print("mDNS server instancing"); | |
while (!MDNS.begin(MDNS_NAME)) { | |
Serial.print("."); | |
delay(100); | |
} | |
Serial.print("success!\n"); | |
} | |
void loop() { | |
IPAddress ip = MDNS.queryHost(API_DOMAIN); | |
Serial.println("API domain: "+ip.toString()); | |
while(1); | |
} |

参考
ESP32でmDNSからIPアドレスを取得したい - Qiita
https://qiita.com/hanapage/items/c2df143dbde33e03e674
arduino-esp32/libraries/ESPmDNS at master · espressif/arduino-esp32
https://github.com/espressif/arduino-esp32/tree/master/libraries/ESPmDNS
arduino-esp32/IPAddress.h at master · espressif/arduino-esp32 https://github.com/espressif/arduino-esp32/blob/master/cores/esp32/IPAddress.h
WindowsのPCのホスト名に対しては、使えないようですね。
返信削除