不到30行代码获取额温计测温结果

正值疫情肆虐,红外测温枪成了当前的紧俏货。本文用不到30行python代码获取红外测温传感器的室温值和目标温度值。

准备工作

  1. 支持Micropython运行的开发板,可以是pyboard 也可以是rtthread开发板上运行的micropython开发环境,如潘多拉开发板、W601开发板等等
  2. MLX9061X红外温度传感器,IIC接口
  3. vscode 开发环境或其他

MicroPython代码

我是用的是正点原子出的W601开发板,运行RTT的官方micropython固件,代码如下:

from machine import Pin, I2C
import time
# w601 iot board test
PIN_CLK = 66   # PB10
PIN_SDA = 65   # PB11
clk = Pin(("clk", PIN_CLK), Pin.OUT_OD)   # Select the PIN_CLK pin device as the clock
sda = Pin(("sda", PIN_SDA), Pin.OUT_OD)   # Select the PIN_SDA pin device as the data line
i2c = I2C(-1, clk, sda, freq=100000)
addr = 0x5a
while True:
    room = i2c.readfrom_mem(addr, 0x06, 2) 
    human = i2c.readfrom_mem(addr, 0x07, 2) 
    room = room[1]*256 + room[0]
    human = human[1]*256 + human[0]
    room*=2
    human*=2
    if room > 27315:
        room -= 27315
    else:
        room = 27315 - room
    if human > 27315:
        human -= 27315
    else:
        human = 27315 - human
    room/=100
    human/=100
    print("room: {room} human: {human} ".format(room = room , human = human) )
    time.sleep(0.5)

运行结果

当我变化手离传感器的距离时,明显看到human的值发生了变化,且和手离传感器的距离成正比,room的值基本不变。截取一段运行结果如下所示:

room: 28.15 human: 28.39 
room: 28.13 human: 33.07 
room: 28.09 human: 34.01 
room: 28.13 human: 34.15 
room: 28.09 human: 30.91 
room: 28.07 human: 29.09 
room: 28.09 human: 29.23 
room: 28.05 human: 32.75 
room: 28.07 human: 33.35 
room: 28.05 human: 26.85 
room: 28.11 human: 26.61 
room: 28.07 human: 28.97 
room: 27.99 human: 32.03 
room: 28.05 human: 28.97 
room: 28.01 human: 30.43 
room: 28.01 human: 32.59 
room: 28.05 human: 32.97 
room: 28.01 human: 31.63 
room: 28.01 human: 28.33 
room: 27.99 human: 28.05 
room: 27.99 human: 30.85 
room: 27.99 human: 32.25 
room: 27.95 human: 29.19 
room: 27.93 human: 26.55 
room: 27.97 human: 26.47 
room: 27.93 human: 26.49 
room: 27.89 human: 26.53 
room: 27.93 human: 26.59 
room: 27.87 human: 26.49 
room: 27.87 human: 26.55 
room: 27.87 human: 26.43 
room: 27.85 human: 26.59 
room: 27.87 human: 26.59 
room: 27.89 human: 26.47 
room: 27.81 human: 26.41 

最后,希望疫情早点过去,生活恢复如初。

你可能感兴趣的:(python)