Micropython学习交流群 学习QQ群:786510434 提供多种固件下载和学习交流。

Micropython-扇贝物联 QQ群:31324057 扇贝物联是一个让你与智能设备沟通更方便的物联网云平台

Micropython学习交流群 学习QQ群:468985481 学习交流ESP8266、ESP32、ESP8285、wifi模块开发交流、物联网。

Micropython老哥俩的IT农场分享QQ群:929132891 为喜欢科创制作的小白们分享一些自制的计算机软硬件免费公益课程,由两位多年从事IT研发的中年大叔发起。

Micropython ESP频道

micropython esp32 唤醒局域网主机


"""

Copyright © 2021 Walkline Wang (https://walkline.wang)

Gitee: https://gitee.com/walkline/micropython-espnow-practice-simple

"""

import network

from machine import Pin

from utime import sleep_ms

from os import uname


import network

import _thread

import time



"""

The MIT License (MIT)

Copyright © 2021 Walkline Wang (https://walkline.wang)

https://gitee.com/walkline/remote-wol-micropython

"""

import socket

import ustruct


def wake_on_lan(mac_address):

if len(mac_address) == 12:

pass

elif len(mac_address) == 12 + 5:

sep = mac_address[2]

mac_address = mac_address.replace(sep, '')

else:

raise ValueError('Incorrect MAC address format')


send_data = __create_magic_packet(mac_address)


sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

sock.sendto(send_data, ('255.255.255.255', 9))  # ok

print("magic packet sent!")


def __create_magic_packet(mac):

data = b'FF' * 6 + (mac * 16).encode()

send_data = b''


for i in range(0, len(data), 2):

send_data = send_data + ustruct.pack(b'B', int(data[i: i + 2], 16))


return send_data




class wlan_ever(object):

    def __init__(self,name,password): #传入宽带账号,密码

        self.wlan_is_connect=0

        self.name = name

        self.password =password

    def threadRun1(self,a,b):  #线程联网,传入账号密码

        print('网络线程启动')

        wl=network.WLAN(network.STA_IF)

        wl.active(True)

        while 1:

            self.wlan_is_connect=wl.isconnected()

            if self.wlan_is_connect :

                time.sleep(60)#这个睡眠很重要,60秒检测一次,其他时间保持CPU空闲

            else:

                print('掉线了,准备中~5秒后抢救')

                time.sleep(5)

                wl.connect(a,b)#重连

    def theadRun(self):

        _thread.start_new_thread(self.threadRun1,(self.name,self.password))#启动线程


import urequests as requests


wlan=wlan_ever('NBWIFI','z7758521') #实例化传入账号密码

wlan.theadRun()# 启动线程

print(wlan.wlan_is_connect)#打印一下连接状态

while 1 :

    if wlan.wlan_is_connect:

        try:

            wake_on_lan("18:68:CB:E3:16:86")

            time.sleep(1)

            wake_on_lan("00:E0:4C:68:06:B7")

            time.sleep(1)

            wake_on_lan("C4:83:4F:10:F0:3F")

            time.sleep(1)

            print("成功")

        except:

            print('失败')



推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 ESP56.com All Rights Reserved

执行时间: 0.0082738399505615 seconds