micropython自行编译可直接运行的固件

本文介绍如何通过修改MicroPython源代码,尤其是ESP8266平台的main.c文件,来定制启动行为,实现将特定的Python脚本作为固件启动时直接运行的功能,避免反复上传main.py。

micropython+ESP8266直接做些小玩具是很简单的。但是如果每次都要上传mian.py就很不理想了

而不是编译出一个通用的玩意,而是一个直接启动功能的固件,需要自行编译micropython,然后改改mian.c

main.py is a proper place to put your start code in. That resides in the flash file system. If you prefer to have it in the scripts folder, which moves into the frozen files space, then you have to change esp8266/main.c and rebuild.
But bothing prevents you from putting your startup code in _boot.py. If you look into main.c, you'll see:
CODE: SELECT ALL

#if MICROPY_MODULE_FROZEN
    pyexec_frozen_module("_boot.py");
    pyexec_file("boot.py");
    pyexec_file("main.py");
#endif
which means, that _boot.py, boot.py and main.py are simply executed one after another.

B.T.W.: I'm not sure whether boot.py and main.py should only be execute whan MICROPY_MODULE_FROZEN is set. Maybe that's just a leftover from an earlier development phase.
If you are compiling your own firmware this was the solution offered to me and I think the most useable at the moment.

https://forum.micropython.org/viewtopic.php?f=15&t=2114&e=1&view=unread#unread

Basically you just put the code you want executed into the scripts/ folder and edit main.c to add:

pyexec_frozen_module("myscript.py");

或者改 _boot.py

Thanks for that, which pointed me to this solution. I modified the _boot.py in my project's frozen directory to read
CODE: SELECT ALL

# Existing code omitted up to here:
    inisetup.setup()

try:
    uos.stat('/main.py')
except OSError:
    with open("/main.py", "w") as f:
        f.write("""\
import mqtt
""")

gc.collect()
Erase the flash before installing the firmware.

 再或者给bin加一段。。。

You can also drop a dos-fs image (must have a sector size=4096) at offset 561152. This image can contain boot.py and whatever other Python or data files you need.

That's what I'm doing.

But this is really not well documented, and I suspect that this offset may be subject to future change.

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值