当前位置:首页 > 技术 > Python > 正文内容

micropython连接到wifi上面

Watrt6年前 (2019-12-31)Python28530
import network,time,ujson,urequests,ntptime
from machine import RTC
nic=network.WLAN(network.STA_IF)
nic.active(True)
#nic.connect('ZCWH','00004157')
nic.connect('028sd.com','88884444')
nic.connect('028sd.com','88884444')
connecti=10
while not(nic.isconnected() or connecti==0):
  print("正在连接",connecti)
  time.sleep(1)
  connecti-=1
if(nic.isconnected()):
  print("连接成功")
  gettext=urequests.get('http://apikey.net')
  #print(gettext.text[1:])
  parsed = ujson.loads(gettext.text[1:])
  #处理IP地址和时间
  IPadd=parsed['data']['IP_Address']
  nowtime=parsed['data']['Current-Time']
  rtc = RTC()
  rtcdate=list(map(int, str.split(str.split(nowtime,' ')[0],'-'))) 
  rtctime=list(map(int, str.split(str.split(nowtime,' ')[1],':'))) 
  print(IPadd)
  print(nowtime)
  print(rtcdate+[5]+rtctime+[0])
  rtc.datetime((rtcdate+[5]+rtctime+[0])) # set a specific date and time
  print(rtc.datetime())
  exec(open('./oled.py').read(),globals())
else:
  print("连接超时..")

我写的比较多一些。就是连接到wifi后从网络读取时间写到rtc里面然后在切换到oled上面显示出来 。


打赏 支付宝打赏 微信打赏

相关文章

micropython解码bmp

micropython解码bmp

from ST7735 import TFT,TFTColor from machine import SPI,Pin spi = SPI(2, baudrate=20000000, polarity=0, phase=0, sck=Pin(14), mosi=Pin(13), miso=Pin(12)) tft=TFT(spi,16,17,18)...

pyqt5 开发入门

pyqt5 开发入门

使用:pyqt5-tools designer 进行窗口设计使用命令:pyuic5 -o textedit.py textedit.ui  把设计的UI文件转换成py文件注意:使用对话框的话要继承于class Ui_MainWindow(QtWidgets.QMainWindow):使用信号的方法:self.actionnew.triggered.connect(self.new_btn)    self.new_btn 为当前类下的方法下面示例:#&nb...

asyncio --- 异步 I/O http服务代码

asyncio --- 异步 I/O http服务代码

import asyncioasync def service(reader,writer):    data = await reader.read(1024) #同步读取数据    data =data.decode().replace('\r','<br/>')    print(writer.get_extra_info('peername'))   &nbs...

micropython 清除flash磁盘内容

micropython 清除flash磁盘内容

对于ESP32,一般是在命令行中使用esptool.py清除flash,如果是在程序中想清除flash内容,或者更换文件系统格式,可以用下面方法:import uos uos.VfsFat.mkfs(bdev)如果使用 LFS2 文件系统,命令是import uos uos.VfsLfs2.mkfs(bdev)对于STM32,默认没有bdev设备,需要使用下面方法:import uos flash = pyb.Flash(start=0)u...

python使用requests库下载文件

python使用requests库下载文件

import os import requests def downfile(url): try: url=url.split("@")[0].split("?")[0] urlfile=url.split("/")[-1] print(urlfile) except Exception as e: print("地址不正确...

Python内置函数

Python内置函数

以下是 Python 中的全部内置函数,并列出了它们的作用和参数说明。请注意,以下列出的内置函数是基于 Python 3.9 版本。abs(): 返回一个数的绝对值。参数:abs(x)all(): 如果可迭代对象中的所有元素都为 True,则返回 True。参数:all(iterable)any(): 如果可迭代对象中的任何元素为 True,则返回 True。参数:any(iterable)ascii(): 返回一个包含 ASCII 转义字符的字符串表示。参数:ascii(object)bin(...

使用 tqdm 库在控制台中实现进度条

使用 tqdm 库在控制台中实现进度条

简单用法:from tqdm import tqdm  for i in tqdm(range(2)):   pass100%|███████████████████| 2/2 [00:00<00:00, 1998.72it/s]  从上面可以看到生成一个长度为2的列表传入tqdm中,在for中迭代,此时输出了进度条,这里tqdm全部使用了默认参数,默认进度条样式就是如上所示;通常默认进度条所输出...

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。