Arduino配合ENC28J60接收WOL信号启动设备

有台NAS,不支持wake-on-lan(wol)启动,用arduino进行改造。

使用EtherCard-master库(https://github.com/njh/EtherCard),并修改使其支持wol接收(非UDP包)

enc28j60.cpp:

//writeRegByte(ERXFCON, ERXFCON_UCEN|ERXFCON_CRCEN|ERXFCON_PMEN|ERXFCON_BCEN);
//
writeRegByte(ERXFCON, ERXFCON_UCEN|ERXFCON_CRCEN|ERXFCON_PMEN|ERXFCON_BCEN|ERXFCON_MPEN);

net.h:

// add
#define ETHTYPE_WOL_H_V 0x08
#define ETHTYPE_WOL_L_V 0x42

tcpip.cpp:

// check ethernet type, wol packet should be 0x8042, and packet length is not checked here
static uint8_t eth_type_is_wol(uint16_t len) {
    return gPB[ETH_TYPE_H_P] == ETHTYPE_WOL_H_V && gPB[ETH_TYPE_L_P] == ETHTYPE_WOL_L_V;
}
void EtherCard::registerGotWOLCallback(void(*callback)) {
  wol_cb = callback;
}

uint16_t EtherCard::packetLoop(uint16_t plen) {
  // add
  if (eth_type_is_wol(plen)) {
        //Serial.println("Got WOL Packet!");
        if (wol_cb) {
            ((void(*)())wol_cb)();
        }
        return 0;
    }
}
static void(*wol_cb)

 

WOL.ino:

ether.registerGotWOLCallback(GotWOL);

static void GotWOL() {
    // do something after wol packet received
    Serial.println("Got WOL Packet !!!");
}

 

posted @ 2018-10-23 09:20  GarfieldLoveCoffe  阅读(670)  评论(0)    收藏  举报