Arduino 使用一个esp8266远程电脑开机无需格外的外设,使用魔术包广播功能。
需要配置网卡的唤醒功能,只要是10年内的电脑应该都有这个功能,配置方法具体操作可百度,要改blos的配置 (usb网卡不行,无线网卡不行)
只需要修改自己电脑的mac即可,本来写来给自己方便用的,结果笔记本不支持插网线,台式机试了下可以开机,想要更多功能可以自己扩展。比如:外网开机。语音控制都很简单。
String mac ="08606E484CAC";//目标电脑主机mac,如:08-60-6E-48-4C-AC 需要去掉:或-
全部代码:
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <server.h>
#ifndef STASSID
#define STASSID "LingFeng"//wif名称
#define STAPSK "a12345678"//wif密码
#endif
const char* ssid = STASSID;
const char* password = STAPSK;
const IPAddress serverIP(255,255,255,255);//广播地址不用修改
String head="FFFFFFFFFFFF";//魔术包头,无需修改
uint16_t port=9;
String mac ="08606E484CAC";//目标电脑主机mac,如:08-60-6E-48-4C-AC 需要去掉:或-
WiFiUDP Udp;
void setup() {
Serial.begin(115200);
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
//连接wif功能
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
String stringSound;
stringSound=head+mac;
//组包
for(int i = 0;i<16;i++)
{
stringSound+=mac;
}
int macLength=stringSound.length() / 2;
byte byteArray[macLength] = {0};
hexCharacterStringToBytes(byteArray,stringSound.c_str());
Udp.beginPacketMulticast(serverIP,port,WiFi.localIP());
Udp.write(byteArray,102);//发送开机数据包
Udp.endPacket();//结束发送
delay(1000);
}
void hexCharacterStringToBytes(byte *byteArray, const char *hexString)
{
bool oddLength = strlen(hexString) & 1;
byte currentByte = 0;
byte byteIndex = 0;
for (byte charIndex = 0; charIndex < strlen(hexString); charIndex++)
{
bool oddCharIndex = charIndex & 1;
if (oddLength)
{
// If the length is odd
if (oddCharIndex)
{
// odd characters go in high nibble
currentByte = nibble(hexString[charIndex]) << 4;
}
else
{
// Even characters go into low nibble
currentByte |= nibble(hexString[charIndex]);
byteArray[byteIndex++] = currentByte;
currentByte = 0;
}
}
else
{
// If the length is even
if (!oddCharIndex)
{
// Odd characters go into the high nibble
currentByte = nibble(hexString[charIndex]) << 4;
}
else
{
// Odd characters go into low nibble
currentByte |= nibble(hexString[charIndex]);
byteArray[byteIndex++] = currentByte;
currentByte = 0;
}
}
}
}
byte nibble(char c)
{
if (c >= '0' && c <= '9')
return c - '0';
if (c >= 'a' && c <= 'f')
return c - 'a' + 10;
if (c >= 'A' && c <= 'F')
return c - 'A' + 10;
return 0; // Not a valid hexadecimal character
}