node-red

[官网][]有安装教程和使用教程
[官网]:http://nodered.org/docs/
启动方式:
命令行中输入 node-red,浏览器访问http://localhost:1880,就可以使用了
1.第一条流
3个模块。

  • inject(输入 可以设置多久发一次)
  • function(输入的处理,比如输入发时间戳,可以处理成可读性强的date形式)
  • debug(显示)

部署后就会生成一个关于这条流的json文件

//每条包括 id编号,类型,名字,(各自特征的一些信息),坐标xy,连线,只记录自己的输出连向谁。
[{"id":"58ffae9d.a7005","type":"debug","name":"","active":true,"complete":false,"x":640,"y":200,"wires":[]},
{"id":"17626462.e89d9c","type":"inject","name":"","topic":"","payload":"","repeat":"","once":false,"x":240,"y":200,"wires":[["2921667d.d6de9a"]]},
{"id":"2921667d.d6de9a","type":"function","name":"Format timestamp","func":"// Create a Date object from the payload\nvar date = new Date(msg.payload);\n// Change the payload to be a formatted Date string\nmsg.payload = date.toString();\n// Return the message so it can be sent on\nreturn msg;","outputs":1,"x":440,"y":200,"wires":[["58ffae9d.a7005"]]}]

把上面这个代码直接导入就可以自动生成流。But。。inject对象没选择repeat 导致看不到结果,可以选择重复,或则开始的时候注入一次。

2.第二条流

从外部抓取数据,转换为有用的格式,输出json格式以备后用或者输出布尔值来表征开和关

直接导入下面代码

[{"id":"11b032a3.ee4fcd","type":"inject","name":"Tick","topic":"","payload":"","repeat":"","crontab":"*/5 * * * *","once":false,"x":161,"y":828,"z":"6480e14.f9b7f2","wires":[["a2b3542e.5d4ca8"]]},
{"id":"a2b3542e.5d4ca8","type":"http request","name":"UK Power","method":"GET","url":"http://realtimeweb-prod.nationalgrid.com/SystemData.aspx","x":301,"y":828,"z":"6480e14.f9b7f2","wires":[["2631e2da.d9ce1e"]]},
{"id":"2631e2da.d9ce1e","type":"function","name":"UK Power Demand","func":"// does a simple text extract parse of the http output to provide an\n// object containing the uk power demand, frequency and time\n\nif (~msg.payload.indexOf('\")[1].split(\"<\")[0]);\n    msg.payload.frequency = parseFloat(fre.split(\">\")[1].split(\"<\")[0]);\n    \n    msg2 = {};\n    msg2.payload = (msg.payload.frequency >= 50) ? true : false;\n\n    return [msg,msg2];\n}\n\nreturn null;","outputs":"2","valid":true,"x":478,"y":828,"z":"6480e14.f9b7f2","wires":[["8e56f4d3.71a908"],["cd84371b.327bc8"]]},
{"id":"8e56f4d3.71a908","type":"debug","name":"","active":true,"complete":false,"x":678,"y":798,"z":"6480e14.f9b7f2","wires":[]},
{"id":"cd84371b.327bc8","type":"debug","name":"","active":true,"complete":false,"x":679,"y":869,"z":"6480e14.f9b7f2","wires":[]}]

自己做得话,有几个点容易忽略

  • function的输出设置成2个,图标上有2个输出点,各自连一个debug节点
  • http httprequest httpresponse三者有区别,不要拖错模块
  • inject时间的设置

你可能感兴趣的:(node-red)