月度归档:2024年05月

按键中断 irq

from machine import Pin
c=Pin(0, Pin.IN, Pin.PULL_UP)

def bt(pin):
    print("xxx")

c.irq(trigger=Pin.IRQ_FALLING,handler=bt)

Pin.PULL_UP上拉 默认为1
IO0 配置 的话 默认1 按下0 则 下降沿Pin.IRQ_FALLING
0到1 上升沿 Pin.IRQ_RISING

c.irq(trigger=Pin.IRQ_FALLING,handler=bt)
trigger 为上升下降 判断 handler为触发函数,若其他参数,需要使用lambda

handler=lambda x:bt(x)

Micropython 简单的队列任务异步任务框架 micropython-aioschedule

https://github.com/ThinkTransit/micropython-aioschedule

micropython-aioschedule 储备知识

及其简单调用

import schedule

async def job():
    print("I'm working...")
schedule.every(1).minutes.do(job)

async def job_with_argument(name):
    print(f"I am {name}")
schedule.every(10).seconds.do(job_with_argument, name="MicroPython")

标准调用

import uasyncio as asyncio


async def job():
    print("I'm working...")
schedule.every(1).minutes.do(job)


async def job_with_argument(name):
    print(f"I am {name}")
schedule.every(10).seconds.do(job_with_argument, name="MicroPython")


async def main_loop():
    # Create the scheduling task
    t = asyncio.create_task(schedule.run_forever())
    await t

try:
    asyncio.run(main_loop())
except KeyboardInterrupt:
    print('Interrupted')
except Exception as e:
    print("caught")
    print(e)
finally:
    asyncio.new_event_loop()

股票信息查询函数

# 股票查询
class gp(object):
    def __init__(self,code="000858",ty="sz"):
        self.code=str(code)
        self.ty=ty
        self.get_url()
        self.get_info()
    def get_url(self):
        init_url="http://qt.gtimg.cn/q="
        self.url=init_url+self.ty+str(self.code)
    def get_info(self):
        try:
            self.re_obj=requests.get(self.url)
            self.tr_info()
            return self.re_obj
        except:
           return False
    def tr_info(self):
        d=self.re_obj.content
        li=d.split(b"~")
        self.name=li[1] #编码问题 无法str
        self.now=li[3]
        self.yestday=li[4]
        self.start=li[5]
        self.ud=li[31] #涨跌
        self.rate=li[32] #涨跌率
        self.top=li[33]
        self.down=li[34]
        self.buy_n=li[36]
        self.buy_m=li[37] #万元
        self.change=li[38] #成交率

TIPS

每次请求大概599B到617B数据之间,约等于0.65KB一次,可以假定1KB一次访问请求

NTP校时间及本地化时间 micropython

import ntptime,time
#本地话时间 东八区
def datetime():
    date=time.localtime(time.time()+28800)
    #(2024, 4, 29, 9, 58, 14, 0, 120) 年 月 日 时 分 秒 周 全年的第几天
    return date



# 定义NTP服务器列表
ntp_servers = [
    'ntp.ntsc.ac.cn',
    'ntp1.aliyun.com',
    'ntp.tencent.com',
    'pool.ntp.org'
]

#NTP同步
def ntp(ntp_servers):
    for server in ntp_servers:
        try:
            # 尝试从当前服务器同步时间
            ntptime.host = server
            ntptime.settime()
            print("时间已从 {} 同步".format(server))
            return
        except Exception as e:
            print("从 {} 同步时间失败: {}".format(server, e))
            continue
    print("无法从任何服务器同步时间")

ntp(ntp_servers)