2016-07-26 760 views
0

我是node-red和IoT的新手,但目前正在开发一个项目。 基本上我的目标是为施工人员创建一个警报系统。我想测量高度,环境温度和安全机制的状态(锁定或不锁定)。根据读数,系统应该做出决定(如果测量超过阈值 - 发送通知[蜂鸣器/ LED])。在Node-RED中实现多个输入

的逻辑如下:

  1. 当系统导通时,测量初始高度(H初始),
  2. 的时间(没有定义还)在特定时间段之后,测量电流高度(h电流)。
  3. 当前和初始高度之间的差异将是工人的实际高度(h)。
  4. 如果高度h高于2米 - 发送蜂鸣器信号。如果h小于0,则再次计算h初始和h电流。

我已将TI CC2650 SensorTag连接到RPi的Node-red,并将测量结果作为json对象发送给节点红色,具体取决于您希望读取多少个传感器读数。在我的情况下(温度和压力)我又接收两个jsons:

{ "d": { "id": "b827ebb2b2bd.c4be84711c81.0", "tstamp": { "$date": 1469565713321 }, "json_data": { "object": 21.40625, "ambient": 27.125 } } } 

{ "d": { "id": "b827ebb2b2bd.c4be84711c81.4", "tstamp": { "$date": 1469565713328 }, "json_data": { "pressure": 1016.36 } } } 

我所面临以下问题:

  1. 我不能养活多个数据节点红色。想知道是否有人能指导我如何发送(温度,压力,机制的状态[1或0]数据)到功能节点;

  2. 关于警报。基本上,要找到我需要进行两次高度测量的实际高度。意思是,我需要以某种方式存储两个压力/温度测量值。我是否需要将测量数据存储为数组,或者有更好的方法来处理这个问题?

想知道是否有人可以指导/帮助我这个。

P.S.流量的剪贴板很长,所以我决定不要粘贴在这里,但如果有人请求,可以发送它。

非常非常原始代码

var hInit; 
var hChecked; 
var h; 

//p0 is the hardcoded pressure on the level of the sea 
//hardcoded for the current area 
var p0 = 1019; 

//extract the pressure value and the ambient temp from jsons 
tagPressure = msg.payload.json_data.pressure; 
tagTemp = msg.payload.json_data.ambient; 

//the formula to convert a pressure to an altitude 

//here it should measure the altitde (hInit) when the testbest is turned on 

hInit = (((Math.pow((tagTemp/p0), (1/5.257)))-1)*(tagTemp + 273.15))/0.0065; 
//hChecked is the measured altitude afterwards 
hChecked = (((Math.pow((tagTemp/p0), (1/5.257)))-1)*(tagTemp + 273.15))/0.0065; 

//h is the actual altitude the worker is working on 
h = hChecked - hInit; 

//in the case if a worker turned the testbed on 
//when he was operating on the altitude he then 
//might go down so altitude can reduce. 
//therefore if the altitude h is < 0 we need to 
//calculate a new reference 

if (h < 0) { 
    hInit = (((Math.pow((tagTemp/p0), (1/5.257)))-1)*(tagTemp + 273.15))/0.0065; 
    hChecked = (((Math.pow((tagTemp/p0), (1/5.257)))-1)*(tagTemp + 273.15))/0.0065; 
    h = hChecked - hInit; 
    return h; 
} 

//check current altitude 
while (h>0){ 

    if (h>2){ 

     if (lockerState == 1) { 
      msg.payload = "safe"; 
      return msg; 
     } 

     else if (lockerState === 0) { 
      msg.payload = "lock your belt!"; 
      //basically i want to send a 1 signal 
      //to the buzzer which is a pin on the RPI3 
      //so probably msg.payload = 1; 
      return msg; 
     } 
    }  
} 
//return msg; 

回答

1

而节点-RED节点只具有一个输入选项卡上,这并不意味着它们不能处理来自多个来源的输入。

可以有多种选择来做到这一点

一种方法是使用一些其他的消息属性来区分它们,通常msg.topic

或者以将查询的有效载荷使用哪些属性Object.hasOwnProperty()

例如

if (msg.payload.json_data.hasOwnProperty('ambient'){ 
    //do something with 
} 

但更与基于流编程保持它可以更好地基于使用开关节点叉基于消息的属性的流消息类型分裂功能起来。

您还应该查看关于context的节点RED doc。这可以用来临时存储消息之间的值,以便比较它们或在计算中使用它们。

context.set('altitude') = foo 
... 
var foo = context.get('altitude'); 
+0

谢谢!上下文帮助我! –