WeMosを使ってみる

Wake On LAN


WeMosを使ったWake On LANを試してみます。
こちらにラ イブラリがありますが、ライブラリ を使うまでもないのでゴリゴリ書いてみました。
WeMosのスケッチは以下の通りです。
broad_ip と mac は起動させたいマシンに合わせて変更してください。
また、起動させたいマシン側のbios設定やLANボードのプロパティ変更が必要です。
こ ちらなどで紹介されています。
/*
 * Wake On LAN Example
 */
#include <ESP8266WiFi.h>
#include <WiFiUDP.h>

const char* ssid = "アクセスポイントのSSID";
const char* password = "アクセスポイントのパスワード";

WiFiUDP udp;

// The Broadcast IP address to send the magic packet to.
IPAddress broad_ip(192, 168, 10, 255);

// The targets MAC address to send the packet to
byte mac[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };

unsigned int localPort = 8;      // local port to listen on

void sendWOL(IPAddress addr, byte * mac,  size_t size_of_mac) {
  byte preamble[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
  byte i;

  udp.beginPacket(addr, localPort);
  udp.write(preamble, sizeof(preamble));
 
  for (i = 0; i < 16; i++) {
    udp.write(mac, size_of_mac);
  }
  udp.endPacket();
}

void setup_wifi() {

  delay(10);

  // We start by connecting to a WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Wait for WiFi...");
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
}

void setup() {
  delay(1000);
  Serial.begin(9600);
  setup_wifi();
  udp.begin(localPort); //start udp client, not sure if really necessary.
}

void loop()
{
  static int flag=0;
  if (flag == 0) {
    Serial.println("Sending WOL Packet...");
    sendWOL(broad_ip, mac, sizeof(mac));
    flag++;
  }
}

今回は起動後に1回だけマジックパケットを送っていますが、ボタン入力、赤外線リモコン、音声入力など、
マジックパケットを送るきっかけはお好みで。
でも、「PC起動!!」など、大声を出して音声識別するのは、ちと苦しいかもしれません....
NTPと組み合わせれば、決まった曜日(例えば月曜日から金曜日)、決まった時間にWakeOnすることもできます。

続く...