[Diaoye learns programming] MicroPython manual WiPy specific port library wipy.heartbeat()

Insert image description here

MicroPython is a lightweight version of the interpreter designed for running the Python 3 programming language in embedded systems. Compared with regular Python, the MicroPython interpreter is small (only about 100KB) and is compiled into a binary Executable file to run, resulting in higher execution efficiency. It uses a lightweight garbage collection mechanism and removes most of the Python standard library to accommodate resource-constrained microcontrollers.

The main features of MicroPython include:
1. The syntax and functions are compatible with standard Python, making it easy to learn and use. Supports most core syntax of Python.
2. Directly access and control hardware, control GPIO, I2C, SPI, etc. like Arduino.
3. Powerful module system, providing file system, network, graphical interface and other functions.
4. Support cross-compilation to generate efficient native code, which is 10-100 times faster than the interpreter.
5. The amount of code is small and the memory usage is small. It is suitable for running on MCU and development boards with small memory.
6. Open source license, free to use. The Shell interactive environment provides convenience for development and testing.
7. The built-in I/O driver supports a large number of microcontroller platforms, such as ESP8266, ESP32, STM32, micro:bit, control board and PyBoard, etc. There is an active community.

MicroPython application scenarios include:
1. Rapidly build prototypes and user interactions for embedded products.
2. Make some small programmable hardware projects.
3. As an educational tool, it helps beginners learn Python and IoT programming.
4. Build smart device firmware to achieve advanced control and cloud connectivity.
5. Various microcontroller applications such as Internet of Things, embedded intelligence, robots, etc.

Things to note when using MicroPython:
1. Memory and Flash space are limited.
2. The explanation and execution efficiency is not as good as C language.
3. Some library functions are different from the standard version.
4. Optimize the syntax for the platform and correct the differences with standard Python.
5. Use memory resources rationally and avoid frequently allocating large memory blocks.
6. Use native code to improve the performance of speed-critical parts.
7. Use abstraction appropriately to encapsulate underlying hardware operations.

Generally speaking, MicroPython brings Python into the field of microcontrollers, which is an important innovation that not only lowers the programming threshold but also provides good hardware control capabilities. It is very suitable for the development of various types of Internet of Things and intelligent hardware.
Insert image description here

MicroPython is a streamlined implementation of the Python language for microcontrollers that allows developers to write embedded applications using Python syntax and standard libraries. WiPy is a wireless development board based on ESP32 chip, which supports running MicroPython firmware. MicroPython WiPy-specific libraries are a set of modules designed for WiPy that provide access to WiPy-specific functionality and hardware interfaces. The main features of MicroPython's WiPy-specific libraries are as follows:

1. It contains some WiPy-related modules, such as machine, network, pycom, uos, etc., which respectively provide WiPy machine-level control, network connection, lighting control, operating system interface and other functions.
2. It also contains some common MicroPython modules, such as binascii, hashlib, math, random, struct, time, etc., which provide some commonly used data processing, encryption, mathematics, random numbers, data structures, time and other functions.
3. It follows the design principles of MicroPython, which are simplicity, efficiency, portability and compatibility.

Insert image description here

MicroPython's wipy.heartbeat() is a function used to control the heartbeat LED on the WiPy development board. It can get or set the status of the heartbeat LED (on or off).

The main features of wipy.heartbeat() are as follows:

It is part of the wipy module, which contains access to some WiPy-specific functions and hardware interfaces.
It accepts and returns a Boolean value (True or False) indicating the status of the heartbeat LED.
It can be called at any time, whether WiPy is connected to a network or other device.

The application scenarios of wipy.heartbeat() are as follows:

The heartbeat LED on the WiPy development board is an indicator light used to indicate whether WiPy is working normally. It flashes once every 5 seconds by default. If developers want to turn off the heartbeat LED to save power or avoid interference, they can use wipy.heartbeat(False) to disable it.
If developers want to control the color and brightness of the heartbeat LED themselves, they can use wipy.heartbeat(False) to disable it, and then use pycom.rgbled(value) to set the LED value, where value is a 24-bit RGB value.
If developers want to change the status of the heartbeat LED based on different conditions, they can use wipy.heartbeat() to get the current status, and then use wipy.heartbeat(True) or wipy.heartbeat(False) to turn it on or off as needed. it. For example, the state of the heartbeat LED can be changed based on the status of the network connection or changes in sensor data.

Things to note about wipy.heartbeat() are as follows:

Before using wipy.heartbeat(), you need to import the wipy module first, using the import wipy statement.
When using wipy.heartbeat(), you need to be careful not to conflict with other modules or devices. For example, if you use machine.Pin('P25') to control other devices, you cannot use wipy.heartbeat() to control the heartbeat LED at the same time.
When using wipy.heartbeat(), you need to note that the status of the heartbeat LED may affect the operation and debugging of the WiPy development board. For example, if the heartbeat LED is turned off, you cannot use it to determine whether WiPy is working normally or whether it has entered sleep mode.

