[Diaoye learns programming] MicroPython manual esp8266 esp32 specific port library esp.deepsleep()

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's esp refers to the MicroPython firmware and related software libraries for ESP8266 and ESP32 chips. ESP8266 and ESP32 are a class of low-cost, low-power Wi-Fi and Bluetooth modules widely used in IoT and embedded systems. MicroPython's esp provides an advanced scripting environment for both chips, allowing developers to use the Python language for rapid prototyping and development.

ESP8266: It is a low-cost, low-power Wi-Fi module/chip developed by Espressif Systems. It has a built-in TCP/IP protocol stack, can be used to connect to the Internet, and has strong processing capabilities. MicroPython's esp provides firmware and related software libraries for ESP8266, allowing developers to use MicroPython language to develop ESP8266 applications.

ESP32: It is a highly integrated Wi-Fi and Bluetooth module/chip launched by Espressif Systems. Compared with ESP8266, it has more powerful processing power, more peripheral interfaces and more memory. MicroPython's esp also provides firmware and related software libraries for ESP32, allowing developers to use MicroPython language to develop ESP32 applications.

MicroPython's esp firmware: It is a MicroPython firmware version specifically for ESP8266 and ESP32 chips. These firmwares have been specifically optimized to run on ESP8266 and ESP32, and provide APIs for hardware interaction, network communication, and peripheral control.

Software libraries: MicroPython's esp also provides a series of software libraries related to ESP8266 and ESP32 hardware to simplify and accelerate the development process. These software libraries provide a rich set of functional interfaces, covering commonly used hardware and communication protocols such as Wi-Fi, Bluetooth, GPIO (General Purpose Input and Output), I2C, SPI, and PWM, allowing developers to easily access and control hardware resources.
Insert image description here

MicroPython's esp.deepsleep() is a function used to put the ESP8266 chip into deep sleep mode. Deep sleep mode is a minimum power state that reduces the chip's current consumption to microamp levels.

The main features of esp.deepsleep() are:

It is a simple and convenient deep sleep mode object operation method. You can use esp.deepsleep() to make the chip enter deep sleep mode without specifying any parameters.
It is a flexible and powerful deep sleep mode object control method. You can set the wake-up time of the chip in deep sleep mode by passing in a time parameter, in microseconds. If no time parameter is passed in, the chip will remain in deep sleep mode until awakened by an external interrupt or reset signal.
It is a compatible and extensible deep sleep mode object interface. You can use other modules or functions to access or modify the properties and methods of the deep sleep mode object. You can use the machine.deepsleep() method to use deep sleep on ESP32. model.

The application scenarios of esp.deepsleep() are:

It is used to implement low-power operation of the chip, such as selecting an appropriate wake-up time to reduce the power consumption of the chip and extend battery life when frequent communication or data processing is not required.
It is used to implement the timing tasks of the chip, such as setting an appropriate wake-up time to ensure the function and efficiency of the chip when data needs to be collected or sent regularly.
It is used to realize flexible switching of the chip, such as dynamically adjusting the wake-up time as needed in different application scenarios to adapt to different needs and conditions.

Things to note about esp.deepsleep() are:

Before using esp.deepsleep(), you need to ensure that the ESP8266 development board has been correctly connected to the power supply and computer, and that the MicroPython firmware has been installed.
When using esp.deepsleep(), you need to pay attention to the voltage range and current limit of the chip to avoid hardware damage or failure.
When using esp.deepsleep(), you need to pay attention to coordination and compatibility issues with other modules or functions to avoid functional conflicts or interference. For example, if you use the esp.deepsleep() method, you cannot use the REPL function at the same time; if you use the machine.deepsleep() method, you cannot use the esp module at the same time.

The following are several practical application examples of using esp.deepsleep():

Case 1: Use esp.deepsleep() to make the chip enter deep sleep mode and wake up automatically after 10 seconds. code show as below:

import esp # 导入 esp 模块
import machine # 导入 machine 模块
import time # 导入 time 模块
led = machine.Pin(2, machine.Pin.OUT) # 创建一个输出引脚对象
led.value(1) # 点亮 LED
time.sleep(1) # 等待 1 秒
led.value(0) # 熄灭 LED
time.sleep(1) # 等待 1 秒
print('Going to sleep for 10 seconds') # 打印提示信息
esp.deepsleep(10000000) # 进入深度睡眠模式,并在 10 秒后唤醒

This program can put the chip into deep sleep mode and wake up automatically after 10 seconds. You can use other methods to check or verify this process.

Case 2: Use esp.deepsleep() to make the chip enter deep sleep mode and wake up on external interrupt signal. code show as below:

import esp # 导入 esp 模块
import machine # 导入 machine 模块
import time # 导入 time 模块
led = machine.Pin(2, machine.Pin.OUT) # 创建一个输出引脚对象
button = machine.Pin(0, machine.Pin.IN, machine.Pin.PULL_UP) # 创建一个输入引脚对象
led.value(1) # 点亮 LED
time.sleep(1) # 等待 1 秒
led.value(0) # 熄灭 LED
time.sleep(1) # 等待 1 秒
print('Going to sleep until button is pressed') # 打印提示信息
esp.deepsleep() # 进入深度睡眠模式,并在外部中断信号唤醒

