代码main.py,通过网页把它传入ESP8266 micropython固件中即可。如何传输自行查找资料吧。本人也忘记如何传输了。本文只是为了记录一下曾经写过的代码案例。方便以后查找。
import usocket #导入socket包
import machine
import network
import time
LED=machine.Pin(2, machine.Pin.OUT)
LED.value(1)
WIFI_flag=1;
is_flag=0
ssid="CMCC-lin-----"
pwd="linjia@15120613059"
ap_ip=""
st_ip=""
while True:
if WIFI_flag == 1:
ap = network.WLAN(network.AP_IF)
ap.active(True)
ap.config(essid='ESP8266_WIFI')
ap.config(authmode=0)
print("ESP8266_IP:",ap.ifconfig()[0]+",Port:23")#获取IP信息
s=usocket.socket()
s.bind(('',23)) #监听本地端口23
s.listen(5)
'''
if WIFI_flag == 0:
#连接热点WIFI
sta_if=network.WLAN(network.STA_IF)
sta_if.active(True)
#sta_if.scan()# 扫描当前可连接的WiFi名称
sta_if.connect(ssid,pwd) # 设置要连接WiFi的名称和连接密码
print("SSID:",ssid+"nPassword:",pwd)
print("Linking WIFI...")
while True:
if sta_if.isconnected()==True:
break
else :
time.sleep(10)
print("Linking WIFI error.Please reset device and input right SSID or PAWD again!n")
print("ESP8266 link WIFI status:",sta_if.isconnected())#检查是否连接成功
print("ESP8266_IP:",sta_if.ifconfig()[0]+",Port:80")#获取IP信息
s=usocket.socket()
s.bind(('',80)) #监听本地端口23
s.listen(5)
'''
print("waiting...")
c,a=s.accept()
print("one device link!")
c.send("Hi,Welcome micropython of ESP8266n")
while True:
client_data=c.recv(1024)#接收到的是字节
print("client_data:",client_data)
if client_data==b"":
#s.listen(5)
print("waiting...")
c,a=s.accept()
print("one device link!")
c.send("Hi,Welcome micropython of ESP8266n")
print("client:",client_data) #输出收到的消息
if client_data==b"ON":#判断字节
LED.value(0)#开灯
print("+++ LED OPEN +++")
c.send("+++ LED OPEN +++n")
if client_data==b"OFF":#判断字节,字符串前面加b为强制类型转换为字节
LED.value(1)#关灯
print("--- LED CLOSE ---")
c.send("Hi,Welcome ESP8266 micropythonn")
c.send("--- LED CLOSE ---n")
if client_data[0:5]==b"SSID:": #判断 要连接的WIFI 名
ssid=client_data[5:]
ssid=bytes.decode(ssid)#字节转换成字符串
print("ssid:",ssid)
if client_data[0:5]==b"PAWD:": #判断WIFI密码
pwd=client_data[5:]
pwd=bytes.decode(pwd)#字节转换成字符串
print("password:",pwd)
if client_data==b"CXIP":
app = network.WLAN(network.AP_IF)
sta = network.WLAN(network.STA_IF)
print("STA_IP:",app.ifconfig()[0]+",Port:23n")
print("AP_IP:",sta.ifconfig()[0]+",Port:23n")
c.send("nPort:23,STA_IP:")
c.send(sta.ifconfig()[0])
c.send("nPort:23,AP_IP:")
c.send(app.ifconfig()[0])
if client_data==b"WIFI_AP":
WIFI_flag=1
print("--- WIFI_AP ---")
for i in range(1):
LED.value(0)#开灯
time.sleep(0.5)
LED.value(1)#关灯
time.sleep(0.5)
ap = network.WLAN(network.AP_IF)
ap.active(True)
ap.config(essid='ESP8266_WIFI')
ap.config(authmode=0)
print("ESP8266_IP:",ap.ifconfig()[0]+",Port:23")#获取IP信息
print("waiting...")
for i in range(2):
LED.value(0)#开灯
time.sleep(0.5)
LED.value(1)#关灯
time.sleep(0.5)
c,a=s.accept()
print("one device link!")
c.send("Hi,Welcome micropython of ESP8266n")
for i in range(3):
LED.value(0)#开灯
time.sleep(0.5)
LED.value(1)#关灯
time.sleep(0.5)
if client_data==b"WIFI_ST":
WIFI_flag=0
print("--- WIFI_ST ---")
for i in range(1):
LED.value(0)#开灯
time.sleep(0.5)
LED.value(1)#关灯
time.sleep(0.5)
#连接热点WIFI
sta_if=network.WLAN(network.STA_IF)
sta_if.active(True)
#sta_if.scan()# 扫描当前可连接的WiFi名称
sta_if.connect(ssid,pwd) # 设置要连接WiFi的名称和连接密码
print("SSID:",ssid+"nPassword:",pwd)
print("Linking WIFI...")
while True:
if sta_if.isconnected()==True:
is_flag=1
break
else :
time.sleep(10)
print("Linking WIFI error.Please reset device and input right SSID or PAWD again!n")
print("ESP8266 link WIFI status:",sta_if.isconnected())#检查是否连接成功
print("ESP8266_IP:",sta_if.ifconfig()[0]+",Port:23")#获取IP信息
print("waiting...")
for i in range(2):
LED.value(0)#开灯
time.sleep(0.5)
LED.value(1)#关灯
time.sleep(0.5)
c,a=s.accept()
print("one device link!")
c.send("Hi,Welcome micropython of ESP8266n")
for i in range(3):
LED.value(0)#开灯
time.sleep(0.5)
LED.value(1)#关灯
time.sleep(0.5)
全部0条评论
快来发表一下你的评论吧 !