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

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

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

也可以命令行安装,可参考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"]
	]
}]

实际程序如下图:

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

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

发布了30 篇原创文章 · 获赞 11 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/armcsdn/article/details/104220369
今日推荐