Here is the code for three practical applications:

Case 1: Use wipy.heartbeat(False) to turn off the heartbeat LED. :

# 导入 wipy 模块
import wipy

# 关闭心跳 LED
wipy.heartbeat(False)

Case 2: Use wipy.heartbeat(False) and pycom.rgbled(value) to control the color and brightness of the heartbeat LED. :

# 导入 wipy 和 pycom 模块
import wipy
import pycom

# 关闭心跳 LED
wipy.heartbeat(False)

# 设置心跳 LED 的颜色为红色
pycom.rgbled(0xff0000)

# 设置心跳 LED 的亮度为低
pycom.rgbled(0x101010)

Case 3: Use wipy.heartbeat() and the network module to change the status of the heartbeat LED based on the status of the network connection.

# 导入 wipy 和 network 模块
import wipy
import network

# 创建 WLAN 对象并连接到网络
wlan = network.WLAN(mode=network.WLAN.STA)
wlan.connect("your_ssid", auth=(network.WLAN.WPA2, "your_password"))

# 循环检查网络连接的状态并改变心跳 LED 的状态
while True:
    # 如果网络已经连接,则开启心跳 LED
    if wlan.isconnected():
        # 如果心跳 LED 当前是关闭的,则开启它
        if not wipy.heartbeat():
            wipy.heartbeat(True)
    # 如果网络没有连接,则关闭心跳 LED
    else:
        # 如果心跳 LED 当前是开启的,则关闭它
        if wipy.heartbeat():
            wipy.heartbeat(False)

Case 4: Hardware status indicator:

import wipy

# 设置心跳LED引脚
heartbeat_pin = 2

# 配置心跳LED
wipy.heartbeat(pin=heartbeat_pin, frequency=2)

# 其他代码...

In this example, we use the wipy.heartbeat() function to configure a heartbeat LED, which is connected to pin 2. The frequency is set to 2 in the configuration, which means the LED will flash at a frequency of 2 times per second. You can choose the appropriate pins and frequency according to your needs.

Case 5: Customized heartbeat indication::

import wipy
import time

# 自定义心跳指示函数
def custom_heartbeat():
    while True:
        # 打开LED
        wipy.heartbeat(on=True)
        time.sleep(0.5)
        
        # 关闭LED
        wipy.heartbeat(on=False)
        time.sleep(0.5)

# 启动自定义心跳指示
custom_heartbeat()

In this example, we define a custom_heartbeat() function to implement custom heartbeat indication. The wipy.heartbeat() function is used in the function to turn the LED on and off respectively, and then the time.sleep() function is used to set the appropriate delay to control the LED flashing frequency. By calling the custom_heartbeat() function, we can start a custom heartbeat indicator.

Case 6: Turn off heartbeat indication::

import wipy

# 关闭心跳指示
wipy.heartbeat(enabled=False)

# 其他代码...

In this example, we use the enabled parameter of the wipy.heartbeat() function to disable the heartbeat indication function. We can turn off the heartbeat indication by setting enabled to False, which will stop any heartbeat-related LED flashing or indication. These examples show how to use MicroPython's wipy.heartbeat() function for practical applications such as hardware status indication, custom heartbeat indication, and turning off heartbeat indication. Please make appropriate adjustments and extensions according to specific needs.

Case 7: Enable the always-on heartbeat light:

import wipy

# 启用常亮心跳灯
wipy.heartbeat(True)

This sample program shows how to use the wipy.heartbeat() function to set the heartbeat LED indicator of the WiPy development board to always on. By setting the parameter to True, the heartbeat light will remain on.

Case 8: Using heartbeat light:

import wipy

# 禁用心跳灯
wipy.heartbeat(False)

This sample program shows how to use the wipy.heartbeat() function to disable the WiPy development board's heartbeat LED indicator. By setting the parameter to False, the heartbeat light will be turned off and no longer blink or light up.

Case 9: Customized heartbeat light mode:

import wipy
import time

# 自定义心跳灯模式
for _ in range(5):
    wipy.heartbeat(True)
    time.sleep(0.5)
    wipy.heartbeat(False)
    time.sleep(0.5)

This sample program shows how to use the wipy.heartbeat() function to customize the heartbeat LED indicator mode of the WiPy development board. In this example, the code segment in the loop alternately sets the heartbeat light on and off, and controls the duration of each state through the time.sleep() function.

These sample programs show how to use the wipy.heartbeat() function to control the behavior of the heartbeat LED on the WiPy development board. You can use this function to adjust the status of the heartbeat light or customize the mode of the heartbeat light according to actual needs.

Insert image description here

Guess you like

Origin blog.csdn.net/weixin_41659040/article/details/132962481