Micropython学习交流群 学习QQ群:786510434 提供多种固件下载和学习交流。
Micropython-扇贝物联 QQ群:31324057 扇贝物联是一个让你与智能设备沟通更方便的物联网云平台
Micropython学习交流群 学习QQ群:468985481 学习交流ESP8266、ESP32、ESP8285、wifi模块开发交流、物联网。
Micropython老哥俩的IT农场分享QQ群:929132891 为喜欢科创制作的小白们分享一些自制的计算机软硬件免费公益课程,由两位多年从事IT研发的中年大叔发起。
wifi 获取网络时间设置Rtc并循环获取
import uasyncio as asyncio
import urequests
import json
from machine import Pin, RTC, ADC
import network # Wi-Fi功能所在库
import utime # 延时API所在组件
import ujson # json字串解析库
wlan = None
# Wi-Fi SSID和Password设置
wifiSsid = "NBWIFI"
wifiPassword = "z7758521"
# 等待Wi-Fi成功连接到路由器
def get_wifi_status():
print("wifi...")
global wlan
wifi_connected = False
wlan.active(True) #激活界面
wlan.scan() #扫描接入点
#print("start to connect ", wifiSsid)
wlan.connect(wifiSsid, wifiPassword) # 连接到指定的路由器(路由器名称为wifiSsid, 密码为:wifiPassword)
while True:
wifi_connected = wlan.isconnected() # 获取Wi-Fi连接路由器的状态信息
if wifi_connected: # Wi-Fi连接成功则退出while循环
break
else:
utime.sleep(0.5)
print("wifi_connected:", wifi_connected)
ifconfig = wlan.ifconfig() #获取接口的IP/netmask/gw/DNS地址
print(ifconfig)
utime.sleep(0.5)
rtc = RTC()
def setTime():
url = "http://worldtimeapi.org/api/timezone/Europe/Moscow"
response = urequests.get(url)
if response.status_code == 200:
datetime_str = str(response.json()["datetime"])
year = int(datetime_str[0:4])
month = int(datetime_str[5:7])
day = int(datetime_str[8:10])
hour = int(datetime_str[11:13])
minute = int(datetime_str[14:16])
second = int(datetime_str[17:19])
subsecond = int(round(int(datetime_str[20:26]) / 10000))
rtc.datetime((year, month, day, 0, hour, minute, second, subsecond))
#date_str = "Date: {2:02d}.{1:02d}.{0:4d}".format(*rtc.datetime())
#time_str = "Time: {4:02d}:{5:02d}:{6:02d}".format(*rtc.datetime())
return True
else:
print("Time is not set")
return False
def getRtcTime():
date_str = "{2:02d}.{1:02d}.{0:4d}".format(*rtc.datetime())
time_str = "{4:02d}:{5:02d}".format(*rtc.datetime())
return [date_str, time_str]
if __name__ == '__main__':
wlan = network.WLAN(network.STA_IF) #创建WLAN对象
get_wifi_status()
setTime()
while True:
print(getRtcTime())
utime.sleep(1)
Copyright © 2014 ESP56.com All Rights Reserved
执行时间: 0.0084390640258789 seconds