I want to use node-red to read a file, but I want the reading speed for each line of data to be 40ms. What should I do?
I don't know how in Node-red you could read one line from a file every 40ms, but you could read the file once and use a rate limit (delay) node to only pass 25 lines per second.
Thanks for your help, but the time intervals of the data inside are very random.
Can you explain what you mean?
Why do you need to read one line every 40ms?
How precise must the 40ms interval be?
How big is the file?
Are it's contents static or is it growing?
- Although the delay node will limit the output per second, the interval of the output result is 30ms or 50ms, not 40ms.
- I want to make a dynamic display of data, combined with influxDB
- The accuracy is ms
- The file has a total of 750 data, each data interval is 40ms, a total of 30s data
- I read data from a file, the file data is static
It might be worth trying a queue node such as node-red-contrib-queue-gate and a function node to release one message at a time something like this:
setInterval(mytimer, 1000) // Interval 1 second
function mytimer() {
node.send({"topic":"control", "payload": "trigger"})
}
You will probably need a mechanism to stop the interval timer with clearInterval(mytimer)
!
I doubt if you could achieve better accuracy than the rate limit node though.
I don't even know if it will work at all.
Depending on use, you could create a timestamp for each message 40ms apart.
e.g
[{"id":"65bead4e367b5e99","type":"inject","z":"d1395164b4eec73e","name":"","props":[{"p":"timestamp","v":"","vt":"date"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":350,"y":7600,"wires":[["d61684d287ddb1ac"]]},{"id":"d61684d287ddb1ac","type":"template","z":"d1395164b4eec73e","name":"read file","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"1\n2\n3\n4\n5\n6\n7\n8\n9","output":"str","x":380,"y":7660,"wires":[["4cc392834d50680b"]]},{"id":"4cc392834d50680b","type":"split","z":"d1395164b4eec73e","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","property":"payload","x":510,"y":7660,"wires":[["a0c1f08bbbf34f9a"]]},{"id":"a0c1f08bbbf34f9a","type":"delay","z":"d1395164b4eec73e","name":"","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"20","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":660,"y":7660,"wires":[["c9b9cb4997fdf4fa"]]},{"id":"c9b9cb4997fdf4fa","type":"change","z":"d1395164b4eec73e","name":"","rules":[{"t":"set","p":"timestamp","pt":"msg","to":"$$.timestamp +( $$.parts.index * 40)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":850,"y":7660,"wires":[["8047327890ff07d0"]]},{"id":"8047327890ff07d0","type":"debug","z":"d1395164b4eec73e","name":"debug 2575","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":670,"y":7600,"wires":[]}]
Thanks for your help, I will try this method soon.
Yes, this method of combining the delay node and redefining the timestamp is indeed a good method.
Can you not get the timestamp added when the data are initially collected and written to the file?
Somehow I got the idea that the OP wanted to read one line per 40ms, but it does make more sense to want one line written per 40 ms.
You can try combining the inject node with the trigger node.
That is what he asked for, but actually what he wants to do, I believe, is add them at that interval to influxdb. My suggestion is to add timestamps when the data are collected so they have timestamps in the file, which can be passed to influxdb.
Yes, but time sync is not possible on my sensor.
How does the value get from the sensor to the file?
BLE .
The wireless receiver is a USB Dongle, not a smart phone.
Then we use the USB Dongle's UART to input to the Node Red. The only place where we can get the timestamp may be the Node Red.
Yes, add the timestamps at the same time as you add the values.
Why are you writing them to a file? Why don't you send them straight to influx?
I have already implemented the function you mentioned, and my purpose is just to show it dynamically later.
I don't think you answered the question, why do you not write the data to influx as you receive it?