ESP32 MicroPython开发之旅 设备篇⑦ —— dht11 OLED ssd1306,简单上手
Posted 单片机菜鸟爱学习
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ESP32 MicroPython开发之旅 设备篇⑦ —— dht11 OLED ssd1306,简单上手相关的知识,希望对你有一定的参考价值。
dht11 OLED ssd1306,简单上手
忘记过去,超越自己
- ❤️博客主页 单片机菜鸟哥,一个野生非专业硬件IOT爱好者❤️
- 🎉欢迎关注🔎点赞👍收藏⭐️留言📝
- 🙏此博客均由博主单独编写,不存在任何商业团队运营,如发现错误,请留言轰炸哦!及时修正!感谢支持!
- 🔥Arduino ESP8266教程累计帮助过超过1W+同学入门学习硬件网络编程,入选过选修课程,刊登过无线电杂志🔥
DHT11 和 DHT22
- Dht11和 dht22传感器用于测量
温度
(temperature)和相对湿度
(relative humidity)
🎉MicroPython 内置库
MicroPython本身内置了dht
模块,支持DHT11
、DHT22
。
在Shell里面分别输入:
>>> import dht
>>> help(dht)
>>> from machine import Pin
>>> sensor = dht.DHT11(Pin(14))
>>> help(sensor)
主要有四个步骤:
- 创建dht实例
- 开始测量
- 读取温度
- 读取湿度
🔎创建dht实例 —— dht.DHT11(Pin)
# 从machine引入Pin模块
from machine import Pin
# 引入dht模块
import dht
# 创建dht实例
sensor = dht.DHT11(Pin(14))
#sensor = dht.DHT11(Pin(14))
🔎开始测量 —— sensor.measure()
# 从machine引入Pin模块
from machine import Pin
# 引入dht模块
import dht
# 创建dht实例
sensor = dht.DHT11(Pin(14))
#sensor = dht.DHT11(Pin(14))
sensor.measure()
🔎读取温度 —— sensor.temperature()
# 从machine引入Pin模块
from machine import Pin
# 引入dht模块
import dht
# 创建dht实例
sensor = dht.DHT11(Pin(14))
#sensor = dht.DHT11(Pin(14))
sensor.measure()
sensor.temperature()
🔎读取湿度 —— sensor.humidity()
# 从machine引入Pin模块
from machine import Pin
# 引入dht模块
import dht
# 创建dht实例
sensor = dht.DHT11(Pin(14))
#sensor = dht.DHT11(Pin(14))
sensor.measure()
sensor.temperature()
sensor.humidity()
🎉测试代码,Shell打印当前温湿度
🔎电路连线
dht11 | ESP32 |
---|---|
Vin | 5V |
GND | GND |
Data | GPIO14 |
🔎测试代码
# 从machine引入Pin模块
from machine import Pin
# 引入dht模块
import dht
# 引入睡眠方法
from time import sleep
# 创建dht实例
sensor = dht.DHT11(Pin(14))
#sensor = dht.DHT11(Pin(14))
print('dht11_test')
# 不断读取温湿度
while True:
try:
# 睡眠10s
sleep(10)
# 开始测量
sensor.measure()
# 读取温度
temp = sensor.temperature()
# 读取湿度
hum = sensor.humidity()
temp_f = temp * (9/5) + 32.0
print('Temperature: %3.1f C' %temp)
print('Temperature: %3.1f F' %temp_f)
print('Humidity: %3.1f %%' %hum)
except OSError as e:
print('Failed to read sensor.')
🔎测试结果
>>> %Run -c $EDITOR_CONTENT
dht11_test
Temperature: 20.0 C
Temperature: 68.0 F
Humidity: 62.0 %
Temperature: 21.0 C
Temperature: 69.8 F
Humidity: 63.0 %
Temperature: 21.0 C
Temperature: 69.8 F
Humidity: 62.0 %
当前广州2022年1月1号的室内温湿度。
🎉测试代码,OLED1306显示DHT11当前温湿度
🔎电路连线
dht11 | ESP32 |
---|---|
Vin | 5V |
GND | GND |
Data | GPIO14 |
OLED | ESP32 |
---|---|
Vin | 3.3V |
GND | GND |
SCL | GPIO5 |
SDA | GPIO4 |
关于ssd1306的讲解,请参考上一篇 ESP32 MicroPython开发之旅 设备篇⑥ —— OLED ssd1306,简单上手,支持中英文数字
🔎测试代码
# 从machine模块导入I2C、Pin子模块
from machine import I2C,Pin
# 引入dht模块
import dht
# 引入睡眠方法
from time import sleep
# 从ssd1306模块中导入SSD1306_I2C子模块
from lib_ssd1306 import SSD1306_I2C
def chinese(ch_str, x_axis, y_axis):
offset_ = 0
y_axis = y_axis*8 # 中文高度一行占8个
x_axis = (x_axis*16) # 中文宽度占16个
for k in ch_str:
code = 0x00 # 将中文转成16进制编码
data_code = k.encode("utf-8")
code |= data_code[0] << 16
code |= data_code[1] << 8
code |= data_code[2]
byte_data = fonts[code]
for y in range(0, 16):
a_ = bin(byte_data[y]).replace('0b', '')
while len(a_) < 8:
a_ = '0'+a_
b_ = bin(byte_data[y+16]).replace('0b', '')
while len(b_) < 8:
b_ = '0'+b_
for x in range(0, 8):
oled.pixel(x_axis+offset_+x, y+y_axis, int(a_[x]))
oled.pixel(x_axis+offset_+x+8, y+y_axis, int(b_[x]))
offset_ += 16
# 中文字库 UTF-8 从 https://www.qqxiuzi.cn/bianma/guojima.php 获取
fonts=
0xE58D95:
[0x10,0x08,0x04,0x3F,0x21,0x21,0x3F,0x21,0x21,0x3F,0x01,0x01,0xFF,0x01,0x01,0x01,
0x10,0x20,0x40,0xF8,0x08,0x08,0xF8,0x08,0x08,0xF8,0x00,0x00,0xFE,0x00,0x00,0x00],#"单"
0xE78987:
[0x00,0x10,0x10,0x10,0x10,0x1F,0x10,0x10,0x10,0x1F,0x10,0x10,0x10,0x20,0x20,0x40,
0x40,0x40,0x40,0x40,0x40,0xFC,0x00,0x00,0x00,0xE0,0x20,0x20,0x20,0x20,0x20,0x20],#"片"
0xE69CBA:
[0x10,0x11,0x11,0x11,0xFD,0x11,0x31,0x39,0x55,0x55,0x91,0x11,0x11,0x12,0x12,0x14,
0x00,0xF0,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x12,0x12,0x12,0x0E,0x00],#"机"
0xE88F9C:
[0x08,0x08,0xFF,0x08,0x00,0x00,0x3F,0x11,0x08,0x01,0x7F,0x05,0x09,0x31,0xC1,0x01,
0x20,0x20,0xFE,0x20,0x10,0xF8,0x00,0x10,0x20,0x00,0xFC,0x40,0x20,0x18,0x06,0x00],#"菜"
0xE9B89F:
[0x01,0x02,0x1F,0x10,0x12,0x11,0x11,0x10,0x10,0x1F,0x00,0x00,0x7F,0x00,0x00,0x00,
0x00,0x00,0xF0,0x10,0x10,0x10,0x50,0x20,0x00,0xFC,0x04,0x04,0xE4,0x04,0x28,0x10],#"鸟"
0xE6B8A9:
[0x00,0x23,0x12,0x12,0x83,0x42,0x42,0x13,0x10,0x27,0xE4,0x24,0x24,0x24,0x2F,0x00,
0x00,0xF8,0x08,0x08,0xF8,0x08,0x08,0xF8,0x00,0xFC,0xA4,0xA4,0xA4,0xA4,0xFE,0x00],#"温"
0xE6B9BF:
[0x00,0x27,0x14,0x14,0x87,0x44,0x44,0x17,0x11,0x21,0xE9,0x25,0x23,0x21,0x2F,0x00,
0x00,0xF8,0x08,0x08,0xF8,0x08,0x08,0xF8,0x20,0x20,0x24,0x28,0x30,0x20,0xFE,0x00],#"湿"
0xE5BAA6:
[0x01,0x00,0x3F,0x22,0x22,0x3F,0x22,0x22,0x23,0x20,0x2F,0x24,0x42,0x41,0x86,0x38,
0x00,0x80,0xFE,0x20,0x20,0xFC,0x20,0x20,0xE0,0x00,0xF0,0x10,0x20,0xC0,0x30,0x0E],#"度"
0xE5B9BF:
[0x01,0x00,0x00,0x3F,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x40,0x40,0x80,
0x00,0x80,0x80,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00],#"广"
0xE5B79E:
[0x10,0x10,0x10,0x10,0x10,0x54,0x52,0x52,0x90,0x10,0x10,0x10,0x20,0x20,0x40,0x80,
0x04,0x84,0x84,0x84,0x84,0xA4,0x94,0x94,0x84,0x84,0x84,0x84,0x84,0x84,0x04,0x04],#"州"
# 创建oled实例
i2c = I2C(sda=Pin(5), scl=Pin(4)) #I2C初始化:sda--> Y8, scl --> Y6
oled = SSD1306_I2C(128, 64, i2c, addr=0x3c) #OLED显示屏初始化:128*64分辨率,OLED的I2C地址是0x3c
# 创建dht实例
sensor = dht.DHT11(Pin(14))
#sensor = dht.DHT11(Pin(14))
print('dht11_ssd1306')
# 全部灭掉
oled.fill(0)
oled.show()
# 不断读取温湿度
while True:
try:
# 睡眠10s
sleep(10)
oled.fill(0)
oled.show()
# 开始测量
sensor.measure()
# 读取温度
temp = sensor.temperature()
# 读取湿度
hum = sensor.humidity()
temp_f = temp * (9/5) + 32.0
print('Temperature: %3.1f C' %temp)
print('Temperature: %3.1f F' %temp_f)
print('Humidity: %3.1f %%' %hum)
# 中文一个字是 16 X 16
chinese('单片机菜鸟',0,0)
chinese('广州',0,2)
chinese('温度',0,4)
oled.text('%d' % (temp), 33, 41, 2)
chinese('湿度',0,6)
oled.text('%d' % (hum), 33, 56, 2)
oled.show()
except OSError as e:
print('Failed to read sensor.')
🔎测试结果
>>> %Run -c $EDITOR_CONTENT
Warning: I2C(-1, ...) is deprecated, use SoftI2C(...) instead
dht11_ssd1306
Temperature: 20.0 C
Temperature: 68.0 F
Humidity: 63.0 %
Temperature: 20.0 C
Temperature: 68.0 F
Humidity: 63.0 %
Temperature: 20.0 C
Temperature: 68.0 F
Humidity: 63.0 %
以上是关于ESP32 MicroPython开发之旅 设备篇⑦ —— dht11 OLED ssd1306,简单上手的主要内容,如果未能解决你的问题,请参考以下文章
ESP32 MicroPython开发之旅 设备篇⑦ —— dht11 OLED ssd1306,简单上手