ID.LODA 发表于 2024-5-23 15:01

【FireBeetle 2 ESP32 C6 开发板】+ 测试使用板载 core modules

<div class='showpostmsg'> 本帖最后由 ID.LODA 于 2024-5-23 15:05 编辑

# Core Modules 的测试、使用

记上一章使用 Web Workflow 之后开发稳定性提高很多,今天来学习下 circuitpython core modules 的使用

## 查看板载支持的 modules
可以在 repl 或者 code.py 中输入 ``help("modules")`` 即可查看



## microcontroller

处理器模块的接口介绍可以查看以下章节 (https://docs.circuitpython.org/en/latest/shared-bindings/microcontroller/index.html),包含了 unique id、cpu、frequency、temperature 等参数的获取,以及终端复位等的控制

### 示例演示
实现了 cpu 基础信息的获取

```python
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

"""
This test will initialize the display using displayio and draw a solid green
background, a smaller purple rectangle, and some yellow text.
"""
import os
import time
import board
import digitalio
import microcontroller


led = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUT

cpu = microcontroller.cpu
print("cpu uid: ", )
print("cpu frequency: ", cpu.frequency)
print("cpu temperature: ", cpu.temperature)
print("cpu voltage: ", cpu.voltage)
print("cpu reset reason: ", cpu.reset_reason)

while True:
    time.sleep(0.5)
    led.value = not led.value

microcontroller.reset()
```

#### 结果展示


## WIFI

wifi 模块的接口介绍可以查看以下章节 (https://docs.circuitpython.org/en/latest/shared-bindings/wifi/index.html),该模块为管理 wifi 连接提供了必要的底层功能,以及使用套接字池通过网络进行通信。

### 基础示例
实现了 WIFI 的连接,socket pool 的创建,域名解析以及 ping 包的实现

#### 示例代码
介于我在 settings.toml 里设置了 `CIRCUITPY_WIFI_SSID`、`CIRCUITPY_WIFI_PASSWORD`,通过 os.getenv 可以很方便的获取对应的数据

```python
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

"""
This test will initialize the display using displayio and draw a solid green
background, a smaller purple rectangle, and some yellow text.
"""
import os
import time
import board
import digitalio
import socketpool
import wifi
import ipaddress


led = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUT


# code handler
while not wifi.radio.ipv4_address:
    try:
      print("Connecting to WiFi")
      #connect to your SSID
      wifi.radio.connect(os.getenv('CIRCUITPY_WIFI_SSID'), os.getenv('CIRCUITPY_WIFI_PASSWORD'))
    except ConnectionError as e:
      print("connect error:{}, retry in 2s".format(e))
    time.sleep(2)

pool = socketpool.SocketPool(wifi.radio)

#prints IP address to REPL
print("My IP address is", wifi.radio.ipv4_address)

#prints MAC address to REPL
print("My MAC addr:", )

#pings eeworld
ip_address = pool.getaddrinfo("eeworld.com.cn", 80)[-1]
ipv4 = ipaddress.ip_address(ip_address)
print("Ping eeworld.com: %f ms" % (wifi.radio.ping(ipv4)*1000))


while True:
    time.sleep(0.5)
    led.value = not led.value
```

#### 运行结果


### http 请求示例
进行 http 请求依赖第三方库,可以在 (https://circuitpython.org/libraries) 根据对应的版本下载示例包,我这边使用的是 9.x,所以下载的 9.x 版本



#### 导入库
依赖 adafruit_connection_manager.mpy、adafruit_requests.mpy 库,将其导入 lib 目录即可



#### 示例代码
通过请求 https://www.adafruit.com/api/quotes.php,获取网页的内容,并打印

```python
# SPDX-FileCopyrightText: 2022 Liz Clark for Adafruit Industries
#
# SPDX-License-Identifier: MIT

import os
import time
import ssl
import wifi
import socketpool
import microcontroller
import adafruit_requests

#adafruit quotes URL
quotes_url = "https://www.adafruit.com/api/quotes.php"

#connect to SSID
wifi.radio.connect(os.getenv('CIRCUITPY_WIFI_SSID'), os.getenv('CIRCUITPY_WIFI_PASSWORD'))

pool = socketpool.SocketPool(wifi.radio)
requests = adafruit_requests.Session(pool, ssl.create_default_context())

while True:
    try:
      #pings adafruit quotes
      print("Fetching text from %s" % quotes_url)
      #gets the quote from adafruit quotes
      response = requests.get(quotes_url)
      print("-" * 40)
      #prints the response to the REPL
      print("Text Response: ", response.text)
      print("-" * 40)
      response.close()
      #delays for 1 minute
      time.sleep(60)
    # pylint: disable=broad-except
    except Exception as e:
      print("Error:\n", str(e))
      print("Resetting microcontroller in 10 seconds")
      time.sleep(10)
      microcontroller.reset()
```

#### 运行结果



# 总结
circuitpython 内置了很多功能模块,官网也提供了对应的接口文档,相对还是比较上手使用
</div><script>                                        var loginstr = '<div class="locked">查看本帖全部内容,请<a href="javascript:;"   style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
                                       
                                        if(parseInt(discuz_uid)==0){
                                               
                                        }                </script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script>
页: [1]
查看完整版本: 【FireBeetle 2 ESP32 C6 开发板】+ 测试使用板载 core modules