1
0

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 1 year has passed since last update.

m5stackのIPアドレス管理をmDNSに丸投げする

Last updated at Posted at 2021-08-23

##WiFiマイコンが生えてくる誤家庭事情
1つ2つならいいですが、M5Atomとか誤家庭にn個あるのかわからなくなりますよね?
正直、いい加減にIPアドレスの管理が面倒ですよね?

そう、mDNSならIPアドレスの管理から逃れられます。

ついでにLinuxサーバだったら、avahi-daemonを動かしておくと、サーバのIPアドレスもmDNSで引けます。超快適M5Stackライフを楽しみましょう。

##ソース

m5stack_mDNS.ino
m5stack_mDNS.ino
#include <M5Stack.h>
#include <WiFi.h>
#include <ESPmDNS.h>
WiFiClient wifi;

const char* wifi_ssid = "Your WiFi AP";
const char* wifi_password = "pass";
const char* MDNS_NAME="m5stack";

//.local不要
const char* server = "Your Server mDNS";

void setup() {
  M5.begin(true, false, true);
  delay(1000);
  //Serial.begin(115200);
  wifi_connect();
  if(!MDNS.begin(MDNS_NAME)){
    Serial.print("Error MDNS_NAME:");
    Serial.println(MDNS_NAME);
    delay(10000);
    ESP.restart();
  }
    IPAddress server_ip = MDNS.queryHost(server);
    Serial.print("Server:");
    Serial.print(server);
    Serial.print(":"); 
    Serial.println(server_ip);
    wifi_disconnect();
    Serial.println("WiFi Disconnect");
}

void loop() {
  //MDNS.update();
  delay(10000);
}

void wifi_connect(void){
  Serial.print("WiFi Connenting");

  WiFi.begin(wifi_ssid, wifi_password);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(1000);
  }
  Serial.println("");
  Serial.print("Connected : ");
  Serial.println(WiFi.localIP());

}

void wifi_disconnect(){
  while(WiFi.status() == WL_CONNECTED ){
  WiFi.disconnect();
  delay(2000);
  }
}

##とはいえ・・
WiFi APの接続上限があるので、ソッチのほうが問題になる人が多い気もします。

##関連
https://qiita.com/DeepSpawn/items/662356c94065ee7f9697
https://qiita.com/hanapage/items/c2df143dbde33e03e674

1
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?