0.0.13 • Published 5 years ago
max6675-raspi v0.0.13
Max6675
在树莓派上使用 Node.js 驱动 Max6675 芯片,读取 K 型热偶的温度值。
npm i max6675-raspi --save
API
Max6675
const Max6675 = require("max6675-raspi");
const CS = 4;
const SCK = 24;
const SO = [25, 12, 16, 20, 21];
const UNIT = 1;
const max6675 = new Max6675(CS, SCK, SO, UNIT);可以接收 4 个参数:
CS: Max6675 模块的CS脚对应的树莓派的 GPIO 号。SCK: Max6675 模块的SCK脚对应的树莓派的 GPIO 号。SO: Max6675 模块的SO脚对应的树莓派的 GPIO 号,可以接收一个数组,也可以接收一个整数。UNIT: 设置结果输出单位,1为°C,0为°F,不传参数则默认值为1,传其他值则直接返回Max6675芯片的二进制数转十进制数值。
setPin
const Max6675 = require("max6675-raspi");
const CS = 4;
const SCK = 24;
const SO = [25, 12, 16, 20, 21];
const UNIT = 1;
// const max = new Max6675(CS, SCK, SO, UNIT);
const max6675 = new Max6675();
max6675.setPin(CS, SCK, SO, UNIT);如果你在new Max6675()的时候没有传参数,就可以调用这个方法设置针脚信息。与Max6675一样接收四个参数:
CS: Max6675 模块的CS脚对应的树莓派的 GPIO 号。SCK: Max6675 模块的SCK脚对应的树莓派的 GPIO 号。SO: Max6675 模块的SO脚对应的树莓派的 GPIO 号,可以接收一个数组,也可以接收一个整数。UNIT: 设置结果输出单位,1为°C,2为°F,不传参数则默认值为1,传其他值则直接返回Max6675芯片的二进制数转十进制数值。
readTemp
在设定了CS,SCK,SO和UNIT(默认值为1) 后,即能调用这个方法来获取值。
const Max6675 = require("max6675-raspi");
const CS = 4;
const SCK = 24;
const SO = [25, 12, 16, 20, 21];
const UNIT = 1;
const max6675 = new Max6675();
max6675.setPin(CS, SCK, SO, UNIT);
const { temp, unit } = max6675.readTemp();
console.log(`${new Date()}:${temp.map(item => item + unit)}`);setPin之后也可以立即调用readTemp
const Max6675 = require("max6675-raspi");
const CS = 4;
const SCK = 24;
const SO = [25, 12, 16, 20, 21];
const UNIT = 1;
const max6675 = new Max6675();
const { temp, unit } = max6675.setPin(CS, SCK, SO, UNIT).readTemp();
console.log(`${new Date()}:${temp.map(item => item + unit)}`);sleep
这是个用Promise封装的延时器。当你需要循环获取值,但又不想自己写延时器的时候,可以像下面一样使用这个sleep方法。
const Max6675 = require("max6675-raspi");
const CS = 4;
const SCK = 24;
const SO = [25, 12, 16, 20, 21];
const UNIT = 1;
const max6675 = new Max6675();
max6675.setPin(CS, SCK, SO, UNIT);
(async () => {
while (true) {
const { temp, unit } = max6675.readTemp();
if (temp.length)
console.log(`${new Date()}:${temp.map(item => item + unit)}`);
await max6675.sleep(2000);
}
})();
The PINs value is BCM GPIOs (green).
GPIO
这里特地提 GPIO,SO,CS,SCK的值,是树莓派上的 BCM GPIO(绿色),不是针脚号。
