esp01s+blinker WOL远程开机

esp01s+blinker WOL远程开机,支持发送mac指定电脑开机

#define BLINKER_WIFI
//#define BLINKER_ESP_SMARTCONFIG
#define BLINKER_WIFI_MULTI
#include <Blinker.h>
//#include "OneButton.h"
#include<String.h>

//OneButton button(BLINKER_BUTTON_PIN, true);
#define BUTTON_1 "btn-1"
#define BUTTON_2 "btn-2"
#define BUTTON_3 "btn-3"
BlinkerButton Button1(BUTTON_1);
BlinkerButton Button2(BUTTON_2);
BlinkerButton Button3(BUTTON_3);
BlinkerText text1("tex-1");
char auth[] = "xxx";
char ssid[] = "xxx";
char pswd[] = "xxx";


char ssid1[] = "TP-xxx";
char pswd1[] = "xxx..";

char ssid2[] = "Your WiFi network SSID or name";
char pswd2[] = "Your WiFi network WPA password or WEP key";


WiFiUDP Udp;
//网卡的IP地址
//const IPAddress remote_ip(192, 168, 11, 1);
//根据自己的路由器选择子网段的广播地址
#define BROADCAST_ADD "192.168.11.255"
//String BROADCAST_ADD = "192.168.11.255"
//网卡唤醒端口(一般为9或7,可以在BIOS中查看)
#define REMOTEPORT 9
//ESP8266端口,随便填一个,也填成9
#define LOCALPORT 9
//网卡的MAC地址 6个字节
char NETCARD_MAC[6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

//WOL包
char WOL_Buffer[102];
void deviceReset()
{
  // Reset device ,erase WiFi config.
  Blinker.reset();
}

void dataRead(const String & data)
{
  BLINKER_LOG("Blinker readString: ", data);

  Blinker.vibrate();

  uint32_t BlinkerTime = millis();

  Blinker.print("millis", BlinkerTime);
  Blinker.print("len", data.length());
  Blinker.print("data", data);
  WOL_packet_init(data);
  
  BLINKER_LOG("Button tap!");
  WOL_packet_transfer();
  BLINKER_LOG("WOL Packet Transfer Completed!");
  
}

//WOL魔术包初始化
void WOL_packet_init(String s)
{
  mac2char(s);
  int i, j;
  for (i = 0; i < 6; i++)
  {
    WOL_Buffer[i] = 0xFF;
  }
  for (i = 1; i < 17; i++)
  {
    for (j = 0; j < 6; j++)
    {
      WOL_Buffer[i * 6 + j] = NETCARD_MAC[j];
    }
  }
#ifdef WOL_DEBUG
  Serial.printf("The WOL packet is :\n");
  for (i = 0; i < 102; i++) {
    Serial.printf("%x ", WOL_Buffer[i]);
  }
#endif
}
void WOL_packet_transfer()
{
#ifdef WOL_DEBUG
  Serial.printf("NOW is send WOL packet!\n");
#endif
  Udp.beginPacket(BROADCAST_ADD, REMOTEPORT);
  Udp.write(WOL_Buffer);
  Udp.endPacket();
}

void button1_callback(const String & state)
{
  BLINKER_LOG("get button state: ", state);

  WOL_packet_init("E0-xxx74");
  if (state == BLINKER_CMD_BUTTON_TAP) {
    BLINKER_LOG("Button tap!");
    WOL_packet_transfer();
    BLINKER_LOG("WOL Packet Transfer Completed!");
  }
  else {
    BLINKER_LOG("Get user setting: ", state);
  }
}

void button2_callback(const String & state)
{
  BLINKER_LOG("get button state: ", state);

  WOL_packet_init("Exxx-4D");
  if (state == BLINKER_CMD_BUTTON_TAP) {
    BLINKER_LOG("Button tap!");
    WOL_packet_transfer();
    BLINKER_LOG("WOL Packet Transfer Completed!");
  }
  else {
    BLINKER_LOG("Get user setting: ", state);
  }
}
void button3_callback(const String & state)
{
  BLINKER_LOG("get button state: ", state);

  WOL_packet_init("84-xxx-5E");
  if (state == BLINKER_CMD_BUTTON_TAP) {
    BLINKER_LOG("Button tap!");
    WOL_packet_transfer();
    BLINKER_LOG("WOL Packet Transfer Completed!");
  }
  else {
    BLINKER_LOG("Get user setting: ", state);
  }
  
}


void setup()
{
  Serial.begin(115200);
  BLINKER_DEBUG.stream(Serial);

  //pinMode(LED_BUILTIN, OUTPUT);
  //digitalWrite(LED_BUILTIN, LOW);
    Blinker.addAP(ssid2, pswd2);
    Blinker.addAP(ssid1, pswd1);
    Blinker.begin(auth, ssid, pswd);
  //Blinker.begin(auth);
  Blinker.attachData(dataRead);
  //button.attachLongPressStop(deviceReset);

  Button1.attach(button1_callback);
  Button2.attach(button2_callback);
  Button3.attach(button3_callback);
  Udp.begin(LOCALPORT);
  while(!Blinker.connect()){
    delay(500);
    }
  text1.print(WiFi.localIP().toString().c_str());
  String partx = "";
  String part01 = getValue(WiFi.localIP().toString(),'.',0);
  String part02 = getValue(WiFi.localIP().toString(),'.',1);
  String part03 = getValue(WiFi.localIP().toString(),'.',2);
  String part04 = getValue(WiFi.localIP().toString(),'.',3);
  partx.concat(part01);
  partx.concat(".");
  partx.concat(part02);
  partx.concat(".");
  partx.concat(part03);
  partx.concat(".");
  BLINKER_LOG("IP:",partx);
  BLINKER_LOG("int:",part01.toInt());
//  BROADCAST_ADD = partx.concat("255");;
  const IPAddress remote_ip(part01.toInt(), part02.toInt(), part03.toInt(), 1);
  /*
  remote_ip(part01.toInt(), part02.toInt(), part03.toInt(), 1);
  BROADCAST_ADD "192.168.3.255";
  const IPAddress remote_ip(192, 168, 3, 1);
  */

  Blinker.attachHeartbeat(heartbeat);
}

void loop()
{
  Blinker.run();
//  button.tick();
}
// 心跳包函数
void heartbeat() {
    text1.print(WiFi.localIP().toString().c_str());
    BLINKER_LOG("heartbeat: ", WiFi.localIP().toString().c_str());
}


String mac = "E0-D5-5E-40-CD-74";

//char NETCARD_MAC[6] = {0xE0,0xD5,0x5E,0x40,0xCD,0x74};
void mac2char(String s) {
  NETCARD_MAC[0] = string2char(s.substring(0, 2));
  NETCARD_MAC[1] = string2char(s.substring(3, 5));
  NETCARD_MAC[2] = string2char(s.substring(6, 8));
  NETCARD_MAC[3] = string2char(s.substring(9, 11));
  NETCARD_MAC[4] = string2char(s.substring(12, 14));
  NETCARD_MAC[5] = string2char(s.substring(15, 17));
}

char string2char(String s) {
  char c = 0;
  int i = string2int(s.substring(0, 1));
  int j = string2int(s.substring(1, 2));
  c = i * 16 + j ;
  return c;
}
int string2int(String s) {
  s.toUpperCase();
  if (s.equals("0")) {
    return 0;
  } else if (s.equals("1")) {
    return 1;
  } else if (s.equals("2")) {
    return 2;
  } else if (s.equals("3")) {
    return 3;
  } else if (s.equals("4")) {
    return 4;
  } else if (s.equals("5")) {
    return 5;
  } else if (s.equals("6")) {
    return 6;
  } else if (s.equals("7")) {
    return 7;
  } else if (s.equals("8")) {
    return 8;
  } else if (s.equals("9")) {
    return 9;
  } else if (s.equals("A")) {
    return 10;
  } else if (s.equals("B")) {
    return 11;
  } else if (s.equals("C")) {
    return 12;
  } else if (s.equals("D")) {
    return 13;
  } else if (s.equals("E")) {
    return 14;
  } else if (s.equals("F")) {
    return 15;
  }

}

//提取字段
String getValue(String data, char separator, int index)
{
  int found = 0;
  int strIndex[] = {0, -1};
  int maxIndex = data.length()-1;

  for(int i=0; i<=maxIndex && found<=index; i++){
    if(data.charAt(i)==separator || i==maxIndex){
        found++;
        strIndex[0] = strIndex[1]+1;
        strIndex[1] = (i == maxIndex) ? i+1 : i;
    }
  }

  return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值