COMPASS_Arduino string to .csv

(updated 2023 for Windows filename compatability) This flow is a Node-RED option for saving sequential strings from an Arduino into the same .csv file. This is provided as an update to a research paper describing a basic system for monitoring activity and sleep in mice. (https://wellcomeopenresearch.org/articles/1-2/v2).

Setup Steps: Use the node pallet option in the menu to install thethingbox-node-timestamp node. If not using a Raspberry Pi, also install the • serial-port node.

Flow steps:

  1. Define file headers at startup and file name based on ISO8601 datetime. (Filename adjusted to be compatibile with Windows)
  2. Timestamp new incoming messages from connected serial device (Arduino) and append existing file.
[{"id":"ab1e3f80a4d839fc","type":"tab","label":"Flow 1","disabled":false,"info":"","env":[]},{"id":"3ac2ad4d.8e5542","type":"timestamp","z":"ab1e3f80a4d839fc","name":"","x":350,"y":300,"wires":[["1918709f.849b3f"]]},{"id":"ea0f294b.577388","type":"timestamp","z":"ab1e3f80a4d839fc","name":"starting timestamp for filename","x":350,"y":180,"wires":[["1256bb1b.3a2de5"]]},{"id":"1256bb1b.3a2de5","type":"function","z":"ab1e3f80a4d839fc","name":"datestring_Set_filename","func":"\n\n// Create a Date object from the payload\nvar date = new Date(msg.timestamp);\n\n// Change the payload to be a formatted Date string\n// Then we will remove the miliseconds,\n//and finally change the colons to underscores, to make windows happy\nmsg.date = date.toISOString().split(\".\")[0].replaceAll(\":\", \"_\");\n\n// define a global variable for the filename of\n// which can then be used for every write to the file\nglobal.set(\"fname1\", \"C:/COMPASS/Data/Box1_\"+ msg.date +\".csv\");\n\n// and add that to the message passing through thtis node\nmsg.filename = global.get(\"fname1\");\n\n// Return the message so it can be sent on\nreturn msg;\n","outputs":"1","noerr":0,"initialize":"","finalize":"","libs":[],"x":650,"y":120,"wires":[["cd0bec6f.c9f24"]]},{"id":"1918709f.849b3f","type":"function","z":"ab1e3f80a4d839fc","name":"add timestamp to payload","func":"msg.payload =msg.timestamp + \",\" +msg.payload;\nmsg.filename =context.global.get(\"fname1\");\nreturn msg;","outputs":1,"noerr":0,"x":510,"y":380,"wires":[["5bb13835.fcc2c8"]]},{"id":"b3cb9d2.fd8046","type":"file","z":"ab1e3f80a4d839fc","name":"arduino1_log","filename":"filename","filenameType":"msg","appendNewline":false,"createDir":true,"overwriteFile":"false","encoding":"none","x":1130,"y":120,"wires":[[]]},{"id":"657956ed.4b9d28","type":"debug","z":"ab1e3f80a4d839fc","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1110,"y":300,"wires":[]},{"id":"77e25226.e54f7c","type":"inject","z":"ab1e3f80a4d839fc","name":"headers","repeat":"","crontab":"","once":true,"onceDelay":"0.1","topic":"headers","payload":"Time, Device, PIR1,PIR2,PIR3,PIR4,PIR5,PIR6,LDR","payloadType":"str","x":160,"y":120,"wires":[["ea0f294b.577388"]]},{"id":"cd0bec6f.c9f24","type":"function","z":"ab1e3f80a4d839fc","name":"add line return","func":"msg.payload = msg.payload + \"\\n\"\nreturn msg;","outputs":1,"noerr":0,"x":880,"y":200,"wires":[["b3cb9d2.fd8046","657956ed.4b9d28"]]},{"id":"9bd35a69.91ead8","type":"serial in","z":"ab1e3f80a4d839fc","name":"Arduino1","serial":"20b3ecf8257c7fa3","x":140,"y":260,"wires":[["3ac2ad4d.8e5542"]]},{"id":"5bb13835.fcc2c8","type":"change","z":"ab1e3f80a4d839fc","name":"Remove any comma at end","rules":[{"t":"change","p":"payload","pt":"msg","from":",\\n","fromt":"re","to":"","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":720,"y":300,"wires":[["cd0bec6f.c9f24"]]},{"id":"20b3ecf8257c7fa3","type":"serial-port","serialport":"COM4","serialbaud":"115200","databits":"8","parity":"none","stopbits":"1","waitfor":"","dtr":"none","rts":"none","cts":"none","dsr":"none","newline":"\\n","bin":"false","out":"char","addchar":"","responsetimeout":"10000"}]

Flow Info

Created 6 years, 1 month ago
Updated 8 months, 3 weeks ago
Rating: not yet rated

Owner

Actions

Rate:

Node Types

Core
  • change (x1)
  • debug (x1)
  • file (x1)
  • function (x3)
  • inject (x1)
Other
  • serial in (x1)
  • serial-port (x1)
  • tab (x1)
  • timestamp (x2)

Tags

  • csv
  • arduino
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option