树莓派Node-Red例1:时间戳转换存入TXT文件

本例实现把时间戳转换成可供人阅读的日期和秒格式并存入txt文件.

首先先安装 node-red-contrib-simpletime,安装方法如图:

树莓派Node-Red例1:时间戳转换存入TXT文件_第1张图片

也可以命令行安装,可参考https://flows.nodered.org/node/node-red-contrib-simpletime:

npm install node-red-contrib-simpletime

可导入代码如下:

[{
	"id": "9b114162.97a71",
	"type": "tab",
	"label": "流程1",
	"disabled": false,
	"info": ""
}, {
	"id": "a30a6c48.6e8f",
	"type": "ui_tab",
	"z": "",
	"name": "Temp",
	"icon": "dashboard"
}, {
	"id": "4c107418.fdb2bc",
	"type": "inject",
	"z": "9b114162.97a71",
	"name": "",
	"topic": "",
	"payload": "",
	"payloadType": "date",
	"repeat": "",
	"crontab": "",
	"once": false,
	"onceDelay": 0.1,
	"x": 290,
	"y": 200,
	"wires": [
		["759d788a.a96ee8"]
	]
}, {
	"id": "759d788a.a96ee8",
	"type": "simpletime",
	"z": "9b114162.97a71",
	"name": "",
	"x": 290,
	"y": 300,
	"wires": [
		["647d6b91.8fb724"]
	]
}, {
	"id": "647d6b91.8fb724",
	"type": "function",
	"z": "9b114162.97a71",
	"name": "DateTime",
	"func": "var payload=msg; //get payload\nmsg.payload=\"写入文件时间\"\nmsg.payload=msg.payload+payload.mydate\nmsg.payload=msg.payload+\" \"+payload.mytimes\nreturn msg;",
	"outputs": 1,
	"noerr": 0,
	"x": 300,
	"y": 380,
	"wires": [
		["e5cf364c.673c28"]
	]
}, {
	"id": "b2b83e19.728db",
	"type": "debug",
	"z": "9b114162.97a71",
	"name": "",
	"active": true,
	"tosidebar": true,
	"console": false,
	"tostatus": false,
	"complete": "payload",
	"targetType": "msg",
	"x": 310,
	"y": 540,
	"wires": []
}, {
	"id": "e5cf364c.673c28",
	"type": "file",
	"z": "9b114162.97a71",
	"name": "保存时间文件",
	"filename": "/home/pi/file/test.txt",
	"appendNewline": true,
	"createDir": false,
	"overwriteFile": "false",
	"encoding": "GBK",
	"x": 320,
	"y": 460,
	"wires": [
		["b2b83e19.728db"]
	]
}]

实际程序如下图:

树莓派Node-Red例1:时间戳转换存入TXT文件_第2张图片

树莓派Node-Red例1:时间戳转换存入TXT文件_第3张图片

树莓派Node-Red例1:时间戳转换存入TXT文件_第4张图片

其中"DateTime"节点是function节点,配置如下图:

树莓派Node-Red例1:时间戳转换存入TXT文件_第5张图片

 其中"保存时间文件"节点是file节点,配置如下:

树莓派Node-Red例1:时间戳转换存入TXT文件_第6张图片

 

你可能感兴趣的:(Node-Red,Node-Red,树莓派)