MicroPython 介绍
现在开源硬件中最热门的 MicroPython 是英国剑桥大学的教授 Damien George(达米安·乔治)所发明,微控制器上实现了Python 3的基本功能,拥有完善的解析器、编译器、虚拟机和类库等。在保留了python语言主要特性的基础上,他还对嵌入式系统的底层做了非常不错的封装,将常用功能都封装到库中,为一些常用的传感器和硬件编写了专门的驱动。我们使用时只需要通过调用这些库和函数,就可以快速控制LED、液晶、舵机、多种传感器、SD、UART、I2C等,实现各种功能,而不用再去研究底层模块的使用方法。这样不但降低了开发难度,而且减少了重复开发工作,可以加快开发速度,提高了开发效率。以前需要较高水平的嵌入式工程师花费数天甚至数周才能完成的功能,现在普通的嵌入式开发者用几个小时就能实现类似的功能,而且要更加轻松和简单。Python作为一种高级语言,可以解释执行;这意味着其可以逐条执行,随时查看变量信息,这对在单片机上的调试非常方便。
单片机逻辑控制越来越复杂,变化也越来越多,高级的脚本语言无疑开发成本更低、迭代效率更高。单片机的性能越来越高,在做逻辑控制时不再斤斤计较于一点性能。
### # MicroPython示例代码目录
* 01.Hello-World | 快速了解MicroPython编程基本方法及REPL使用方法
* 02.外设使用基础案例 | 通过一些案例快速了解BPI-Leaf-S3外设功能的基本使用方法
[00.点亮板载全彩LED灯珠](/Example/MicroPython-zh/02.Use_Peripherals/00.blink_NeoPixel_LED)
[01.全彩LED灯珠循环显示彩虹色](/Example/MicroPython-zh/02.Use_Peripherals/01.rainbow_NeoPixel_LED)
[02.PWM单色LED呼吸灯 | PWM驱动电机 TB6612FNG模块](/Example/MicroPython-zh/02.Use_Peripherals/02.PWM)
[03.ADC 读取电位器的电压值](/Example/MicroPython-zh/02.Use_Peripherals/03.ADC)
[04.UART 串口数据读写](/Example/MicroPython-zh/02.Use_Peripherals/04.UART)
[05.SPI MAX7219 8X8LED点阵显示](/Example/MicroPython-zh/02.Use_Peripherals/05.SPI_MAX7219)
[06.DHT11](/Example/MicroPython-zh/02.Use_Peripherals/06.DHT11)
[[07.I2C_OLED_SSD1306](https:
* 03.综合使用案例 | 一些涉及多个外设及功能的综合使用案例
[01.使用ADC测量电位器,用以调整PWM,控制电机转速](/Example/MicroPython-zh/03.Comprehensive_Case/01.ADC+PWM)
快速了解MicroPython编程基本方法及REPL使用方法
基于Thonny IDE,开始菜单栏里找到它。
我们可以从输出一段“Hello World”文字开始,以此作为了解和学习MicroPython的第一步。
使用REPL
REPL即Read-Eval-Print-Loop的缩写名词,译为 读取-求值-输出-循环 。
我们可以通过实际操作来明白它的意思。
将已经安装了MicroPython固件的开发板连接电脑,运行Thonny IDE并正确配置后,在Shell窗口中将出现这样的文本内容:
MicroPython v1.19.1-dirty on 2022-08-17; BPI-Leaf-S3 with ESP32-S3
Type "help()" for more information.
>>>
注意最后一行的>>>
提示符,我们可以直接在这后面输入算式或是代码,按下键盘enter
回车键就会立即在下一行得到输出结果。python
> > > 1+2
> > >
> > > 3
> > >
> > > print("Hello World")
> > >
> > > Hello World
现在可以很直观的理解了,它会读取我们输入的信息,执行运算求值,输出结果,然后等待我们后续的输入,一直循环这个过程,这也是REPL又被译为交互式解释器的原因,我们可以直接通过输入代码来和硬件交互,没有像传统的C语言那样需要在中间执行编译的过程,我们输入的信息没有经过编译就传输给芯片自行解释并运行了,这本是Python语言的一大重要特性,MicroPython完美继承了它。
如果仅仅是使用MicroPython REPL,很多具有串口信息收发功能的软件都可以操作,感兴趣的话可以试试各种串口工具,这可以令人更深刻的理解 “没有中间执行编译的过程” 的意思。
关于REPL的应用,更详尽全面的内容可以参考MicroPython文档:REPL
代码编辑器
Thonny IDE当然不仅仅可以进行REPL的操作,作为python代码编辑器,本职功能还是有的。
新建一个文件并在其编辑区内输入代码。python
print(1+2)
print("Hello World")
完成代码编辑后,点击 保存 ,可以选择将文件保存到MicroPython设备中,这将直接将整个文件的数据传输到flash中。可将文件命名为main.py
,设备会在每次上电或复位后执行有这个文件名的程序,而其他名称的文件仅在被main.py
调用时或是我们在Thonny中点击运行时被执行。
现在点击 运行 ,同样是无需编译的,在Shell中会立即得到结果。
3
Hello World
另外也可以尝试REPL的键盘控制快捷键ctrl+D软件复位,可以看到复位后程序立即执行并打印出信息。
帮助信息
>>> help()
Welcome to MicroPython on the ESP32!
For generic online docs please visit http:
For access to the hardware use the 'machine' module:
import machine
pin12 = machine.Pin(12, machine.Pin.OUT)
pin12.value(1)
pin13 = machine.Pin(13, machine.Pin.IN, machine.Pin.PULL_UP)
print(pin13.value())
i2c = machine.I2C(scl=machine.Pin(21), sda=machine.Pin(22))
i2c.scan()
i2c.writeto(addr, b'1234')
i2c.readfrom(addr, 4)
Basic WiFi configuration:
import network
sta_if = network.WLAN(network.STA_IF); sta_if.active(True)
sta_if.scan() # Scan for available access points
sta_if.connect("<AP_name>", "<password>") # Connect to an AP
sta_if.isconnected() # Check for successful connection
Control commands:
CTRL-A -- on a blank line, enter raw REPL mode
CTRL-B -- on a blank line, enter normal REPL mode
CTRL-C -- interrupt a running program
CTRL-D -- on a blank line, do a soft reset of the board
CTRL-E -- on a blank line, enter paste mode
For further help on a specific object, type help(obj)
For a list of available modules, type help('modules')
>>>
中断正在运行程序快捷键ctrl+c
运行help('modules')可获得的安装的模块
>>> help('modules')
__main__ framebuf uasyncio/stream uplatform
_boot gc ubinascii urandom
_onewire inisetup ubluetooth ure
_thread math ucollections uselect
_uasyncio micropython ucryptolib usocket
_webrepl neopixel uctypes ussl
apa106 network uerrno ustruct
btree ntptime uhashlib usys
builtins onewire uheapq utime
cmath uarray uio utimeq
dht uasyncio/__init__ ujson uwebsocket
ds18x20 uasyncio/core umachine uzlib
esp uasyncio/event uos webrepl
esp32 uasyncio/funcs upip webrepl_setup
flashbdev uasyncio/lock upip_utarfile websocket_helper
Plus any modules on the filesystem
根据资料,此板先运行boot .py再运行main.py。
准备在boot .py中加配置wifi-sta和GPIO48上的彩灯NeoPixel和SSD1306_I2C。
import esp
esp.osdebug(0)
from time import sleep,time
from machine import I2C,Pin,RTC,unique_id
from neopixel import NeoPixel
import network
from ssd1306 import SSD1306_I2C
from gc import mem_free
sda_pin=Pin(15,Pin.PULL_UP)
scl_pin=Pin(16,Pin.PULL_UP)
i2c = I2C(1,sda=sda_pin, scl=scl_pin, freq=700_000)
a=i2c.scan()
print('i2c.scan()',a)
print("\nstr(mem_free1: %s Byte" % str(mem_free()))
oled = SSD1306_I2C(128, 64, i2c, addr=0x3c)
rtc = RTC()
print('rtc.datetime',rtc.datetime())
pin_48 = Pin(48, Pin.OUT)
np = NeoPixel(pin_48, 1,bpp=3, timing=1)
np[0] = (25,9,0)
np.write()
a = (0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xF0,\
0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x0C,0x7C,0xF0,0x80,\
0x00,0x00,0x00,0xC0,0xF8,0x3C,0xFC,0xF0,\
0x80,0x00,0x00,0x80,0xF0,0x7C,0x0C,0x00,\
0xC0,0xF0,0xB8,0x98,0x8C,0x8C,0x8C,0x8C,\
0x8C,0x98,0xF0,0xE0,0x00,0x00,0x00,0xFF,\
0xFF,0x00,0x00,0x00,0xC0,0xF0,0x38,0x18,\
0x0C,0x0C,0x0C,0x0C,0x0C,0x18,0x00,0x00,\
0xC0,0xF0,0x38,0x18,0x0C,0x0C,0x0C,0x0C,\
0x1C,0x38,0xF0,0xE0,0x00,0x00,0x00,0x00,\
0xFC,0xFC,0x30,0x08,0x0C,0x0C,0x0C,0x1C,\
0xF8,0xF0,0x38,0x18,0x0C,0x0C,0x0C,0x1C,\
0xF8,0xF0,0x00,0x00,0x00,0xC0,0xF0,0xB8,\
0x98,0x8C,0x8C,0x8C,0x8C,0x8C,0x98,0xF0,\
0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x1F,\
0x78,0x78,0x3E,0x07,0x00,0x00,0x00,0x07,\
0x3F,0x78,0x78,0x1F,0x03,0x00,0x00,0x00,\
0x07,0x1F,0x39,0x71,0x61,0x61,0x61,0x61,\
0x61,0x71,0x31,0x01,0x00,0x00,0x00,0x7F,\
0x7F,0x00,0x00,0x00,0x07,0x1F,0x38,0x30,\
0x60,0x60,0x60,0x60,0x60,0x30,0x00,0x00,\
0x07,0x1F,0x38,0x70,0x60,0x60,0x60,0x60,\
0x70,0x38,0x1F,0x07,0x00,0x00,0x00,0x00,\
0x7F,0x7F,0x00,0x00,0x00,0x00,0x00,0x00,\
0x7F,0x7F,0x00,0x00,0x00,0x00,0x00,0x00,\
0x7F,0x7F,0x00,0x00,0x00,0x07,0x1F,0x39,\
0x71,0x61,0x61,0x61,0x61,0x61,0x71,0x31,\
0x01,0x00,0x00,0x70,0x70,0x70,0x00,0x00,\
0x00,0x70,0x70,0x70,0x00,0x00,0x00,0x70,\
0x70,0x70,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00)
x = 0
y = 0
for v in a:
for i in range(8):
if (v & (1<<i)):
oled.pixel(x,y,1)
else:
oled.pixel(x,y,0)
i += 1
x += 8
if ((x//128) > 0):
x = 0
y += 1
oled.text('Welcome', 20, 20, 1)
oled.show()
oled.rotate(False)
wlan = network.WLAN(network.STA_IF); wlan.active(True)
print(rtc.datetime())
a=wlan.scan() # Scan for available access points
print("wlan.scan() ", rtc.datetime())
c=0;f=0
gp=[0 for x in range(30)]
np[0] = (0,25,0)
np.write()
print(ps, rtc.datetime())
f=0
np[0] = (0,0,25)
np.write()
for b in a:
print(f," is ",b[0]," is ",gp[f])
if b"Dream" in b[0]:
wlan.connect(b[0], 'mlf19721123') # Connect to an AP
f=f+1
sleep(0.1)
print("Dream", rtc.datetime())
print(wlan.isconnected() ) # Check for successful connection
print(wlan.config('mac'))
a=wlan.ifconfig()
print(a)
oled.text(a[0], 05, 0, 1)
oled.show()
import webrepl
webrepl.start()
np[0] = (2,2,2)
np.write()
print("webrepl.start()", rtc.datetime())
unique_id = unique_id()
id_str = "{:.02x}{:.02x}{:.02x}{:.02x}{:.02x}{:.02x}".format(unique_id[0],unique_id[1],unique_id[2],unique_id[3],unique_id[4],unique_id[5])
print("client_id:"+id_str+"@gwc8f0")
****板正常,达到预期状态。环境搭建并初步测试 结束