马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 maker_tinker 于 2023-7-30 17:22 编辑 import time,ntptime,network
import re
from machine import Pin
def wifi_main():
global inif
wifi=network.WLAN(network.STA_IF) #WIFI模式
if not wifi.isconnected():
#print('wifi ing......')
wifi.active(True)
wifi.connect('xxxx','password') #连接WIFI
while not wifi.isconnected():
pass
print('Wifi connection succeeded')
print('network config:', wifi.ifconfig())
wifi_main()
while True: #时间校准
#---------------------------------------------分隔符部分有时运行太慢,有无其他方式获取时间?或能优化提高成功率也好。
try:
#print('time ing')
ntptime.settime()
#print('time ok')
break;
except:
print('time no')
time.sleep(1)
#---------------------------------------------
mytime=time.localtime()
mytime='%d-%d-%d %d:%d:%d'%(mytime[0],mytime[1],mytime[2],mytime[3]+8,mytime[4],mytime[5])
alllist = list(mytime)
#print(alllist)
print(alllist[-8], end = '')
print(alllist[-7])
time0 = mytime.split(' ')
time_date = time0[0]
time_time = time0[1]
time_hour_etc = time_time.split(':')
hour_full = int(time_hour_etc[0])
print (time_hour_etc[0])
if hour_full > 12:
#print(hour_full - 12)
print(bin(hour_full - 12))
else:
print(hour_full)
decimal_num = hour_full - 12
binary_num = "{0:b}".format(decimal_num)
print(binary_num)
num = binary_num
listn = [int(i) for i in str(num)]
print(listn)
try:
n1 = listn[-1]
except:
n1 =0
try:
n3 = listn[-3]
except:
n3 =0
try:
n2 = listn[-2]
except:
n2 =0
try:
n4 = listn[-4]
except:
n4 =0
print(n4, n3, n2, n1)
led1 = Pin(21, Pin.OUT)
led2 = Pin(20, Pin.OUT)
led3 = Pin(19, Pin.OUT)
led4 = Pin(18, Pin.OUT)
while True:
if n4 > 0:
led1.on()
else:
led1.off()
if n3 > 0:
led2.on()
else:
led2.off()
if n2 > 0:
led3.on()
else:
led3.off()
if n1 > 0:
led4.on()
else:
led4.off()
分隔符部分有时运行太慢,有无其他方式获取时间?
或能优化提高成功率也好。 |