esp01s+blinker WOL远程开机,支持发送mac指定电脑开机
#define BLINKER_WIFI
#define BLINKER_WIFI_MULTI
#include <Blinker.h>
#include<String.h>
#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;
#define BROADCAST_ADD "192.168.11.255"
#define REMOTEPORT 9
#define LOCALPORT 9
char NETCARD_MAC[6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
char WOL_Buffer[102];
void deviceReset()
{
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!");
}
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);
Blinker.addAP(ssid2, pswd2);
Blinker.addAP(ssid1, pswd1);
Blinker.begin(auth, ssid, pswd);
Blinker.attachData(dataRead);
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());
const IPAddress remote_ip(part01.toInt(), part02.toInt(), part03.toInt(), 1);
Blinker.attachHeartbeat(heartbeat);
}
void loop()
{
Blinker.run();
}
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";
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]) : "";
}