This program can put the chip into deep sleep mode and wake up on external interrupt signal. You can use other methods to check or verify this process.

Case 3: Use esp.deepsleep() to make the chip enter deep sleep mode and wake up when the reset button is pressed. code show as below:

import esp # 导入 esp 模块
import machine # 导入 machine 模块
import time # 导入 time 模块
led = machine.Pin(2, machine.Pin.OUT) # 创建一个输出引脚对象
led.value(1) # 点亮 LED
time.sleep(1) # 等待 1 秒
led.value(0) # 熄灭 LED
time.sleep(1) # 等待 1 秒
print('Going to sleep until reset button is pressed') # 打印提示信息
esp.deepsleep() # 进入深度睡眠模式,并在复位按钮按下时唤醒

This program can put the chip into deep sleep mode and wake up when the reset button is pressed. You can use other methods to check or verify this process.

Case 4: Periodic wake-up:

import esp

# 设置唤醒时间为10秒
wakeup_time = 10000

# 进入深度睡眠并设置唤醒时间
esp.deepsleep(wakeup_time)

In this example, we use the esp.deepsleep() function to put the MicroPython device into deep sleep mode. We set the wake-up time to 10 seconds (10000 milliseconds), then the device will go to sleep and wake up automatically after the specified time.

Case 5: External trigger wake-up:

import esp
from machine import Pin

# 设置唤醒引脚
wake_pin = Pin(2, Pin.IN)

# 进入深度睡眠并设置唤醒引脚
esp.deepsleep(0, wake=Pin.WAKE_LOW, enable_pull=True)

# 在这里添加您需要执行的代码,当设备被唤醒时将执行该代码

In this example, we use the esp.deepsleep() function to put the MicroPython device into deep sleep mode. We set the wake-up pin to pin 2 and pass it to the esp.deepsleep() function. The device will enter deep sleep and wake up automatically when the wake pin is triggered. You can add the code you want to execute when the device wakes up in the code block in the comments section.

Case 6: Low power consumption application:

import esp

# 进入深度睡眠并关闭所有外设
esp.deepsleep(0, True)

In this example, we use the esp.deepsleep() function to put the MicroPython device into deep sleep mode. We pass parameter 0, which means no wake-up time is set. By setting the second parameter to True, we can turn off all peripherals to reduce power consumption. The device will enter a deep sleep state and resume upon external conditions or manual wakeup.

Case 7: Periodic sleep wake-up:

import esp

# 设置休眠时间为10秒
sleep_time = 10000

# 执行一些任务...

# 进入深度睡眠模式,并设置唤醒时间
esp.deepsleep(sleep_time)

In this example, we set the sleep time to 10 seconds (in milliseconds). After performing some tasks, enter deep sleep mode by calling the esp.deepsleep() function and set the wake-up time to 10 seconds. During sleep, the chip will enter a low-power state until the wake-up time arrives.

Case 8: External pin wake-up:

import esp
from machine import Pin

# 设置唤醒引脚
wake_pin = Pin(4, Pin.IN)

# 进入深度睡眠模式,并配置外部引脚唤醒
esp.deepsleep(0, wake=Pin.IRQ_RISING, wake_pin=wake_pin)

In this example, we set up an external pin (Pin 4) as the wake-up pin. By setting the pin to input mode (default is high-impedance input), we can use the esp.deepsleep() function to enter deep sleep mode and configure the external pin's wake-up mode to rising edge trigger (Pin.IRQ_RISING). This way, the chip will wake up when the level on the pin changes from low to high.

Case 9: Use RTC to wake up:

import esp
import machine

# 获取RTC对象
rtc = machine.RTC()

# 设置唤醒时间为10秒
wake_time = 10

# 配置RTC唤醒
rtc.irq(trigger=rtc.ALARM0, wake=machine.DEEPSLEEP)

# 设置RTC闹钟触发时间
rtc.alarm(rtc.ALARM0, wake_time * 1000)

# 进入深度睡眠模式
esp.deepsleep()

In this example, we use RTC (Real Time Clock) to implement the wake-up function. First, we get the RTC object and set the wake-up time to 10 seconds. Then, by calling the rtc.irq() function, configure the interrupt triggering mode of RTC to ALARM0, and set the wake-up mode to deep sleep (machine.DEEPSLEEP). Next, we use the rtc.alarm() function to set the trigger time of the RTC alarm clock to 10 seconds. Finally, enter deep sleep mode by calling the esp.deepsleep() function. When the wake-up time arrives, the chip will be awakened by the RTC interrupt.

These examples show how to use MicroPython's esp.deepsleep() function to implement different sleep and wake-up functions. The specific application depends on your needs and chip support. Please make appropriate settings and parameter adjustments according to the specific situation.

Insert image description here

Guess you like

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