• 【MicroPython ESP32】 esp模块功能函数详解和使用示例


    【MicroPython ESP32】 esp模块功能函数详解和使用示例


    • 本示例基于Thonny开发平台。

    esp功能模块函数以及参数信息

    • 在Shell调试窗口获取的信息如下:
    >>> import esp
    >>> help(esp)
    object <module 'esp'> is of type module
      __name__ -- esp
      osdebug -- <function>
      flash_read -- <function>
      flash_write -- <function>
      flash_erase -- <function>
      flash_size -- <function>
      flash_user_start -- <function>
      gpio_matrix_in -- <function>
      gpio_matrix_out -- <function>
      dht_readinto -- <function>
      LOG_NONE -- 0
      LOG_ERROR -- 1
      LOG_WARNING -- 2
      LOG_INFO -- 3
      LOG_DEBUG -- 4
      LOG_VERBOSE -- 5
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • flash_size():获取flash容量。
    >>> esp.flash_size()
    16777216
    
    • 1
    • 2
    • esp.flash_user_start():用户可使用的flash起始地址。
    >>> esp.flash_user_start()
    2097152
    
    • 1
    • 2
    • esp.flash_read(byte_offset, buf):从地址为 byte_offset 的 flash 起始点读取 buf.len()个长度的数据并放入 buf 中。
    byte_offset:flash偏移地址
    buf:接收数据缓冲区,缓冲区的长度为len
    
    • 1
    • 2
    • Shell窗口调试示例代码。

    前提是前面y引用了import esp

    >>> buf = bytearray(100)
    >>> esp.flash_read(2097152, buf)
    >>> print(buf)
    bytearray(b'\x01\x00\x00\x00\xf0\x0f\xff\xf7littlefs/\xe0\x00\x10\x00\x00\x02\x00\x00\x10\x00\x00\x00\x02\x00\x00\xff\x00\x00\x00\xff\xff\xff\x7f\xfe\x03\x00\x00p\x1f\xfc\x08.\x101\xfc\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff')
    
    • 1
    • 2
    • 3
    • 4
    • esp.flash_write(byte_offset, buf):从地址为 byte_offset 的 flash 起始点写入 buf数据。和前面的读取函数相反。
    byte_offset:flash偏移地址
    buf:数据缓冲区,缓冲区长度为len
    
    • 1
    • 2
    • esp.flash_erase(sector_no):擦除flash扇区。
    sector_no:要擦除的扇区
    
    • 1
    esp.flash_erase(512)
    
    • 1
    • osdebug(0/None):
    esp.osdebug(None):关闭厂商O/S调试消息.
    esp.osdebug(0):将厂商O/S调试消息重定向到UART(0)
    
    • 1
    • 2
    from machine import Pin
    # import machine #导入machine模块
    import time
    import esp
    esp.osdebug(None)       # turn off vendor O/S debugging messages
    esp.osdebug(0)          # redirect vendor O/S debugging messages to UART(0)
    led = Pin(2, Pin.OUT) #用machine模块的pin功能设置引脚2为输出。
    while True:   
        led.value(0) # 将引脚2设置为高电平
        # read and print the pin value
        print(led.value())
        print('From ESP32 MicroPyton')
        time.sleep(3)
        led.value(1) # 将引脚2设置为低电平
        print('Hello world!')
        print(led.value())
        time.sleep(3)
        # low level methods to interact with flash storage
        print(esp.flash_size())
        print(esp.flash_user_start())
        time.sleep(1)
        # esp.flash_erase(sector_no)
        # esp.flash_write(byte_offset, buffer)
        #esp.flash_read(byte_offset, buffer)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • Shell调试输出信息
      在这里插入图片描述

    • 重映射输入输出引脚:只有环回信号224-228可以配置为从一个输入GPIO直接路由到另一个输出GPIO。(暂时没有想到使用方法)

    GPIO matrix:即为 GPIO 交换矩阵,是外设输入和输出信号和 pad 之间的全交换矩阵。
    在这里插入图片描述

    • 参考信息:https://blog.csdn.net/qq_40078905/article/details/107435351

    • esp.gpio_matrix_in(int, int,bool,bool):

    • gpio_matrix_out(int, int,bool,bool):


    • esp.dht_readinto(pin, buf):读dht的值(温湿度传感器),此函数不依赖引入第三方模块来实现。
    pin:读取数据的引脚
    buf:数据缓冲区
    
    • 1
    • 2
    • 实例代码

    DHT11数据引脚接到22引脚上,3.3V供电
    在这里插入图片描述

    from machine import Pin
    import time
    import esp
    buf = bytearray(5)
    
    def measure(pin):
      global buf
      esp.dht_readinto(pin, buf)
      if(buf[0]+buf[1]+buf[2]+buf[3])&0xff!= buf[4]:
        raise Exception("checksum error")
    
    def dht11_humidity():
      return buf[0]
    
    def dht11_temperature():
      return buf[2]
    
    def dht22_humidity():
      return (buf[0]<<8 | buf[1])*0.1
    
    def dht22_temperature():
      t = ((buf[2] & 0x7f) << 8 |buf[3])*0.1
      if buf[2] & 0x80:
        t = -t
      return t
    
    try:
      while True:
        measure(Pin(22))
        print("dht11 humidity:", dht11_humidity(),end='RH\n')
        print("dht11 temperature:", dht11_temperature(),end='℃\n')
       # print("dht22 humidity:", dht22.humidity())
       # print("dht22 temperature:", dht22.temperature())
        time.sleep(0.5)
    
    except:
      print("Abnormal program!")
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • Shell调试信息
      在这里插入图片描述
  • 相关阅读:
    青鸾剪辑-全自动视频混剪,批量剪辑批量剪视频,探店带货系统,精细化顺序混剪,故事影视解说,视频处理大全,精细化顺序混剪,多场景裂变,多视频混剪
    数组操作UNIAPP
    原子性,可见性,有序性详解及DCL单例模式两次校验的目的(拓展懒汉式,饿汉式)
    make quick-example I: 变量语法 & 变量插值
    Tomcat的Host容器
    每天一个面试题之类加载机制、spirngboot的启动机制
    M的编程备忘录之C++——类和对象(上)
    2.(vue3.x+vite)使用vue-router
    车载通信与DDS标准解读系列(1):DDS-RPC
    c语言之字符串数组
  • 原文地址:https://blog.csdn.net/weixin_42880082/article/details/126341197