本站改版新增arduino频道
import network
import uasyncio
from machine import Pin
import network
import time,sys
import urequests
def wifi_connect():
wifi_led=Pin(12,Pin.OUT) # 板载指示灯初始化
wlan = network.WLAN(network.STA_IF) # 以工作站 (wlan) 模式运行,需要创建一个工作站Wi-Fi接口的实例
wlan.active(True) # 在工作站对象上调用激活方法并以True作为输入值传递来激活网络接口
start_time=time.time() # 记录开始时间
if not wlan.isconnected(): # 如果尚未联网成功
print("当前无线未联网,正在连接中....")
wlan.connect(SSID, SSID_PWD) # 无线网SSID、密码,开始联网
while not wlan.isconnected(): # 如果还未连接成功,则LED灯闪烁提示
wifi_led.value(1)
time.sleep_ms(1000)
wifi_led.value(1)
time.sleep_ms(1000)
print("正在尝试连接到wifi....")
print(time.time())
if time.time()-start_time>15: # 如果超过15秒还不行,就退出
print("连接失败!!!无线网连接超过15秒,请检查无线网名称和密码是否正确..")
break
if wlan.isconnected(): # 如果联接成功
wifi_led.value(1) # LED灯常亮
IP_info=wlan.ifconfig()
print("无线网已经连接,信息如下:")
print("IP地址:"+IP_info[0])
print("子网掩码:"+IP_info[1])
print("网关:"+IP_info[2])
print("DNS:"+IP_info[3])
own_ip_address = wlan.ifconfig()[0]
print("own_ip_address "+own_ip_address)
#开始执行联网模块
wifi_start_smartconfig()
print("wifi_start_smartconfig:")
wifi_connect()
def downloadFromUrlToFile(url, File):
"""从url下载一个文件到给定的文件名中"""
print(f"from {url} downloading:\n\t{File}")
r = urequests.get(url)
with open(File, 'wb') as f:
f.write(r.content)
print(f"download done")
downloadFromUrlToFile("http://www.xxx.com/t/call_json.php", "j.txt")
Copyright © 2014 ESP56.com All Rights Reserved
晋ICP备14006235号-22 晋公网安备14108102001165号
执行时间: 0.0093510150909424 seconds