WIFI网络通信通常有两种设备 1无线接入点(AP),相当于提供热点的设备,AP和AP可以相互连接。 2无线终端(STA),连接到AP的装置,这个模式不允许其他设备接入。
查看官方列程 http://docs.micropython.org/en/latest/esp8266/quickref.html
1、以下是连接我的手机热点代码
import network
wlan = network.WLAN(network.STA_IF) # create station interface
wlan.active(True) # 激活wifi
wlan.scan() # 扫描wifi
wlan.isconnected() #检测设备是否连接上
wlan.connect('iPhone and yu', '1234567890') # 名字+密码
wlan.config('mac') # get the interface's MAC address 校验
wlan.ifconfig() # get the interface's IP/netmask/gw/DNS获取本机IP信息
连接以后默认开机进行连接(默认永久连接状态,之前LED灯复位后,代码会自动删除,这个不会) 通过终端输入这个 wlan.ifconfig() 可以打印本机信息
2、通信测试(开发板与PC端数据通信)
from socket import *
import network
wlan = network.WLAN(network.STA_IF) # create station interface
wlan.active(True) # activate the interface
wlan.scan() # scan for access points
wlan.isconnected() # check if the station is connected to an AP
wlan.connect('zufangzhaoziru301', 'zufangzhaoziru') # connect to an AP
wlan.config('mac') # get the interface's MAC address
wlan.ifconfig() # get the interface's IP/netmask/gw/DNS addresses
udp_socket = socket(AF_INET, SOCK_DGRAM)#创建UDP
dest_addr = ('192.168.18.185', 8080)#输入电脑地址和端口
send_data = "hello world"#需要发送的消息
udp_socket.sendto(send_data.encode('utf-8'), dest_addr)//数据发送
可以才看到上位机接受到了数据 3、我们让上位机发送数据给esp8266接受试试,那么接收就要使用接收的命令
from socket import *
import network
wlan = network.WLAN(network.STA_IF) # create station interface
wlan.active(True) # activate the interface
wlan.scan() # scan for access points
wlan.isconnected() # check if the station is connected to an AP
wlan.connect('zufangzhaoziru301', 'zufangzhaoziru') # connect to an AP
wlan.config('mac') # get the interface's MAC address
wlan.ifconfig() # get the interface's IP/netmask/gw/DNS addresses
udp_socket = socket(AF_INET, SOCK_DGRAM)
dest_addr = ('192.168.18.185', 8080)
send_data = "hello world"
udp_socket.sendto(send_data.encode('utf-8'), dest_addr)
recv_data = udp_socket.recvfrom(1024)
print('%c',recv_data)
可以看见已经接受到了上位机发来的数据了
4、接下来就用PC端控制开发板LED啦
from socket import *
import network
import machine
import time
wlan = network.WLAN(network.STA_IF) # create station interface
wlan.active(True) # activate the interface
wlan.scan() # scan for access points
wlan.isconnected() # check if the station is connected to an AP
wlan.connect('zufangzhaoziru301', 'zufangzhaoziru') # connect to an AP
wlan.config('mac') # get the interface's MAC address
wlan.ifconfig() # get the interface's IP/netmask/gw/DNS addresses
while not wlan.isconnected():#连接成功
print('正在连接网络....')
time.sleep(1)
print('network config -OK:',wlan.ifconfig())
udp_socket = socket(AF_INET, SOCK_DGRAM)#UDP套接字创建
udp_socket.bind(("0.0.0.0",7788))#绑定固定ip 端口
dest_addr = ('192.168.18.185', 8080)
pin16 = machine.Pin(16,machine.Pin.OUT)
while True:
#拆包
recv_data, sender_info = udp_socket.recvfrom(1024)#等待数据接收1024 字节
print("{}发送的数据,{}".format(sender_info,recv_data))#b代表没有解码
recv_data_str = recv_data.decode("utf-8")#解码
print("解码后的数据,{},".format(recv_data_str))
if recv_data_str == "亮":
pin16.value(0)
elif recv_data_str == "灭":
pin16.value(1)
time.sleep(1)
相关文章
发表评论