本站改版新增arduino频道
from machine import Pin, SPI, UART,I2C
import urequests as request
import ure as re
import time
import _thread
import network,sys
time_api_url='''http://quan.suning.com/getSysTime.do'''
ip_address =""
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("NBWIFI", "z7758521") # 无线网SSID、密码,开始联网 WIFI_Connect("NBWIFI","z7758521")
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])
global ip_address
ip_address = wlan.ifconfig()[0]
print("ip_address "+ip_address)
def get_time():
r = request.get(time_api_url)
content = r.text
time = re.search(r'{"sysTime2":"(.*?)-(.*?)-(.*?) (.*?):(.*?):(.*?)","sysTime1":"', content)
print(time.group(2)+'-'+time.group(3))
print(time.group(4)+':'+time.group(5))
print("Network Connecting...")
#开始执行联网模块
wifi_connect()
while True:
get_time()
time.sleep_ms(5000)
Copyright © 2014 ESP56.com All Rights Reserved
晋ICP备14006235号-22 晋公网安备14108102001165号
执行时间: 0.0097479820251465 seconds