master
IoTcat 5 years ago
parent 3d1c4d7276
commit f51f2cdc21
  1. 39
      node/README.md
  2. 15
      node/app.js
  3. 2
      node/package.json
  4. 27
      node/wiot.js

@ -261,6 +261,45 @@ myPIR.on("change", ()=>{
});
```
### IR 红外循迹/障碍传感器
+ `wiot.ir(MCU, pin)`: 声明一个IR模块
+ `wiot.ir.getStatus()`: 获取IR状态,返回值wiot.HIGH(有障碍),wiot.LOW(无障碍)
#### 事件触发器
+ `wiot.ir.on(event, handler)`
**事件列表**
- `detected` 探测到障碍
- `undetected` 障碍消失
- `change` 状态改变
```js
MCU0 = new wiot.client({MAC: "xx:xx:xx:xx:xx:xx"});
var myPIR = wiot.pir(MCU0, wiot.D2); //新建一个pir对象,使用MCU0上的D2口
/* 输出pir状态到控制台 */
console.log(myPIR.getStatus());
/* 当探测到人,打印 "Detected People!" 到控制台 */
myPIR.on("detected", ()=>{
console.log("Detected People!");
});
/* 人移动出探测范围,打印 "No People!!" 到控制台 */
myPIR.on("undetected", ()=>{
console.log("No People!!");
});
/* 当状态改变,执行指令 */
myPIR.on("change", ()=>{
/* 你的指令 */
});
```
### lightSensor 光敏传感器
+ `wiot.lightSensor(MCU, pin)`: 声明一个lightSensor模块

@ -1,5 +1,6 @@
var wiot = require('./wiot');
MyMCU = new wiot.client({MAC: "3C:71:BF:3A:F7:66", pin: {D4: 1}, okDelayTime: 30,hint: 1,debug: 1});
MyMCU2 = new wiot.client({MAC: "BC:DD:C2:2F:EC:A8"});
//q = new wiot.client({MAC: "3C:71:BF:3A:F6:83", pin: {D3: 1} });
/*
var i = 0;
@ -58,7 +59,7 @@ pir.on("change", ()=>{
var light = wiot.lightSensor(MyMCU, wiot.D1);
/*
wiot.register.set(light.getStatus, wiot.HIGH, ()=>{
myLED.set(wiot.HIGH);
});
@ -66,3 +67,15 @@ wiot.register.set(light.getStatus, wiot.HIGH, ()=>{
wiot.register.set(wiot.LOW, light.getStatus, ()=>{
myLED.clear();
});
*/
var ir = wiot.ir(MyMCU2, wiot.D8);
wiot.register.set(ir.getStatus, wiot.HIGH, ()=>{
myLED.set(wiot.HIGH);
});
wiot.register.set(wiot.LOW, ir.getStatus, ()=>{
myLED.clear();
});

@ -1,6 +1,6 @@
{
"name": "wiot",
"version": "0.0.22",
"version": "0.0.24",
"description": "An awesome iot system for web developers~",
"main": "wiot.js",
"scripts": {

@ -2,7 +2,7 @@
* @Author: IoTcat (https://iotcat.me)
* @Date: 2019-05-04 18:59:49
* @Last Modified by: IoTcat
* @Last Modified time: 2019-05-17 17:12:46
* @Last Modified time: 2019-05-17 17:40:46
*/
var wiot_client = function (o_params) {
var o = {
@ -971,6 +971,30 @@ var wiot_lightSensor = (obj, pin) => {
return o;
};
/* ir */
var wiot_ir = (obj, pin) => {
var o = {
MCU: obj,
pin: pin,
getStatus: () => {
return (o.MCU.read(o.pin) == 255) ? 0 : 255;
},
on: (event, handler = ()=>{}) => {
if(event == "detected"){
o.MCU.pinOn(o.pin, "on", handler);
}
if(event == "undetected"){
o.MCU.pinOn(o.pin, "off", handler);
}
if(event == "change"){
o.MCU.pinOn(o.pin, "change", handler);
}
}
};
return o;
};
@ -996,4 +1020,5 @@ exports.register = wiot_register;
exports.led = wiot_led;
exports.pir = wiot_pir;
exports.ir = wiot_ir;
exports.lightSensor = wiot_lightSensor;
Loading…
Cancel
Save