esp32 s2 C3 在micropython下使用内置温度传感器

需要自行编译固件 下面代码基于micropython-1.22.1 和 esp-idf-v5.0.4

esp-idf 从4.x到5.x有一些变化 记录一下新版本的使用方法

添加一个C模块 需要4个文件

cd micropython-1.22.1/ports/esp32/

1
2
3
4
#ls cmodules/
micropython.cmake  sensorcoretemper
ls cmodules/sensorcoretemper 
micropython.cmake  micropython.mk  sensorcoretemper.c

sensorcoretemper.c

 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "py/obj.h"
#include "py/runtime.h"
#include "esp_log.h"
#include "driver/temperature_sensor.h"

// 定义一个全局变量来保存句柄
static temperature_sensor_handle_t tsens = NULL;


static const char *TAG = "sensorcoretemper";

// 函数 初始化 并启动
STATIC mp_obj_t sensorcoretemper_init(void) {
    ESP_LOGI(TAG, "Install temperature sensor, expected temp ranger range: 10~50 ℃");
    temperature_sensor_config_t temp_sensor = TEMPERATURE_SENSOR_CONFIG_DEFAULT(10, 50);
    //ESP_ERROR_CHECK(temperature_sensor_install(&temp_sensor));
    ESP_ERROR_CHECK(temperature_sensor_install(&temp_sensor, &tsens));  // 注意这里的改动


    ESP_LOGI(TAG, "Enable temperature sensor");
    //ESP_ERROR_CHECK(temperature_sensor_enable());
    ESP_ERROR_CHECK(temperature_sensor_enable(tsens));  // 注意这里的改动

    return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(sensorcoretemper_init_obj, sensorcoretemper_init);

// 函数 获取
STATIC mp_obj_t sensorcoretemper_getT(void) {
    ESP_LOGI(TAG, "Read temperature");
    float tsens_value;
    //ESP_ERROR_CHECK(temperature_sensor_get_celsius(&tsens_value));
    ESP_ERROR_CHECK(temperature_sensor_get_celsius(tsens, &tsens_value));
    ESP_LOGI(TAG, "Temperature value %.02f ℃", tsens_value);


    return mp_obj_new_float(tsens_value);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(sensorcoretemper_getT_obj, sensorcoretemper_getT);

// 函数 停止
STATIC mp_obj_t sensorcoretemper_stop(void) {
    //ESP_ERROR_CHECK(temperature_sensor_disable());
    ESP_ERROR_CHECK(temperature_sensor_disable(tsens));  // 注意这里的改动
    return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(sensorcoretemper_stop_obj, sensorcoretemper_stop);

STATIC const mp_rom_map_elem_t sensorcoretemper_module_globals_table[] = {
    { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_sensorcoretemper) },
    { MP_ROM_QSTR(MP_QSTR_getT), MP_ROM_PTR(&sensorcoretemper_getT_obj) },
    { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&sensorcoretemper_init_obj) },
    { MP_ROM_QSTR(MP_QSTR_stop), MP_ROM_PTR(&sensorcoretemper_stop_obj) },
};
STATIC MP_DEFINE_CONST_DICT(sensorcoretemper_module_globals, sensorcoretemper_module_globals_table);

const mp_obj_module_t sensorcoretemper_user_cmodule = {
    .base = { &mp_type_module },
    .globals = (mp_obj_dict_t *)&sensorcoretemper_module_globals,
};

//MP_REGISTER_MODULE(MP_QSTR_sensorcoretemper, sensorcoretemper_user_cmodule, MODULE_sensorcoretemper_ENABLED);
MP_REGISTER_MODULE(MP_QSTR_sensorcoretemper, sensorcoretemper_user_cmodule);

其他三个文件

 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

cd ~/mpy-bin/micropython-1.22.1/ports/esp32
mkdir -p cmodules/sensorcoretemper
# 文件 ===> micropython.cmake
cat >  cmodules/micropython.cmake<< \EOF
# Add sensorcoretemper.
include(${CMAKE_CURRENT_LIST_DIR}/sensorcoretemper/micropython.cmake)
EOF
# 文件 ===> micropython.cmake 
cat >  cmodules/sensorcoretemper/micropython.cmake << \EOF
# Create an INTERFACE library for our C module.
add_library(usermod_sensorcoretemper INTERFACE)

# Add our source files to the lib
target_sources(usermod_sensorcoretemper INTERFACE
    ${CMAKE_CURRENT_LIST_DIR}/sensorcoretemper.c
)

# Link our INTERFACE library to the usermod target.
target_link_libraries(usermod INTERFACE usermod_sensorcoretemper)
EOF

# 文件 ===> micropython.mk
cat >  cmodules/sensorcoretemper/micropython.mk<< \EOF
sensorcoretemper_MOD_DIR := $(USERMOD_DIR)
# Add all C files to SRC_USERMOD.
SRC_USERMOD_C += $(sensorcoretemper_MOD_DIR)/sensorcoretemper.c
EOF

编译

1
2
3
make clean BOARD=LOLIN_S2_MINI  USER_C_MODULES=../cmodules/micropython.cmake #esp32-s2
make submodules
make BOARD=LOLIN_S2_MINI  USER_C_MODULES=../cmodules/micropython.cmake

使用方法

1
2
3
4
5
6
7
8
import sensorcoretemper
import time
sensorcoretemper.init()

for _ in range(10):
     print(sensorcoretemper.getT())
     time.sleep(1)
sensorcoretemper.stop()

技巧

内置传感器温度 通常比环境温度高1-3摄氏度不等,在稳定的环境中 可以简单校准一下作为参考温度

Licensed under CC BY-NC-SA 4.0
comments powered by Disqus
Built with Hugo
主题 StackJimmy 设计