WeMosを使ってみる

NetBIOSによる名前解決


WeMos(ESP8266)をサーバーとして使う場合、WeMos(ESP8266)側のIPアドレスを知る必要があります。
前回はLinux(Raspberry Pi)でWeMos側のIPアドレスを知る方法を紹介しましたが、
今回はWindows10でWeMos側のIPアドレスを知る方法を紹介します。

【WeMos側】

WeMos側のスケッチは以下の通りです。
こちらのスケッチからボールド文字の部分だけを変えています。
赤字の部分は自分の環境に合わせて変更してください。

/*
 *  This sketch receive a message from a TCP client
 */

#include <ESP8266WiFi.h>
#include <ESP8266NetBIOS.h>

const char* ssid = "**********";
const char* password = "**********";

// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(9876);

void setup() {
  Serial.begin(9600);

#if 0
// config static IP
  Serial.println();
  Serial.println();
  IPAddress ip(192, 168, 111, 90);
  IPAddress gateway(192, 168, 111, 1);
  IPAddress subnet(255, 255, 255, 0);
  Serial.print(F("Setting static ip to : "));
  Serial.println(ip);
  WiFi.config(ip, gateway, subnet);
#endif
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
 
  // Start the server
  server.begin();
  Serial.println("Server started");

  // Print the IP address
  Serial.println(WiFi.localIP());

  // Start the NetBIOS responder
  if (!NBNS.begin("esp8266")) {
    Serial.println("Error setting up NetBIOS responder!");
  }
  Serial.println("NetBIOS responder started");
}

void loop() {
  char smsg[128];

  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
 
  // Wait until the client sends some data
  Serial.println("new client");
  while(!client.available()){
    delay(1);
  }
 
  // Read the first line of the request
  String rmsg = client.readStringUntil('\0');
  Serial.print(rmsg);
  client.flush();

  // Prepare the response
  memset(smsg,0,sizeof(smsg));
  for(uint32_t i = 0; i < rmsg.length(); i++) {
    if(isalpha(rmsg[i])) {
      smsg[i] = toupper(rmsg[i]);
    } else {
      smsg[i] = rmsg[i];
    } // end if
  } // end for

  Serial.print("->");
  Serial.println(smsg);

  // Send the response to the client
  client.print(smsg);
  delay(1);
  Serial.println("Client disonnected");

  // The client will actually be disconnected
  // when the function returns and 'client' object is detroyed
}

このスケッチを実行するとWeMos側のIPアドレスはDHCPから払い出されたIPアドレスとなります。
DHCPから払い出されるアドレスなので毎回変わる可能性が有ります。

【Windows10側】

こ ちらの 記事に従ってNetBIOS(NBT)における名前解決の調査方法を行うだけです。




これでWindows10からWeMos側のIPアドレス(192.168.10.122)を知ることができます。

続く...