Post your best Lua script examples here

User avatar
By tertius
#16984 Hi

Just thought I'd share a WOL implementation, used to boot a PC over the local LAN. Its based on the code found here: https://gist.github.com/maiconio/2865500

Note that your target machine needs to be setup to accept this packet. Typically this calls for both a BIOS and network adapter settings change (the latter is in your operating system / device manager, etc.). Google is helpful to get this setup.

Code: Select allwolcon=net.createConnection(net.UDP, 0) --Not tested with TCP. UDP works fine
wolcon:connect(9,"255.255.255.255") --Magic packet is broadcast. Can also limit to broadcast address of your own network

mac1=string.char(0xe0,0x69,0x95,0xab,0x36,0xe7) -- Insert MAC address here of target machine
for i=1,4 do
 mac1 = mac1..mac1  --Magic packet requires 16x target PC MAC as payload
end

mac2 = string.char(0xff,0xff,0xff,0xff,0xff,0xff)..mac1 --Magic packet needs broadcast MAC in front
wolcon:send(mac2)   --Send magic packet
print("Wake-On-Lan packet sent")

wolcon:close()
collectgarbage();


Enjoy!
Last edited by tertius on Sun May 10, 2015 9:05 am, edited 1 time in total.
User avatar
By Thomas Jakober
#87662 Thanks for the tip! :D I just modified it for use with Nodemcu 3.0.0.0 onwards:

Code: Select allwolcon=net.createUDPSocket() --Not tested with TCP. UDP works fine

mac1=string.char(0xa4,0x5d,0x36,0xbc,0x2c,0x5d) -- Insert MAC address here of target machine
for i=1,4 do
 mac1 = mac1..mac1  --Magic packet requires 16x target PC MAC as payload
end

mac2 = string.char(0xff,0xff,0xff,0xff,0xff,0xff)..mac1 --Magic packet needs broadcast MAC in front
wolcon:send(9, "255.255.255.255", mac2)   --Send magic packet
print("Wake-On-Lan packet sent")

wolcon:close()
collectgarbage();